Why can't Prim's or Kruskal's algorithms be used on a directed graph?

和自甴很熟 提交于 2019-11-27 20:13:19

问题


Prim's and Kruskal's algorithms are used to find the minimum spanning tree of a graph that is connected and undirected. Why can't they be used on a graph that is directed?


回答1:


It's a minor miracle that these algorithms work in the first place -- most greedy algorithms just crash and burn on some instances. Assuming that you want to use them to find a minimum spanning arborescence (directed paths from one vertex to all others), then one problematic graph for Kruskal looks like this.

 5
  --> a
 /   / ^
s   1| |2
 \   v /
  --> b
 3

We'll take the a->b arc of cost 1, then get stuck because we really wanted s->b of cost 3 and b->a of cost 2.

For Prim, this graph is problematic.

 5
  --> a
 /   /
s   1|
 \   v
  --> b
 3

We'll take s->b of cost 3, but we really wanted s->a of cost 5 and a->b of cost 1.




回答2:


Prim's and Kruskal's algorithm output a minimum spanning tree for connected and "undirected" graph. If it is not connected, we can tweak them to output minimum spanning forests.

In Prim's algorithm, we divide the graph in two sets of vertices. One set of the explored vertices which have already formed MST (Set1) and another set of unexplored vertices which will eventually join the first set to complete "spanning"(Set2). At each instant, we select a minimum weighted edge in the cut joining the two disjoint sets. If there is no directed edge from explored nodes of MST to remaining unexplored, the algorithm gets stuck even though there are edges from unexplored nodes to explored nodes in MST.

In Kruskal's algorithm, the idea is to sort the edges in ascending order by their weight and pick them up in order and include them in MST explored nodes/edges if they donot already form a cycle with explored nodes. This is done by Union-Find DS. But detection of cycle for directed graphs fails with this method. For ex: Graph containing edges [1->2] [2->3] [1->3] will be reported to contain a cycle with the Union-Find method.

So Prim's fails because it assumes, every node is reachable from every node which though valid for undirected graphs may not be true for digraphs. Kruskal fails because of failure to detect cycles and sometimes it is essential to add edges making cycles to satisfy "minimum" weighted property of MST.

Also, in case of digraphs, MST doesn't make complete sense. Its equivalent for digraphs is "minimum spanning arborescence" which will produce a tree where every vertex can be reached from a single vertex.



来源:https://stackoverflow.com/questions/22649416/why-cant-prims-or-kruskals-algorithms-be-used-on-a-directed-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!