Lecture from: 29.04.2025 | Video: Homelab

The Long-Path Problem (Decision Version)

Problem Definition

Given an undirected graph and a non-negative integer , the Long-Path problem asks:

Does there exist a path in of length exactly ?

  • A path is a sequence of vertices with no repetitions.
  • A path of length contains distinct vertices.

Computational Complexity

The Long-Path problem is NP-complete. It is closely related to the Hamiltonian Path and Hamiltonian Cycle problems:

  • Hamiltonian Path: Does contain a simple path that visits every vertex exactly once? ↳ Equivalent to Long-Path with .

  • Hamiltonian Cycle: Does contain a simple cycle that visits every vertex exactly once?

Since both Hamiltonian problems are NP-complete, and no polynomial-time algorithms are known for NP-complete problems (assuming ), it is unlikely that Long-Path admits an efficient algorithm.

Reduction from Hamiltonian Cycle to Long-Path

To show that Long-Path is NP-complete, we reduce from the Hamiltonian Cycle problem, which is a known NP-complete problem.

Theorem

If Long-Path can be solved in time for graphs with nodes, then the Hamiltonian Cycle problem can be solved in time

for graphs with nodes.

High-Level Idea

Given a graph with vertices, we construct a new graph such that:

This reduction takes polynomial time.

Construction of

Let with . Construct as follows:

  1. Select an arbitrary vertex .
  2. Remove from (i.e., work with ).
  3. For each neighbor of in :
    • Add a new vertex to ,
    • Add the edge to .

So, contains:

  • All original vertices except : ,
  • One new vertex for each neighbor of ,
  • The same edges as , minus those incident to ,
  • New edges from each to its corresponding .

Let , then:

  • .

Correctness of the Reduction

We prove:

(⇒) If has a Hamiltonian Cycle, then has a Path of Length

Suppose has a Hamiltonian cycle:

where each is distinct and .

In the cycle:

  • and are neighbors of in .
  • In , the edges and are replaced by:
    • ,
    • , where and are new vertices.

Define the path in :

  • This is a simple path,
  • Contains vertices → path of length ,
  • All edges exist in .

Thus, contains a simple path of length .

(⇐) If has a Path of Length , then has a Hamiltonian Cycle

Suppose has a simple path:

with distinct vertices.

Note:

  • we only have nodes originally from , so at least of them are new.
  • The “hat” nodes have degree 1 in .
  • Therefore, they must appear at the endpoints of the path.

So:

  • , for some neighbors of ,
  • , .

The subpath:

consists of distinct vertices from . Since there are exactly such vertices, this must be a Hamiltonian path in .

To complete a Hamiltonian cycle in :

  • Re-insert ,
  • Add edges and , which exist in by construction.

Thus, the cycle:

is a Hamiltonian cycle in .

Conclusion

We have shown that the Hamiltonian Cycle problem can be reduced in polynomial time to the Long-Path problem:

  • If Long-Path can be solved in time ,
  • Then Hamiltonian Cycle can be solved in time ,
  • Therefore, Long-Path is NP-complete.

Solving the Hamiltonian Cycle Problem via Dynamic Programming

The Hamiltonian Cycle problem asks whether a given graph contains a simple cycle that visits every vertex exactly once.

Although this problem is NP-complete, we can solve it in exponential time using dynamic programming (DP), specifically via a method similar to the Held-Karp algorithm. While this is not efficient for large graphs, it is significantly better than brute-force enumeration of all permutations.

Dynamic Programming Strategy

We fix an arbitrary starting vertex and build up solutions by considering:

  • Subsets of vertices such that , and
  • A destination vertex , if .

DP State Definition

Let:

That is, indicates whether there’s a path that:

  • Starts at ,
  • Ends at ,
  • Visits every vertex in exactly once.

Base Case

We initialize:

  • — the trivial path starting and ending at , visiting only .

Recurrence Relation

For all with and , and for all , compute:

In words:

  • We can extend a valid path ending at in to a path ending at by appending the edge , if such an edge exists.

Final Step: Detecting a Hamiltonian Cycle

After computing all , we check:

This indicates:

  • A path from to visiting all vertices (Hamiltonian path), and
  • A closing edge , completing the Hamiltonian cycle.

Time and Space Complexity

Let . Then:

  • Number of states: subsets For each , at most values of total states

  • Time per state: For each , we may consider up to predecessors per state

  • Total time complexity:

  • Space complexity:

    to store the DP table.

Summary

StepDescription
Start VertexFix as the starting point
DP State: path from to , visiting exactly
Base Case
RecurrenceExtend smaller solutions using edge
Final CheckLook for and
Time Complexity
Space Complexity

This is one of the most efficient known exact algorithms for solving the Hamiltonian Cycle problem, thanks to its clever use of subproblem reuse via dynamic programming.

Long-Path via Randomized Coloring

We consider the Long-Path problem:

Given an undirected graph and an integer , determine whether contains a simple path of length at least .

Motivation

A dynamic programming approach for Long-Path (based on subset-DP) runs in time , which is exponential in . This becomes impractical for moderate values of , though it works fine if is extremely small.

We want an algorithm with runtime for some constant , which would be polynomial when .

Randomized Approach: Color Coding

Key Idea

We randomly color the graph and search for a colorful path - a path where all vertices have distinct colors.

Let . We seek a path of length , using colors.

Probability That a Path Is Colorful

Suppose the graph contains a path of length . Let us randomly color each vertex independently with a color in :

  • There are possible colorings of the vertices on .

  • Exactly of them assign distinct colors to all vertices on , i.e., make colorful.

  • So,

Using Stirling’s approximation , we get:

Therefore, a lower bound is:

This means that the chance of a particular path becoming colorful under a random coloring is at least .

Monte Carlo Algorithm for Long-Path

  1. Let .
  2. Repeat the following times:
    • Randomly color the vertices of using colors.
    • Check whether the graph contains a colorful path of length .
    • If such a path is found, return YES.
  3. If no colorful path is found in any iteration, return NO.

Correctness

  • If the algorithm returns YES, the graph indeed contains a path of length . (Because colorful paths are simple paths by definition.)

  • If the graph contains a path of length , then with probability at least , one random coloring will make some such path colorful.

  • The probability that none of the trials succeeds is:

    using the inequality .

So the error probability is at most . This is a Monte Carlo algorithm: it may return NO incorrectly with small probability, but never returns YES incorrectly.

Runtime

The only missing piece is how to check for a colorful path in each iteration.

  • This will be handled in the next lecture, where we will show how to check for the existence of a colorful path of length in time where , using a dynamic programming approach over subsets of colors.

Thus, the total runtime becomes:

This is polynomial in when , i.e., when .

Summary

  • We can solve Long-Path in randomized FPT time using color coding.
  • Error probability is at most , with no false positives.
  • Runtime is efficient for small , especially .
  • In the next lecture, we’ll see how to efficiently check for colorful paths in a given coloring.

Continue here: 20 Detecting Colorful Paths using DP, Flow Networks