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

后端 未结 2 784
我寻月下人不归
我寻月下人不归 2020-12-08 02:58

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

2条回答
  •  鱼传尺愫
    2020-12-08 03:40

    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.

提交回复
热议问题