Activity Selection, Huffman Codes, Minimum Spanning Trees
Greedy doesn't always work! Example: Making change with arbitrary coin denominations, or finding shortest path in graphs with negative weights.
| Algorithm | Problem | Greedy Choice | Time Complexity |
|---|---|---|---|
| Activity Selection | Max non-overlapping activities | Earliest finish time | O(n log n) |
| Huffman | Optimal prefix codes | Merge two min frequencies | O(n log n) |
| Kruskal's MST | Minimum spanning tree | Min weight edge (no cycle) | O(E log E) |
| Prim's MST | Minimum spanning tree | Min edge to new vertex | O((V+E) log V) |
| Dijkstra's | Shortest paths | Min distance unvisited vertex | O((V+E) log V) |
Click edges in the order they would be added to the MST. Test your understanding of both algorithms!
Instructions: Click edges in the order Prim's algorithm would add them to the MST. Start from vertex A.
Total MST weight: 2 + 3 + 4 + 1 + 8 = 18
Note: Prim's grows the MST from a single vertex, always choosing the minimum-weight edge that connects the tree to a new vertex.
Instructions: Click edges in the order Kruskal's algorithm would add them. Remember: sort by weight, add if no cycle!
Step 1: Sort all edges by weight:
B-D(1), A-C(2), C-E(3), A-B(4), B-C(5), D-E(6), B-E(7), D-F(8), E-F(9)
Step 2: Add edges in order, skipping those that create cycles:
Skipped edges: B-C(5), D-E(6), B-E(7), E-F(9) - all would create cycles
Total MST weight: 1 + 2 + 4 + 3 + 8 = 18
Note: Kruskal's processes edges by weight globally, using Union-Find to detect cycles.
Both algorithms produce the same MST (total weight 18) but add edges in different orders:
Both are correct! The MST is the same, but the construction process differs.