Kruskal's Algorithm: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:


=Overview=
=Overview=
Kruskal's algorithms is a greedy algorithm that computes the [[The Minimum Spanning Tree Problem|minimum cost spanning tree]] for an undirected connected graph. It has a different approach than [[Prim%27s_Algorithm#Overview|Prim's algorithm]], in that it does not grow the connected explored territory one node at a time, but it grows the trees in parallel with a lot of simultaneous little pieces, unconnected at first, which eventually merge into the minimum cost spanning tree. It does that by sorting the edges in the ascending order of their cost, and picking edges in that order, one by one, careful not to create cycles.
Kruskal's algorithms is a [[Algorithms#Greedy_Algorithms|greedy algorithm]] that computes the [[The Minimum Spanning Tree Problem|minimum cost spanning tree]] for an undirected connected graph. It has a different approach than [[Prim%27s_Algorithm#Overview|Prim's algorithm]], in that it does not grow the connected explored territory one node at a time, but it grows the trees in parallel with a lot of simultaneous little pieces, unconnected at first, which eventually merge into the minimum cost spanning tree. It does that by sorting the edges in the ascending order of their cost, and picking edges in that order, one by one, careful not to create cycles. The greediness of the algorithm comes from the fact that at each step, it chooses an edge by attempting to minimize a [[Algorithms#Greedy_Score|greedy score]], which is the cost of the edge.
:::[[File:KruskalAlgorithm.png]]
:::[[File:KruskalAlgorithm.png]]



Revision as of 17:34, 22 October 2021

External

Internal

Overview

Kruskal's algorithms is a greedy algorithm that computes the minimum cost spanning tree for an undirected connected graph. It has a different approach than Prim's algorithm, in that it does not grow the connected explored territory one node at a time, but it grows the trees in parallel with a lot of simultaneous little pieces, unconnected at first, which eventually merge into the minimum cost spanning tree. It does that by sorting the edges in the ascending order of their cost, and picking edges in that order, one by one, careful not to create cycles. The greediness of the algorithm comes from the fact that at each step, it chooses an edge by attempting to minimize a greedy score, which is the cost of the edge.

KruskalAlgorithm.png

Non-Optimized Implementation

Non-Optimized Implementation Running Time

sort edges in the order of increasing cost
initialize T = ∅ # the tree we are growing
for edge e = (u,v) in the order of increasing cost:
  if T ⋃ e has no cycles # there is no path in T from u to v
    add e to T

We could count the edges added to the spanning tree, and once we have n-1 edges, which means the spanning tree is complete, we can abort the loop.

Correctness Proof

https://www.coursera.org/learn/algorithms-greedy/lecture/U3ukN/correctness-of-kruskals-algorithm

Optimized Implementation

https://www.coursera.org/learn/algorithms-greedy/lecture/e0TJP/implementing-kruskals-algorithm-via-union-find-i