Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Gary Sham HKOI 2010 Greedy, Divide and Conquer

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

3 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.

4 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

5 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

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

7 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?

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

9 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

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

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

12 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)

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

14 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? ……

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

16 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?

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

18 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

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


Download ppt "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."

Similar presentations


Ads by Google