Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.

Similar presentations


Presentation on theme: "Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem."— Presentation transcript:

1 Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

2 Example: Coin Changing Problem Give change using the minimum number of coins. Greedy approach: always use larger coins first Example when it doesn’t work: 16c due, coins of 12, 10, 5 and 1. Greedy: 12, 1, 1, 1, 1 Optimal: 10, 5, 1 Moral: need to prove that greedy produces optimal

3 Greedy Algorithm Applies to Optimization Problems (find minimum or maximum such that…) Strategy: construct the solution by choosing elements one at a time in a greedy fashion (local optimum item), hoping that it will produce the global optimum structure.

4 Minimum Spanning Tree Given undirected connected weighted graph (“costs” assigned to edges). Spanning tree: minimum connected subgraph Minimum weight (or cost) spanning tree: sum over all edges is minimum over all possible spanning trees

5 Optimum Spanning Tree Minimum Maximum

6 Naïve Solution Enumerate all trees, compute their costs Find minimum (resp. maximum) Why bad? Number of trees on n vertices is very large (exponential): n n-2. Hence number of spanning trees can be very large => inefficient algorithm (exponential)

7 Greedy Algorithms for MST Prim’s algorithm: start with some vertex v. At next step, choose the minimum edge adjacent to already constructed tree, while maintaining the tree property Kruskal’s algorithm: sort edges in increasing order, add one at a time while maintaining forest property

8 Prim’s Algorithm for MST

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23 Prim’s Algorithm: Invariant At each step maintain a tree Compute fringe: list of all edges out of the current tree Find minimum edge in the fringe Add it to the tree

24 Prim’s Algorithm: Analysis At each step add a vertex: n steps Compute fringe: list of all edges out of the current tree Find minimum edge in the fringe: O(m) Add it to the tree Total: O(nm) Naive

25 Prim’s Algorithm: Better Implementation and Analysis At each step add a vertex: n steps Compute fringe: list of all edges out of the current tree, keep as heap. Must look at all edges O(m) Find minimum edge in the fringe: O(log m) = O(log n) Add it to the tree Total: O(m+nlog n)

26 Kruskal’s Algorithm for MST Sort all edges O(m 2 log n) At each step, choose the next min weight edge from the list that does not create a cycle. Invariant: maintain a forest of the min weight edges encountered up to step k

27 Kruskal’s Algorithm for MST

28

29

30

31

32

33

34 Kruskal’s Algorithm for MST: forest along the way

35 Kruskal’s Algorithm for MST: Analysis Sort all edges O(m 2 log n) At each step, choose the next min weight edge from the list that does not create a cycle: O(log n). Total n-1 steps Total: O(m 2 log n)

36 Correctness Must prove that the greedy strategy produces the optimum solution Prim: show that if the min edge is not chosen at some step k, then it is not a MST (there exists a tree of smaller cost) Kruskal: show that min edge must be in MST; then next one, and next one etc.

37 Correctness of Prim’s Algorithm Assume MST unique By contradiction. Show that if the min fringe edge is not chosen at some step k, then it is not a MST (there exists a tree of smaller cost)

38 Prim’s Algorithm: Correctness Assume turquoise tree is MST but does not use red edge (min at this step) Red edge forms a cycle with tree edges, with at least one other edge in the fringe Removing this edge of the cycle and putting back the red edge creates a smaller cost tree Contradiction


Download ppt "Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem."

Similar presentations


Ads by Google