How to find the longest simple path in a graph?
Asked Answered
C

2

6

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!

Carolinacaroline answered 19/2, 2014 at 12:24 Comment(4)
You have to put extra constraints. Otherwise the path is infinite...Kew
Each vertex is included in the path only ones - is a simple path.Carolinacaroline
Just because it's NP-complete doesn't mean you should use brute force. Sure, you can't (as far as known) do better than exponential time in the worst case, but worst cases are rare and to give a couple of examples, 3-SAT instances with thousands of clauses and variables and TSP instances with thousands of cities are routinely solved, clearly not with brute force.Raja
unweighted: #2525816Pointsman
B
7

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).

Badderlocks answered 19/2, 2014 at 12:46 Comment(12)
This is the trivial brute-force solution.Feldt
Fine what about modifying DFS for doing this brute-force?Carolinacaroline
@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.Badderlocks
@Carolinacaroline 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.Badderlocks
@Carolinacaroline I updated my answer. Basically, it is very much DFS-ish approach, without building the tree explicitly.Badderlocks
@Badderlocks what a pity you deleted your first message. This one and the one you have written before were valuable. Thanks for your help.Carolinacaroline
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 :)Carolinacaroline
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 :)Carolinacaroline
let us continue this discussion in chatBadderlocks
That's n^3 not n*n^nArgyll
@Argyll Would not it mean that a NP-hard problem was solved in polynomial time )?Badderlocks
@Badderlocks "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: wikipediaArgyll
K
4

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.

Kindred answered 19/2, 2014 at 12:49 Comment(2)
how about undirected acyclic graphs, is it still NP-complete?Backpack
undirected acyclic graph is a tree, so the problem is finding the diameter of the tree, which is linear time.Forego

© 2022 - 2024 — McMap. All rights reserved.