Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Chapter 23 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
Minimum Spanning Trees1 JFK BOS MIA ORD LAX DFW SFO BWI PVD
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Dijkstra's algorithm.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
UNCA CSCI November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
Minimum spanning trees (MST) Def: A spanning tree of a graph G is an acyclic subset of edges of G connecting all vertices in G. A sub-forest of G is an.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
CS38 Introduction to Algorithms Lecture 3 April 8, 2014.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some MST),
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Introduction to Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Minimum-Cost Spanning Tree
ICS 353: Design and Analysis of Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
ICS 353: Design and Analysis of Algorithms
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 417: Algorithms and Computational Complexity
Lecture 12 Algorithm Analysis
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Minimum Spanning Trees
Presentation transcript:

Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming algorithms, –greedy algorithms are iterative in nature. –An optimal solution is reached from local optimal solutions. –This approach does not work all the time. –A proof that the algorithm does what it claims is needed, and usually not easy to get.

Shortest Paths Problems Input: A graph with non-negative weights or costs associated with each edge. Output: The list of edges forming the shortest path. Sample problems: –Find shortest path between two named vertices –Find shortest path from S to all other vertices –Find shortest path between all pairs of vertices Will actually calculate only distances, not paths.

Shortest Paths Definitions  (A, B) is the shortest distance from vertex A to B. length(A, B) is the weight of the edge connecting A to B. –If there is no such edge, then length(A, B) = . A D C B

Single-Source Shortest Paths Problem: Given G=(V,E), start vertex s, find the shortest path from s to all other vertices. –Assume V={1, 2, …, n} and s=1 Solution: A greedy algorithm called Dijkstra’s Algorithm

Dijkstra’s Algorithm Outline Partition V into two sets: X={1} and Y= {2, 3, …, n} Initialize [i] for 1  i  n as follows: … Select y  Y such that [y] is minimum – [y] is the length of the shortest path from 1 to y that uses only vertices in set X. Remove y from Y, add it to X, and update [w] for each w  Y where (y,w)  E if the path through y is shorter.

Example ABCDE

Dijkstra’s Algorithm ABCDE Initial0  Process A Process

Dijkstra’s Algorithm Input: A weighted directed graph G = (V,E), where V = {1,2,…,n} Output: The distance from vertex 1 to every other vertex in G. 1. X = {1}; Y = {2,3,…,n}; [1]=0; 2. for y = 2 to n do 3. if y is adjacent to 1 then [y]=length[1,y]; 4. else [y]=  end if 5. end for 6. for j = 1 to n – 1 do 7. Let y  Y be such that [y] is minimum; 8. X = X  {y} // add vertex y to X 9. Y = Y  - {y} // delete vertex y from Y 10. for each edge (y,w) do 11. if w  Y and [y] + length[y,w] < [w] then 12. [w] = [y] + length[y,w] 13. end for 14. end for

Correctness of Dijkstra’s Algorithm Lemma: In Dijkstra’s algorithm, when a vertex y is chosen in Step 7, if its label [y] is finite then [y] =  [y]. Proof:

Time Complexity Mainly depends on how we implement step 7, i.e., finding y s.t. [y] is minimum. Approach 1: Scan through the vector representing current distances of vertices in Y: Approach 2: Use a min-heap to maintain vertices in the set Y:

Minimum Cost Spanning Trees Minimum Cost Spanning Tree (MST) Problem: Input: An undirected weighted connected graph G. Output: The subgraph of G that 1) has minimum total cost as measured by summing the weights of all the edges in the subset, and 2) keeps the vertices connected. –What does such subgraph look like?

MST Example ABCDE

Kruskal’s Algorithm Initially, each vertex is in its own MST. Merge two MST’s that have the shortest edge between them. –Use a priority queue to order the unprocessed edges. Grab next one at each step. How to tell if an edge connects two vertices that are already in the same MST? –Use the UNION/FIND algorithm with parent- pointer representation.

Example ABCDE

Kruskal’s MST Algorithm Sort the edges of E(G) by weight in non-decreasing order; For each vertex v  V(G) do New_Tree(v); // creating tree with one root node v T=  ; // MST initialized to empty While |T| < n - 1 do Let (u,v) be the next edge in E(G) if FIND(u)  FIND(v) then T = T  (u,v); UNION(u,v); Return T;

Asymptotic Analysis of Kruskal’s Algorithm

Correctness of Kruskal’s Algorithm Lemma: Algorithm Kruskal correctly finds a minimum cost spanning tree in a weighted undirected graph. Proof: Theorem: Algorithm Kruskal’s finds a minimum cost spanning tree in a weighted undirected graph in O(m log m) time, where m is the number of edges in G.

Prim’s Algorithm Very similar to Dijkstra’s algorithm Grows a minimum spanning tree from an arbitrary vertex u  V

Idea of Prim’s Algorithm X Y

Idea of Prim’s Algorithm (Cont.) X Y

Prim’s Algorithm 1. T =  ; X={1}; Y = V(G) – {1}; 2. while Y   do 3. Let (x,y) be of minimum weight such that x  X and y  Y. 4. X = X  {y}; 5. Y = Y – {y}; 6. T = T  {(x,y)}; 7. end while;

Example ABCDE

Prim’s MST Implementation As with Dijkstra’s algorithm, the key issue is determining which vertex is next closest. As with Dijkstra’s algorithm, the alternative is to use a priority queue (min-heap). Running times for the two implementations are identical to the corresponding Dijkstra’s algorithm implementations. For implementation details of the corresponding algorithms, check the book pages 245 and 247.

Correctness of Prim’s Algorithm Lemma: Algorithm Prim correctly finds a minimum cost spanning tree in a connected undirected graph. Proof: