0% found this document useful (0 votes)
7 views84 pages

Vit DP

Uploaded by

yv5pgh7z84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views84 pages

Vit DP

Uploaded by

yv5pgh7z84
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

Greedy Method

Dynamic Programming
Problem and Solutions
P

S1 S2 S3 S4 S5 S6
Problem and Solutions
Buy Mobile Phone

Apple Samsung one+ Vivo Oppo


Types of Solutions

•Feasible Solution
• Constraint (8GB RAM & 128GB Storage)

•Optimal Solution
• Objective Function (Minimum Cost)
Min Coins Problem
Notes = { 1, 2, 5, 10, 20, 50, 100, 500, 2000}

Value = 350 (constraint)

• S1 ={ 2000,500}
• S2 ={ 100,100,100,500,500,500}
Solutions • S3 ={ 50,50,50,50,50,50,50}
• S4 ={ 100,100,100,50}
• S5 ={ 100,100,50,50,20,20,10}
Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}

Value = 350 (constraint)

Solution = { }
Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}

Value = 250

Solution = { 100, }
Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}

Value = 150

Solution = { 100, 100 }


Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}

Value = 50

Solution = { 100, 100, 100 }


Greedy Approach
Notes = { 2000, 500, 100, 50, 20, 10, 5, 2, 1}

Value = 0

Solution = { 100, 100, 100, 50 }


Fraction Knapsack Problem
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Capacity of Knapsack: 15

Example Solution x= { 1, 1, 1, 1/2, 0, 0, 1 }


Solution Based on Highest Profits
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on highest Profit


X

Capacity of Knapsack: 15 Profit = 0


Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on highest Profit


X 1

Capacity of Knapsack: 15-5 = 10 Profit = 15


Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on highest Profit


X 1 1

Capacity of Knapsack: 15-5-3 = 7 Profit = 15+10


Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on highest Profit


X 1 1 1

Capacity of Knapsack: 15-5-3-3 = 4 Profit = 15+10+9


Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on highest Profit


X 1 1 1 1

Capacity of Knapsack: 15-5-3-3-1 = 3 Profit = 15+10+9+8


Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on highest Profit


X 1 1 3/4 1 1

Capacity of Knapsack: 15-5-3-3-1-3 = 0


Profit = 15+10+9+8+7*3/4 = 47.25
Solution Based on Lowest Weights
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1

Capacity of Knapsack: 15-1 =14


Profit = 5
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1 1

Capacity of Knapsack: 15-1-1=13


Profit = 5+8
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1 1 1

Capacity of Knapsack: 15-1-1-2=11


Profit = 5+8+4
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1 1 1 1

Capacity of Knapsack: 15-1-1-2-3=8


Profit = 5+8+4+10
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1 1 1 1 1

Capacity of Knapsack: 15-1-1-2-3-3=5


Profit = 5+8+4+10+9
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1 1 1 1 1 1

Capacity of Knapsack: 15-1-1-2-3-3-4=1


Profit = 5+8+4+10+9+7
Knapsack Problem
Objects 1 2 3 4 5 6 7

Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2

Solution based on Lowest Weights


X 1 1 1/5 1 1 1 1

Capacity of Knapsack: 15-1-1-2-3-3-4-1=0


Profit = 5+8+4+10+9+7+15*1/5=46
Solution Based on Profit/Weight
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

Capacity of Knapsack: 15
Profit = 0
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

X 1

Capacity of Knapsack: 15-1=14


Profit = 8
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

X 1 1

Capacity of Knapsack: 15-1-1=13


Profit = 8+5
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

X 1 1 1

Capacity of Knapsack: 15-1-1-3=10


Profit = 8+5+10
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

X 1 1 1 1

Capacity of Knapsack: 15-1-1-3-5=5


Profit = 8+5+10+15
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

X 1 1 1 1 1

Capacity of Knapsack: 15-1-1-3-5-3=2


Profit = 8+5+10+15+9
Knapsack Problem
Objects 1 2 3 4 5 6 7
Profits 5 10 15 7 8 9 4
Weights 1 3 5 4 1 3 2
Pi/Wi 5 3.33 3 1.75 8 3 2

X 1 1 1 1 1 1

Capacity of Knapsack: 15-1-1-3-5-3-2=0


Profit = 8+5+10+15+9+4=51
0/1 Knapsack Problem using DP
0/1 Knapsack Problem
• You can either TAKE it or DON’T Take it

• CANNOT take fractions


0/1 Knapsack Problem
• M=8 Capacity of Bag

• N=4 Number of Objects

Profits (Pi) 1 2 5 6

Weights (Wi) 2 3 4 5
0/1 Knapsack Problem
• M=8 Capacity of Bag

• N=4 Number of Objects

Profits (Pi) 1 2 5 6

Weights (Wi) 2 3 4 5

Solution Xi = 1 0 0 1

max Σ Pi * Xi Σ Wi * Xi <= M
0/1 Knapsack Problem Possible Solutions
0 0 0 0
0 0 0 1
• M=8 Capacity of Bag 0 0 1 0
0 0 1 1
• N=4 Number of Objects 0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
Profits (Pi) 1 2 5 6 1 0 0 0
1 0 0 1
Weights (Wi) 2 3 4 5 1 0 1 0
1 0 1 1
1 1 0 0
Solution Xi = 1 0 0 1
1 1 0 1
1 1 1 0
max Σ Pi * Xi Σ Wi * Xi <= M 1 1 1 1
Total = 2^4 = 16
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi)
0
1 2 1
2 3 2
5 4 3

6 5 4
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0

2 3 2 0

5 4 3 0

6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0

5 4 3 0

6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0 0 1 2 2 3 3 3 3

5 4 3 0

6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0 0 1 2 2 3 3 3 3

5 4 3 0 0 1 2 5 5 6 7 7

6 5 4 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0 0 1 2 2 3 3 3 3

5 4 3 0 0 1 2 5 5 6 7 7

6 5 4 0 0 1 2 5 6 6 7 8

V[ i, w] = max { V[ i-1, w] , V[ i-1, w - w[ i ] ] + p[ i ] }


0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0 0 1 2 2 3 3 3 3

5 4 3 0 0 1 2 5 5 6 7 7

6 5 4 0 0 1 2 5 6 6 7 8

Solution x1 x2 x3 x4 8-6 = 2
1
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0 0 1 2 2 3 3 3 3

5 4 3 0 0 1 2 5 5 6 7 7

6 5 4 0 0 1 2 5 6 6 7 8

Solution x1 x2 x3 x4 8-6 = 2
1 0 1 2-2 = 0
0/1 Knapsack Problem
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

0 1 2 3 4 5 6 7 8
Profits (Pi) Weights (Wi) 0 0 0 0 0 0 0 0 0
0
1 2 1 0 0 1 1 1 1 1 1 1

2 3 2 0 0 1 2 2 3 3 3 3

5 4 3 0 0 1 2 5 5 6 7 7

6 5 4 0 0 1 2 5 6 6 7 8

Solution x1 x2 x3 x4 8-6 = 2
0 1 0 1 2-2 = 0
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

Profits (Pi) Weights (Wi) S0 = { ( 0 , 0 ) }

1 2
2 3
5 4
6 5
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

Profits (Pi) Weights (Wi)


S0 = { ( 0 , 0 ) }

1 2 S01 = { ( 1 , 2 ) }

2 3
5 4 S1 = { (0 , 0) , ( 1 , 2 )}

6 5
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

S0 = { (0 , 0) }

Profits (Pi) Weights (Wi) S01 = { ( 1 , 2 ) }

1 2 S1 = { ( 0 , 0 ) , ( 1 , 2 ) }
2 3 S11 = { ( 2 , 3 ) , ( 3 , 5 ) }

5 4
S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
6 5
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

S0 = { (0 , 0) }

Profits (Pi) Weights (Wi)


S01 = { ( 1 , 2 ) }

S1 = { (0 , 0) , ( 1 , 2 )}
1 2
S11 = { ( 2 , 3 ) , ( 3 , 5 ) }
2 3
S2 = { ( 0 , 0 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) }
5 4 S21 = { ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 ) }

6 5
S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 )}
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects S0 = { (0 , 0) }
S01 = { ( 1 , 2 ) }
S1 = { (0 , 0) , ( 1 , 2 )}
Profits (Pi) Weights (Wi) S11 = { ( 2 , 3 ) , ( 3 , 5 ) }
S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
1 2 S21 = { ( 5 , 4 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 9 ) }

2 3
5 4
S3 = { ( 0 , 0 ) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
6 5 S31 = { ( 6 , 5 ) , ( 7 , 7 ) , ( 8 , 8 ) , ( 9 , 10 ) , ( 12 , 11 ) , ( 13 , 12 ) }

S4 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 5 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 8 )}
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

S0 = { (0 , 0) }

S1 = { (0 , 0) , ( 1 , 2 )}

S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}

S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}

( 8, 8 ) belongs to S4 but not in S3 S4 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 8 )}


x4 = 1 ( 8-6 , 8-5 ) = ( 2 , 3 )
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

S0 = { (0 , 0) }

S1 = { (0 , 0) , ( 1 , 2 )}

S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}

( 2 , 3 ) belongs to S3 also in S2
S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
x3 = 1 (2,3)

( 8, 8 ) belongs to S4 but not in S3 S4 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 8 )}


x4 = 1 ( 8-6 , 8-5 ) = ( 2 , 3 )
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

S0 = { (0 , 0) }

S1 = { (0 , 0) , ( 1 , 2 )}

( 8, 8 ) belongs to S2 but not in S1


S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
x2 = 1 ( 2-2 , 3-3 ) = ( 0 , 0 )

( 2 , 3 ) belongs to S3 also in S2 S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
x3 = 1 (2,3)

( 8, 8 ) belongs to S4 but not in S3 S4 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 8 )}


x4 = 1 ( 8-6 , 8-5 ) = ( 2 , 3 )
0/1 Knapsack Problem (Second Method)
• M=8 Capacity of Bag max Σ Pi * Xi Σ Wi * Xi <= M
• N=4 Number of Objects

S0 = { (0 , 0) }

( 0 , 0 ) belongs to S1 also in S0
S1 = { (0 , 0) , ( 1 , 2 )}
x1 = 0 (0,0)

( 8, 8 ) belongs to S2 but not in S1


S2 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 )}
x2 = 1 ( 2-2 , 3-3 ) = ( 0 , 0 )

( 2 , 3 ) belongs to S3 also in S2 S3 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 )}
x3 = 0 (2,3)

( 8, 8 ) belongs to S4 but not in S3 S4 = { (0 , 0) , ( 1 , 2 ) , ( 2 , 3 ) , ( 3 , 5 ) , ( 6 , 6 ) , ( 7 , 7 ) , ( 8 , 8 )}


x4 = 1 ( 8-6 , 8-5 ) = ( 2 , 3 )
All Pairs Shortest Path
8 1 2 3 4

1 2 1
3
0 3 ∞ 7
2
2 8 0 2 ∞
2 7 5
A0 = 3
5 ∞ 0 1
4 3
4
1 2 ∞ ∞ 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1

1 2 3 4

1
0 3 ∞ 7
min { A0[2,3] , A0[2,1]+A0[1,3] } 2
8 0 2
A1 = 3
2 , 8 + ∞ 5 0
4
2 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1

1 2 3 4

1
0 3 ∞ 7
min { A0[2,4] , A0[2,1]+A0[1,4] } 2
8 0 2 15
A1 = 3
∞ , 8 + 7 5 0
4
2 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1

1 2 3 4

1
0 3 ∞ 7
min { A0[3,2] , A0[3,1]+A0[1,2] } 2
8 0 2 15
A1 = 3
∞ , 5 + 3 5 8 0
4
2 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1

1 2 3 4

1
0 3 ∞ 7
min { A0[3,4] , A0[3,1]+A0[1,4] } 2
8 0 2 15
A1 = 3
1 , 5 + 7 5 8 0 1
4
2 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1

1 2 3 4

1
0 3 ∞ 7
min { A0[4,2] , A0[4,1]+A0[1,2] } 2
8 0 2 15
A1 = 3
∞ , 2 + 3 5 8 0 1
4
2 5 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
1 2 2
8 0 2 ∞
3
A0 = 3
2 7 5
2 5 ∞ 0 1
4
2 ∞ ∞ 0
4 3
1

1 2 3 4

1
0 3 ∞ 7
min { A0[4,3] , A0[4,1]+A0[1,3] } 2
8 0 2 15
A1 = 3
∞ , 2 + ∞ 5 8 0 1
4
2 5 ∞ 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4

1
0 3 5
2
min { A1[1,3] , A1[1,2]+A1[2,3] } 8 0 2 15
A2 = 3
8 0
∞ , 3 + 2 4
5 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4

1
0 3 5 7
2
min { A1[1,4] , A1[1,2]+A1[2,4] } 8 0 2 15
A2 = 3
8 0
7 , 3 + 15 4
5 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4

1
0 3 5 7
2
min { A1[3,1] , A1[3,2]+A1[2,1] } 8 0 2 15
A2 = 3
5 8 0
5 , 8 + 8 4
5 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4

1
0 3 5 7
2
min { A1[3,4] , A1[3,2]+A1[2,4] } 8 0 2 15
A2 = 3
5 8 0 1
1 , 8 + 15 4
5 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4

1
0 3 5 7
2
min { A1[4,1] , A1[4,2]+A1[2,1] } 8 0 2 15
A2 = 3
5 8 0 1
2 , 5 + 8 4
2 5 0
All Pairs Shortest Path 1 2 3 4

1
8 0 3 ∞ 7
2
1 2 8 0 2 15
3 A1 = 3
2 5 8 0 1
2 7 5
4
2 5 ∞ 0
4 3
1
1 2 3 4

1
0 3 5 7
2
min { A1[4,3] , A1[4,2]+A1[2,3] } 8 0 2 15
A2 = 3
5 8 0 1
∞ , 5 + 2 4
2 5 7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5
2
0 2
min { A2[1,2] , A2[1,3]+A2[3,2] } A3 = 3
5 8 0 1
3 , 5 + 8 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
0 2
min { A2[1,4] , A2[1,3]+A2[3,4] } A3 = 3
5 8 0 1
7 , 5 + 1 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
7 0 2
min { A2[2,1] , A2[2,3]+A2[3,1] } A3 = 3
5 8 0 1
8 , 2 + 5 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
7 0 2 3
min { A2[2,4] , A2[2,3]+A2[3,4] } A3 = 3
5 8 0 1
15 , 2 + 1 4
7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
7 0 2 3
min { A2[4,1] , A2[4,3]+A2[3,1] } A3 = 3
5 8 0 1
2 , 7 + 5 4
2 7 0
1 2 3 4
All Pairs Shortest Path
1
0 3 5 7
8 2
8 0 2 15
1 2 A2 = 3
3 5 8 0 1
2 4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
7 0 2 3
min { A2[4,2] , A2[4,3]+A2[3,2] } A3 = 3
5 8 0 1
5 , 7 + 8 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4

1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 6
2
0 3
min { A3[1,2] , A3[1,4]+A3[4,2] } A4 = 3
0 1
3 , 6 + 5 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4

1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
0 3
min { A3[1,3] , A3[1,4]+A3[4,3] } A4 = 3
0 1
5 , 6 + 7 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4

1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
5 0 3
min { A3[2,1] , A3[2,4]+A3[4,1] } A4 = 3
0 1
7 , 3 + 2 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4

1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
5 0 2 3
min { A3[2,3] , A3[2,4]+A3[4,3] } A4 = 3
0 1
2 , 3 + 7 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4

1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
5 0 2 3
min { A3[3,1] , A3[3,4]+A3[4,1] } A4 = 3
3 0 1
5 , 1 + 2 4
2 5 7 0
All Pairs Shortest Path 1 2 3 4

1
0 3 5 6
8
2
7 0 2 3
1 2 A3 = 3
3
5 8 0 1
2
4
2 7 5 2 5 7 0
4 3
1
1 2 3 4

1
0 3 5 6
2
5 0 2 3
min { A3[3,2] , A3[3,4]+A3[4,2] } A4 = 3
3 6 0 1
8 , 1 + 5 4
2 5 7 0
All Pairs Shortest Path
8
1 2 3 4

1
1
3
2
0 3 5 6
2
2 5 0 2 3
2 7 5
A4 = 3
3 6 0 1
4 3
4
1 2 5 7 0

You might also like