Study Atlas › Computer Science › Algorithms

Greedy algorithms and minimum spanning trees

Greedy algorithms make the locally best choice at each step; for some problems this gives the global optimum.

Edited by Alex C, EduAlx · Sources cited below

Explanation

Kruskal (1956) and Prim (1957, earlier Jarník 1930) find minimum spanning trees. Huffman coding and activity selection are greedy. Greedy correctness is proved by exchange arguments or matroid theory; it fails for problems like 0/1 knapsack.

Example

Laying cable to connect cities with minimum total length is an MST problem.

How it works, step by step

  1. Sort edges by weight
  2. Take the cheapest edge that does not form a cycle (union–find)
  3. Repeat until V–1 edges are chosen
  4. Result is a minimum spanning tree

Open the interactive page with diagrams →

People behind it

References

  1. J. B. Kruskal, Proc. Am. Math. Soc. 1956, 7, 48–50.
  2. R. C. Prim, Bell Syst. Tech. J. 1957, 36, 1389–1401.

Textbooks

Related topics

String algorithms and pattern matchingDynamic programmingAlgorithms and Big-O complexityGraph algorithms: BFS, DFS and Dijkstra's shortest pathSorting algorithms (quicksort, mergesort, heapsort)Heaps and priority queuesAutomata and formal languagesRecursionMemory hierarchy and cachesNumber systems and data representation