Given an undirected graph G=(V,E) and a non-negative integer B∈N0, the Long-Path problem asks: Does there exist a path in G of length exactly B?
Definitions
A path is a sequence of vertices with no repetitions.
A path of length B contains B+1 distinct vertices.
NOTE
The Long-Path problem is NP-complete. It is closely related to the Hamiltonian Path and Hamiltonian Cycle problems. If we can solve the Long-Path problem in t(n) for graph with n nodes, then we can solve the Hamiltonian Path problem in t(2n−2)+O(n2)
Futhermore:
G=(V,E) has a Hamiltonian Cycle⟺G′ has path of length n
Long Path - Hamiltonian Cycle Proof.excalidraw
⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’
Excalidraw Data
Text Elements
⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’
By brute force we can solve the Long-Path problem in O(nB+2)Goal: under O(cB) where c is a constant
Randomized Approach: Color Coding
Key Idea
We randomly color the graph and search for a colorful path — a path where all k vertices have distinct colors.
Let k=B+1. We seek a path of length k−1, using k colors.
For v∈V and i∈{0,…,k−1} define
Pi(v):={S⊆[k]∣∣S∣=i+1∧∃ a colorful path ending in v that is colored exactly with S}
Goal: Compute all sets Pi(v) for every v∈V and every i∈{0,1,…,k−1}
Example:
Colorful Path.excalidraw
⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠ You can decompress Drawing data with the command palette: ‘Decompress current Excalidraw file’. For more info check in plugin settings under ‘Saving’
P0(v)={{γ(v)}} (color of v)
for i≥1 there is:
∃a colorful path that ends in v that ends in S⟺∃ a neighbor x of v and a colorful path ending in x that is colored with S∖{γ(v)}
Therefore:
Pi(v)=x∈N(v)⋃{R∪{γ(v)}∣R∈Pi−1(x)∧γ(v)∈R}
Computation of Pi(v) for all v∈V, given Pi−1(v) for all v∈V
BUNT(G,i)
for all v∈V do
Pi(v)←∅
for all x∈N(v) do
for all R∈Pi−1(x) with γ(v)∈/R do
Pi(v)←Pi(v)∪{R∪{γ(v)}}
Run Time
This algorithm checks for the existence of a colorful path of length k−1 in time
O(2k⋅k⋅m)
where m=∣E∣
Probability That a Path Is Colorful
Suppose the graph contains a path P of length k−1.
Randomly color each vertex independently with a color in {1,…,k}.
There are kk possible colorings of the k vertices on P.
Exactly k! of them assign distinct colors to all vertices on P, i.e., make P colorful.
So,
Pr[P is colorful]=kkk!
Using Stirling’s approximation:
k!≈2πk(ek)k
we get
kkk!≈2πk(e1)k
Therefore, a lower bound is
kkk!≥(e1)k=e−k
This means the chance of a particular path becoming colorful under a random coloring is at least e−k.
Summary
Theorem
Let G be a graph containing a path of length k−1.
A random coloring with k colors produces a colorful path of length k−1 with probability
p≈e−k.
For repeated colorings with k colors, the expected number of trials until a colorful path of length k−1 is obtained is