Search code examples
algorithmgraphgraph-algorithmprims-algorithm

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


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?


Solution

  • 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.