+ All Categories
Home > Documents > Gary Sham HKOI 2010 Greedy, Divide and Conquer. Greedy Algorithm Solve the problem by the “BEST”...

Gary Sham HKOI 2010 Greedy, Divide and Conquer. Greedy Algorithm Solve the problem by the “BEST”...

Date post: 04-Jan-2016
Category:
Upload: reginald-turner
View: 215 times
Download: 1 times
Share this document with a friend
19
Gary Sham HKOI 2010 Greedy, Divide and Conquer
Transcript
Page 1: 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.

Gary Sham

HKOI 2010

Greedy,Divide and Conquer

Page 2: 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.

Greedy Algorithm

Solve the problem by the “BEST” choice.

To find the global optimal through local optimal choices, and solve the sub-problem.

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

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.

Page 4: 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.

Coins 2There 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

Page 5: 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.

Example 2: Fractional Knapsack There are N objects, each with weight wi and value

vi. 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 vi : wi

Page 6: 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.

0-1 Knapsack problem

Similar to Fractional Knapsack, but you can only choose to pick the whole item or not.

Greedy?

Page 7: 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.

Example 3: Activity

There are n activities, starting at time si and finishing at fi.

Choose the maximum number of activities so that they do not overlap each other.

Greedy?

Page 8: 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.

Example 4: Diamond Chain

To find the maximum interval sum.

When will you choose a negative value?

Page 9: 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.

Example 5: Advertisement

There are n intervals, [ai, bi]

Choosing the minimum number of points {pj} so that each interval contains at least one of the points

Page 10: 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.

Example 6: Egyptian fraction

An Egyptian fraction is a sum of distinct unit fraction

Given a fraction and express it as an Egyptian fraction

Page 11: 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.

Conclusion

Hard to prove or disprove.

Usually easy to code.

Usually efficient.

Try to guess.

Page 12: 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.

Divide and ConquerDivide

Break a problem into sub problem(s)

ConquerSolve all sub problem(s)

CombineSolve the problem using the results of the sub

problem(s)

Page 13: 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.

Example 1: Merge Sort, Quick Sort

1 ~ N

X+1 ~ N1 ~ X

1 ~ Y Y+1 ~ X X+1 ~ Z Z+1 ~ N

1 2 3 N

Page 14: 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.

Example 2: Tower of HanoiFind 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?……

Page 15: 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.

Example 3: Big Mod

R = XP mod M

O(P) is easy: XP = X * XP-1

0 P 2≦ ≦ 31 -1 ……

X2N = XN * XN

X2N+1 = X * XN * XN

Page 16: 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.

Example 4: L-pieces

A 2N * 2N square board with 1 hole.

How to place L-pieces to cover it?

What is the sub-problem?

Page 17: 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.

Solution

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

Page 18: 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.

Example 5: Range Maximum QueryGiven N numbers, find the maximum value from ai to

bi.

O(NQ) ?

If we know RMQ(3,7), can we find RMQ(3,10) faster?

RMQ(ai, bi) = MAX( RMQ(ai, ci), RMQ(ci+1, bi) )

Segment Tree

Page 19: 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.

Conclusion

Try to divide the original problem to some easier sub-problems.

You will see some similar ideas in DP.


Recommended