Greedy Algorithm Enyue (Annie) Lu.

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
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)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Chapter 4 The Greedy Approach. Minimum Spanning Tree A tree is an acyclic, connected, undirected graph. A spanning tree for a given graph G=, where E.
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 1 (Part 3) Tuesday, 9/4/01 Greedy Algorithms.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
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.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2002 Lecture 1 (Part 3) Tuesday, 1/29/02 Design Patterns for Optimization.
Week 2: Greedy Algorithms
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.
Algorithmics - Lecture 101 LECTURE 10: Greedy technique.
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
Greedy Algorithms. What is “Greedy Algorithm” Optimization problem usually goes through a sequence of steps A greedy algorithm makes the choice looks.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Unit-3: Greedy Algorithm Knapsack Algorithm Code Assignment: Implementation of code using Java and approach discussed in class: Roll no’s to submit assignment:
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
1 Algorithms CSCI 235, Fall 2015 Lecture 29 Greedy Algorithms.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
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
Dynamic Programming Sequence of decisions. Problem state.
CSCE 411 Design and Analysis of Algorithms
Lecture on Design and Analysis of Computer Algorithm
We consider the problem of optimally arranging files on a tape
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Algorithm Design Methods
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
The Greedy Method and Text Compression
Greedy Algorithms (Chap. 16)
Introduction to Algorithms`
Greedy Algorithms Basic idea Connection to dynamic programming
The Greedy Approach Winter-2004 Young CS 331 D&A of Algo. Greedy.
Algorithm Design Methods
Unit 3 (Part-I): Greedy Algorithms
CS 3343: Analysis of Algorithms
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
The Greedy Method Spring 2007 The Greedy Method Merge Sort
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
CS4335 Design and Analysis of Algorithms/WANG Lusheng
Exam 2 LZW not on syllabus. 73% / 75%.
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Lecture 6 Topics Greedy Algorithm
Algorithms CSCI 235, Spring 2019 Lecture 29 Greedy Algorithms
Algorithm Design Methods
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Lecture 6 Greedy Algorithms
Algorithm Design Methods
The results for Challenging Problem 1.
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Greedy algorithms.
Advance Algorithm Dynamic Programming
Algorithm Design Methods
Presentation transcript:

Greedy Algorithm Enyue (Annie) Lu

Greedy Algorithm It makes the choice that looks best at the moment and adds it to the current subsolution. It makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution. Examples Prim/Kruskal’s MST algorithms Dijkstra’s shortest path algorithm Optimal Storage on Tapes

Knapsack Problem (fractional) The problem: Given a knapsack with a certain capacity M, n items, are to be put into the knapsack, each has a weight and a profit if put in the knapsack . The objective: find where s.t. is maximized and   Note: All items can break into small pieces or xi can be any fraction between 0 and 1.

Example: Greedy Strategy#1: Profits are ordered in nonincreasing order (1,2,3) Greedy Strategy#2: Weights are ordered in nondecreasing order (3,2,1)

Example: Optimal solution Greedy Strategy#3: p/w are ordered in nonincreasing order (2,3,1) Optimal solution

Algorithm O(n) O(nlogn) O(n) O(nlogn) 1. Calculate vi = pi / wi for i = 1, 2, …, n 2. Sort items by nonincreasing vi. (all wi are also reordered correspondingly ) 3. Let M' be the current weight limit (Initially, M' = M and xi=0 ).In each iteration, choose item i from the head of the unselected list. If M' >= wi , set xi=1, and M' = M'-wi If M' < wi , set xi=M'/wi and the algorithm is finished. O(nlogn) O(n) O(nlogn)

Proof Proof by contradiction: Given some knapsack instance Suppose the items are ordered s.t. let the greedy solution be Show that this ordering is optimal Case1: it’s optimal Case2: s.t. where

Proof Assume X is not optimal, and then there exists s.t. and Y is optimal examine X and Y, let yk be the 1st one in Y that yk  xk. yk  xk  yk < xk same Since we sort x_k in nondecresing order Now we increase yk to xk and decrease as many of as necessary, so that the capacity is still M.

Proof Let this new solution be where

Proof So, if else (Repeat the same process. At the end, Y can be transformed into X.  X is also optimal. Contradiction! )

Greedy Strategy Obtain an optimal solution by making a sequence of choices. The choice that seems best at the moment is chosen. This strategy does not always produces an optimal solution. Then how can one tell if a greedy algorithm will solve a particular optimization problem?? There is no way in general. But there are 2 ingredients exhibited by most greedy problems: Greedy Choice Property Optimal Sub Structure

Greedy Choice Property A globally optimal solution can be arrived at by making a locally optimal (Greedy) choice. We make whatever choice seems best at the moment and then solve the sub problems arising after the choice is made. The choice made by a greedy algorithm may depend on choices so far, by it cannot depend on any future choices or on the solutions to sub problems. Thus, a greedy strategy usually progresses in a top-down fashion, making one greedy choice after another, iteratively reducing each given problem instance to a smaller one.

Optimal Sub Structure A problem exhibits optimal substructure if an optimal solution to the problem contains (within it) optimal solution to sub problems. knapsack problem: an optimal solution X begins with the item with maximum pi/wi, then the X-xi is an optimal solution to the knapsack problem with remaining n-1 items and capacity M-wi

0-1 Knapsack Problem (xi can be 0 or 1) Knapsack capacity: 50 80 + 100 60 =240 + 100 60 =160 + 120 60 =180 + 120 100 =220 i 1 2 3 wi 10 20 30 pi 60 100 120 Does 0-1 Knapsack has a greedy solution?