Greedy Algorithms
Change Making ProblemHow to make 48 cents of change usingcoins of denominations of 25, 10, 5, and 1so that the total numbe...
Greedy AlgorithmsA greedy algorithm makes a locally optimal choice in the hopethat this choice will lead to a globally opt...
Applications of the Greedy StrategyOptimal solutions:  change making  Minimum Spanning Tree (MST)  Single-source shortest ...
Minimum Spanning Tree (MST)Spanning tree of a connected graph G: a connected acyclicsubgraph (tree) of G that includes all...
Prim’s MST algorithmStart with a tree , T0 ,consisting of one vertex“Grow” tree one vertex/edge at a time Construct a seri...
Examples         4       3 1                     1         6 2                         4     2       3                    ...
The Key Point   NotationsT: the expanding subtree.Q: the remaining vertices.At each stage, the key point of expanding the ...
ALGORITHM MST-PRIM( G, w, r ) //w: weight; r: root, the starting vertex1.    for each u ∈ V[G]2.       do key[u] ← ∞3.    ...
Notes about Prim’s algorithmNeed priority queue for locating the nearest vertex  Use unordered array to store the priority...
Another Greedy Algorithm for MST: Kruskal   Edges are initially sorted by increasing weight   Start with an empty forest  ...
Kruskal’s AlgorithmALGORITHM Kruscal(G)//Input: A weighted connected graph G = <V, E>//Output: ET, the set of edges compos...
Shortest Paths – Dijkstra’s Algorithm    Shortest Path Problems      All pair shortest paths (Floy’s algorithm)      Singl...
Prim’s and Dijkstra’s AlgorithmsGenerate different kinds of spanning trees   Prim’s: a minimum spanning tree.   Dijkstra’s...
Shortest Paths-Dijkstra’s AlgorithmDijkstra’s algorithm: Similar to Prim’s MST algorithm, with the followingdifference:   ...
Dijkstra’s AlgorithmALGORITHM Dijkstra(G, s)//Input: A weighted connected graph G = <V, E> and a source vertex s//Output: ...
Notes on Dijkstra’s AlgorithmDoesn’t work with negative weights  Can you give a counter example?Applicable to both undirec...
Test 3, Apr. 13th, chapter 7,8,9Programming 2 (Apr. 21st)Homework 5 dueInstructor evaluation                              ...
Upcoming SlideShare
Loading in …5
×

Algorithm chapter 9

1,419 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,419
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
47
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Algorithm chapter 9

  1. 1. Greedy Algorithms
  2. 2. Change Making ProblemHow to make 48 cents of change usingcoins of denominations of 25, 10, 5, and 1so that the total number of coins is thesmallest?The idea: make the locally best choice at each step.Is the solution optimal? 2
  3. 3. Greedy AlgorithmsA greedy algorithm makes a locally optimal choice in the hopethat this choice will lead to a globally optimal solution.The choice made at each step must be: Feasible Satisfy the problem’s constraints locally optimal Be the best local choice among all feasible choices Irrevocable Once made, the choice can’t be changed on subsequent steps.Do greedy algorithms always yield optimal solutions? Example: change making problem with a denomination set of 7, 5 and 1? 3
  4. 4. Applications of the Greedy StrategyOptimal solutions: change making Minimum Spanning Tree (MST) Single-source shortest paths Huffman codesApproximations: Traveling Salesman Problem (TSP) Knapsack problem other optimization problems 4
  5. 5. Minimum Spanning Tree (MST)Spanning tree of a connected graph G: a connected acyclicsubgraph (tree) of G that includes all of G’s vertices.Minimum Spanning Tree of a weighted, connected graph G: aspanning tree of G of minimum total weight.Example: 4 3 1 1 6 2 4 2 3 5
  6. 6. Prim’s MST algorithmStart with a tree , T0 ,consisting of one vertex“Grow” tree one vertex/edge at a time Construct a series of expanding subtrees T1, T2, … Tn-1. .At each stage construct Ti+1 from Ti by adding the minimum weight edge connecting a vertex in tree (Ti) to one not yet in tree choose from “fringe” edges Fringe edges: one vertex is in Ti and the other is not. (this is the “greedy” step!) Unseen edges: both vertices are not in Ti. Or (another way to understand it) expanding each tree (Ti) in a greedy manner by attaching to it the nearest vertex not in that tree. (a vertex not in the tree connected to a vertex in the tree by an edge of the smallest weight)Algorithm stops when all vertices are included 6
  7. 7. Examples 4 3 1 1 6 2 4 2 3 5 a b 1 4 6 2 3 7 c d e 7
  8. 8. The Key Point NotationsT: the expanding subtree.Q: the remaining vertices.At each stage, the key point of expanding the current subtree T is to determine which vertex in Q is the nearest vertex. Q can be thought of as a priority queue: The key(priority) of each vertex, key[v], means the minimum weight edge from v to a vertex in T. Key[v] is ∞ if v is not linked to any vertex in T. The major operation is to to find and delete the nearest vertex (v, for which key[v] is the smallest among all the vertices) Remove the nearest vertex v from Q and add it and the corresponding edge to T. With the occurrence of that action, the key of v’s neighbors will be changed. 8
  9. 9. ALGORITHM MST-PRIM( G, w, r ) //w: weight; r: root, the starting vertex1. for each u ∈ V[G]2. do key[u] ← ∞3. π[u] ← NIL // π[u] : the parent of u4. key[r] ← 05. Q ← V[G] //Now the priority queue, Q, has been built.6. while Q ≠ ∅7. do u ← Extract-Min(Q) //remove the nearest vertex from Q8. for each v ∈ Adj[u] // update the key for each of v’s adjacent nodes.9. do if v ∈ Q and w(u,v) < key[v]10. then π[v] ← u11. Key[v] ← w(u,v) 9
  10. 10. Notes about Prim’s algorithmNeed priority queue for locating the nearest vertex Use unordered array to store the priority queue:Efficiency: Θ(n2) use min-heap to store the priority queueEfficiency: For graph with n vertices and m edges: (n + m) logn Key decreases/deletion from min-heapnumber of stages number of edges considered(min-heap deletions) (min-heap key decreases) O(m log n) 10
  11. 11. Another Greedy Algorithm for MST: Kruskal Edges are initially sorted by increasing weight Start with an empty forest “grow” MST one edge at a time intermediate stages usually have forest of trees (not connected) at each stage add minimum weight edge among those not yet used that does not create a cycle 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 cycles algorithm stops when all vertices are included 11
  12. 12. Kruskal’s AlgorithmALGORITHM Kruscal(G)//Input: A weighted connected graph G = <V, E>//Output: ET, the set of edges composing a minimum spanning tree of G.Sort E in nondecreasing order of the edge weights w(ei1) <= … <= w(ei|E|)ET ∅; ecounter 0 //initialize the set of tree edges and its sizek 0while encounter < |V| - 1 do k k+1 if ET U {eik} is acyclic ET ET U {eik} ; ecounter ecounter + 1return ET 12
  13. 13. Shortest Paths – Dijkstra’s Algorithm Shortest Path Problems All pair shortest paths (Floy’s algorithm) Single Source Shortest Paths Problem (Dijkstra’s algorithm): Given a weighted graph G, find the shortest paths from a source vertex s to each of the other vertices. 4 a b 3 2 5 6 7 4c d e 13
  14. 14. Prim’s and Dijkstra’s AlgorithmsGenerate different kinds of spanning trees Prim’s: a minimum spanning tree. Dijkstra’s : a spanning tree rooted at a given source s, such that the distance from s to every other vertex is the shortest.Different greedy strategies Prims’: Always choose the closest (to the tree) vertex in the priority queue Q to add to the expanding tree VT. Dijkstra’s : Always choose the closest (to the source) vertex in the priority queue Q to add to the expanding tree VT.Different labels for each vertex Prims’: parent vertex and the distance from the tree to the vertex.. Dijkstra’s : parent vertex and the distance from the source to the vertex. 14
  15. 15. Shortest Paths-Dijkstra’s AlgorithmDijkstra’s algorithm: Similar to Prim’s MST algorithm, with the followingdifference: Start with tree consisting of one vertex. “Grow” tree one vertex/edge at a time. Construct a series of expanding subtrees T1, T2, … Keep track of shortest path from source to each of the vertices in Ti At each stage construct Ti+1 from Ti: add minimum weight edge connecting a vertex in tree (Ti) to one not yet in tree choose from “fringe” edges (this is the “greedy” step!) edge (u*,u) with lowest du* + w(u*, u) Algorithm stops when all vertices are included. 15
  16. 16. Dijkstra’s AlgorithmALGORITHM Dijkstra(G, s)//Input: A weighted connected graph G = <V, E> and a source vertex s//Output: The length dv of a shortest path from s to v and its penultimate vertex pv for every vertex v in VInitialize (Q) //initialize vertex priority in the priority queuefor every vertex v in V do dv ∞ ; P v null // Pv , the parent of v insert(Q, v, dv) //initialize vertex priority in the priority queueds 0; Decrease(Q, s, ds) //update priority of s with ds, making ds, the minimumVT ∅for i 0 to |V| - 1 do //produce |V| - 1 edges for the tree u* DeleteMin(Q) //delete the minimum priority element VT VT U {u*} //expanding the tree, choosing the locally best vertex for every vertex u in V – VT that is adjacent to u* do if du* + w(u*, u) < du du du + w(u*, u); pu u* Decrease(Q, u, du) 16
  17. 17. Notes on Dijkstra’s AlgorithmDoesn’t work with negative weights Can you give a counter example?Applicable to both undirected and directedgraphsEfficiency Use unordered array to store the priority queue: Θ(n2) Use min-heap to store the priority queue: O(m log n) 17
  18. 18. Test 3, Apr. 13th, chapter 7,8,9Programming 2 (Apr. 21st)Homework 5 dueInstructor evaluation 18

×