Greedy Algorithms
Greedy Algorithms
Greedy Philosophy
Build up a solution piece by piece
Always choose the next piece that offers the most obvious
and immediate benefit
Without violating given constraints
1 2
1 2 3
6 4 5 6
4
3 8
4 5 6
4 7 3
7
CS 312 – Greedy Algorithms 6
MST – Minimum Spanning Tree
What greedy algorithm might we use
assuming we start with the entire graph
1 2
Iteratively take away the largest remaining 1 2 3
edge in the graph which does not 4
6
4
5
6
disconnect the graph 3 8
– Is it a greedy approach? 4 5 6
Complexity? 7
4 3
How do we prove if it works/optimal or 7
not
– Counterexamples – natural first attempt
– If no easily found counterexamples, we then
seek a more formal proof
4 7 3
7
1 2 1: (1,2) {1,2}{3}{4}{5}{6}{7}
1 2 3
2: (2,3) {1,2,3}{4}{5}{6}{7}
6 4 5 6 3: (4,5) {1,2,3}{4,5}{6}{7}
4
3 8 3: (6,7)
4 5 6 4: (1,4)
4: (2,5)
4 7 3 4: (4,7)
5: (3,5)
7
1 2 1: (1,2) {1,2}{3}{4}{5}{6}{7}
1 2 3
2: (2,3) {1,2,3}{4}{5}{6}{7}
6 4 5 6 3: (4,5) {1,2,3}{4,5}{6}{7}
4
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4)
4: (2,5)
4 7 3 4: (4,7)
5: (3,5)
7
1 2 1: (1,2) {1,2}{3}{4}{5}{6}{7}
1 2 3
2: (2,3) {1,2,3}{4}{5}{6}{7}
6 4 5 6 3: (4,5) {1,2,3}{4,5}{6}{7}
4
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4) {1,2,3,4,5}{6,7}
4: (2,5)
4 7 3 4: (4,7)
5: (3,5)
7
1 2 1: (1,2) {1,2}{3}{4}{5}{6}{7}
1 2 3
2: (2,3) {1,2,3}{4}{5}{6}{7}
6 4 5 6 3: (4,5) {1,2,3}{4,5}{6}{7}
4
3 8 3: (6,7) {1,2,3}{4,5}{6,7}
4 5 6 4: (1,4) {1,2,3,4,5}{6,7}
4: (2,5) rejected
4 7 3 4: (4,7) {1,2,3,4,5,6,7} done
5: (3,5)
7
X
CS 312 – Greedy Algorithms 21
Cut Property: Assume edges X are part of an MST for G=(V,E). Pick
any subset S for which X does not cross between S and V-S, and let e
be the lightest edge across this partition. Then X ∪ {e} is part of some
MST. – Why?
1. Assume edges X are part of a partial MST T (Inductive hypothesis)
2. If e is a part of T then done, so consider case where e is not part of T
3. Now add e to T, creating a cycle, meaning there must be another edge
e' across the cut (S, V-S) (note that weight(e') ≥ weight(e))
4. Create T' by replacing e' with e: T' = T ∪ {e} – {e'}
5. T' is a tree since it is connected and still has |V|-1 edges
6. T' is an MST since:
1. weight(T') = weight(T) + w(e) – w(e')
2. both e and e' cross the cut (S, V-S)
3. by cut property e was the lightest edge across the cut
4. Therefore, w(e') = w(e) and T' is an MST
Thus, any (and only a) lightest edge across a cut will lead to an MST
1. Given top image, show the new image and array representation after union(B, G)
2. What is the time and space complexity for Kruskal's algorithm with this data
structure? CS 312 – Greedy Algorithms 27
Kruskal Algorithm Complexity
Decreasekey does not update path length, but updates the key with the
decreased edge cost between v and z
Almost the same as Dijkstra's Algorithm, same complexity values
CS 312 – Greedy Algorithms 32
Prim’s Algorithm
4 8 3
7
4 8 3
7
4 8 3
7
1 2 X: S = {5}
1 2 3 1: 4
{4,5} S = {4,5} 2: 4
6 4 5 6
4 3: 5
6: 8
3 8
4 5 6 7: 4
4 8 3
7
4 8 3
7
4 8 3
7
4 8 3
7
– Tree must have exactly 2n-1 nodes; n leafs and n-1 internal nodes
CS 312 – Greedy Algorithms 50
Huffman Algorithm
Using Huffman show a final tree, tree cost, and encoding given:
– A: 10
– B: 5
– C: 15
– D: 5
– E: 40
If we use a binary heap implementation for the priority queue, then
what is Huffman time and space complexity?
CS 312 – Greedy Algorithms 52
Huffman Algorithm
Until all elements in U covered, select Si with the largest number of uncovered elements.
CS 312 – Greedy Algorithms 62
Set Cover
Set Cover is another NP-complete problem, thus there
exists no polynomial solution to find the optimum
It can be proven that our simple greedy algorithm gives an
answer within ln(n) of the optimal solution
Thus, if k is the optimal solution, our simple greedy
algorithm is guaranteed to find a value between k and
kln(n)
– ln(n) is the approximation factor
There is provably no polynomial time algorithm for Set
Cover with a smaller approximation factor
Thin
Stuffed A2
Thick
A1
Veggie Meaty
CS 312 – Greedy Algorithms 65
Decision Tree Learning
Assume A1 is binary feature (Veggie, Meaty)
Assume A2 is 3 value feature (Crust: Thin/Stuffed/Thick)
Circles are good pizzas and Squares are great pizzas
Greedily divide on attribute which gives the purest children
A1
Thin Thin
Stuffed A2 Stuffed A2
Thick Thick
A1 A1
Veggie Meaty Veggie Meaty
CS 312 – Greedy Algorithms 66
Decision Tree Learning
Assume A1 is binary feature (Veggie, Meaty)
Assume A2 is 3 value feature (Crust: Thin/Stuffed/Thick)
Circles are good pizzas and Squares are great pizzas
Greedily divide on attribute which gives the purest children
A1
A2
Thin Thin
Stuffed A2 Stuffed A2
Thick Thick
A1 A1
Veggie Meaty Veggie Meaty
CS 312 – Greedy Algorithms 67
Decision Tree Learning
Assume A1 is binary feature (Veggie, Meaty)
Assume A2 is 3 value feature (Crust: Thin/Stuffed/Thick)
Circles are good pizzas and Squares are great pizzas
Greedily divide on attribute which gives the purest children
A1
A2
Thin Thin
Stuffed A2 Stuffed A2
Thick Thick
A1 A1
Veggie Meaty Veggie Meaty
CS 312 – Greedy Algorithms 68
Conclusion on Greedy Algorithms
Leads to optimal solutions for a number of important
problems
– MST, Huffman, Horn Formulas, etc.
Often do not lead to optimal solutions, but very powerful
and common approximation approach for otherwise
exponential tasks
– TSP, Decision trees, Set Cover, etc.
Usually lead to relatively simple and efficient algorithms,
avoiding expensive look ahead