IT lexicon Programming Greedy algorithm

Greedy algorithm

Programming På svenska → Updated: 2026-05-23

Strategy: make the locally best choice at every step and hope the result is globally optimal. Not always — but often enough to apply.

Works when the problem has the greedy choice property (local optimum leads to global) + optimal substructure. Classic examples: Dijkstra (greedy + DP), Kruskal's MST, Prim's MST, Huffman coding, activity selection.

When it works: fast (O(n log n) typical), easy to implement. Trap: problems that look greedy but aren't (knapsack, partition). Competitor: DP (when greedy fails), backtracking.

← Back to the lexicon