Suppose we have $$$n$$$ vertices and we add edge $$$(i, j)$$$ with probability $$$p$$$. Is there a way to check if it is hamiltonian path/cycle or to find the longest non-self-intersecting path/cycle in polynomial time?

# | User | Rating |
---|---|---|

1 | tourist | 3845 |

2 | jiangly | 3707 |

3 | Benq | 3630 |

4 | orzdevinwang | 3573 |

5 | Geothermal | 3569 |

5 | cnnfls_csy | 3569 |

7 | jqdai0815 | 3532 |

8 | ecnerwala | 3501 |

9 | gyh20 | 3447 |

10 | Rebelz | 3409 |

# | User | Contrib. |
---|---|---|

1 | maomao90 | 171 |

2 | adamant | 163 |

3 | awoo | 162 |

4 | nor | 153 |

5 | maroonrk | 152 |

6 | -is-this-fft- | 151 |

7 | TheScrasse | 150 |

8 | Petr | 145 |

9 | atcoder_official | 144 |

9 | pajenegod | 144 |

Suppose we have $$$n$$$ vertices and we add edge $$$(i, j)$$$ with probability $$$p$$$. Is there a way to check if it is hamiltonian path/cycle or to find the longest non-self-intersecting path/cycle in polynomial time?

↑

↓

Codeforces (c) Copyright 2010-2024 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/23/2024 05:42:47 (k3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

How to solve $$$p = 1$$$ xD

Output the identity permutation.

NP Harder?

Yes, this is NP-hard as phrased. The random graph model used here, $$$G_{n,p}$$$, is supported on all $$$n$$$-vertex graphs (for $$$p \neq 0,1$$$). So, solving this question as phrased amounts to solving longest path/Hamiltonian cycle.

However, there is a related question which can have nontrivial answers. For specific values of $$$p$$$ like $$$p = 0.5$$$, or specific growth functions such as $$$p = \frac{1}{\sqrt{n}}$$$, the distribution of graphs coming from $$$G_{n,p}$$$ is weighted towards certain graphs. So, we could ask:

"Is there an algorithm which correctly answers the Hamiltonian path decision problem at least 99% of the time, given that the input is drawn from $$$G_{n,p}$$$?"

For some values of $$$p$$$, the algorithm can even be trivial! For instance, when $$$p < \frac{\log(n)}{n}$$$, then (with high probability) the graph will not even be connected, so we can always output "no Hamiltonian cycle detected" and be right 99% of the time. Similarly, when $$$p > \frac{c \log (n)}{n}$$$ for a specific constant $$$c$$$, then there is a Hamiltonian path with high probability, and we can always answer yes.

All of the above claims are only true asymptotically. So, to make an algorithm, you would have to say "for $$$n \le n_0$$$, just use the exponential algorithm. Then, for $$$n > n_0$$$, use the algorithm which succeeds with high probability."

What about finding longest non-self-intersecting path/cycle? Also can you describe this trivial algorithm?

The trivial algorithm I mentioned only works for the "decision problem" version. In this version, you only have to say "yes, there is a Hamiltonian path" or "no, there is not a Hamiltonian path"; that is, you do not need to list the path itself. I call the algorithm trivial, because it is literally either

`cout << "YES\n"`

when $$$p$$$ is "large", and`cout << "NO\n"`

when $$$p$$$ is "small".Your other question, the constructive version where you have to actually spell out a longest path, is more interesting, but I don't know a good way to do it :(

The one thing I can say is that these sorts of problems are very sensitive to the growth rate of $$$p$$$, and sometimes there are different regimes in which different techniques/restrictions apply. Crossing the "threshold" between $$$p$$$ regimes changes the flavor of the problem a lot. For instance, here, if $$$p = O\left(\frac{\log n}{n^2}\right)$$$, then there will be $$$O(\log n)$$$ edges, and we can do any brute force which is exponential in the number of edges. This is essentially fast because $$$G_{n,p}$$$ is so sparse.

On the other end of the spectrum, say $$$p = 0.5$$$, in which case $$$G_{n,p}$$$ is rather dense. Now, there are so many edges, that we can do this: start with vertex $$$v_0$$$. Then, let $$$v_1$$$ be any vertex with an edge $$$v_0v_1 \in E(G)$$$. Such a vertex exists with probabilty $$$1 - 2^{-(n-1)}$$$. Then, let $$$v_2$$$ be any vertex not yet chosen with an edge $$$v_1v_2 \in E(G)$$$. Such a vertex exists with probabilty $$$1 - 2^{-(n-2)}$$$. Proceed this way until you have a path of length $$$n - 10$$$ (this succeeds with probability 99.9%). Among the remaining 10 vertices, just brute force all Hamiltonian paths (I didn't do the exact calculation, but there should be many, with high probability). Then, for each length-10 path, there are "4 chances" to link it with the big path (each path has 2 ends, and they may be connected either way). So, with probability $$$15/16$$$, any given path works. The probability that they all fail should be small (I didn't calculate this either; dependence is a factor). But anyway, this should succeed overall like >95% of the time.

The weird case in these problems is always the values near the threshold. So, if $$$p = \Theta\left(\frac{\log n}{n}\right)$$$, I don't know what to expect. It may be the case that there are "very few, but more than 0" Hamiltonian paths, in which case, my instinct is that finding one in polynomial time would be difficult.

I'm no expert, but this and this will probably work well in practice

Some known results on this problem: As bpdolson mentioned, the critical window of $$$p$$$ on whether $$$G_{n, p}$$$ contains a Hamiltonian cycle is well understood: When $$$p = \frac{\log n + \log \log n + c}{n}$$$ the probability will converge to $$$e^{-e^{-c}}$$$ as $$$n\to \infty$$$. The proof can be found in the famous textbook

Random Graphsby Béla Bollobás.A recent algorithmic result (Rajko Nenadov, Angelika Steger, Pascal Su, An $$$O(n)$$$ time algorithm for finding Hamilton cycles with high probability,

ITCS 2021) claims that, there exists a constant $$$C$$$ such that, for $$$p\geq C \log n / n$$$, a randomized algorithm canfinda Hamiltonian cycle, even in linear time (of course, since the number of edges is already superlinear, the algorithm need some other ways to read the input).