Greedy Algorithms and Matroids Andreas Klappenecker.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Greedy Algorithms, Part 2 Accessible Set Systems
Chapter 3 The Greedy Method 3.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Greedy Algorithms for Matroids Andreas Klappenecker.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
CSE 780 Algorithms Advanced Algorithms Greedy algorithm Job-select problem Greedy vs DP.
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.
Lecture 10 Matroid. Independent System Consider a finite set S and a collection C of subsets of S. (S,C) is called an independent system if i.e., it is.
Lecture 7: Greedy Algorithms II
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
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
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
1 Introduction to Approximation Algorithms. 2 NP-completeness Do your best then.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.
Accessible Set Systems Andreas Klappenecker. Matroid Let S be a finite set, and F a nonempty family of subsets of S, that is, F  P(S). We call (S,F)
1 Greedy algorithm 叶德仕 2 Greedy algorithm’s paradigm Algorithm is greedy if it builds up a solution in small steps it chooses a decision.
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 Summary: Design Methods for Algorithms Andreas Klappenecker.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
EMIS 8374 Optimal Trees updated 25 April slide 1 Minimum Spanning Tree (MST) Input –A (simple) graph G = (V,E) –Edge cost c ij for each edge e 
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Greedy Algorithms Analysis of Algorithms.
What is a matroid? A matroid M is a finite set E, with a set I of subsets of E satisfying: 1.The empty set is in I 2.If X is in I, then every subset of.
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.
6/13/20161 Greedy A Comparison. 6/13/20162 Greedy Solves an optimization problem: the solution is “best” in some sense. Greedy Strategy: –At each decision.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Greedy Algorithms General principle of greedy algorithm
CSCE 411 Design and Analysis of Algorithms
Chapter 5. Greedy Algorithms
Greedy Algorithms (Chap. 16)
Lecture 12 Algorithm Analysis
CSCE350 Algorithms and Data Structure
CS200: Algorithm Analysis
ICS 353: Design and Analysis of Algorithms
Accessible Set Systems
CSE 373 Data Structures and Algorithms
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Lecture 12 Algorithm Analysis
CSE 332: Minimum Spanning Trees
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Greedy Algorithms and Matroids Andreas Klappenecker

Giving Change

Coin Changing Suppose we have n types of coins with values v[1] > v[2] > … > v[n] > 0 Given an amount C, a positive integer, the following algorithm tries to give change for C: m[1] := 0; m[2]:=0; … ; m[n] := 0; // multiplicities of coins for i = 1 to n do while C > v[i] do C := C – v[i]; m[i]++; od;

Coin Changing Suppose that v[1] = 5; v[2] =2; v[1] = 1; Let C = 14. Then the algorithm calculates m[1] = 2 and m[2] = 2 This gives the correct amount of change: C = v[1]*m[1]+v[2]*m[2] = 5*2+2*2 = 14

Correctness Suppose we have n types of coins with values v[1] > v[2] > … > v[n] > 0 Input: A positive integer C m[1] := 0; m[2]:=0; … ; m[n] := 0; // multiplicities of coins for i = 1 to n do while C > v[i] do C := C – v[i]; m[i]++; od; // When is the algorithm correct for all inputs C?

Optimality Suppose we have n types of coins with values v[1] > v[2] > … > v[n] = 1 Input: A positive integer C m[1] := 0; m[2]:=0; … ; m[n] := 0; // multiplicities of coins for i = 1 to n do while C > v[i] do C := C – v[i]; m[i]++; od; // Does the algorithm gives the least number of coins?

Example v[1] = 5; v[2] = 2; v[3] = 1; If C<2, then the algorithm gives a single coin, which is optimal. If C<5, then the algorithm gives at most 2 coins: C = 4 = 2*2 // 2 coins C = 3 = 2+1 // 2 coins C = 2 = 2 // 1 coins In each case this is optimal. If C >= 5, then the algorithm uses the most coins of value 5 and then gives an optimal change for the remaining value < 5. One cannot improve upon this solution. Let us see why.

Example Any optimal solution to give change for C with C = v[1]*m[1] + v[2]*m[2] + v[3] * m[3] with v[1] = 5; v[2] = 2; v[3] = 1; must satisfy a)m[3] = 2, replace two 1’s with 2. b)2*m[2] + m[1] < 5 // otherwise use a 5 c)If C >= 5*k, then m[1] >= k // otherwise solution violates b) Thus, any optimal solution greedily chooses maximal number of 5s. The remaining value in the optimal value has to choose maximal number of 2s, so any optimal solution is the greedy solution!

Example 2 Let v[1]=4; v[2]=3; v[3]=1; The algorithm yields 3 coins of change for C = 6 namely 6=4+1+1 Is this optimal?

Example 2 Any optimal solution to give change for C with C = v[1]*m[1] + v[2]*m[2] + v[3] * m[3] with v[1] = 4; v[2] = 3; v[3] = 1; must satisfy a)m[3] 2, replace two 1’s with 3. b)We cannot have both m[3]>0 and m[2]>0, for otherwise we could use a 4 to reduce the coin count. c)m[2]<4 // otherwise use 4 to reduce number of coins Greedy will fail in general. One can still find an optimal solution efficiently, but the solution is not unique. Why? 9=3+3+3=4+4+1

How Likely is it Optimal? Let N be a positive integer. Let us choose integer a and b uniformly at random subject to N>b>a>1. Then the greedy coin-changing algorithm with v[1]=b; v[2]=a; v[3]=1 gives always optimal change with probability 8/3 N -1/2 + O(1/N) as Thane Plambeck has shown [AMM 96(4), April 1989, pg 357].

Greedy Algorithms

The development of a greedy algorithm can be separated into the following steps: 1.Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve. 2.Prove that there is always an optimal solution to the original problem that makes the greedy choice, so that the greedy choice is always safe. 3.Demonstrate that, having made the greedy choice, what remains is a subproblem with the property that if we combine an optimal solution to the subproblem with the greedy choice that we have made, we arrive at an optimal solution to the original problem. 13

Greedy-Choice Property The greedy choice property is that a globally optimal solution can be arrived at by making a locally optimal (=greedy) choice. 14

Optimal Substructure A problem exhibits optimal substructure if and only if an optimal solution to the problem contains within it optimal solutions to subproblems. 15

Greedy Algorithms Greedy algorithms are easily designed, but correctness of the algorithm is harder to show. We will look at some general principles that allow one to prove that the greedy algorithm is correct.

Matroids

Matroid Let S be a finite set, and F a nonempty family of subsets of S, that is, F  P(S). We call (S,F) a matroid if and only if M1) If B  F and A  B, then A  F. [The family F is called hereditary] M2) If A,B  F and |A|<|B|, then there exists x in B\A such that A  {x} in F [This is called the exchange property]

Example 1 (Matric Matroids) Let M be a matrix. Let S be the set of rows of M and F = { A | A  S, A is linearly independent } Claim: (S,F) is a matroid. Clearly, F is not empty (it contains every row of M). M1) If B is a set of linearly independent rows of M, then any subset A of M is linearly independent. Thus, F is hereditary. M2) If A, B are sets of linearly independent rows of M, and |A|<|B|, then dim span A < dim span B. Choose a row x in B that is not contained in span A. Then A  {x} is a linearly independent subset of rows of M.Therefore, F satisfied the exchange property.

Undirected Graphs Let V be a finite set, E a subset of { e | e  V, |e|=2 } Then (V,E) is called an undirected graph. We call V the set of vertices and E the set of edges of the graph. a b c d V = {a,b,c,d} E = { {a,b}, {a,c}, {a,d}, {b,d}, {c,d} }

Induced Subgraphs Let G=(V,E) be a graph. We call a graph (V,E’) an induced subgraph of G if and only if its edge set E’ is a subset of E.

Spanning Trees Given a connected graph G, a spanning tree of G is an induced subgraph of G that happens to be a tree and connects all vertices of G. If the edges are weighted, then a spanning tree of G with minimum weight is called a minimum spanning tree. a b c d

Example 2 (Graphic Matroids) Let G=(V,E) be an undirected graph. Choose S = E and F = { A | H = (V,A) is an induced subgraph of G such that H is a forest }. Claim: (S,F) is a matroid. M1) F is a nonempty hereditary set system. M2) Let A and B in F with |A| < |B|. Then (V,B) has fewer trees than (V,A). Therefore, (V,B) must contain a tree T whose vertices are in different trees in the forest (V,A). One can add the edge x connecting the two different trees to A and obtain another forest (V,A  {x}).

Weight Functions A matroid (S,F) is called weighted if it equipped with a weight function w: S->R +, i.e., all weights are positive real numbers. If A is a subset of S, then w(A) :=  a in A w(a). Weight functions of this form are sometimes called “linear” weight functions.

Greedy Algorithm for Matroids Greedy(M=(S,F),w) A :=  ; Sort S into monotonically decreasing order by weight w. for each x in S taken in monotonically decreasing order do if A  {x} in F then A := A  {x}; fi; od; return A;

Correctness Theorem: Let M= (S,F) be a weighted matroid with weight function w. Then Greedy(M,w) returns a set in F of maximal weight. [Thus, even though Greedy algorithms in general do not produce optimal results, the greedy algorithm for matroids does! This algorithm is applicable for a wide class of problems. Yet, the correctness proof for Greedy is not more difficult than the correctness for special instance such as Huffman coding. This is economy of thought!]

Complexity Let n = |S| = # elements in the set S. Sorting of S: O(n log n) The for-loop iterates n times. In the body of the loop one needs to check whether A  {x} is in F. If each check takes f(n) time, then the loop takes O(n f(n)) time. Thus, Greedy takes O(n log n + n f(n)) time.

Minimizing or Maximizing? Let M=(S,F) be a matroid. The algorithm Greedy(M,w) returns a set A in F maximizing the weight w(A). If we would like to find a set A in F with minimal weight, then we can use Greedy with weight function w’(a) = m-w(a) for a in A, where m is a real number such that m > max s in S w(s).

Matric Matroids Let M be a matrix. Let S be the set of rows of the matrix M and F = { A | A  S, A is linearly independent }. Weight function w(A)=|A|. What does Greedy((S,F),w) compute? The algorithm yields a basis of the vector space spanned by the rows of the matrix M.

Graphic Matroids Let G=(V,E) be an undirected connected graph. Let S = E and F = { A | H = (S,A) is an induced subgraph of G such that H is a forest }. Let w be a weight function on E. Define w’(a)=m-w(a), where m>w(a), for all a in A. Greedy((S,F), w’) returns a minimum spanning tree of G. This algorithm in known as Kruskal’s algorithm.

Kruskal's MST algorithm Consider the edges in increasing order of weight, add an edge iff it does not cause a cycle [Animation taken from Prof. Welch’s lecture notes]

Conclusion Matroids characterize a group of problems for which the greedy algorithm yields an optimal solution. Kruskals minimum spanning tree algorithm fits nicely into this framework.