0

I have an un-directed graph that weight of each edge is 1. The graph may have cycles. I need to find a longest path in the graph (each node appear once). The length of the path is number of nodes. Any simple/effective solution? Thanks!

Loc
  • 8,364
  • 6
  • 36
  • 73

2 Answers2

1

According to http://en.wikipedia.org/wiki/Longest_path_problem, finding the longest path is NP-hard. So it is considered to be a hard to solve problem for big instances unless P = NP. In contrast to finding the shortest path, where BFS algorithm is linear.

JuniorCompressor
  • 18,603
  • 4
  • 24
  • 54
0

I had a similar case but my nodes were limited, the number was less than 50.

I modelled it in a SQL database table (from, to and length columns) and tried to find each path between two given nodes and calculated the length of the path to identify the longest path.

On SQL Server, I build a SQL Recursive CTE query to define the longest path. Please refer to find the longest path article at referred document

Please note that, even with 50 nodes the query calculated over 70m possible paths from start node to end node without passing the same node twice and it took about 2 hours for SQL Server engine on my development computer to complete the execution of this query.

Eralper
  • 6,081
  • 2
  • 17
  • 27