Subset Sum Shortest Paths - Knapsack
Subset Sum Shortest Paths - Knapsack
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 1 / 55
Subset Sum; Shortest Paths
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 2 / 55
a scheduling 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
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 4 / 55
let us try another greedy rule
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 5 / 55
guidelines for dynamic programming
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 6 / 55
Does weighted interval scheduling apply?
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 7 / 55
Subset Sum; Shortest Paths
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
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 9 / 55
exploring a dichotomy
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
Lemma (1)
If wk > w, then
OPT(k , w ) = OPT(k − 1, w ),
else
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 11 / 55
Subset Sum; Shortest Paths
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).
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
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.
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
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 20 / 55
Subset Sum; Shortest Paths
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.
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
Lemma (4)
If wk > w, then
OPT(k , w ) = OPT(k − 1, w ),
else
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).
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
input: w1 = 2, w2 = 1, w3 = 3, w4 = 2, W = 5,
v1 = 12, v2 = 10, v3 = 20, v4 = 15,
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.
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 32 / 55
Subset Sum; Shortest Paths
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 33 / 55
Subset Sum; Shortest Paths
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 34 / 55
shortest paths in a graph
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
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
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 37 / 55
nd a negative cycle
Denition (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 dene a cycle, and
2
ci,j < 0
(i,j)∈C
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
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 40 / 55
Subset Sum; Shortest Paths
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.
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.
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.
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 43 / 55
dening the 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 ),
OPT(i, v ) = cv ,w + OPT(i − 1, w )
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
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.
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 54 / 55
homework exercises
Computer Algorithms I (CS 401/MCS 401) Subset Sum; Shortest Paths L-14 12 July 2024 55 / 55