0% found this document useful (0 votes)
13 views55 pages

Subset Sum Shortest Paths - Knapsack

The document discusses two main topics in computer algorithms: the Subset Sum problem and Shortest Paths in a graph. It describes the Subset Sum problem as a scheduling issue involving natural numbers and outlines the dynamic programming approach to solve it, including the formulation of the algorithm and its analysis. Additionally, it introduces the Bellman-Ford algorithm for finding shortest paths in directed graphs with negative weights.

Uploaded by

Amaresh Swain
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)
13 views55 pages

Subset Sum Shortest Paths - Knapsack

The document discusses two main topics in computer algorithms: the Subset Sum problem and Shortest Paths in a graph. It describes the Subset Sum problem as a scheduling issue involving natural numbers and outlines the dynamic programming approach to solve it, including the formulation of the algorithm and its analysis. Additionally, it introduces the Bellman-Ford algorithm for finding shortest paths in directed graphs with negative weights.

Uploaded by

Amaresh Swain
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/ 55

Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

CS 401/MCS 401 Lecture 14


Computer Algorithms I
Jan Verschelde, 12 July 2024

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 1 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 2 / 55
a scheduling problem

Consider a resource, available between time 0 and W.


We have n requests { 1, 2,    , n } for the resource.
Denote the set of requests by R = { 1, 2,    , n }.
Request i has weight wi , the time it requests on the resource.
Determine the subset of R to compute the maximum sum of weights

max wi
S⊆R
i∈S 
subject to wi ≤ W
i∈S

This constrained optimization problem is known as


the subset sum problem.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 3 / 55
let us try a greedy rule

Requests R = { 1, 2,    , n }, with weights wi and bound W.


Determine the subset of R to compute the maximum sum of weights
 
max wi subject to wi ≤ W
S⊆R
i∈S i∈S

Sort the weights of the requests, in decreasing order.


Greedy rule: select heaviest request rst.
Consider R = { W2 + 1, W2, W2 }, and apply the rule:
1 Select W2 + 1 rst.
2 Not possible to select W2 because W2 + 1 + W2 > W.
3 Selecting W2 and W2 is the optimal choice.
This rule fails!

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 4 / 55
let us try another greedy rule

Requests R = { 1, 2,    , n }, with weights wi and bound W.


Determine the subset of R to compute the maximum sum of weights
 
max wi subject to wi ≤ W
S⊆R
i∈S i∈S

Sort the weights of the requests, in increasing order.


Greedy rule: select lightest request rst.
Consider R = { 1, W2, W2 }, and apply the rule:
1 Select 1 rst and then select W2.
2 Not possible to select W2 because 1 + W2 + W2 > W.
3 Selecting W2 and W2 is the optimal choice.
This rule fails!

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 5 / 55
guidelines for dynamic programming

We need dynamic programming when greedy rules do not work.


For dynamic programming to apply,
we need a collection of subproblems derived from the original problem
that satises the properties:
1 There are a polynomial number of subproblems.
2 The solution to the original problem can be easily computed
from the solutions to the subproblems.
3 There is a natural order from smallest to largest subproblem,
jointly with an easy-to-compute recurrence.

While dynamic programming resembles divide and conquer,


if not carefully implemented, it may not lead to an efcient algorithm.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 6 / 55
Does weighted interval scheduling apply?

Recall the weighted interval scheduling:

Input: n requests (si , fi , vi ), i = 1, 2,    , n,


si is the start time of request i,
fi is the nish time of request i,
vi is the value of request i.
The requests are sorted: f1 ≤ f2 ≤ · · · ≤ fn .

For this problem, once a request [si , fi ] is selected,


automatically all conicting intervals [sj , fj ] are excluded
in the remaining subproblems left to consider.
For the subset sum problem, once we select a weight,
we are left with all remaining n − 1 requests.
We have to nd a better way to explore the problem space.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 7 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 8 / 55
only natural numbers in the input data

If all weights are positive natural numbers,


then we can work with a threshold w ≤ W to dene subproblems.
Given w , 0 ≤ w ≤ W, and
determine the subset of Rk , Rk = { 1, 2,    , k },
with maximum sum of weights over the rst k requests:

OPT(k , w ) = max wi
S⊆Rk
i∈S 
subject to wi ≤ w
i∈S

Our goal is to determine S for OPT(n, W).

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 9 / 55
exploring a dichotomy

Denote an optimal solution by O ⊆ { 1, 2,    , n }.


Consider n, the last request. We have two cases:
1 Either n ̸∈ O, and then

OPT(n, W) = OPT(n − 1, W)


We then simply ignore the n-th request.
2 Or n ∈ O, and then

OPT(n, W) = wn + OPT(n − 1, W − wn )
The above formula is wrong when W − wn < 0.
In case W − wn < 0, then n ̸∈ O.
So the rst case applies always when wn > W.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 10 / 55
a recurrence
For w , 0 ≤ w ≤ W, and Rk = { 1, 2,    , k }, denote

OPT(k , w ) = max wi
S⊆Rk
i∈S 
subject to wi ≤ w
i∈S

The dichotomy leads to the recurrence formulated in a lemma:

Lemma (1)
If wk > w, then
OPT(k , w ) = OPT(k − 1, w ),
else

OPT(k , w ) = max(OPT(k − 1, w ), wk + OPT(k − 1, w − wk ))

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 11 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 12 / 55
the subset sum algorithm
Subset-Sum(n, W, wi )
Input: n, the number of requests,
W, the bound on the sum of weights,
wi , weights of all requests i = 1, 2,    , n.
Output: returns OPT(n, W).

let M[0    n, 0    W] be an array of arrays


for k = 0, 1,    , n do M[k , 0] := 0
for w = 0, 1,    , W do M[0, w] := 0
for k = 1, 2,    , n do
for w = 0, 1,    , W do
if wk > w then
M[k , w] := M[k − 1, w ]
else
M[k , w] := max(M[k − 1, w], wk + M[k − 1, w − wk ])
return M[n, W].

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 13 / 55
a numerical example

Input: w1 = 2, w2 = 2, w3 = 3, W = 6.
Step 0:
weights
item 0 1 2 3 4 5 6
3 0
2 0
1 0
0 0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 14 / 55
a numerical example

Input: w1 = 2, w2 = 2, w3 = 3, W = 6.
Step 1:
weights
item 0 1 2 3 4 5 6
3 0
2 0
1 0 0 2 2 2 2 2
0 0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 15 / 55
a numerical example

Input: w1 = 2, w2 = 2, w3 = 3, W = 6.
Step 2:
weights
item 0 1 2 3 4 5 6
3 0
2 0 0 2 2 4 4 4
1 0 0 2 2 2 2 2
0 0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 16 / 55
a numerical example

Input: w1 = 2, w2 = 2, w3 = 3, W = 6.
Step 3:
weights
item 0 1 2 3 4 5 6
3 0 0 2 2 4 5 5
2 0 0 2 2 4 4 4
1 0 0 2 2 2 2 2
0 0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 17 / 55
recovering the optimal solution

Exercise 1: on the example with


input: w1 = 2, w2 = 2, w3 = 3, W = 6,
demonstrate the recovery of the optimal solution,
given the output table M.
Formulate the algorithm to recover the optimal solution, given M.
What is the running time of this algorithm?

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 18 / 55
analysis of the subset sum algorithm

Theorem (2)
For n requests with bound W, W is a natural number,
Subset-Sum returns OPT(n, W) in O(nW) time.

The correctness is proven by induction on n.


The cost follows from the size of M.
Corollary (3)
Given the table M computed by Subset-Sum,
the optimal set can be computed in O(n) time.

Even as the size of M is nW,


the computation of the optimal set follows one path in the table
and the size of the optimal set is bounded by n.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 19 / 55
on the space requirement for subset sum

Exercise 2: Consider six requests:


w1 = 33, w2 = 11, w3 = 27, w4 = 32, w5 = 37, w6 = 31, and W = 100.
1 Running the subset sum algorithm, what do you observe on the
number of nonzero elements in M, versus the entire size of M?
2 Describe an efcient data structure to efciently store only the
nonzero elements needed in the algorithm.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 20 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 21 / 55
an extension: the knapsack problem
Consider a resource, available between time 0 and W.
We have n requests { 1, 2,    , n } for the resource.
Denote the set of requests by R = { 1, 2,    , n }.
Each request i has
a weight wi , the time it requests on the resource, and
a value vi , relative to the importance of the request.

Determine the subset of R to compute the maximum sum of values



max vi
S⊆R
i∈S 
subject to wi ≤ W
i∈S

This constrained optimization problem is known as


the knapsack problem.
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 22 / 55
adjusting the dichotomy for knapsack

Denote an optimal solution by O ⊆ { 1, 2,    , n }.


Consider n, the last request. We have two cases:
1 Either n ̸∈ O, and then

OPT(n, W) = OPT(n − 1, W)


We then simply ignore the n-th request.
2 Or n ∈ O, and then

OPT(n, W) = vn + OPT(n − 1, W − wn )
The above formula is wrong when W − wn < 0.
In case W − wn < 0, then n ̸∈ O.
So the rst case applies always when wn > W.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 23 / 55
a recurrence for the knapsack problem
For w , 0 ≤ w ≤ W, and Rk = { 1, 2,    , k }, denote

OPT(k , w ) = max vi
S⊆Rk
i∈S 
subject to wi ≤ w
i∈S

The dichotomy leads to the recurrence formulated in a lemma:

Lemma (4)
If wk > w, then
OPT(k , w ) = OPT(k − 1, w ),
else

OPT(k , w ) = max(OPT(k − 1, w ), vk + OPT(k − 1, w − wk ))

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 24 / 55
the knapsack algorithm
Knapsack(n, W, wi , vi )
Input: n, the number of requests,
W, the bound on the sum of weights,
wi , weights of all requests i = 1, 2,    , n,
vi , values of all requests i = 1, 2,    , n.
Output: returns OPT(n, W).

let M[0    n, 0    W] be an array of arrays


for k = 0, 1,    , n do M[k , 0] := 0
for w = 0, 1,    , W do M[0, w] := 0
for k = 1, 2,    , n do
for w = 0, 1,    , W do
if wk > w then
M[k , w] := M[k − 1, w ]
else
M[k , w] := max(M[k − 1, w], vk + M[k − 1, w − wk ])
return M[n, W].

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 25 / 55
a numerical example

Input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15.

Step 0:
weights
item 0 1 2 3 4 5
4 0
3 0
2 0
1 0
0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 26 / 55
a numerical example

Input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15.

Step 1:
weights
item 0 1 2 3 4 5
4 0
3 0
2 0
1 0 0 12 12 12 12
0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 27 / 55
a numerical example

Input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15.

Step 2:
weights
item 0 1 2 3 4 5
4 0
3 0
2 0 10 12 22 22 22
1 0 0 12 12 12 12
0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 28 / 55
a numerical example

Input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15.

Step 3:
weights
item 0 1 2 3 4 5
4 0
3 0 10 12 22 30 32
2 0 10 12 22 22 22
1 0 0 12 12 12 12
0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 29 / 55
a numerical example

Input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15.

Step 4:
weights
item 0 1 2 3 4 5
4 0 10 15 25 30 37
3 0 10 12 22 30 32
2 0 10 12 22 22 22
1 0 0 12 12 12 12
0 0 0 0 0 0 0

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 30 / 55
recovering the optimal solution

Exercise 3: on the example with

input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15,

demonstrate the recovery of the optimal solution,


given the output table M.
Formulate the algorithm to recover the optimal solution, given M.
What is the running time of this algorithm?

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 31 / 55
analysis of the knapsack algorithm

Theorem (5)
For n requests with bound W, W is a natural number,
Knapsack returns OPT(n, W) in O(nW) time.

The correctness is proven by induction on n.


The cost follows from the size of M.
Corollary (6)
Given the table M computed by Knapsack,
the optimal set can be computed in O(n) time.

Even as the size of M is nW,


the computation of the optimal set follows one path in the table
and the size of the optimal set is bounded by n.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 32 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

CS 401/MCS 401 Lecture 14


Computer Algorithms I
Jan Verschelde, 12 July 2024

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 33 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 34 / 55
shortest paths in a graph

Given is a weighted graph G = (V , E)


where each edge (i, j) ∈ E has a cost ci,j ,
the cost of going directly from vertex i to j.
Costs can be negative. A negative cost is a prot.
The problem is to compute shortest paths in G.

Does Dijkstra’s algorithm work on graphs with negative costs?

Let us compute an example ...

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 35 / 55
Why would Dijkstra’s algorithm not work?

Compute all shortest paths from 0 to all other nodes in the graph:

2 3

0 2

1 −6

The shortest path from 0 to 3 goes via 1 and 2.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 36 / 55
Can we make the weights positive?
If the smallest weight is −w , for w > 0,
then add w to all weights in the graph, as illustrated below:

1 1
2 2 5 5
0 2 0 2

3 3 6 6
3 4 3 4
−3 0

The shortest path from 0 to 2


1 has length 3 in the graph at the left and passed through 3 and 4,
2 has length 10 in the graph at the right and passed through 1.
The greedy algorithm of Dijkstra does not apply.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 37 / 55
nd a negative cycle

Denition (7)
Given a graph G = (V , E) with weights ci,j on edges (i, j) ∈ E,
a negative cycle C is a sequence of edges such that
1 the edges in C dene a cycle, and
2

ci,j < 0
(i,j)∈C

Consider a sequence of nancial transactions:


1 buy from i1 ,
2 sell to i2 , sell to i3 , etc,
3 return to i1 with a net prot.
A negative cycle is viewed as a good arbitrage opportunity.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 38 / 55
an exercise

Exercise 4:
Consider a directed graph G = (V , E) with weights ci,j on edges
(i, j) ∈ E.
1 Give an example of a graph with 9 vertices which contains at least
one negative cycle.
2 Demonstrate the application of breadth-rst search on your graph
and illustrate the detection of the negative cycle(s).
3 Describe a general algorithm to compute all negative cycles in a
directed weighted graph.
4 Express the cost of the algorithm as O(f (n, m)) where n is the
number of vertices and m is the number of edges in the graph.
Read section 6.10 in the textbook.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 39 / 55
the minimum cost or shortest path problem

If the graph G = (V , E) has no negative cycles,


given s, t ∈ V , nd a path P from the origin s to destination t
such that 
ci,j
(i,j)∈P

is as small as possible for any path from s to t.


If there is a negative cycle, then the minimum cost is −∞.
In what follows, we assume the input graph has no negative cycles.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 40 / 55
Subset Sum; Shortest Paths

1 Subset Sum and Knapsack


a scheduling problem
only natural numbers in the input data
the subset sum algorithm
the knapsack problem

2 Shortest Paths in a Graph


directed graphs with negative weights
the Bellman-Ford algorithm

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 41 / 55
an important observation

Lemma (8)
Consider G = (V , E), #V = n, and s, t ∈ V . If G has no negative
cycles, then there is a shortest path from s to t with no repeat nodes.

Proof. Let P denote the shortest path from s to t.


If P would have repeat nodes, then there are two cases to consider.
1 Either, we could remove the repeat nodes and obtain a path that is
shorter, which contradicts that P is the shortest path.
2 Or, if removing repeat nodes results in a path that is longer,
then G has negative cycles, which leads also to a contradiction.
Thus P has no repeat nodes.
Q.E.D.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 42 / 55
on the length of the shortest path

Lemma (8)
Consider G = (V , E), #V = n, and s, t ∈ V . If G has no negative
cycles, then there is a shortest path from s to t with no repeat nodes.

No repeat nodes in a shortest path of a graph of n vertices implies:

Corollary (9)
Consider G = (V , E), #V = n, and s, t ∈ V .
If G has no negative cycles, then there is a shortest path from s to t
that has at most n − 1 edges.

The important observation bounds the length of the shortest path.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 43 / 55
dening the subproblems

The important observation bounds the length of the shortest path.


Let t be the destination vertex is in the graph G.
Denote OPT(i, v ) the minimum cost of
1 a path from v to t,
2 of at most i edges.
We want to compute OPT(n − 1, s).
We need to express OPT(i, v ) using smaller subproblems.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 44 / 55
deriving a recurrence
Let P be the optimal path with minimum cost OPT(i, v ).
We have one of the following:
1 either P uses at most i − 1 edges, and then

OPT(i, v ) = OPT(i − 1, v ),

2 or P uses i edges and the rst edge is (v , w ), then

OPT(i, v ) = cv ,w + OPT(i − 1, w )

To determine w, we consider all vertices.


Lemma (10)
If i > 0, then
 
OPT(i, v ) = min OPT(i − 1, v ), min cv ,w + OPT(i − 1, w ) 
w ∈V \{v }

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 45 / 55
the Shortest-Path algorithm
Shortest-Path(G, s, t, ci,j )
Input: G = (V , E), a graph with no negative cycles,
s, the start vertex,
t, the destination vertex,
ci,j , costs of all edges (i, j) ∈ E.
Output: returns OPT(n − 1, s).

n := #V
let M[0    n − 1, 1    n] be an array of arrays
M[0, t] := 0
for all v ∈ V \ {t} do M[0, v ] := ∞
for i = 1, 2,    , n − 1 do
for all v ∈ V do  
M[i, v ] := min M[i − 1, v ], min cv ,w + M[i − 1, w]
w ∈V \{v }
return M[n − 1, s]

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 46 / 55
a numerical example
V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2.
Compute all shortest paths to f . Step 0:


 
a
 



 6 vertices
−3  length a b c d e f
 4 d

−4

 5
   



 
4
−1 
   
f b 3
 
 2  

−2  2

 
3 e 8 1

 
−3 0 ∞ ∞ ∞ ∞ ∞ 0





c

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 47 / 55
a numerical example
V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2.
Compute all shortest paths to f . Step 1:


 
a
 



 6 vertices
−3  length a b c d e f
 4 d

−4

 5
   



 
4
−1 
   
f b 3
 
 2  

−2  2

 
3 e 8 1 −3 ∞ 3 4 2 0

 
−3 0 ∞ ∞ ∞ ∞ ∞ 0





c

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 48 / 55
a numerical example
V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2.
Compute all shortest paths to f . Step 2:


 
a
 



 6 vertices
−3  length a b c d e f
 4 d

−4

 5
   



 
4
−1 
   
f b 3
 
 2  

−2  2 −3 0 3 3 0 0

 
3 e 8 1 −3 ∞ 3 4 2 0

 
−3 0 ∞ ∞ ∞ ∞ ∞ 0





c

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 49 / 55
a numerical example
V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2.
Compute all shortest paths to f . Step 3:


 
a
 



 6 vertices
−3  length a b c d e f
 4 d

−4

 5
   



 
4
−1 
   
f b 3 −4 −2 3 3 0 0
 
 2  

−2  2 −3 0 3 3 0 0

 
3 e 8 1 −3 ∞ 3 4 2 0

 
−3 0 ∞ ∞ ∞ ∞ ∞ 0





c

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 50 / 55
a numerical example
V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2.
Compute all shortest paths to f . Step 4:


 
a
 



 6 vertices
−3  length a b c d e f
 4 d

−4

 5
   



 
4 3 2 0 0
−1  −6 −2
   
f b 3 −4 −2 3 3 0 0
 
 2  

−2  2 −3 0 3 3 0 0

 
3 e 8 1 −3 ∞ 3 4 2 0

 
−3 0 ∞ ∞ ∞ ∞ ∞ 0





c

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 51 / 55
a numerical example
V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2.
Compute all shortest paths to f . Step 5:


 
a
 



 6 vertices
−3  length a b c d e f
 4 d

−4

 5 3 0 0 0
   
 −6 −2


 
4 3 2 0 0
−1  −6 −2
   
f b 3 −4 −2 3 3 0 0
 
 2  

−2  2 −3 0 3 3 0 0

 
3 e 8 1 −3 ∞ 3 4 2 0

 
−3 0 ∞ ∞ ∞ ∞ ∞ 0





c

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 52 / 55
recovering the shortest paths

Exercise 5: on the example with


V = {a, b, c, d, e, f }, c(a,b) = −4, c(a,f ) = −3, c(b,d) = −1, c(b,e) = −2,
c(c,b) = 8, c(c,f ) = 3, c(d,a) = 6, c(d,f ) = 4, c(e,c) = −3, c(e,f ) = 2,
demonstrate the recovery of all shortest paths to f
given the output table M.
Formulate the algorithm to recover the optimal solution, given M.
What is the running time of this algorithm?

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 53 / 55
analysis of the algorithm

Theorem (11)
The Shortest-Path algorithm correctly computes the minimum cost
of the shortest path between any two vertices in a graph that has no
negative cycles in O(n3 ) time.

The correctness proof goes by induction, using the recurrence.


For the running time, consider
1 the table M has n2 entries and
2 each entry takes O(n) time to compute
as there are at most n vertices w to consider.

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 54 / 55
homework exercises

Homework collected on Monday 15 July at 10AM:


1 Exercise 1 on slide 14 of Lecture 10
2 Exercise 2 on slide 21 of Lecture 10
3 Exercise 4 on slide 41 of Lecture 10
4 Exercise 1 on slide 6 of Lecture 13
5 Exercise 3 on slide 17 of Lecture 13
6 Exercise 4 on slide 19 of Lecture 13
7 Exercise 5 on slide 20 of Lecture 13
8 Exercise 6 on slide 37 of Lecture 13
9 Exercise 2 on slide 20 of Lecture 14
10 Exercise 5 on slide 53 of Lecture 14

Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 55 / 55

You might also like