How to find the longest simple path in a graph?
Solution 1
A naïvem approach could run through all possible vertex permutations.
For every permutation {v1, ..., vN}
you check if you can get from v1
to v2
, then from v2
to v3
etc. If you can, add corresponding edge length to the current path length. If not, go to the next permutation.
The longest of such paths is your answer.
Or, you could do pretty much the same using recursion.
path = 0
bestPath = 0
used = new bool[N] // initialize with falses
for(u = 0; u < N; u++)
Path(u); // build paths starting from u
print bestPath
where
Path(u)
used[u] = true
foreach v in neighborhood(u)
if(!used[v])
path += distance(u, v)
bestPath = max(bestPath, path)
Path(v)
path -= distance(u, v)
used[u] = false
Time complexity is horrible O(N * N^N)
.
Solution 2
If your graph is a special case in which it's directed and acyclic, you could do a dynamic programming approach such as the one described here. You basically sort your graph topologically, then in the topological order, for every node V, you check all its neighbors and update their "distance" value if it's bigger than the "distance" already memorized (initialized with -infinity or something).
Otherwise, in the general case, the problem is indeed NP-complete as it reduces to the Hamiltonian cycle. One thing you could do is negate all the edges and try the Bellman-Ford Algorithm. Beware that it's not good for negative cycles, however.
Comments
-
Narek almost 2 years
I know that for non-directed graph this problem is NP-complete hence we should do Brute Force in order to check all possible paths. How we can do that? Please suggest a pseudo code and tell me the complexity of that algorithm.
If there are optimizations, then that would be awesome!
-
Bas Swinckels about 10 yearsThis is the trivial brute-force solution.
-
Narek about 10 yearsFine what about modifying DFS for doing this brute-force?
-
AlexD about 10 years@BasSwinckels Sure. But the question says: "we should do Brute Force in order to check all possible paths. How we can do that?". So it sounded to me that brute force is OK.
-
AlexD about 10 years@Narek What do you mean exactly? For any vertex
R
we can construct a tree whereR
is the root and the number of levels in the tree is (less or) equal to the number of vertices in our graph. Every leaf corresponds to a different path then, and we need to find the farthermost one. -
AlexD about 10 years@Narek I updated my answer. Basically, it is very much DFS-ish approach, without building the tree explicitly.
-
Narek about 10 years@AlexD what a pity you deleted your first message. This one and the one you have written before were valuable. Thanks for your help.
-
Narek about 10 yearsThis solution is so not intuitive. It is very similar to DFS, but slightly different, and I don't feel the difference, why this one in not a DFS, but brute-force :)
-
Narek about 10 yearsEvent not sure that this works. :( Is there something that I can read about this solution, or this is just your solution - you have invented :)
-
AlexD about 10 years
-
CME64 almost 5 yearsThat's n^3 not n*n^n
-
AlexD almost 5 years@CME64 Would not it mean that a NP-hard problem was solved in polynomial time )?
-
CME64 almost 5 years@AlexD "A common misconception is that the NP in "NP-hard" stands for "non-polynomial" when in fact it stands for "non-deterministic polynomial acceptable problems". Although it is suspected that there are no polynomial-time algorithms for NP-hard problems, this has not been proven. Moreover, the class P, in which all problems can be solved in polynomial time, is contained in the NP class." Source: wikipedia
-
Sush over 3 yearshow about undirected acyclic graphs, is it still NP-complete?
-
Megadardery over 3 yearsundirected acyclic graph is a tree, so the problem is finding the diameter of the tree, which is linear time.