How to find the longest simple path in a graph?

12,645

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.

Share:
12,645
Narek
Author by

Narek

Game developer.

Updated on June 06, 2022

Comments

  • Narek
    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
    Bas Swinckels about 10 years
    This is the trivial brute-force solution.
  • Narek
    Narek about 10 years
    Fine what about modifying DFS for doing this brute-force?
  • AlexD
    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
    AlexD about 10 years
    @Narek What do you mean exactly? For any vertex R we can construct a tree where R 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
    AlexD about 10 years
    @Narek I updated my answer. Basically, it is very much DFS-ish approach, without building the tree explicitly.
  • Narek
    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
    Narek about 10 years
    This 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
    Narek about 10 years
    Event 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
    AlexD about 10 years
  • CME64
    CME64 almost 5 years
    That's n^3 not n*n^n
  • AlexD
    AlexD almost 5 years
    @CME64 Would not it mean that a NP-hard problem was solved in polynomial time )?
  • CME64
    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
    Sush over 3 years
    how about undirected acyclic graphs, is it still NP-complete?
  • Megadardery
    Megadardery over 3 years
    undirected acyclic graph is a tree, so the problem is finding the diameter of the tree, which is linear time.