Download as pdf or txt
Download as pdf or txt
You are on page 1of 87

DYNAMIC PROGRAMMING

Contents

Calculation of the number of rabbits


Factorial
Memoization
Dynamic Programming
Number of coins
Knapsack
LIS
All pair shortest path

2
Presentation of the problem: Calculation of
the number of rabbits
Conditions are given as follows. What is the number of rabbits
in nth month?
In the first month, only a pair of new born rabbits exist.
Rabbits with two month or more old can reproduce.
A pair of reproducible rabbits give birth to a pair of babies every
month.
Rabbits never die.

3
Calculation of the number of rabbits
1st month 2nd month 3rd month 4th month 5th month 6th month 7th month

1 pair 1 pair 2 pairs 3 pairs 5 pairs ?

8 pairs
4
Calculation of the number of rabbits

There were a pairs of rabbits in nth month. nth n+1th n+2th

Let there be b pairs of rabbits including new born

babies in the next n+1th month.

Then, there will be a+b pairs of rabbits in the next

n+2th month.

The reason is that the rabbits born in the previous

n+1th month cannot give birth to babies

although the rabbits living in the nth month are

at the mature ages to give birth to kids.

5
Calculation of the number of rabbits
Let f(n) be the number of rabbits in nth month

Then, f(n+2) = f(n) + f(n+1)

This is the famous Fibonacci sequence researched by Leonardo


Fibonacci.

6
Fibonacci sequence
The sequence starting with 0 and 1, and having the sum of the
previous two numbers as its next element is called the
Fibonacci sequence.
0, 1, 1, 2, 3, 5, 8, 13, ...
The function F calculating the ith value of Fibonacci sequence
can be defined as:
F0 = 0, F1 = 1
Fi = Fi-1 + Fi-2 for i ≥2

From the above definition, the function returning ith term of


Fibonacci sequence can be implemented by a recursive
function.

7
Recursive function for obtaining Fibonacci number

fibo(n)
IF n < 2 : RETURN n
ELSE : RETURN fibo(n - 1) + fibo(n - 2)

8
In the previous example, the algorithm implementing function
calculating the Fibonacci number with a recursive function has
a problem.
The problem is “there are tremendous redundant calls”.
Call Tree of Fibonacci sequence
fib(7)

fib(6)

fib(5) fib(5)

fib(4) fib(4) fib(4)

fib(3) fib(3) fib(3) fib(3) fib(3)

fib (2) fib(2) fib(2) fib (2) fib(2) fib (2) fib(2) fib(2)

fib (1) fib(1) fib (1) fib (1) fib(1)

Example of redundant call


9
Questions when Fibonacci sequences are recursively
implemented
How many times are they redundant?

What is the way to avoid redundancy?

10
Redundancy of Fibonacci sequence
The values which should be known to find the nth Fibonacci
number are:
fibo(n) can be obtained when the values of fibo(0) ~ fibo(n-1) are
known.
In other words, the value can be obtained by calling fibo()function n
times.
When calculating the nth Fibonacci number through recursion,
if fibo(n)function is called,
fibo()function is called more than 2 n/2 times.
2 n/2 > n
Therefore, it can be seen that the function is called
redundantly by applying the principle of pigeon hole.

11
Exercise 1

An apartment is to be painted with blue or yellow paints for


each floor, with the rules set as follows.
Yellow can be used for both of neighboring two floors.
Blue cannot be used for both of neighboring two floors.
If f(n) is the number of ways to paint the floors of the
apartment with the above rules, f(1) = 2 and f(2) = 3 as shown
in the figure. Calculate f(8)?

Yellow Blue Yellow


Yellow Blue Yellow Yellow Blue

f(1) f(2)

12
Memoization

Memoization is a technique that stores previously calculated


values in a memory when executing a computer program so
that the calculation is not repeated every time, enabling fast
execution speed overall. It is the key technique of dynamic
programming.
Interpreting literally, ‘memoization’ means ‘to put in memory’,
and has originated from the Latin word memorandum meaning
‘thing to be remembered’. It is usually confused with
‘memorization’ meaning ‘learning by heart’, but the correct
word is memorization, and the verbal term is memoize.

13
In the algorithm calculating Fibonacci number, the execution
time can be reduced to O(n) by storing the value of fibo1(n) as
soon as the calculation is performed (memoize).
The algorithm to which memoization method is applied is as
follows.

Allocate an array for memo, and initialize al elements to 0.


Initialize memo[0] to 0 and memo[1] to 1.

fibo1(n)
IF n > 2 AND memo[n] == 0
memo[n] ← fibo1(n-1) + fibo1(n-2)
RETURN memo[n]

14
Memoization
Additional memory space is needed.

System call stack due to recursive function call should be used,


causing the possibility of drop in execution speed or overflow.

What is the solution?

15
Dynamic Programming Algorithm
Dynamic Programming algorithm is, like greedy algorithm, an
algorithm for solving optimization problem.

Optimization problem: The problem of obtaining optimal (such as


maximum or minimum) value
Multiple solutions can exist for the problem. It is not to obtain the optimal
solution but an optimal solution.

Dynamic Programming algorithm is an algorithm designing method in


which solutions of small subproblems are first obtained and then the
given problem is finally solved by solving bigger subproblems by
using the solutions of previous subproblems.

16
Application requirements of Dynamic
programming
Problems should comply with the following requirements for
applying dynamic programming.
Optimal substructure
Overlapping subproblems

17
Overlapping subproblems
DP solves a big problem by solving the small problems constituting
the big problem first, then by using the optimal solutions of small
problems in circulation.
In order to explicitly express the recurrence relation, dynamic programming
generally uses recurrence formula, a general mathematical tool.

Due to the circular feature of the problem, DP requires the previously


calculated solutions of small problems at some stage (overlapping
subproblems), and the resolved solutions of small problems are
stored in a certain storage (table) for DP.

When the stored solutions are needed again, they are obtained by
referencing the table without recalculating the problem for the
solution, avoiding redundant calculations.

18
Optimal substructure
Dynamic programming cannot be applied to all problems on
optimization. Given problem should satisfy the principle of optimality
for applying dynamic programming.

Principle of optimality is that, when a solution to a certain problem is


optimal, then the solutions of small problems constituting the
solution should be also optimal. Since the method of dynamic
programming itself obtains the optimal solution of the big problem
by using optimal solutions of smaller problems, the dynamic
programming method cannot be applied to the problem if the
optimal solution of the big problem is not constructed of optimal
solutions of smaller problems.

19
Example to which the principle of optimality does not apply:
Longest path problem A
The longest path from A to D is [A, C, B, D]. 1
1
However, the longest path from A to C, which 2
B C
is a subpath of the above path, is [A, B, C] 3

rather than [A, C]. 1

So, the principle of optimality does not apply. D

Therefore, the longest path problem cannot be solved by DP.

20
Comparison of Divide and Conquer and
Dynamic programming
Divide and Conquer
Divide the problem into unassociated subproblems.
Solve the subproblem recursively.
Combine the solutions of subproblems.
Example: Merge sort, quick sort
DP
Cannot be applied when subproblems are not associated, i.e.,
subproblems share smaller subproblems.
Every subproblem in calculated just once and then the result is
stored for reuse.
Calculation is performed again when subproblems like Divide and
Conquer appear.

21
In DP, dependent relationships exist among subproblems.
For example, solutions of E, F and G are used for solving C.

These relationships vary depending on the problem, and in most cases,


are called the implicit order since they are not explicitly shown.

Divide and Conquer approaches in top down way, and DP in bottom up


way.

A A

B C B C

D E F G D E F G

22
Approach by applying 3-step DP

Identify the features of optimal solution structure.


Divide a problem into subproblems.
Define the value of optimal solution recursively.
The value of optimal solution of the problem is defined based on
the value of optimal value of subproblems.
Calculate the value of optimal solution through bottom up way.
Solution of the smallest subproblem is obtained first and stored in a
table.
By using the solution of the subproblem stored in the table,
progressively obtain the optimal solution of the problems at upper
level. (bottom up way)

23
Application of DP for Fibonacci number
Fibonacci number is constructed of optimal substructures since the answer of
the main problem can be obtained from answers of subproblems.
1. Divide the problem into subproblems.
2. Define the problem through recurrence formulae.
3. Obtain the solution starting from the smallest subproblem. Store the
result in a table, and obtain the solution of upper problem by using
the solutions of subproblems stored in the table.

Table index Stored value


[n] fibo(n)
… …
[4] 3
[3] 2
[2] 1
[1] 1
[0] 0

24
Algorithm of applying DP for Fibonacci number

fibo_dp(n)
f[0] ← 0
f[1] ← 1
FOR i in 2 → n
f[i] ← f[i - 1] + f[i - 2]
RETURN f[n]

25
Calculating Fibonacci number – DP algorithm analysis
DP algorithm shows faster execution speed.

Reasons

There is no redundant calculation as in recursive algorithm.

Function call does not occur since repetition statement is used.

Total number of calculated terms (fIbo_dp[i])


T(n) = n + 1

That is, only one calculation is made from fIbo_dp[0] to fIbo_dp[n].

26
Exercise 2

There are blue bars of 1 cm length, yellow bars of 1 cm length,


and red bars of 2 cm length.
Let the number of methods of making a bar of n cm length by
connecting the bars be f(n).
For example, methods of making a bar of 2 cm length are
(Blue bar, Blue bar),

(Blue bar, Yellow bar),


(Yellow bar, Blue bar),
(Yellow bar, Yellow bar),
(Red bar)
Since there are 5 methods, f(2) = 5. What is f(6)?

27
Presentation of the problem: Making changes
of coins
Kinds of coins
1 cent, 4 cents, 6 cents
8 cents are to be given as changes. At least how many coins
are necessary for the changes?

28
Approach of greedy method
6 cents, 1 cent, 1 cent
What is optimal?
4 cents, 4 cents

Greedy methods do not always guarantee the optimal solutions.


How should the problem be solved?
Let’s approach through dynamic programming.

29
First, a recursive algorithm for changes of 8 cents
Select each of 3 types of coins and solve recursively.

One 1 cent coin + optimal solution for 7 cents


One 4 cent coin + optimal solution for 4 cents
One 6 cent coin + optimal solution for 2 cents
Select optimal solution from the above 3 solutions
For optimal solution for 7 cents, select coins of 1,
4 and 6 cents again, and obtain optimal solution for
other amounts.

30
8
1 6
Call tree 4

7 4 2

6 3 1 3 0 X 1 X X

5 2 0 2 XX 0 2 X X 0 X X

4 1 X 1 X X 1 X X 1 X X

3 0 X 0 X X 0 X X 0 X X 0 X X

2 X X

1 X X
Redundancy (overlapping subproblem)

0 X X

31
DP Approach: Bottom up approach
Optimal solution for 1 cent  (select) optimal solution for 2 cents 
(select) optimal solution for 3 cents  (select) optimal solution for 4
cents (select) …

Optimal solution when giving changes of C[ j] = j cents


n sol choice
8 2 C[n - 1] + 1, C[n - 4] + 1, C[n - 6] + 1 = 3, 2, 3  2
7 2 C[n - 1] + 1, C[n - 4] + 1, C[n - 6] + 1 = 2, 4, 2  2
6 1 C[n - 1] + 1, C[n - 4] + 1, C[n - 6] + 1 = 3, 3, 1  1
5 2 C[n - 1] + 1, C[n - 4] + 1 = 2, 2  2
4 1 C[n - 1] + 1, C[n - 4] + 1 = 4, 1  1
3 3 C[n - 1] + 1  3
2 2 C[n - 1] + 1  2
1 1 C[n - 1] + 1  1
0 0 0

32
Subproblem definition
C[i][ j] = Optimum when giving j cents with Di coins
(Di1 is 1 cent, Di2 is 4 cents, Di3 is 6 cents)

Principle of optimization
Looking at the subproblems, the number of changes for the whole
problem is not the optimal solution if optimal change is not
accomplished when giving changes of j cents with ith coin.
Recurrence formula and table
C[i][ j] = min( C[i][ j - di] + 1, C[i - 1][ j] )  O(i * j)

0 1 2 3 4 5 6 7 8 9
Changes
cent cent cent cent cent cent cent cent cent cent

Di1 => 1 cent 0 1 2 3 4 5 6 7 8 9

Di2 => 4 cents 0 1 2 3 1 2 3 4 2 3

Di3 => 6 cents 0 1 2 3 1 2 1 2 2 3

33
Presentation of the problem: Birthday gift

4 gifts can be selected and put to a knapsack with 10 kg


capacity. What is the selection that makes the maximum value?

4 kg/400
dollars 10kg
5 kg/100
dollars

6 kg/300
dollars 3 kg/500
dollars

34
0-1 Knapsack
Knapsack problem is the problem of finding the maximum values of
items that can be put into a knapsack of capacity W when n items, the
weight, wi, and value, vi, of the item i are given, provided that the
total weight of the items put into the knapsack should not exceed W
and that there are only one for each item.

4 kg/400
dollars 10kg
5 kg/100
dollars

6 kg/300
dollars 3 kg/500
dollars

35
Let’s approach the knapsack problem with DP.
First, consider the given conditions of the problem in order to find out
subproblems of the knapsack problem.
A total of 4 elements include the item, the weight of the item, the value of the
item and the capacity of the knapsack.

Among them, the item and its weight are essential in defining the
subproblem.
The reason is that, starting from the state of empty knapsack, whether
to put an item in the knapsack or not should be determined based on
the sum of the items currently contained in the knapsack.
Also, it should be checked if the capacity of the knapsack is exceeded
when putting an item into the knapsack.

36
Therefore, subproblems of the knapsack problem can be defined as
follows:

W = Capacity of knapsack (kg)

(vi, wi) = Value (10 dollars), weight (kg) of item

K[i, w] = Maximum value when considering only items 1-i and the capacity of

the (temporary) knapsack is w, where i = 1, 2, ⋯, n, and, w = 1, 2, 3, ⋯, W.

Defining K[i, w] recursively

0 if i = 0 or w = 0
K[i, w] = K[i - 1], w] if wi > w
max(vi + K[i - 1, w - wi], K[i - 1, w]) if i > 0 and wi ≤ w

37
Considering the ith item:

0 if i = 0 or w = 0
K[i, w] = K[i - 1], w] if wi > w
max(vi + K[i - 1, w - wi], K[i - 1, w]) if i > 0 and wi ≤ w

Case1: The optimal solution does not include item i.

Total value is the same as the previous case.

Case2: The optimal solution includes item i.

Total value is

The maximum value in the case the capacity of the knapsack is (w - wi)

considering the values to the item i + item 1-(i - 1)


Means the space for
containing item i
38
Implicit order between of the knapsack problem is as follows. That is,
K[i,w] can be calculated only when two subproblems K[i-1,w-wi]
and K[i-1,w] have been calculated.

K[i - 1, w – wi] K[i - 1, w] K[i, w]

39
0-1 Knapsack algorithm
Capacity of knapsack W
n items and weight, wi, and value, vi, of the ith item, provided i = 1,
2, ⋯, n
Output: K[n, W]

FOR i in 0 → n : K[i, 0] ← 0
FOR w in 0 → W : K[0, w] ← 0

FOR i in 1 → n
FOR w in 1 → W
IF wi > w
K[i, w] ← K[i - 1, w]
ELSE
K[i, w] ← max(K[i-1, w - wi] + vi , K[i-1, w])
RETURN K[n, W]

40
Process of performing 0-1 Knapsack
algorithm
The capacity of the knapsack is W = 10kg, and the weight and
value of each item is given as follows.

i v w
2
1
1 10 5 10kg
5 kg/100 4 kg/400
2 40 4 dollars dollars

3 30 6
3 4
4 50 3
6 kg/300 3 kg/500
dollars dollars

41
In lines 1-2, the elements at the 0th row and the 0th column
are initialized to 0.

W = 10

i v w K[i, w] w
i Selected items 0 1 2 3 4 5 6 7 8 9 10
1 10 5
0 {} 0 0 0 0 0 0 0 0 0 0 0
2 40 4
1 {1} 0
3 30 6 2 {1, 2} 0
4 50 3 3 {1, 2, 3} 0
4 {1, 2, 3, 4} 0

42
In order to consider item one by one in line 3, the item number i
varies in the range of 1-4, and in line 4, the (temporary) capacity of
the knapsack w is incremented by 1 kg to finally give 10 kg, the
capacity of the knapsack.

When i=1 (i.e., item 1 is only considered.) 5kg/100


dollars

When w=1 (capacity of the knapsack is 1 kg), item 1 is to be put into


the knapsack. Since w1>w, however (that is, the weight of the item 1
is 5 kg and cannot be put into the knapsack) K[1,1] = K[i-1,w] =
K[1-1,1] = K[0,1] = 0. That is, K[1,1]=0. 1kg

43
When w=2, 3, 4, item 1 cannot be put into the knapsack since
w1>w respectively. Thus, K[2,1]=0, K[1,3]=0, K[1,4]=0
respectively. That is, item 1 with weight of 5 kg cannot be put
into the knapsack even if the capacity of the knapsack is
increased to 4 kg.
4kg
3kg
2kg

5kg/100
dollars

44
When w=5 (capacity of the knapsack is 5 kg), item 1 can be
put into the knapsack since w1=w, i.e., the weight of item 1 is
5 kg. Therefore,
5 kg

K[1, 5] = max( K[i-1, w], K[i-1, w-wi] + vi )


= max( K[1-1, 5], K[1-1, 5-5] + 10 )
= max( K[0, 5], K[0, 0] + 10 )
= max( 0, 0 + 10 )
1
= max( 0, 10 ) = 10. 5 kg/100
dollars

45
When w=6, 7, 8, 9, 10, item 1 can be put into the knapsack in each
case as in the case of w=5. Therefore, K[1,6] = K[1,7] = K[1,8] =
K[1,9] = K[1,10] = 10 respectively.

6kg 7kg 8kg 9kg 10kg

46
Following tables show the results of algorithm which is carried out by
increasing the capacity of the knapsack from 1 to W only for item 1.

W = 10

i v w K[i, w] w
i Selected items 0 1 2 3 4 5 6 7 8 9 10
1 10 5
0 {} 0 0 0 0 0 0 0 0 0 0 0
2 40 4
1 {1} 0 0 0 0 0 10 10 10 10 10 10
3 30 6 2 {1, 2} 0
4 50 3 3 {1, 2, 3} 0
4 {1, 2, 3, 4} 0

47
When i=2 (that is, the solution of subproblem for item 1 has been obtained
when i=1, and item 2 is considered by using the solution.)

When w=1, 2, 3 (capacities of the knapsack are 1, 2 and 3 kg


respectively), item 2 is to be put into the knapsack. Since w2>w,
however, i.e., the weight of item 2 is 4 kg, it cannot be put into the
knapsack. So, K[2,1]=0, K[2,2]=0, and K[2,3]=0.

4kg/400 dollars

48
When w=4 (capacity of the knapsack is 4 kg), item 2 can be put into
the knapsack.

K[2, 4] = max( K[i-1, w], K[i-1, w-wi] + vi )

= max( K[2-1, 4], K[2-1, 4-4] + 40 )


4kg
= max( K[1, 4], K[1, 0] + 40 )

= max( 0, 0 + 40 )
2
= max( 0, 40 ) = 40.
4 kg/400
dollars

49
When w=5 (capacity of the knapsack is 5 kg), item 2 can also be put
into the knapsack since its weight is 4 kg. In this case, however, the
values of item 1 and item 2 are compared when they are put into the
knapsack, and the item giving more value is put into the knapsack.
5kg

K[2, 5] = max( K[i-1, w], K[i-1, w-wi] + vi )

= max(K[2-1, 5], K[2-1, 5-4] + 40 )

= max( K[1, 5], K[1, 1] + 40)

= max(10, 0 + 40 ) 2
1

= max( 10, 40 ) = 40. 5 kg/100 4 kg/400


dollars dollars
That is, item 1 is taken out of the knapsack, and then item 2 is put into

the knapsack. The value, then, is 40.

50
When w=6, 7, 8, more value is obtained in each case when item 1 is
taken out and item 2 is put into the knapsack. Therefore, K[2,6] =
K[2,7] = K[2,8] = 40 respectively.
6kg 7kg 8kg

4 kg/400
dollars

51
When w=9 (capacity of the knapsack is 9 kg), item 2 is to be put into
the knapsack. Since w2<w, however, item 2 can be put into the
knapsack. Therefore,

9kg
1
K[2, 9] = max( K[i-1, w], K[i-1, w-wi] + vi )
5kg/100
= max( K[2-1, 9], K[2-1, 9-4] + 40 ) dollars

= max( K[1, 9], K[1, 5] + 40 )

= max( 10, 10 + 40 ) 2

= max( 10, 50 ) = 50이다. 4kg/400


dollars

That is, both items 1 and 2 can be put into the knapsack, which
means that the value becomes 50.
52
When w=10 (capacity of the knapsack is 10 kg), K[2,10]=50 since
w2<w, in the same way as in the case of w=9, which means that
value of 50 is acquired by putting items 1 and 2 into the knapsack.

Following tables show the results of algorithm which is carried out by


increasing the capacity of the knapsack from 1 to W only for items 1
and 2.
W = 10

i v w K[i, w] w
i Selected items 0 1 2 3 4 5 6 7 8 9 10
1 10 5
0 {} 0 0 0 0 0 0 0 0 0 0 0
2 40 4
1 {1} 0 0 0 0 0 10 10 10 10 10 10
3 30 6 2 {1, 2} 0 0 0 0 40 40 40 40 40 50 50
4 50 3 3 {1, 2, 3} 0
4 {1, 2, 3, 4} 0

53
LIS
Presentation of the problem: What is the
length of the longest increasing sequence?
A sequence is arranged from left to right as follows.
3, 2, 6, 4, 5, 1

What is the length of the longest subsequence of the above


sequence, in which the size increases sequentially while
maintaining the order of the sequence?

55
LIS (Longest Increasing Subsequence)

Longest increasing subsequence


The problem is to extract the longest subsequence where the
elements increase gradually while maintaining the order in a
sequence arranged from left to right.
3, 2, 6, 4, 5, 1
3, 2, 6, 4, 5, 1

The longest subsequence is 2, 4, 5, with its length 3.

How can the longest subsequence be obtained?

56
Brute-force approach
First calculate all subsets of the sequence, and determine if the
subsets are increasing sequences. Then, find the sequence with the
longest length from the increasing sequences.
Ex: S = { 3, 2, 6, 4, 5, 1}
2N = 26 = 64 subsets

Length
0 {}
1 {3}, {2}, {6}, {4}, {5}, {1}
2 {3,2}, {3,6}, {3,4}, {3,5}, {3,1}, {2,6}, {2,4}, …
… …
5 {3, 2, 6, 4, 5}, {3, 2, 6, 4, 1}, {3, 2, 6, 5, 1}, …
6 {3, 2, 6, 4, 5, 1}
It is advantageous to examine from
the subset with longer length
57
Algorithm
O(2n), exponential time complexity

FOR i in N → 1
find all subsequence of S with length of i
IF there is one increasing subsequence
BREAK

58
DP approach

Input: Sequence of numbers, a1, a2, . . . , an


LIS(i): The length of the longest subset of a1, a2, . . . , ai

Can LIS(i) be expressed as the relation with LIS(1),


LIS(2), . . . , LIS(i − 1)?

Case 1: If LIS(i) does not contain ai, LIS(i) = LIS(i-1)


Case 2: If LIS(i) contains ai, LIS(i) = ?

59
Case2: If LIS(i) contains ai, LIS(i) = ?
Find aj with the relation of increasing sequences aj < ai.

Every sequence should be searched since the value of j is not known.

Find the maximum value of them, increment it by 1, and store at


LIS(i).

LIS(i) = 1 + max LIS( j)


j < i and aj < ai

Find the maximum value of LIS().

60
DP approach algorithm
O(n2)

FOR i in 1 → n
LIS[i] = 1
FOR j in 1 → i − 1
IF aj < ai AND 1 + LIS[ j] > LIS[i]
LIS[i] = 1 + LIS[ j]
RETURN max LIS[]

{ 3, 2, 6, 4, 5, 1}

LIS(1) LIS(2) LIS(3) LIS(4) LIS(5) LIS(6)


1 1 {3,6} : 2 {3,4} : 2 {3,4,5} : 3 1

61
More efficient method using binary search
C[k]: Store the smallest value of the increasing sequences with length
k at C[k].

Perform binary search at each location to renew C[].

O(nlogn)
8, 2, 4, 3, 6, 11, 7, 10, 14, 5

8 2 2 2 2 2 2 2 2 2 C[1]

4 3 3 3 3 3 3 3 C[2]
6 6 6 6 6 5 C[3]
11 7 7 7 7 C[4]
10 10 10 C[5]
14 14 C[6]

62
Exercise 1

Find the longest increasing sequence of the following


sequence and output the result.

3, 2, 6, 4, 5, 1

Output example: The longest increasing sequence is 2, 4, 5, with


length 3.

63
ALL PAIRS SHORTEST PATH
Presentation of the problem: All pairs
shortest path
What is the shortest path between each pair of vertices? The
value of the shortest path from 1 to 2 is 4, and the value of the
shortest path from 1 to 5 is 8. What are the values of the
shortest paths between other vertices?

4
1 2
2 3

1 1

5 -2 3 4 -3
2
1
3
1
4 5
2

65
Summary of terms on Graph

Weighted directed graph


Vertex (node) 4
v1 v2
Edge (arc) 2 3
Weight 1 1

Path 5 -2 v3 4 -3

Simple path (Cycle) 2


1
Cyclic graph 1
3

Acyclic graph v4 v3
2
Length
Adjacent vertices

66
Shortest Path

Problem of finding the shortest path from a city to another


when there is no direct route between them
Finding the shortest path in a directed graph with weights
Optimization problem
When one or more answers exist for a given problem, the problem is
to find the most optimal solution

67
Brute-force approach
First find the lengths of all paths from a vertex to another vertex, and
then find the shortest length among them.
Assume the graph is a complete graph having n vertices,
When the paths from a certain vertex i to another vertex j are
collected, the paths also include the paths which pass through other
all vertices only once, the number of which is to be calculated first.
The number of vertices that can reach the first starting from i is n-2,
and by selecting one vertex among them, the number of vertices
that can reach next is n-3, which leads to total number of paths of
(n-2)(n-3)…1 = (n-2)!.
Even when considering the number of the paths, it can be seen that
the algorithm is absolutely inefficient since the number is much
larger than the exponent!

68
DP approach
In order to solve this problem, it is necessary to carry out the
shortest path algorithm of Dijkstra with each point as the starting
point.
The time complexity at this step is (n-1)xO(n2) = O(n3) when arrays
are used, where n is the number of points.
Warshall proposed a dynamic programming algorithm for finding out
whether the path for all pairs exists in a graph (transitive closure),
and Floyd modified the algorithm to design the algorithm for finding
out all pairs shortest path.
Therefore, the dynamic programming algorithm for finding all pairs
shortest path is called the Floyd-Warshall algorithm (shortly referred
to as ‘Floyd algorithm’).

69
The time complexity of Floyd algorithm is O(n3), which is the
same as the time complexity when Dijkstra algorithm is used
(n-1) times.
However, Floyd algorithm is very simple and so more efficient
than using Dijkstra algorithm.

70
In order to solve all pairs shortest path problem through
dynamic programming algorithm, subproblems should be found
first.
For this purpose, first consider the case where the number of
points in a graph is small.
When there are 3 points in a graph, in order to find the
shortest path from point i to point j, the shorter of the 2 paths,
that is, the path of going directly from point i to point j and the
path via the point 1 should be selected.

i j

71
Another important idea is to calculate
the distance of the shortest path by adding the points that can be
visited starting from point 1, then points 1 and 2, and then points 1,
2 and 3, and so on, and then by finally considering all points from 1
to n as the points that can be visited.
Definition of sub problems: Suppose the points in the input
graph are 1, 2, 3, ... , n respectively.
Dijk = Distance of the shortest path among all paths from point
i to point j, considering only points {1, 2, ⋯, k}
as the points that can be visited
It should be noted that it does not mean the path that should
pass through all points from point 1 to point k.

72
1

i j Dij1

73
Then the shorter of the distance of the path going from i to j
via point 2 and Dij1 is set as Dij2, where Di21 + D2j1 is the
distance of the path passing through point 2.
The next bigger problem is to calculate Dij2 for all pairs i and j,
where i≠2 and j≠2.

i j Dij2

74
The shorter of the distance of the path going from i to j via
point k and Dijk-1 is set as Dijk, where Dikk-1 + Dkjk-1 is the
distance of the path passing through point k, and i≠k and j≠k.

i j Dijk

75
In this way, calculations of Dijk are made until k reaches n from
1, then Dijn, that is, the distance of the shortest path of all
pairs i and j which are considered as the points that are
available for passing through can be found, which is the all
pairs shortest path algorithm of Floyd.

76
All pairs shortest path algorithm

D[i][ j] = weight of line segment (i, j),


Output: 2-d array D storing lengths of all pairs shortest paths
AllPairsShortest(D[][])
FOR k in 1 → n
FOR i in 1 → n (provided i ≠ k)
FOR j in 1 → n (provided j ≠ k, j ≠ i)
D[i][ j] ← min(D[i][k] + D[k][ j], D[i][ j])

77
In the for-loop of line 1, k varies from 1 to n, which is for extending
the points that can be passed to the range from 1 to n.

Lines 2~3: Loops for considering each pair of points, i.e., 1-1, 1-2,
1-3, ⋯, 1-n, 2-1, 2-2, ⋯, 2-n, ⋯, n-1, n-2, ⋯, n-n one by one.
However, the loop is not executed when i-i, i=k or j=k.

Line 4: D[i,j] is renewed by determining which is shorter for pair i-j


of each point, the distance of the path passing through k, i.e.,
D[i,k]+D[k,j], or D[i,j], the distance of paths calculated considering
only points {1, 2, ⋯, (k-1)} as the points that can be visited.

78
D[i,k] k D[k,j]

i j new D[i,j]
old D[i,j]

The implicit order between subproblems of all pairs shortest


path problem is represented in line 4.
That is, the subproblems to be pre-calculated for calculating a
new D[i,j] are D[i,k] and D[k,j].

79
Execution process of AllPairsShortest
algorithm
The process of renewing the elements of the array D as k
increases from 1 to 5 will be considered.

4
v1 v2 D 1 2 3 4 5
2 3
1 0 4 2 5 ∞
1 1
2 ∞ 0 1 ∞ 4
5 -2 v3 4 -3
3 1 3 0 1 2
2
1 4 -2 ∞ ∞ 0 2
3
1
v4 v3 5 ∞ -3 3 1 0
2

80
When k=1:
D[2,3] = min{D[2,3], D[2,1]+D[1,3]} = min{1, ∞+2} = 1
D[2,4] = min{D[2,4], D[2,1]+D[1,4]} = min{∞, ∞+5} = ∞
D[2,5] = min{D[2,5], D[2,1]+D[1,5]} = min{4, ∞+∞} = 4
D[3,2] = min{D[3,2], D[3,1]+D[1,2]} = min{3, 1+4} = 3
D[3,4] = min{D[3,4], D[3,1]+D[1,4]} = min{1, 1+5} = 1
D[3,5] = min{D[3,5], D[3,1]+D[1,5]} = min{2, 1+∞} = 2
D[4,2] = min{D[4,2], D[4,1]+D[1,2]} = min{∞, -2+4} = 2 // Renewed
D[4,3] = min{D[4,3], D[4,1]+D[1,3]} = min{∞, -2+2} = 0 // Renewed
D[4,5] = min{D[4,5], D[4,1]+D[1,5]} = min{2, -2+∞} = 2
D[5,2] = min{D[5,2], D[5,1]+D[1,2]} = min{-3, ∞+4} = -3
D[5,3] = min{D[5,3], D[5,1]+D[1,3]} = min{3, ∞+2} = 3
D[5,4] = min{D[5,4], D[5,1]+D[1,4]} = min{1, ∞+5} = 1

81
When k=1, D[4,2] and D[4,3] are renewed to 2 and 0
respectively. Other elements remain unchanged.

4
v1 v2 D 1 2 3 4 5
2 3
1
1 0 4 2 5 ∞
1
5 -2 v3 4 -3
2 ∞ 0 1 ∞ 4
2 3 1 3 0 1 2
1
3 4 -2 2 0 0 2
1
v4 v3 5 ∞ -3 3 1 0
2

82
When k=2:
D[1,5] is renewed to 8, the distance of 1 → 2 → 5.
D[5,3] is renewed to -2, the distance of 5 → 2 → 3.

4
v1 v2 D 1 2 3 4 5
2 3
1
1 0 4 2 5 8
1
5 -2 v3 4 -3
2 ∞ 0 1 ∞ 4
2 3 1 3 0 1 2
1
1
3 4 -2 2 0 0 2
v4 v3 5 ∞ -3 -2 1 0
2

83
When k=3

D 1 2 3 4 5
1 0 4 2 3 4
2 2 0 1 2 3 4
3 1 3 0 1 2 v1 v2
2 3
4 -2 2 0 0 2
1 1
5 -1 -3 -2 -1 0
5 -2 v3 4 -3
When k=4 2
1
D 1 2 3 4 5 1
3
1 0 4 2 3 4 v4 v3
2
2 0 0 1 2 3
3 -1 3 0 1 2
4 -2 2 0 0 2
5 -3 -3 -2 -1 0

84
When k=5, total of 3 elements are renewed, which becomes
the final solution for the given input.

D 1 2 3 4 5 v1
4
v2
1 0 1 2 3 4 2 3

2 0 0 1 2 3 1 1

3 -1 -1 0 1 2 5 -2 v3 4 -3
2
4 -2 -1 0 0 2 1
3
5 -3 -3 -2 -1 0 v4
1
v3
2

85
Time complexity
Since the time complexity of AllPairsShortest is calculated for every i,
j pair for each k as shown in the above example, total of n x n x n =
n3 calculations are made, with each calculation taking O(1) time.
Therefore, the time complexity of AllPairsShortest is O(n3).

86
Exercise 2

Calculate and output the shortest path for each pair of the
following graph G.

4
v1 v2
2 3 D 1 2 3 4 5
1 1 1 0 1 2 3 4
2 0 0 1 2 3
5 -2 v3 4 -3
3 -1 -1 0 1 2
2
1
4 -2 -1 0 0 2
3 5 -3 -3 -2 -1 0
1
v4 v3
2

Shortest path for each pair


Graph G

87

You might also like