Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally."— Presentation transcript:

1 Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally optimal b irrevocable Not all optimization problems can be approached in this manner!

2 Design and Analysis of Algorithms - Chapter 92 Applications of the Greedy Strategy b Optimal solutions: change makingchange making Minimum Spanning Tree (MST)Minimum Spanning Tree (MST) Single-source shortest pathsSingle-source shortest paths simple scheduling problemssimple scheduling problems Huffman codesHuffman codes b Approximations: Traveling Salesman Problem (TSP)Traveling Salesman Problem (TSP) Knapsack problemKnapsack problem other combinatorial optimization problemsother combinatorial optimization problems

3 Design and Analysis of Algorithms - Chapter 93 Minimum Spanning Tree (MST) b Spanning tree of a connected graph G: a connected acyclic subgraph of G that includes all of G’s vertices. b Minimum Spanning Tree of a weighted, connected graph G: a spanning tree of G of minimum total weight. b Example: 3 4 2 1 4 2 6 1 3

4 Design and Analysis of Algorithms - Chapter 94 Prim’s MST algorithm b Start with tree consisting of one vertex b “grow” tree one vertex/edge at a time to produce MST Construct a series of expanding subtrees T 1, T 2, …Construct a series of expanding subtrees T 1, T 2, … b at each stage construct T i+1 from T i : add minimum weight edge connecting a vertex in tree (T i ) to one not yet in tree choose from “fringe” edgeschoose from “fringe” edges (this is the “greedy” step!)(this is the “greedy” step!) b algorithm stops when all vertices are included

5 Design and Analysis of Algorithms - Chapter 95 Examples: 3 4 2 1 4 2 6 1 3 a edc b 1 5 2 4 6 3 7

6 6 Notes about Prim’s algorithm b Need to prove that this construction actually yields MST b Need priority queue for locating lowest cost fringe edge: use min-heap b Efficiency: For graph with n vertices and m edges: (n – 1 + m) log n (n – 1 + m) log n Θ(m log n) Θ(m log n) number of stages (min-heap deletions) number of edges considered (min-heap insertions) insertion/deletion from min-heap

7 Design and Analysis of Algorithms - Chapter 97 Another Greedy algorithm for MST: Kruskal b Start with empty forest of trees b “grow” MST one edge at a time intermediate stages usually have forest of trees (not connected)intermediate stages usually have forest of trees (not connected) b at each stage add minimum weight edge among those not yet used that does not create a cycle edges are initially sorted by increasing weightedges are initially sorted by increasing weight at each stage the edge may:at each stage the edge may: –expand an existing tree –combine two existing trees into a single tree –create a new tree need efficient way of detecting/avoiding cyclesneed efficient way of detecting/avoiding cycles b algorithm stops when all vertices are included

8 Design and Analysis of Algorithms - Chapter 98 Examples: 3 4 2 1 4 2 6 1 3 a edc b 1 5 2 4 6 3 7

9 9 Notes about Kruskal’s algorithm b Algorithm looks easier than Prim’s but is harder to implement (checking for cycles!)harder to implement (checking for cycles!) less efficient Θ(m log m)less efficient Θ(m log m) b Cycle checking: a cycle exists iff edge connects vertices in the same component. b Union-find algorithms – see section 9.2

10 Design and Analysis of Algorithms - Chapter 910 Shortest paths-Dijkstra’s algorithm b Single Source Shotest Paths Problem: Given a weighted graph G, find the shortest paths from a source vertex s to each of the other vertices. b Dijkstra’s algorithm: Similar to Prim’s MST algorithm, with the following difference: Start with tree consisting of one vertexStart with tree consisting of one vertex “grow” tree one vertex/edge at a time to produce MST“grow” tree one vertex/edge at a time to produce MST –Construct a series of expanding subtrees T 1, T 2, … Keep track of shortest path from source to each of the vertices in T iKeep track of shortest path from source to each of the vertices in T i at each stage construct T i+1 from T i : add minimum weight edge connecting a vertex in tree (T i ) to one not yet in treeat each stage construct T i+1 from T i : add minimum weight edge connecting a vertex in tree (T i ) to one not yet in tree –choose from “fringe” edges –(this is the “greedy” step!) algorithm stops when all vertices are includedalgorithm stops when all vertices are included edge (v,w) with lowest d(s,v) + d(v,w)

11 Design and Analysis of Algorithms - Chapter 911 Example: a edc b 1 5 2 4 6 3 7

12 Design and Analysis of Algorithms - Chapter 912 Notes on Dijkstra’s algorithm b Doesn’t work with negative weights b Applicable to both undirected and directed graphs b Efficiency:


Download ppt "Design and Analysis of Algorithms - Chapter 91 Greedy algorithms Optimization problems solved through a sequence of choices that are: b feasible b locally."

Similar presentations


Ads by Google