[Math] Longest path in undirected graph

graph theory

let's say I have an undirected unweighted graph where each edge can be "visited" only once. How can I get the longest path? I've seen something like NP-hard or depth-first search. Thanks for help

Best Answer

If there are no cycles in your graph, you can put -1 weights on each edge and search for the shortest path with the Bellman-Ford algorithm.

If there are cycles, your problem is NP-hard indeed, and you need to proceed differently, with integer programming for example.

Note. The shortest path with negative weights equals the longest path. If weights are unitary, and the shortest path is, say -20, then the longest path has length 20.