Gary Sham HKOI 2010 Greedy, Divide and Conquer. Greedy Algorithm Solve the problem by the “BEST” choice. To find the global optimal through local optimal.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Algorithm Design Methods Spring 2007 CSE, POSTECH.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Algorithm Design approaches Dr. Jey Veerasamy. Petrol cost minimization problem You need to go from S to T by car, spending the minimum for petrol. 2.
Greedy Algorithms.
The Greedy Method1. 2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1) Task Scheduling (§5.1.2) Minimum Spanning.
1.1 Data Structure and Algorithm Lecture 6 Greedy Algorithm Topics Reference: Introduction to Algorithm by Cormen Chapter 17: Greedy Algorithm.
Chapter 5 Fundamental Algorithm Design Techniques.
TIM AU YEUNG HKOI 2012 Greedy. Try some cases Make a guess local optimum -> global optimum Not really a specific algorithm  Practice more  By feelings.
Analysis of Algorithms
Greedy Algorithms Basic idea Connection to dynamic programming
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
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.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
CSE 780 Algorithms Advanced Algorithms Greedy algorithm Job-select problem Greedy vs DP.
CSC401 – Analysis of Algorithms Lecture Notes 12 Dynamic Programming
KNAPSACK PROBLEM A dynamic approach. Knapsack Problem  Given a sack, able to hold K kg  Given a list of objects  Each has a weight and a value  Try.
Week 2: Greedy Algorithms
Fundamental Techniques
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.
Dynamic Programming Introduction to Algorithms Dynamic Programming CSE 680 Prof. Roger Crawfis.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Fundamentals of Algorithms MCS - 2 Lecture # 7
CSC 413/513: Intro to Algorithms Greedy Algorithms.
DP (not Daniel Park's dance party). Dynamic programming Can speed up many problems. Basically, it's like magic. :D Overlapping subproblems o Number of.
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.
Exhaustion, Branch and Bound, Divide and Conquer.
December 14, 2015 Design and Analysis of Computer Algorithm Pradondet Nilagupta Department of Computer Engineering.
Dynamic Programming1. 2 Outline and Reading Matrix Chain-Product (§5.3.1) The General Technique (§5.3.2) 0-1 Knapsack Problem (§5.3.3)
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
HKOI 2004 Team Training Greedy Algorithms (Intermediate Level)
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
Dynamic Programming. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable.
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.
Lecture on Design and Analysis of Computer Algorithm
Algorithm Design Methods
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
Greedy Algorithms (Chap. 16)
Greedy Algorithm.
Prepared by Chen & Po-Chuan 2016/03/29
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.
Exam 2 LZW not on syllabus. 73% / 75%.
Greedy Algorithms.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Merge Sort 1/18/ :45 AM Dynamic Programming Dynamic Programming.
Dynamic Programming Merge Sort 1/18/ :45 AM Spring 2007
Merge Sort 2/22/ :33 AM Dynamic Programming Dynamic Programming.
Algorithm Design Methods
Lecture 6 Greedy Algorithms
Algorithm Design Methods
Merge Sort 4/28/ :13 AM Dynamic Programming Dynamic Programming.
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Dynamic Programming Merge Sort 5/23/2019 6:18 PM Spring 2008
COMPSCI 330 Design and Analysis of Algorithms
Advance Algorithm Dynamic Programming
Algorithm Design Methods
Presentation transcript:

Gary Sham HKOI 2010 Greedy, Divide and Conquer

Greedy Algorithm Solve the problem by the “BEST” choice. To find the global optimal through local optimal choices, and solve the sub-problem.

Example 1: Coins There are 7 kinds of coins: $0.1, $0.2, $0.5, $1, $2, $5, $10 What is the minimum number needed to pay $18 ? Use the greatest possible coin every time.

Coins 2 There are 7 kinds of coins: $0.1, $0.2, $0.5, $2, $5, $9, $10 What is the minimum number needed to pay $18 ? Greedy? $10 + $5 + $2 + $2 Actually…… $9 + $9

Example 2: Fractional Knapsack There are N objects, each with weight w i and value v i. Any amount ( including fractions ) of each item can be taken provided that the total weight does not exceed W. How much of each item should we take in order to maximize the total value? Consider v i : w i

0-1 Knapsack problem Similar to Fractional Knapsack, but you can only choose to pick the whole item or not. Greedy?

Example 3: Activity There are n activities, starting at time s i and finishing at f i. Choose the maximum number of activities so that they do not overlap each other. Greedy?

Example 4: Diamond Chain To find the maximum interval sum. When will you choose a negative value?

Example 5: Advertisement There are n intervals, [a i, b i ] Choosing the minimum number of points {p j } so that each interval contains at least one of the points

Example 6: Egyptian fraction An Egyptian fraction is a sum of distinct unit fraction Given a fraction and express it as an Egyptian fraction

Conclusion Hard to prove or disprove. Usually easy to code. Usually efficient. Try to guess.

Divide and Conquer Divide Break a problem into sub problem(s) Conquer Solve all sub problem(s) Combine Solve the problem using the results of the sub problem(s)

Example 1: Merge Sort, Quick Sort 1 ~ N X+1 ~ N1 ~ X 1 ~ YY+1 ~ XX+1 ~ ZZ+1 ~ N 123 N

Example 2: Tower of Hanoi Find the minimum number of steps to move N stacks from Pag0 to Pag2. How to move N stacks? Move N-1 stacks from Pag0 to Pag1. Move the Nth stack to Pag2. Move N-1 stacks from Pag1 to Pag2. How to move N-1 stacks? ……

Example 3: Big Mod R = X P mod M O(P) is easy: X P = X * X P-1 0 ≦ P ≦ …… X 2N = X N * X N X 2N+1 = X * X N * X N

Example 4: L-pieces A 2 N * 2 N square board with 1 hole. How to place L-pieces to cover it? What is the sub-problem?

Solution 2N-1 * 2N-1 board with 1 hole

Example 5: Range Maximum Query Given N numbers, find the maximum value from a i to b i. O(NQ) ? If we know RMQ(3,7), can we find RMQ(3,10) faster? RMQ(a i, b i ) = MAX( RMQ(a i, c i ), RMQ(c i +1, b i ) ) Segment Tree

Conclusion Try to divide the original problem to some easier sub-problems. You will see some similar ideas in DP.