Apply color-coding for path of length k in O(2^k n^{O(1)})
You are given an undirected graph and an integer k. Decide whether the graph contains a simple path on exactly k distinct vertices, which is a sequence of k vertices where every consecutive pair is joined by an edge and no vertex repeats.
The input is a single JSON string [n, edges, k], where n is the number of vertices, vertices are numbered 0 through n - 1, and edges is a list of [u, v] undirected edges.
Use color-coding: assign every vertex one of k colors at random, then run a subset DP over color masks. A coloring that gives the k vertices of a target path pairwise different colors turns "distinct vertices" into "distinct colors", and a bitmask can track distinct colors, so the DP grows paths along real edges without ever revisiting a vertex. Design the amplification yourself: with T independent uniform trials, a fixed k-vertex path is missed with probability at most (1 - k!/k^k)^T, so T = ceil(16 k^k / k!) pushes that below exp(-16) ~ 1.1e-7.
That probability statement is a theorem about independent uniform colorings. A real implementation is deterministic, and the code below replays one fixed pseudo-random stream, so the bound is not a claim about a concrete run. The full specification therefore has two phases:
- Color-coding search. Run the color-mask DP over
Tfixed trials. Atruehere is a certificate, because the DP actually constructed a path along real edges. - Exact verification. If all
Ttrials fail, run a vertex-subset DP over the original graph, with state(vertex set, endpoint), and decide existence exactly. Afalseis only reported after this phase, so it is a proof of absence inside the declared domain.
Both phases are required for the answer to be exact. The color-coding phase is the lesson; the exact phase is what makes the deterministic program honest. The declared bounds (k <= 6, n <= 12) keep the 2^n verification phase small, so this companion is not the general-graph FPT algorithm whose complexity is exponential only in k.
Apply color-coding for path of length k in O(2^k n^{O(1)})
You are given an undirected graph and an integer `k`. Decide whether the graph contains a simple path on exactly `k` distinct vertices, which is a sequence of `k` vertices where every consecutive pair is joined by an edge and no vertex repeats.