# Kruskals algorithm selects the lightest edge e1 x y c

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: d Design 20 / 49 Correctness Proof of Kruskal's Algorithm The proof is by induction. Kruskal's algorithm selects the lightest edge e1 = (x, y). By Greedy Choice Property, there exists an optimal MST of G that contains e1 . By induction hypothesis, Kruskal's algorithm construct a MST T in the graph G = ((V - {x, y} {z}), E ) which is obtained from G by merging the two end vertices x, y of e1 . c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 20 / 49 Correctness Proof of Kruskal's Algorithm The proof is by induction. Kruskal's algorithm selects the lightest edge e1 = (x, y). By Greedy Choice Property, there exists an optimal MST of G that contains e1 . By induction hypothesis, Kruskal's algorithm construct a MST T in the graph G = ((V - {x, y} {z}), E ) which is obtained from G by merging the two end vertices x, y of e1 . By the Optimal Substructure Property of MST, T = T {e1 } is a MST of G. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 20 / 49 Correctness Proof of Kruskal's Algorithm The proof is by induction. Kruskal's algorithm selects the lightest edge e1 = (x, y). By Greedy Choice Property, there exists an optimal MST of G that contains e1 . By induction hypothesis, Kruskal's algorithm construct a MST T in the graph G = ((V - {x, y} {z}), E ) which is obtained from G by merging the two end vertices x, y of e1 . By the Optimal Substructure Property of MST, T = T {e1 } is a MST of G. This T is the tree constructed by Kruskal's algorithm. Hence, Kruskal's algorithm indeed returns a MST. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 20 / 49 Outline 1 Greedy Algorithms Elements of Greedy Algorithms Greedy Choice Property for Kruskal's Algorithm 0/1 Knapsack Problem Activity Selection Problem Scheduling All Intervals 2 3 4 5 6 c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 21 / 49 0/1 Knapsack Problem We mentioned that some seemingly intuitive greedy strategies do not really work. Here is an example. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 22 / 49 0/1 Knapsack Problem We mentioned that some seemingly intuitive greedy strategies do not really work. Here is an example. 0/1 Knapsack Problem Input: n itemi (1 i n). Each itemi has an integer weight w[i] 0 and a profit p[i] 0. A knapsack with an integer capacity K. Find: A subset of items so that the total weight of the selected items is at most K, and the total profit is maximized. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 22 / 49 0/1 Knapsack Problem We mentioned that some seemingly intuitive greedy strategies do not really work. Here is an example. 0/1 Knapsack Problem Input: n itemi (1 i n). Each itemi has an integer weight w[i] 0 and a profit p[i] 0. A knapsack with an integer capacity K. Find: A subset of items so that the total weight of the selected items is at most K, and the total profit is maximized. There are several greedy strategies that seem reasonable. But none of them works. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 22 / 49 0/1 Knapsack Problem Greedy Strategy 1 Since the goal is to maximize the profit without exceeding the capacity, we fill the items in the order of increasing weights. Namely: Sort the items by increasing item weight: w[1] w[2] . Fill the knapsack in the order item1 , item2 , ... until no more items can be put into the knapsack without exceeding the capacity. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 23 / 49 0/1 Knapsack Problem Greedy Strategy 1 Since the goal is to maximize the profit without exceeding the capacity, we fill the items in the order of increasing weights. Namely: Sort the items by increasing item weight: w[1] w[2] . Fill the knapsack in the order item1 , item2 , ... until no more items can be put into the knapsack without exceeding the capacity. Counter Example: n = 2, w[1] = 2, w[2] = 4, p[1] = 2, p[2] = 3, K = 4. This strategy puts item1 into the knapsack with total profit 2. The optimal solution: put item2 into the knapsack with total profit 3. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 23 / 49 0/1 Knapsack Problem For this greedy strategy, we can still show the Optimal Substructure Property holds: if S is an optimal solution, that contains the item1 , for the original input, c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 24 / 49 0/1 Knapsack Problem For this greedy strategy, we can still show the Optimal Substructure Property holds: if S is an optimal solution, that contains the item1 , for the original input, then S - {item1 } is an optimal solution for the input consisting of item2 , item3 , , itemn and the knapsack with capacity K - w[1]. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 24 / 49 0/1 Knapsack Problem For this greedy strategy, we can still show the Optimal Substructure Property holds: if S is an optimal solution, that contains the item1 , for the original input, then S - {item1 } is an optimal solution for the input consisting of item2 , item3 , , itemn and the knapsack with capacity K - w[1]. However, we cannot prove the Greedy Choice Property: We are not able to show there is an optimal solution...
View Full Document

## This note was uploaded on 02/27/2012 for the course CSE 431/531 taught by Professor Xinhe during the Fall '11 term at SUNY Buffalo.

Ask a homework question - tutors are online