简体   繁体   English

无向非加权图中的最长路径

[英]Longest Path in an undirected unweighted graph

I came across a problem where I have to find out the longest path in a given graph. 我遇到了一个问题,我必须找出给定图中的最长路径。 I have list of edges ( eg.{AB, BC} ) which states there is an edge between vertices/nodes (A,B,C). 我有一条边列表(例如{AB,BC}),它指出在顶点/节点(A,B,C)之间有一条边。 Now i want to figure out the longest path possible (not repeating the vertex) such that it covers maximum nodes starting from any vertex/node. 现在,我想找出可能的最长路径(不重复顶点),使其涵盖从任何顶点/节点开始的最大节点。

What can be the best way to solve this? 解决此问题的最佳方法是什么? I have to implement this as a program. 我必须将此作为程序来实现。

I looked up google for Minimum Spanning Tree, Dijkstra's Alogorithms , and many more. 我在Google上查找了最小生成树,Dijkstra的Alogorithms等。 but can't figure out what would suit best for this problem. 但无法找出最适合此问题的方法。

Any help or reading references would be much appreciated. 任何帮助或阅读参考文献将不胜感激。

This is NP-Hard. 这是NP-Hard。

You can solve Hamiltonian Path problem, if you can solve this problem. 如果可以解决此问题,则可以解决哈密顿路径问题。

(Longest path = |V| <=> Hamiltonial Path). (最长路径= | V | <=>哈密顿路径)。

So, just pick up any algorithm for Travelling Salesman Problem. 因此,只需选择旅行商问题的任何算法即可。 Give all the edges weight one. 给所有边缘重一。

I heard a dynamic programming version is particularly nice. 我听说动态编程版本特别好。

Similar to finding Hamiltonian paths, this needs to be solved by backtracking. 与查找汉密尔顿路径相似,这需要通过回溯来解决。

For each vertex in the graph, initialize a path list. 对于图中的每个顶点,初始化path列表。 Each element of path will contain a vertex, and a list of vertices ( adj ) which can be reached from that vertex. path每个元素都将包含一个顶点,以及可以从该顶点到达的顶点列表( adj )。 That is, path[i] -> (v, adj) . 也就是说, path[i] -> (v, adj) We'll make it a rule that the vertices in adj[v] must not be vertices that occur earlier in path . 我们将制定一条规则,即adj[v]中的顶点不得为path较早出现的顶点。

path[i+1] is built from path[i] by letting path[i+1].v = pop(path[i].adj) , and setting path[i+1].adj equal the adjacent vertices of path[i+1].v that are not yet in the path. path[i+1]从内置path[i]通过使path[i+1].v = pop(path[i].adj)以及设定path[i+1].adj等于的相邻顶点path[i+1].v尚未在路径中。 If there is no element of path[i].adj to pop, we've reached a dead end. 如果没有要弹出的path[i].adj元素,那么我们已经走到了尽头。 If the path is longer than the largest path found so far record it. 如果该路径比到目前为止找到的最大路径长,请记录下来。

Now pop all the elements from path until you find one that doesn't have an empty adj list (this is called backtracking). 现在流行一切从元素path ,直到找到一个不有一个空的adj列表(这被称为回溯)。 After backtracking, either the last path element has a non-empty adj list, or the path is empty, ie you've backtracked to the beginning. 回溯后,最后一个path元素具有非空的adj列表,或者路径为空,即您已回溯到起点。 If the path is not empty, extend the path as before. 如果路径不为空,请像以前一样扩展路径。 If the path is empty, choose a new starting vertex and begin again. 如果路径为空,请选择一个新的起始顶点,然后重新开始。

Eventually all possible paths are enumerated, so the longest path found is your answer. 最终将列举所有可能的路径,因此找到的最长路径就是您的答案。

You may be able to find ways to short-cut this process, possibly using cut-edges (aka bridge edges). 您可能能够找到缩短此过程的方法,可能使用切边(也称为桥边)。

Since your question doesn't speak whether the Graph is Cyclic or Not you have two options: 由于您的问题不会说图形是循环的还是不是,因此您有两个选择:

Option1 : Graph is DAG 选项1图表为DAG

You are Lucky, you can use topological sort on the graph and get the longest path! 您很幸运,您可以在图形上使用拓扑排序并获得最长的路径!

Option 2 : Graph is NOT DAG: 选项2图表不是DAG:

Use the Hamiltonian Algorithm as mentioned in the Comments! 使用评论中提到的哈密顿算法!

Give all edges weight 1. The non-existent, give weight n+1. 赋予所有边权重1.不存在,赋予权重n + 1。 Now apply TSP. 现在应用TSP。

Leave something for the OP to work out, but alas apparently this was harder than I assumed it was. 给OP留点时间解决,但是很显然,这比我想象的要难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM