Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.

Slides:



Advertisements
Similar presentations
Greedy Technique The first key ingredient is the greedy-choice property: a globally optimal solution can be arrived at by making a locally optimal (greedy)
Advertisements

Chapter 23 Minimum Spanning Tree
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Greedy Algorithms Greed is good. (Some of the time)
Analysis of Algorithms
Greed is good. (Some of the time)
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Analysis of Algorithms CS 477/677
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Design and Analysis of Algorithms Minimum Spanning trees
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Analysis of Algorithms
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
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.
Introduction to Algorithms L ECTURE 14 (Chap. 22 & 23) Greedy Algorithms I 22.1 Graph representation 23.1 Minimum spanning trees 23.1 Optimal substructure.
Spring 2015 Lecture 11: Minimum Spanning Trees
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
Data Structures and Algorithms A. G. Malamos
 2004 SDU Lecture 7- Minimum Spanning Tree-- Extension 1.Properties of Minimum Spanning Tree 2.Secondary Minimum Spanning Tree 3.Bottleneck.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Lectures on Greedy Algorithms and Dynamic Programming
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)
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 Algorithms CSCI 235, Fall 2015 Lecture 29 Greedy Algorithms.
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.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Algorithms
Review: Dynamic Programming
Introduction to Algorithms`
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Data Structures – LECTURE 13 Minumum spanning trees
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Kruskal’s Minimum Spanning Tree Algorithm
Analysis of Algorithms CS 477/677
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
Greedy Algorithms Comp 122, Spring 2004.
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Presentation transcript:

Comp 122, Spring 2004 Greedy Algorithms

greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.  Problems exhibit optimal substructure (like DP).  Problems also exhibit the greedy-choice property. »When we have a choice to make, make the one that looks best right now. »Make a locally optimal choice in hope of getting a globally optimal solution.

greedy - 3 Lin / Devi Comp 122, Fall 2003 Greedy Strategy  The choice that seems best at the moment is the one we go with. »Prove that when there is a choice to make, one of the optimal choices is the greedy choice. Therefore, it’s always safe to make the greedy choice. »Show that all but one of the subproblems resulting from the greedy choice are empty.

greedy - 4 Lin / Devi Comp 122, Fall 2003 Activity-selection Problem  Input: Set S of n activities, a 1, a 2, …, a n. »s i = start time of activity i. »f i = finish time of activity i.  Output: Subset A of maximum number of compatible activities. »Two activities are compatible, if their intervals don’t overlap. Example: Activities in each line are compatible.

greedy - 5 Lin / Devi Comp 122, Fall 2003 Optimal Substructure  Assume activities are sorted by finishing times. »f 1  f 2  …  f n.  Suppose an optimal solution includes activity a k. »This generates two subproblems. »Selecting from a 1, …, a k-1, activities compatible with one another, and that finish before a k starts (compatible with a k ). »Selecting from a k+1, …, a n, activities compatible with one another, and that start after a k finishes. »The solutions to the two subproblems must be optimal. Prove using the cut-and-paste approach.

greedy - 6 Lin / Devi Comp 122, Fall 2003 Recursive Solution  Let S ij = subset of activities in S that start after a i finishes and finish before a j starts.  Subproblems: Selecting maximum number of mutually compatible activities from S ij.  Let c[i, j] = size of maximum-size subset of mutually compatible activities in S ij. Recursive Solution:

greedy - 7 Lin / Devi Comp 122, Fall 2003 Greedy-choice Property  The problem also exhibits the greedy-choice property. »There is an optimal solution to the subproblem S ij, that includes the activity with the smallest finish time in set S ij. »Can be proved easily.  Hence, there is an optimal solution to S that includes a 1.  Therefore, make this greedy choice without solving subproblems first and evaluating them.  Solve the subproblem that ensues as a result of making this greedy choice.  Combine the greedy choice and the solution to the subproblem.

greedy - 8 Lin / Devi Comp 122, Fall 2003 Recursive Algorithm Recursive-Activity-Selector (s, f, i, j) 1.m  i+1 2.while m < j and s m < f i 3. do m  m+1 4.if m < j 5. then return {a m }  Recursive-Activity-Selector(s, f, m, j) 6. else return  Recursive-Activity-Selector (s, f, i, j) 1.m  i+1 2.while m < j and s m < f i 3. do m  m+1 4.if m < j 5. then return {a m }  Recursive-Activity-Selector(s, f, m, j) 6. else return  Initial Call: Recursive-Activity-Selector (s, f, 0, n+1) Complexity:  (n) Straightforward to convert the algorithm to an iterative one. See the text.

greedy - 9 Lin / Devi Comp 122, Fall 2003 Typical Steps  Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve.  Prove that there’s always an optimal solution that makes the greedy choice, so that the greedy choice is always safe.  Show that greedy choice and optimal solution to subproblem  optimal solution to the problem.  Make the greedy choice and solve top-down.  May have to preprocess input to put it into greedy order. »Example: Sorting activities by finish time.

greedy - 10 Lin / Devi Comp 122, Fall 2003 Elements of Greedy Algorithms  Greedy-choice Property. »A globally optimal solution can be arrived at by making a locally optimal (greedy) choice.  Optimal Substructure.

Comp 122, Spring 2004 Minimum Spanning Trees

greedy - 12 Lin / Devi Comp 122, Fall 2003 Minimum Spanning Trees Given: Connected, undirected, weighted graph, G Find: Minimum - weight spanning tree, T Example: bc a def a bc fed Acyclic subset of edges(E) that connects all vertices of G.

greedy - 13 Lin / Devi Comp 122, Fall 2003 Generic Algorithm “Grows” a set A. A is subset of some MST. Edge is “safe” if it can be added to A without destroying this invariant. A :=  ; while A not complete tree do find a safe edge (u, v); A := A  {(u, v)} od A :=  ; while A not complete tree do find a safe edge (u, v); A := A  {(u, v)} od

greedy - 14 Lin / Devi Comp 122, Fall 2003  cut partitions vertices into disjoint sets, S and V – S. bca def this edge crosses the cut a light edge crossing cut (could be more than one) Definitions cut respects the edge set {(a, b), (b, c)} one endpoint is in S and the other is in V – S. no edge in the set crosses the cut

greedy - 15 Lin / Devi Comp 122, Fall 2003 Proof: Let T be a MST that includes A. Case: (u, v) in T. We’re done. Case: (u, v) not in T. We have the following: u y x v edge in A cut shows edges in T Theorem 23.1 Theorem 23.1: Let (S, V-S) be any cut that respects A, and let (u, v) be a light edge crossing (S, V-S). Then, (u, v) is safe for A. Theorem 23.1: Let (S, V-S) be any cut that respects A, and let (u, v) be a light edge crossing (S, V-S). Then, (u, v) is safe for A. (x, y) crosses cut. Let T´ = T - {(x, y)}  {(u, v)}. Because (u, v) is light for cut, w(u, v)  w(x, y). Thus, w(T´) = w(T) - w(x, y) + w(u, v)  w(T). Hence, T´ is also a MST. So, (u, v) is safe for A.

greedy - 16 Lin / Devi Comp 122, Fall 2003 In general, A will consist of several connected components. Corollary Corollary: If (u, v) is a light edge connecting one CC in (V, A) to another CC in (V, A), then (u, v) is safe for A. Corollary: If (u, v) is a light edge connecting one CC in (V, A) to another CC in (V, A), then (u, v) is safe for A.

greedy - 17 Lin / Devi Comp 122, Fall 2003 Kruskal’s Algorithm  Starts with each vertex in its own component.  Repeatedly merges two components into one by choosing a light edge that connects them (i.e., a light edge crossing the cut between them).  Scans the set of edges in monotonically increasing order by weight.  Uses a disjoint-set data structure to determine whether an edge connects vertices in different components.

greedy - 18 Lin / Devi Comp 122, Fall 2003 Prim’s Algorithm  Builds one tree, so A is always a tree.  Starts from an arbitrary “root” r.  At each step, adds a light edge crossing cut ( V A, V - V A ) to A. »V A = vertices that A is incident on.

greedy - 19 Lin / Devi Comp 122, Fall 2003 Prim’s Algorithm  Uses a priority queue Q to find a light edge quickly.  Each object in Q is a vertex in V - V A.  Key of v is minimum weight of any edge ( u, v ), where u  V A.  Then the vertex returned by Extract-Min is v such that there exists u  V A and ( u, v ) is light edge crossing ( V A, V - V A ).  Key of v is  if v is not adjacent to any vertex in V A.

greedy - 20 Lin / Devi Comp 122, Fall 2003 Q := V[G]; for each u  Q do key[u] :=  od; key[r] := 0;  [r] := NIL; while Q   do u := Extract - Min(Q); for each v  Adj[u] do if v  Q  w(u, v) < key[v] then  [v] := u; key[v] := w(u, v) fi od Q := V[G]; for each u  Q do key[u] :=  od; key[r] := 0;  [r] := NIL; while Q   do u := Extract - Min(Q); for each v  Adj[u] do if v  Q  w(u, v) < key[v] then  [v] := u; key[v] := w(u, v) fi od Complexity: Using binary heaps: O(E lg V). Initialization – O(V). Building initial queue – O(V). V Extract-Min’s – O(V lgV). E Decrease-Key’s – O(E lg V). Using Fibonacci heaps: O(E + V lg V). (see book) Prim’s Algorithm Note: A = {(v,  [v]) : v  v - {r} - Q}.  decrease-key operation

greedy - 21 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/  c/  a/0 d/  e/  f/  Q = a b c d e f 0   Not in tree

greedy - 22 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/5 c/  a/0 d/11 e/  f/  Q = b d c e f 5 11  

greedy - 23 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/5 c/7 a/0 d/11e/3 f/  Q = e c d f 

greedy - 24 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/5 c/1 a/0 d/0 e/3 f/ Q = d c f 0 1 2

greedy - 25 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/5 c/1 a/0 d/0e/3 f/ Q = c f 1 2

greedy - 26 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/5c/1 a/0 d/0 e/3 f/ Q = f -3

greedy - 27 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm b/5c/1 a/0 d/0 e/3 f/ Q = 

greedy - 28 Lin / Devi Comp 122, Fall 2003 Example of Prim’s Algorithm 0 b/5c/1 a/0 d/0 e/3f/