However the weight of any two msts of g must be the

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e ei , we may get a different MST. Runtime of Kruskal's algorithm: Sorting of edge list takes (m log m) time. Then we process the edges one by one. So the loop iterates m time. When processing an edge ei , we check if T {ei } contains a cycle or not. If not, add ei into T. If yes, do nothing. By using proper data structures, the processing of an edge ei can be done in O(log n) time. (The detail was discussed in CSE250). So the loop takes O(m log n) time. Since G is connected, m n. The total runtime is (m log m + m log n) = (m log m). c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 9 / 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 10 / 49 Elements of Greedy Algorithms Are we done? c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Elements of Greedy Algorithms Are we done? No! A big task is not done yet: How do we know Kruskal's algorithm is correct? c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Elements of Greedy Algorithms Are we done? No! A big task is not done yet: How do we know Kruskal's algorithm is correct? Namely, how do we know the tree constructed by Kruskal's algorithm is indeed a MST? c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Elements of Greedy Algorithms Are we done? No! A big task is not done yet: How do we know Kruskal's algorithm is correct? Namely, how do we know the tree constructed by Kruskal's algorithm is indeed a MST? You may have convinced yourself that we are using an obvious strategy towards the optimization goal. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Elements of Greedy Algorithms Are we done? No! A big task is not done yet: How do we know Kruskal's algorithm is correct? Namely, how do we know the tree constructed by Kruskal's algorithm is indeed a MST? You may have convinced yourself that we are using an obvious strategy towards the optimization goal. In this case, we are lucky: our intuition is correct. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Elements of Greedy Algorithms Are we done? No! A big task is not done yet: How do we know Kruskal's algorithm is correct? Namely, how do we know the tree constructed by Kruskal's algorithm is indeed a MST? You may have convinced yourself that we are using an obvious strategy towards the optimization goal. In this case, we are lucky: our intuition is correct. But in other cases, the strategies that seem equally obvious may lead to wrong solutions. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Elements of Greedy Algorithms Are we done? No! A big task is not done yet: How do we know Kruskal's algorithm is correct? Namely, how do we know the tree constructed by Kruskal's algorithm is indeed a MST? You may have convinced yourself that we are using an obvious strategy towards the optimization goal. In this case, we are lucky: our intuition is correct. But in other cases, the strategies that seem equally obvious may lead to wrong solutions. In general, the correctness of a greedy algorithm requires proof. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 11 / 49 Correctness Proof of Algorithms An algorithm A is correct, if it works on all inputs. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 12 / 49 Correctness Proof of Algorithms An algorithm A is correct, if it works on all inputs. If A works on some inputs, but not on some other inputs, then A is incorrect. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 12 / 49 Correctness Proof of Algorithms An algorithm A is correct, if it works on all inputs. If A works on some inputs, but not on some other inputs, then A is incorrect. To show A is correct, you must argue that for all inputs, A produces intended solution. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 12 / 49 Correctness Proof of Algorithms An algorithm A is correct, if it works on all inputs. If A works on some inputs, but not on some other inputs, then A is incorrect. To show A is correct, you must argue that for all inputs, A produces intended solution. To show A is incorrect, you only need to give a counter example input I: You show that, for this particular input I, the output from A is not the intended solution. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 12 / 49 Correctness Proof of Algorithms An algorithm A is correct, if it works on all inputs. If A works on some inputs, but not on some other inputs, then A is incorrect. To show A is correct, you must argue that for all inputs, A produces intended solution. To show A is incorrect, you only need to give a counter e...
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