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” edges (this is the greedy step)choose from “fringe” edges (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 Prim’s algorithm formally Algorithm Prim(G) V T  {v 0 } E T  { } for i  1 to |V|-1 do find a minimum weight edge e*=(v*,u*) among all the edges (v,u) such that v Є V T and u Є V-V T V T  V T ∪ {u*} E T  E T ∪ {e*} return E T

7 Design and Analysis of Algorithms - Chapter 97 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 Θ(m log n) Θ(m log n) number of stages (min-heap deletions) number of edges considered (min-heap insertions) insertion/deletion from min-heap

8 Design and Analysis of Algorithms - Chapter 98 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

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

10 10 Kruskal’s algorithm Algorithm Kruskal(G) Sort E in nondecreasing order of the weights E T  { }; k  0; ecounter  0 while ecounter < |V|-1 k  k+1 if E T ∪ {e ik } is acyclic E T  E T ∪ {e ik } ecounter  ecounter + 1 return E T

11 Design and Analysis of Algorithms - Chapter 911 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

12 Design and Analysis of Algorithms - Chapter 912 Shortest paths-Dijkstra’s algorithm b Single Source Shortest Paths Problem: Given a weighted graph G, find the shortest paths from a source vertex s to each of the other vertices. b Doesn’t work with negative weights b Applicable to both undirected and directed graphs

13 Design and Analysis of Algorithms - Chapter 913 Shortest paths-Dijkstra’s algorithm 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)

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

15 Design and Analysis of Algorithms - Chapter 915 Dijkstra’s algorithm Algorithm Dijkstra(G,s) Initialize(Q) for every vertex v Є V do d v  ∞; p v  null; Insert(Q,v, d v ) d s  0; Decrease(Q,s,d s ); V T  0 for i  0 to |V|-1 do u*  DeleteMin(Q); V T  V T ∪ {u*} for every vertex u in V-V T that is adjacent to u* do if d u* +w(u*,u)<d u d u  d u +w(u*,u); p u  u*; Decrease(Q,u,d u ) 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