# Longest path

• June 23rd, 2013, 11:57 AM
Donutto
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?
• July 22nd, 2013, 12:58 PM
razzle
Re: Longest path
Quote:

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.