lecture 19 greedy algorithms minimum spanning tree problem

38
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Upload: corey-nicholson

Post on 04-Jan-2016

233 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Lecture 19

Greedy Algorithms

Minimum Spanning Tree Problem

Page 2: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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

Page 3: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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.

Page 4: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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

Page 5: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Optimum Spanning Tree

Minimum Maximum

Page 6: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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): nn-2.

• Hence number of spanning trees can be very large => inefficient algorithm (exponential)

Page 7: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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

Page 8: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 9: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 10: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 11: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 12: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 13: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 14: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 15: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 16: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 17: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 18: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 19: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 20: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 21: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 22: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Prim’s Algorithm for MST

Page 23: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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

Page 24: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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

Page 25: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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)

Page 26: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

• Sort all edges O(m2 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

Page 27: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 28: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 29: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 30: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 31: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 32: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 33: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST

Page 34: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST: forest along the way

Page 35: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

Kruskal’s Algorithm for MST:Analysis

• Sort all edges O(m2 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(m2 log n)

Page 36: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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.

Page 37: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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)

Page 38: Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem

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