CSC 413/513: Intro to Algorithms Greedy Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

Lecture 7 Paradigm #5 Greedy Algorithms
CS 332: Algorithms NP Completeness David Luebke /2/2017.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Greedy Algorithms.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Analysis of Algorithms
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Introduction to Algorithms Jiafen Liu Sept
Dynamic Programming.
David Luebke 1 5/4/2015 CS 332: Algorithms Dynamic Programming Greedy Algorithms.
Review: Dynamic Programming
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.
0-1 Knapsack Problem A burglar breaks into a museum and finds “n” items Let v_i denote the value of ith item, and let w_i denote the weight of the ith.
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: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
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.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 5. Greedy Algorithms - 1 Greedy.
1 Foundations of Software Design Lecture 26: Text Processing, Tries, and Dynamic Programming Marti Hearst & Fredrik Wallenberg Fall 2002.
Week 2: Greedy Algorithms
Dynamic Programming 0-1 Knapsack These notes are taken from the notes by Dr. Steve Goddard at
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.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Greedy Algorithms Dr. Yingwu Zhu. Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that.
1 Summary: Design Methods for Algorithms Andreas Klappenecker.
David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Greedy Methods and Backtracking Dr. Marina Gavrilova Computer Science University of Calgary Canada.
Dynamic Programming continued David Kauchak cs302 Spring 2012.
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
COSC 3101A - Design and Analysis of Algorithms 8 Elements of DP Memoization Longest Common Subsequence Greedy Algorithms Many of these slides are taken.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Greedy Algorithms Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004.
David Luebke 1 1/14/2016 CS 332: Algorithms NP Completeness.
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Dynamic Programming David Kauchak cs161 Summer 2009.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the.
Greedy Algorithms Chapter 16 Highlights
Greedy Algorithms Analysis of Algorithms.
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.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
Greedy Algorithms. Zhengjin,Central South University2 Review: Dynamic Programming Summary of the basic idea: Optimal substructure: optimal solution to.
Divide and Conquer. Problem Solution 4 Example.
A greedy algorithm is an algorithm that follows the problem solving heuristic of making the locally optimal choice at each stage with the hope of finding.
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.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
CS583 Lecture 12 Jana Kosecka Dynamic Programming Longest Common Subsequence Matrix Chain Multiplication Greedy Algorithms Many slides here are based on.
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: CSC317
CSC317 Greedy algorithms; Two main properties:
Review: Dynamic Programming
Algorithm Design Methods
CS6045: Advanced Algorithms
CS4335 Design and Analysis of Algorithms/WANG Lusheng
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
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
The results for Challenging Problem 1.
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Advance Algorithm Dynamic Programming
Presentation transcript:

CSC 413/513: Intro to Algorithms Greedy Algorithms

Review: Dynamic Programming ● Dynamic programming is another strategy for designing algorithms ● Use when problem breaks down into recurring small subproblems

Review: Optimal Substructure of LCS ● Observation 1: Optimal substructure ■ A simple recursive algorithm will suffice ■ Draw sample recursion tree from c[3,4] ■ What will be the depth of the tree? ● Observation 2: Overlapping subproblems ■ Find some places where we solve the same subproblem more than once

Review: Structure of Subproblems ● For the LCS problem: ■ There are few subproblems in total ■ And many recurring instances of each (unlike divide & conquer, where subproblems unique) ● How many distinct problems exist for the LCS of x[1..m] and y[1..n]? ● A: mn

Memoization ● Memoization is another way to deal with overlapping subproblems ■ After computing the solution to a subproblem, store in a table ■ Subsequent calls just do a table lookup ● Can modify recursive alg to use memoziation: ■ There are mn subproblems ■ How many times is each subproblem wanted? ■ What will be the running time for this algorithm? The running space?

Review: Dynamic Programming ● Dynamic programming: build table bottom-up ■ Same table as memoization, but instead of starting at (m,n) and recursing down, start at (1,1) ● Longest Common Subsequence: LCS easy to calculate from LCS of prefixes ● Knapsack problem: we’ll review this in a bit

Review: Dynamic Programming ● Summary of the basic idea: ■ Optimal substructure: optimal solution to problem consists of optimal solutions to subproblems ■ Overlapping subproblems: few subproblems in total, many recurring instances of each ■ Solve bottom-up, building a table of solved subproblems that are used to solve larger ones ● Variations: ■ “Table” could be 3-dimensional, triangular, a tree, etc.

Greedy Algorithms ● A greedy algorithm always makes the choice that looks best at the moment ■ Everyday examples: ○ Walking to the corner ○ Making change for 16 cents ■ The hope: a locally optimal choice will lead to a globally optimal solution ■ For some problems, it works ● Dynamic programming can be overkill; greedy algorithms tend to be easier to code

A major distinction ● Dynamic programming ■ Makes a choice after solving subproblems ■ E.g. r n = max 1<=k<=n ( p k +r n-k ) ● Greedy algorithm ■ Makes a choice before solving the subproblems ■ E.g.. We will see. choice Subproblem

Activity-Selection Problem ● Problem: get your money’s worth out of a carnival ■ Buy a wristband that lets you onto any ride ■ Lots of rides, each starting and ending at different times ■ Your goal: ride as many rides as possible ○ Another, alternative goal that we don’t solve here: maximize time spent on rides ● Welcome to the activity selection problem

Activity-Selection ● Formally: ■ Given a set S of n activities s i = start time of activity i f i = finish time of activity i ■ Find max-size subset A of compatible activities n Assume (wlog) that f 1  f 2  …  f n

Activity Selection: Optimal Substructure ● Let k be the minimum activity in A (i.e., the one with the earliest finish time). Then A - {k} is an optimal solution to S’ = {i  S: s i  f k } ■ In words: once activity #1 is selected, the problem reduces to finding an optimal solution for activity- selection over activities in S compatible with #1 ■ Proof : if we could find optimal solution B to S’ with |B| > |A - {k}|, ○ Then B U {k} is compatible ○ And |B U {k}| > |A|

Activity Selection: Repeated Subproblems ● Consider a recursive algorithm that tries all possible compatible subsets to find a maximal set, and notice repeated subproblems: S 1  A? S’ 2  A? S-{1} 2  A? S-{1,2}S’’S’-{2}S’’ yesno yes

Greedy Choice Property ● Dynamic programming? Memoize? Yes, but… ● Activity selection problem also exhibits the greedy choice property: ■ Locally optimal choice  globally optimal sol’n ■ Theorem 16.1: if S is an activity selection problem sorted by finish time, then  optimal solution A  S such that {1}  A

Proof: Optimality of the greedy choice ● Statement (again): Suppose a m has the earliest finish time of all activities in S. Then a m must be included in some optimal solution ● Proof: ■ Let A be an optimal solution of S, where a j has the earliest finish time ■ If a j =a m, done ■ Otherwise, consider A’=A - {a j } U {a m } ○ A’ is feasible (because a m finishes before a j ) ○ |A’| = |A| ○ Therefore, A’ is also optimal, and it includes a m

Activity Selection: A Greedy Algorithm ● So actual algorithm is simple: ■ Sort the activities by finish time ■ Schedule the first activity ■ Then schedule the next activity in sorted list which starts after previous activity finishes ■ Repeat until no more activities ● Intuition is even simpler: ■ Always pick the shortest ride available at the time

Review: The Knapsack Problem ● The famous knapsack problem: ■ A thief breaks into a museum. Fabulous paintings, sculptures, and jewels are everywhere. The thief has a good eye for the value of these objects, and knows that each will fetch hundreds or thousands of dollars on the clandestine art collector’s market. But, the thief has only brought a single knapsack to the scene of the robbery, and can take away only what he can carry. What items should the thief take to maximize the haul?

Review: The Knapsack Problem ● More formally, the 0-1 knapsack problem: ■ The thief must choose among n items, where the ith item worth v i dollars and weighs w i pounds ■ Carrying at most W pounds, maximize value ○ Note: assume v i, w i, and W are all integers ○ “0-1” b/c each item must be taken or left in entirety ● A variation, the fractional knapsack problem: ■ Thief can take fractions of items ■ Think of items in 0-1 problem as gold ingots, in fractional problem as buckets of gold dust

Review: The Knapsack Problem And Optimal Substructure ● Both variations exhibit optimal substructure ● To show this for the 0-1 problem, consider the most valuable load weighing at most W pounds ■ If we remove item j from the load, what do we know about the remaining load? ■ A: remainder must be the most valuable load weighing at most W - w j that thief could take from museum, excluding item j

Solving The Knapsack Problem ● The optimal solution to the fractional knapsack problem can be found with a greedy algorithm ■ How? (next slide) ● The optimal solution to the 0-1 problem cannot be found with the same greedy strategy ■ Greedy strategy: take in order of dollars/pound ■ Example: 3 items weighing 10, 20, and 30 pounds, knapsack can hold 50 pounds ○ Suppose item 1: $60, item 2: $100, item 3: $120. ○ Greedy picks {1,3}. Optimal ={2,3}

Fractional Knapsack ● The fractional problem can be solved greedily ● Pick the item i with highest v i /w i from S. Take min(w i.W)/w i fraction of it. ● Recursion with ■ S ← S - {i}, ■ W ←W - min(w i,W) ● Proof of optimality of the greedy choice: ■ Suppose highest v i /w i is not included in some optimal sol A ■ w i < W: Find an item j in A that weighs < w i. Replace w j by w j /w i fraction of i to get a better solution. If no such j, pick j in A with the lowest weight, w j. Replace w i /w j fraction of it with w i, and again get a better solution ■ w i >= W: Including as much of i as possible => better solution