If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Longest path

Hey guys!

I've read a little about finding the longest path in undirected graph, but haven't found any solutions to that problem. So let's assume we have undirected, unweighted graph - how do we determine the longest possible path, where each node can be visited only once?

As far as I understood, this is NP-complete problem, but do you guys know the algorithm to solve it?

Re: Longest path

Originally Posted by Donutto

As far as I understood, this is NP-complete problem, but do you guys know the algorithm to solve it?

You can always use brute force and make an exhaustive search.

You start at each of the N nodes. From each of them you can reach at most N-1 not yet visited nodes. And from each of those you can reach at most N-2 unvisited nodes. Etcetera. Sooner or later you will reach a node from where no further nodes can be reached. That's the end of one path. If this path contains N nodes no other path can be longer. If there are fewer nodes in the path the search must continue because there may be longer paths.

To implement solutions like this recursion is often used. The complexity will be something like O(N!) so it's not very efficient but if there are few edges in the graph it can still be pretty fast.