Dynamic Programming I

Download Report

Transcript Dynamic Programming I

Cave Adventure
HKOI2006 Junior Question 4
Liu Chi Man (cx)
7 Jan 2006
Background
• Cave Adventure – a Role Playing Game
Enemy
Hero
2
HKOI Training Team 2006
Problem
• 1 Hero (Chris)
• N monsters
• N weapons
3
HKOI Training Team 2006
Problem
• Each weapon can kill some of the N monsters
• Objective: to buy a set of weapons so that each
monster can be killed by one and only one of
those bought weapons
• There may be no solution
4
HKOI Training Team 2006
Problem
• Strange constraint: weapon x can kill
• monster x, and
• perhaps also some monsters with label less than x
• Example (N = 4)
•
•
•
•
Weapon 1 can kill monster 1
Weapon 2 can kill monster 2
Weapon 3 can kill monsters 1 and 3
Weapon 4 can kill monsters 2, 3 and 4
5
HKOI Training Team 2006
Visualization
Monster
1
1
3
4
O
2
3
2
O
O
O
Weapon
4
O
O
O
6
HKOI Training Team 2006
Naive Algorithm – Exhaustion
• Try every combination of weapons
•
•
•
•
{1}, {2}, {3}, {4}
{1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}
{1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}
{1,2,3,4}
• How many combinations are there?
• 2N - 1
• When N = 500, 2N is very large
• When N  15, exhaustion is fine
7
HKOI Training Team 2006
Observation
• How can we kill monster N?
• Weapon N
• Conclusion: we must buy weapon N
8
HKOI Training Team 2006
Another Example
Monster
Weapon
1 2 3 4 5 6
1 O
2
O
3
O
4
O
O
5
O
6 O
O O
9
HKOI Training Team 2006
Another Example
• How to kill monster 4?
Weapon
Monster
1 2 3 4 5 6
1 O
2
O
3
O
O
4
O
5
O
6 O
O O
10
HKOI Training Team 2006
Another Example
• How to kill monster 3?
Weapon
Monster
1 2 3 4 5 6
1 O
2
O
3
O
4
O
O
5
O
6 O
O O
11
HKOI Training Team 2006
Another Example
• Solution
Weapon
Monster
1 2 3 4 5 6
1 O
2
O
3
O
4
O
O
5
O
6 O
O O
12
HKOI Training Team 2006
The Solution is Unique
• You may have observed that the solution, if
exists, is unique
• I guess you didn’t benefit from observing this fact
• Simpler judging program :o)
13
HKOI Training Team 2006
Yet Another Example
• How to kill monster 6?
Weapon
Monster
1 2 3 4 5 6
1 O
2
O
3
O
4 O O
O
5
O
6 O
O O
14
HKOI Training Team 2006
Yet Another Example
• How to kill monster 4?
Weapon
Monster
1 2 3 4 5 6
1 O
2
O
No solution!!
3
O
4 O O
O
5
O
6 O
O O
15
HKOI Training Team 2006
Efficient Algorithm
• while there is some non-killed monster
• let k be the largest labeled non-killed monster
• buy weapon k
• if ‘no solution’ is detected, exit
• report the set of bought weapons as a solution
16
HKOI Training Team 2006
Analysis
• Memory
• An N  N array to store the input
• An array with N entries to mark the killed monsters
• An array with N entries to mark the bought weapons
• Time
• The while loop executes at most N times
• Read/write at most 3N array entries in each iteration
• Only if your program is well-written...
• Total – roughly proportional to N2
• Notation: O(N2)
17
HKOI Training Team 2006
A Wrong Algorithm
• for k = 1 to N do
• if weapon k can kill some already killed monster
• do not buy weapon k
• otherwise
• buy weapon k
• kill all monsters that can be killed by weapon k
• Why is this algorithm wrong?
• Can you give a counterexample?
18
HKOI Training Team 2006
Variations
• What if the strange constraint is removed?
• Also known as the Exact Cover Problem (ECP)
• This is a very ‘hard’ problem
• If you can find a really efficient algorithm to solve
this problem, you will be famous
• Interested trainees may come to [Talk] Introduction to
Complexity Theory on June 3
• What if weapon k kills some monsters with label
not greater than k, but may not include k?
• As hard as ECP
19
HKOI Training Team 2006
Q&A
• Any question?
• Probably no
20
HKOI Training Team 2006
Bye
• Enjoy
21
HKOI Training Team 2006