Algorithm Design Practice
Algorithm Design Practice
Yonghui Wu
Shanghai Key Laboratory of Intelligent Information Processing
School of Computer Science, Fudan University
[email protected]
Algorithm Design Practice
• This lecture is supported by Office of Global
Partnerships (Key Projects Development Fund),
Fudan University.
• Ad Hoc Problems
• Greedy Algorithms
• Dynamic Programming
Ad Hoc Problems
• Solving Problems by Mechanism Analysis
• Solving Problems by Statistical Analysis
Ad Hoc Problems
• Ad hoc: “for a special purpose …… ”.
• No classical algorithms that can solve ad hoc
problems. Programmers need to design specific
algorithms to solve ad hoc problems.
• There are two strategies to design algorithms for
solving ad hoc problems:
• Mechanism analysis
• Statistical analysis.
Solving Problems by Mechanism Analysis
• Mechanism analysis examines the characteristics and
internal mechanisms of an object to find a
mathematical representation of the problem. Therefore,
the key to mechanism analysis is mathematical
modeling. Solving problems by mechanism analysis is
a top-down method.
Factstone Benchmark
• Source: Waterloo local 2005.09.24
• IDs for Online Judges: POJ 2661, UVA 10916
• Amtel has announced that it will release a 128-bit
computer chip by 2010, a 256-bit computer by
2020, and so on, continuing its strategy of
doubling the word-size every ten years. (Amtel
released a 64-bit computer in 2000, a 32-bit
computer in 1990, a 16-bit computer in 1980, an
8-bit computer in 1970, and a 4-bit computer, its
first, in 1960.)
• Amtel will use a new benchmark - the Factstone
- to advertise the vastly improved capacity of its
new chips. The Factstone rating is defined to be
the largest integer n such that n! can be
represented as an unsigned integer in a computer
word.
• Given a year 1960 ≤ y ≤ 2160, what will be the
Factstone rating of Amtel's most recently
released chip?
• Input
• There are several test cases. For each test case,
there is one line of input containing y. A line
containing 0 follows the last test case.
• Output
• For each test case, output a line giving the
Factstone rating.
Analysis
Bridge
• Source: Waterloo local 2000.09.30
• IDs for Online Judge: POJ 2573 , ZOJ 1877 , UVA 10037
• n people wish to cross a bridge at night. A group of at most
two people may cross at any time, and each group must have
a flashlight. Only one flashlight is available among the n
people, so some sort of shuttle arrangement must be arranged
in order to return the flashlight so that more people may
cross.
• Each person has a different crossing speed; the speed of a
group is determined by the speed of the slower member. Your
job is to determine a strategy that gets all n people across the
bridge in the minimum time.
• Input
• The first line of input contains n, followed by n lines giving the crossing
times for each of the people. There are not more than 1000 people and
nobody takes more than 100 seconds to cross the bridge.
• Output
• The first line of output must contain the total number of seconds required
for all n people to cross the bridge. The following lines give a strategy for
achieving this time. Each line contains either one or two integers, indicating
which person or people form the next group to cross. (Each person is
indicated by the crossing time specified in the input. Although many people
may have the same crossing time the ambiguity is of no consequence.) Note
that the crossings alternate directions, as it is necessary to return the
flashlight so that more may cross. If more than one strategy yields the
minimal time, any one will do.
Analysis
• The strategy that gets all n people across the
bridge in the minimum time:
• Fast people return the flashlight to help slow
people.
• A group of at most two people may cross the
bridge each time
• Solve the problem by analyzing members of
groups.
• First n people’s crossing times are sorted in
descending order.
• Suppose in the current sequence, A is the current
fastest person’s crossing time, B is the current
second fastest person’s crossing time, a is the
current slowest person’s crossing time, and b is the
current second slowest person’s crossing time.
• There are two methods making the current slowest
person and the current second slowest person to cross
the bridge.
• Method 1: The fastest person helps the slowest person and
the second slowest person to cross the bridge.
• Step 1: The fastest person and the slowest person cross
the bridge;
• Step 2: The fastest person is back;
• Step 3: The fastest person and the second slowest person
cross the bridge;
• Step 4: The fastest person is back.
• It takes time 2*A+a+b.
• Method 2: The fastest person and the second fastest person
help the current slowest person and the current second
slowest person to cross the bridge.
• Step 1: The fastest person and the second fastest person
cross the bridge;
• Step 2: The fastest person is back and returns the flashlight
to the slowest person and the second slowest person;
• Step 3: The slowest person and the second slowest person
cross the bridge and give the flashlight to the second
fastest person;
• Step 4: The second fastest person is back.
• It takes time 2*B+A+a.
• Each time we need compare method 1 and
method 2:
• If (2*A+a+b <2*B +A+a), then we use method 1,
else we use method 2.
• Each time the current slowest person and the
current second slowest person cross the bridge.
• Finally, there are two cases:
• Case 1: If there are only two persons need cross the
bridge, then the two persons cross the bridge. It
takes time B.
• Case 2: There are three persons need cross the
bridge. First, the fastest person and the slowest
person cross the bridge. Then, the fastest person is
back. Finally, the last two persons cross the bridge.
It takes time a+A +b.
Solving Problems by Statistical Analysis
• Statistical analysis begins with a partial solution to the
problem and the overall global solution is found based
on analyzing the partial solution.
• Solving problems by statistical analysis is a bottom-up
method.
Ants
• Source: Waterloo local 2004.09.19
• IDs for Online judges: POJ 1852, ZOJ 2376, UVA 10714
• An army of ants walk on a horizontal pole of length l
cm, each with a constant speed of 1 cm/s. When a
walking ant reaches an end of the pole, it immediately
falls off it. When two ants meet they turn back and start
walking in opposite directions. We know the original
positions of ants on the pole, unfortunately, we do not
know the directions in which the ants are walking. Your
task is to compute the earliest and the latest possible
times needed for all ants to fall off the pole.
• Input
• The first line of input contains one integer giving the number of cases that
follow. The data for each case start with two integer numbers: the length of
the pole (in cm) and n, the number of ants residing on the pole. These two
numbers are followed by n integers giving the position of each ant on the pole
as the distance measured from the left end of the pole, in no particular order.
All input integers are not bigger than 1000000 and they are separated by
whitespace.
• Output
• For each case of input, output two numbers separated by a single space. The
first number is the earliest possible time when all ants fall off the pole (if the
directions of their walks are chosen appropriately) and the second number is
the latest possible such time.
Analysis
• The upper limit of the number of ants is
1000000.
• The upper limit of the number of combinations
for ants’ walking is 21000000.
• The problem can’t be solved by enumerating
ants’ walking.
Matches Game
• Source: POJ Monthly, readchild
• ID for Online Judge: POJ 2234
• Here is a simple game. In this game, there are several piles of
matches and two players. The two players play in turn. In
each turn, one can choose a pile and take away an arbitrary
number of matches from the pile (Of course the number of
matches, which is taken away, cannot be zero and cannot be
larger than the number of matches in the chosen pile). If after
a player’s turn, there is no match left, the player is the
winner. Suppose that the two players are all very clear. Your
job is to tell whether the player who plays first can win the
game or not.
• Input
• The input consists of several lines, and in each line there is a test case.
At the beginning of a line, there is an integer M (1≤M≤20), which is
the number of piles. Then comes M positive integers, which are not
larger than 10000000. These M integers represent the number of
matches in each pile.
• Output
• For each test case, output "Yes" in a single line, if the player who play
first will win, otherwise output "No".
Analysis
• The problem is a Nimm’s Game problem.
• Cases for the game are analyzed as follows.
• Case 1:
• There is only one pile of matches.
• The player who plays first will take away all
matches from the pile and win the game.
• Case 2: There are two piles of matches. Numbers of matches in the
two piles are N1 and N2 respectively.
• [1] If N1≠N2,
• the player who plays first will take away some matches from
the larger pile to make the two piles have the same number of
matches. Then by mimicking the player who plays second and
taking the same number of matches that he takes, just from the
opposite pile, the player who plays first will win the game.
• [2] If N1=N2,
• the player who plays second will take the same number of
matches as the player who plays first takes, just from the
opposite pile, then the player who plays second will win the
game.
• Case 3: There are more than two piles of matches.
• Each natural number can be represented as a binary number.
For example, 57(10) = 111001(2), that is, 57(10)=25+24+23+20. A
pile with 57 matches can be regarded as 4 little piles, a pile
with 25 matches, a pile with 24 matches, a pile with 23
matches, and a pile with 20 matches.
• Suppose there are k piles of matches, k>2, and numbers of
matches in the k piles are N1, N2, ……, and Nk respectively. Ni
can represented as a (s+1)-digit binary number, that is,
Ni=nis…ni1ni0, nij is a binary digit, 0js, 1ik. If the digit
of a binary number is less than s+1, leading zeros are added.
• The game state is balanced if n10 + n20 +…+ nk0 is even,
n11 + n21 +…+ nk1 is even, ……, and n1s + n2s +…+ nks
is even, that is, n10 XOR n20 XOR…XOR nk0 is 0, n11
XOR n21 XOR…XOR nk1 is 0, ……, and n1s XOR n2s
XOR…XOR nks is 0; else the game state is
unbalanced.
• If a player faces an unbalanced state, he can take away
some matches from a pile to make the state become a
balanced state.
• And if a player faces a balanced state, no matter what
strategies he takes, the state will become an
unbalanced state.
• The final state for the game is all binary numbers are
zero. That is, the final sate is balanced.
• The strategy wining the game (Bouton's Theorem) is
follows.
• The player who plays first will win the game if the
initial state is unbalanced. And the player who plays
second will win the game if the initial state is
balanced.
• The method that the player who plays first takes away some matches from
a pile to make the state become a balanced state is to select a row (a pile),
and to flip values of bits in odd columns in the row.
• After flipping values of bits in odd columns, the number of matches is
less than the original number of matches in the row. The number of
matches that the player who plays first takes away from the corresponding
pile is the difference between the original number of matches and the new
number of matches.
• Then, the player who plays second takes away matches under a balanced
state. The state will become an unbalanced state. And the player who
plays first can make the state balance no matter how the player who plays
second takes away matches. The process is repeated until the player who
plays second takes away some matches under a balanced state last time,
and then the player who plays first can take away all remainder matches.
• By the same reason, the player who plays second
will win the game when the initial state is a
balanced game.
• The algorithm
• N piles of matches are represented as N binary
numbers.
• If the initial state is unbalanced, the player who
plays first will win the game, else the player who
plays second will win the game.
Greedy Algorithms
• Greedy algorithms are used to solve optimization problems
through a sequence of steps.
• At each step greedy algorithms make the locally optimal
choice in order to find a globally optimal solution.