1 Greedy 2 Jose Rolim University of Geneva
Algorithmique Greedy 2Jose Rolim2 Examples Greedy Minimum Spanning Trees Shortest Paths Dijkstra
Algorithmique Greedy 2Jose Rolim3 Definition MST Given a connected graph G = (V, E), with weight function w : E --> R Min-weight connected subgraph Spanning tree T: A tree that includes all nodes from V T = (V, E’), where E’ E Weight of T: W( T ) = w(e) Minimum spanning tree (MST): A tree with minimum weight among all spanning trees
Algorithmique Greedy 2Jose Rolim4 Example:
Algorithmique Greedy 2Jose Rolim5 MST: MST for given G may not be unique Since MST is a spanning tree: # edges : |V| - 1 If the graph is unweighted: All spanning trees have same weight
Algorithmique Greedy 2Jose Rolim6 Kruskal’s algorithm (Greedy) Start with A empty, and each vertex being its own connected component Repeatedly merge two components by connecting them with a light edge crossing them Two issues: Maintain sets of components Choose light edges
Algorithmique Greedy 2Jose Rolim7 Pseudo-code
Algorithmique Greedy 2Jose Rolim8 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim9 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim10 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim11 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim12 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim13 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim14 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim15 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim16 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim17 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim18 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim19 The Greedy Algorithm in Action
Algorithmique Greedy 2Jose Rolim20 The Greedy Algorithm in Action
The Greedy Algorithm in Action Node First root node
The Greedy Algorithm in Action Node First
The Greedy Algorithm in Action Node First
The Greedy Algorithm in Action Node First
The Greedy Algorithm in Action Node First
The Greedy Algorithm in Action Node First
Algorithmique Greedy 2Jose Rolim27 Complexity Time complexity: #make-set, find-set and union operations: O(|V| + |E|) O( (|V| + |E|) (|V| + |E|)) Sorting: O(|E| log |E|) = O(|E| log |V|) Total: O(|E| log |V|)
Algorithmique Greedy 2Jose Rolim28 Prim’s algorithm (also Greedy) Start with an arbitrary node from V Instead of maintaining a forest, grow a MST At any time, maintain a MST for V’ V At any moment, find a light edge connecting V’ with (V-V’) I.e., the edge with smallest weight connecting some vertex in V’ with some vertex in V-V’ !
Algorithmique Greedy 2Jose Rolim29 Issues: Again two issues: Maintain the tree already build at any moment Easy: simply a tree rooted at r : the starting node Find the next light edge efficiently For v V - V’, define key(v) = the min distance between v and some node from V’ At any moment, find the node with min key.
Algorithmique Greedy 2Jose Rolim30 Pseudo-code
Algorithmique Greedy 2Jose Rolim31 Prim’s Algorithm in Action The minimum cost arc from yellow nodes to green nodes can be found by placing arc values in a priority queue.
Algorithmique Greedy 2Jose Rolim32 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim33 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim34 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim35 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim36 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim37 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim38 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim39 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim40 20 Prim’s Algorithm in Action
Algorithmique Greedy 2Jose Rolim41 Complexity Time complexity # insert: O(|V|) # Decrease-Key: O( |E|) # Extract-Min O( |V| ) Using heap for priority queue: Each operation is O ( log |V| ) Total time complexity: O (|E| log |V|)
Algorithmique Greedy 2Jose Rolim42 Single-Source Shortest Paths Problem Definition Shortest paths Dijkstra’s algorithm (can be viewed as a greedy algorithm)
Algorithmique Greedy 2Jose Rolim43 Problem Definition: Real problem: A motorist wishes to find the shortest possible route from Chicago to Boston.Given a road map of the United States on which the distance between each pair of adjacent intersections is marked, how can we determine this shortest route? Formal definition: Given a graph G=(V, E, W), where each edge has a weight, find a shortest path from s to v for some interesting vertices s and v. s—source v—destination.
Algorithmique Greedy 2Jose Rolim44 Shortest path: The weight of path p= is the sum of the weights of its constituent edges: The cost of the shortest path from s to v is denoted as (s, v).
Algorithmique Greedy 2Jose Rolim45 Representing shortest paths: we maintain for each vertex v V, a predecessor [ v] that is the vertex in the shortest path right before v. With the values of, a backtracking process can give the shortest path. (We will discuss that after the algorithm is given)
Algorithmique Greedy 2Jose Rolim46 Dijkstra’s Algorithm: Dijkstra’s algorithm assumes that w(e) 0 for each e in the graph. maintain a set S of vertices such that Every vertex v S, d[v]= (s, v), i.e., the shortest-path from s to v has been found. (Intial values: S=empty, d[s]=0 and d[v]= ) (a) select the vertex u V-S such that d[u]=min {d[x]|x V-S}. Set S=S {u} (b) for each node v adjacent to u do RELAX(u, v, w). Repeat step (a) and (b) until S=V.
Algorithmique Greedy 2Jose Rolim47 Implementation: a priority queue Q stores vertices in V-S, keyed by their d[] values. the graph G is represented by adjacency lists.
Algorithmique Greedy 2Jose Rolim s uv x y (a)
Algorithmique Greedy 2Jose Rolim49 0 5/s 10/s s uv x y 8 8 (b) (s,x) is the shortest path using one edge. It is also the shortest path from s to x.
Algorithmique Greedy 2Jose Rolim50 0 7/x 14/x 5/s 8/x s uv x y (c)
Algorithmique Greedy 2Jose Rolim51 0 7/x 13/y 5/s 8/x s uv x y (d)
Algorithmique Greedy 2Jose Rolim52 0 7/x 9/u 5/s 8/x s uv x y (e)
Algorithmique Greedy 2Jose Rolim53 Time complexity of Dijkstra’s Algorithm: Time complexity: #Extract-min: O(|V|) #Decrease-key = #Relax: O(|E|) Use binary heap for priority heap: Time complexity: O((|V| + |E|) log |V|) Use Fibonacci heap for priority heap: Amortized time complexity: O(|V| log |V| + |E|) Greedy algorithm: Any moment, select the lightest vertex in V-S to add to set S