Lecture 23 (OBST, Knapsack) (17 Files Merged)
Lecture 23 (OBST, Knapsack) (17 Files Merged)
3 7 7 12
3 12 3 9 9
7
9 9 12 3
7
12
(a) (b) (c) (d)
8 -2
Optimal binary search trees
P Q
i 1
i
i 1
i 1
8 -3
10
• Identifiers : 4, 5, 8, 10, 11,
12, 14
• Internal node : successful
5 14
search, Pi
4 8 11 E7
• External node : unsuccessful
E0 E1 E2 E3 E4 12 search, Qi
E5 E6
P level(a
1
i i ) Qi (level(E i ) 1)
Thenlevel of the root : 1 n 0
8 -4
The dynamic programming approach
ak
P1...Pk-1 Pk+1...Pn
Q0...Qk-1 Qk...Qn
a1 ...a
k-1
ak+1 ...a
n
C(1,k-1) C(k+1,n) 8 -5
General formula
k 1
C(i, j) min Pk Qi-1 Pm Q m Ci, k 1
i k j
m i
j
Q k Pm Q m Ck 1, j
m k 1
j
min Ci, k 1 Ck 1, j Qi-1 Pm Q m
i k j
m i
ak
P1...Pk-1 Pk+1...Pn
Q0...Qk-1 Qk...Qn
a1 ...a
k-1
ak+1 ...a
n
C(1,k-1) C(k+1,n) 8 -6
Computation relationships of sub trees
• e.g. n=4
C(1,4)
C(1,3) C(2,4)
O( m(n m) ) O(n )
1 m n
3
8 -7
Knapsack problem
11-2
Matrix-chain Multiplication …contd
11-4
Algorithm to Multiply 2 Matrices
Input: Matrices Ap×q and Bq×r (with dimensions p×q and q×r)
Result: Matrix Cp×r resulting from the product A·B
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
Scalar multiplication in line 5 dominates time to compute
CNumber of scalar multiplications = pqr
11-5
Matrix-chain Multiplication …contd
11-7
Dynamic Programming Approach
11-8
Dynamic Programming Approach …contd
11-9
Dynamic Programming Approach …contd
11-10
Dynamic Programming Approach …contd
11-11
Dynamic Programming Approach …contd
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i ≤ k< j
11-12
Dynamic Programming Approach …contd
11-13
Algorithm to Compute Optimal Cost
Input: Array p[0…n] containing matrix dimensions and n
Result: Minimum-cost table m and split table s
MATRIX-CHAIN-ORDER(p[ ], n)
for i ← 1 to n Takes O(n3) time
m[i, i] ← 0
Requires O(n 2) space
for l ← 2 to n
for i ← 1 to n-l+1
j ← i+l-1
m[i, j] ←
for k ← i to j-1
q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j]
if q < m[i, j]
m[i, j] ← q
s[i, j] ← k
return m and s
11-14
Constructing Optimal Solution
11-15
Example
11-16
CSE408
Longest Common Sub
Sequence
Lecture # 25
Dynamic programming
LCS(X, Y) = BCB
X=AB C B
Y= BDCAB 10
ABCB
LCS Example (0) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
A
1
2 B
3 C
4 B
X = ABCB; m = |X| = 4
Y = BDCAB; n = |Y| = 5
Allocate array c[5,4]
11
ABCB
LCS Example (1) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0
2 B
0
3 C 0
4 B 0
for i = 1 to m c[i,0] = 0
for j = 1 to n c[0,j] = 0
12
ABCB
LCS Example (2) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 13
ABCB
LCS Example (3) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 14
ABCB
LCS Example (4) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 15
ABCB
LCS Example (5) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 16
ABCB
LCS Example (6) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 17
ABCB
LCS Example (7) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 18
ABCB
LCS Example (8) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 19
ABCB
LCS Example (10) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 20
ABCB
LCS Example (11) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 21
ABCB
LCS Example (12) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 22
ABCB
LCS Example (13) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 23
ABCB
LCS Example (14) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 24
ABCB
LCS Example (15) BDCAB
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
if ( Xi == Yj )
c[i,j] = c[i-1,j-1] + 1
else c[i,j] = max( c[i-1,j], c[i,j-1] )
3/2/2022 25
LCS Algorithm Running Time
O(m*n)
since each c[i,j] is calculated in
constant time, and there are m*n
elements in the array
3/2/2022 26
How to find actual LCS
• So far, we have just found the length of LCS,
but not LCS itself.
• We want to modify this algorithm to make it
output Longest Common Subsequence of X
and Y
Each c[i,j] depends on c[i-1,j] and c[i,j-1]
or c[i-1, j-1]
For each c[i,j] we can say how it was acquired:
2 2 For example, here
2 3 c[i,j] = c[i-1,j-1] +1 = 2+1=3
27
How to find actual LCS - continued
• Remember that
c[i 1, j 1] 1 if x[i] y[ j ],
c[i, j ]
max( c[i, j 1], c[i 1, j ]) otherwise
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
3/2/2022 29
Finding LCS (2)
j 0 1 2 3 4 5
i Yj B D C A B
0 Xi
0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B
0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
LCS (reversed order): B C B
LCS (straight order): B C B
(this string turned out to be a palindrome) 30
CSE408
Minimum Spanning
Tree(Prims,Kruskshal)
Lecture #26
Minimum Spanning Tree
Lecture # 27
Dijkstra’s Algorithm
Edge Relaxation
3 9 5 3 9 8
2 5 2 5
E F E F
0 0
8 A 4 8 A 4
2 2
8 7 2 1 3 7 7 2 1 3
B C D B C D
5 3 9 11 5 3 9 8
2 5 2 5
E F E F
Example (cont.)
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
Dijkstra’s Algorithm
• Graph operations
– Method incidentEdges is called once for each vertex
• Label operations
– We set/get the distance and locator labels of vertex z O(deg(z)) times
– Setting/getting a label takes O(1) time
• Priority queue operations
– Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log n) time
– The key of a vertex in the priority queue is modified at most deg(w) times,
where each key change takes O(log n) time
• Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is
represented by the adjacency list structure
– Recall that Sv deg(v) = 2m
• The running time can also be expressed as O(m log n) since the graph
is connected
Extension
E e r i space
ysnarlk.
Building a Tree Scan the original text
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
y l k . r s n a sp e
2
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
k . r s n a sp e
2
1 1 2 2 2 2 4 8
E i
1 1
y l
1 1
Building a Tree
2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
1 1
E i
1 1
Building a Tree
r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i 1 1
1 1
k .
1 1
Building a Tree
r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .
1 1 1 1 1 1
Building a Tree
n a 2 sp e
2 2
2 2 4 8
E i y l k .
1 1 1 1 1 1
r s
2 2
Building a Tree
n a 2 sp e
2 2 4
2 2 4 8
E i y l k . r s
1 1 1 1 1 1 2 2
Building a Tree
2 4 e
2 2 sp
8
4
y l k . r s
E i 1 1 1 1 2 2
1 1
n a
2 2
Building a Tree
2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i 1 1 1 1 2 2 2 2
1 1
Building a Tree
4 4 e
2 sp
8
4
k . r s n a
1 1 2 2 2 2
2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
2 sp e
4 2 2 8
k . r s n a
1 1 2 2 2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
e
2 2 8
r s n a
2 2 2 2
E i y l
1 1 1 1
2 sp
4
k .
1 1
Building a Tree
4 4 4 6 e
2 sp 8
r s n a 2 2
4
2 2 2 2 k .
E i y l 1 1
1 1 1 1
4
6 e
2 2 2 8
sp
4
E i y l k .
1 1 1 1 1 1
8
4 4
r s n a
2 2 2 2
Building a Tree
4
6 e 8
2 2 2 8
sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
8
e
8
4 4
10
r s n a
2 2 2 2 4
6
2 2 2 sp
4
E i y l k .
1 1 1 1 1 1
Building a Tree
8 10
e
8 4
4 4
6
2 2
r s n a 2 sp
2 2 2 2 4
E i y l k .
1 1 1 1 1 1
Building a Tree
10
16
4
6
2 2 e 8
2 sp 8
4
E i y l k . 4 4
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
10 16
4
6
e 8
2 2 8
2 sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
26
16
10
4 e 8
6 8
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
16
10
4 e 8
6 8
2 2
2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
Char Code
E 0000
i 0001
y 0010
l 0011 26
k 0100 16
. 0101 10
space 011 4
e 10 e 8
6 8
r 1100 2 2 2
s 1101 sp 4 4
4
n 1110 E i y l k .
a 1111 1 1 1 1 1 1 r s n a
2 2 2 2
Encoding the File
• 0 go left 10
16
• 1 go right 4 e 8
6 8
2 2 2 sp 4 4
4
101000110111101111011 E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
11110000110101
Summary
Lecture #28
Outline and Reading
• Weighted graphs
– Shortest path problem
– Shortest path properties
• Dijkstra’s algorithm
– Algorithm
– Edge relaxation
• The Bellman-Ford algorithm
• All-pairs shortest paths
Weighted Graphs
PVD
ORD
SFO
LGA
HNL
LAX
DFW
MIA
Shortest Path Problem
HNL
LAX
DFW
MIA
Shortest Path Properties
Property 1:
A subpath of a shortest path is itself a shortest path
Property 2:
There is a tree of shortest paths from a start vertex to all the other vertices
Example:
Tree of shortest paths from Providence
PVD
ORD
SFO
LGA
HNL
LAX
DFW
MIA
Dijkstra’s Algorithm
Edge Relaxation
3 9 5 3 9 8
2 5 2 5
E F E F
0 0
8 A 4 8 A 4
2 2
8 7 2 1 3 7 7 2 1 3
B C D B C D
5 3 9 11 5 3 9 8
2 5 2 5
E F E F
Example (cont.)
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
0
8 A 4
2
7 7 2 1 3
B C D
5 3 9 8
2 5
E F
Dijkstra’s Algorithm
• Graph operations
– Method incidentEdges is called once for each vertex
• Label operations
– We set/get the distance and locator labels of vertex z O(deg(z)) times
– Setting/getting a label takes O(1) time
• Priority queue operations
– Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log n) time
– The key of a vertex in the priority queue is modified at most deg(w) times,
where each key change takes O(log n) time
• Dijkstra’s algorithm runs in O((n + m) log n) time provided the graph is
represented by the adjacency list structure
– Recall that Sv deg(v) = 2m
• The running time can also be expressed as O(m log n) since the graph
is connected
Extension
0
8 A 4
– If a node with a negative incident 6
edge were to be added late to the 7 7 5 1 4
cloud, it could mess up distances B C D
for vertices already in the cloud. 0 -8
5 9
2 5
E F
8 0 4 8 0 4
-2 -2
7 1 8 7 -2 1 4
3 9 3 9
-2 5 -2 5
8 0 4 8 0 4
-2 -2
5 8 7 1 -1 7 1
-2 4 5 -2 -1
1 3 9 3 9
-2 6 9 5 -2 4 5
1 9
All-Pairs Shortest Paths
Lecture #29
All pairs shortest path
• The problem: find the shortest path between every
pair of vertices of a graph
• The graph: may contain negative edges but no
negative cycles
1
1 2 3 4 5 3 v1 v2
9
1 0 1 1 5 5
1 2 3
2 9 0 3 2 v5
3 0 4 3
v4
2
v3
4 2 0 3 4
5 3 0
The subproblems
– D(0)=W
– D(n)=D which is the goal matrix
Vj
Vi
Vj
Vi
1 2 3
D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] )
1 0 0 0
= min (-3,)
P= 2 0 0 1 = -3
3 0 0 0
1 2 3 k=2
1 5 D1 = 1
3 2
0 4 5
Vertices 1,
4 2 2 0 7
2
-3 3 -3 0
2 can be
1 2 3 intermediat
D2 =
1 0 4 5 2 1
e
D [1,3] = min( D [1,3],
= min (5, 4+7)
1 1
D [1,2]+D [2,3] )
2 2 0 7
=5
3 -1 -3 0
1 2 3
1 0 0 0
D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )
P= 2 0 0 1 = min (, -3+2)
3 2 0 0 = -1
1 5 D2 =
1 2 3 k=3
1 0 4 5
4 2 3
2 2 0 7
Vertices 1,
2
-3
3 -1 -3 0 2, 3 can be
1 2 3 intermediat
D3 =
1 0 2 5 3 2e D [1,3]+D [3,2] )
D [1,2] = min(D [1,2], 2 2
1 2 3
1 0 3 0 D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] )
= min (2, 7+ (-1))
P= 2 0 0 1
=2
3 2 0 0
Floyd's Algorithm: Using 2 D matrices
Floyd
1. D W // initialize D array to W [ ]
2. P 0 // initialize P array to [0]
3. for k 1 to n
// Computing D’ from D
4. do for i 1 to n
5. do for j 1 to n
6. if (D[ i, j ] > D[ i, k ] + D[ k, j ] )
7. then D’[ i, j ] D[ i, k ] + D[ k, j ]
8. P[ i, j ] k;
9. else D’[ i, j ] D[ i, j ]
10. Move D’ to D.
Can we use only one D matrix?
Floyd
1. D W // initialize D array to W [ ]
2. P 0 // initialize P array to [0]
3. for k 1 to n
4. do for i 1 to n
5. do for j 1 to n
6. if (D[ i, j ] > D[ i, k ] + D[ k, j ] )
7. then D[ i, j ] D[ i, k ] + D[ k, j ]
8. P[ i, j ] k;
Printing intermediate nodes on shortest path from q to r
path(index q, r) 1 2 3
if (P[ q, r ]!=0)
1 0 3 0
path(q, P[q, r])
println( “v”+ P[q, r]) P= 2 0 0 1
path(P[q, r], r)
3 2 0 0
return;
//no intermediate nodes
else return
1 5
Before calling path check D[q, r] < , and print
node q, after the call to
4 3
path print node r 2
2
-3
Example
The final distance matrix and P
Path(1, 4)
Path(1, 6) Path(6, 4)
Print
v6
P(1, 6)=0
The intermediate nodes on the shortest path from 1 to 4 are v6, v3.
The shortest path is v1, v6, v3, v4.
CSE408
Maximum Flow
Lecture #30
Maximum Flow
12
16 20
s 10 9 7 t
4
13 4
14
Flow:
f (e) f (e)
e.in.v e.out.v
Net flow and value of a flow f:
f f ( s, v )
– The total flow fromvsource
V
to any other vertices.
– The same as the total flow from any vertices to
the sink.
12/12
15/20
11/16
8/13 4/4
11/14
• FORD-FULKERSON-METHOD(G,s,t)
• initialize flow f to 0
• while there exists an augmenting path p
• do augment flow f along p
• return f
Residual networks:
12 4/12
16 v1 v3 20 4/16 v1 v3 20
s 10 4 7 t s 7
9 10 4 t
4/9
13 4 13 4/4
v2 v4 v2 v4
14 4/14
(a)
Example of Residual network (continued)
8
12
v1 v3 20
4
4
s 10 4 4
7 t
5
13 10 4
v2 v4
4
(b)
Fact 1:
• Let G=(V,E) be a flow network with source s and sink t, and let
f be a flow in G.
• Let Gf be the residual network of G induced by f,and let f’ be a
flow in Gf.Then, the flow sum f+f’ is a flow in G with
value .
f in
• f+f’: the flow fsame
f ' the f 'direction will be added.
the flow in different directions will be cnacelled.
Augmenting paths:
2 3 1
8
12
v1 v3 20
4
4
s 10 4 4
7 t
5
13 10 4
v2 v4
4
(b)
The basic Ford-Fulkerson algorithm:
• FORD-FULKERSON(G,s,t)
• for each edge (u,v) E[G]
• do f[u,v] 0
• f[v,u] 0
• while there exists a path p from s to t in the residual network
Gf
• do cf(p) min{cf(u,v): (u,v) is in p}
• for each edge (u,v) in p
• do f[u,v] f[u,v]+cf(p)
•
Example:
s 10 4 7 t s 7
9 10 4 t
4/9
13 4 13 4/4
v2 v4 v2 v4
14 4/14
(a)
8
12 4/12
v1 v3 20 v1 v3 7/20
4 11/16
4
s 10 4 4
7 t s 7/10 4 7/7 t
5 4/9
13 10 4 13 4/4
v2 v4 v2 v4
4 11/14
(b)
8 12/12
5 v1 v3 13
4 11/16 v1 v3 15/20
11 4
s 3 11 7 7 t s 7/7
10 1/4 t
5 4/9
13 3
4 8/13 4/4
v2 v4 v2 v4
11 11/14
(c)
12 12/12
5 v1 v3 5 11/16 v1 v3 19/20
11 4
s 11 3 7 15 t s 7/7
5 10 1/4 t
5 9
8 3
4 12/13 4/4
v2 v4 v2 v4
11 11/14
(d)
12
5 v1 v3 1
s 3 19
1 11 9 7 t
12 3 4
v2 v4
11
(e)
Time complexity:
M M'
L R L R
(a) (b)
L R L R
(a) (b)
• Lemma .
• Let G=(V,E) be a bipartite graph with vertex partition
V=LR,and let G’=(V’,E’) be its corresponding flow network.If
M is a matching in G, then there is an integer-valued flow f in
G’ with value .Conversely, if f is an integer-valued
flow in G’,then there is a matchingfMinM G with
cardinality .
• Reason: The edges incidentM tof s and t ensures this.
– Each node in the first column has in-degree 1
– Each node in the second column has out-degree 1.
– So each node in the bipartite graph can be involved once in the flow.
Example:
Aug. path:
1
1
1
1
1
1
Residual network. Red edges are new edges in the residual network.
The new aug. path is bold. Green edges are old aug. path. old flow=1.
CSE408
Lower bound theory
Lecture # 31
Lower bound
Lecture # 32
Backtracking
• Backtracking is a technique
used to solve problems with
a large search space, by
systematically trying and
eliminating possibilities.
Portion A
• A standard example of
backtracking would be going
Portion B
through a maze.
– At some point in a maze, you
might have two options of
which direction to go:
Backtracking
One strategy would be
to try going through
Portion A of the maze.
Portion B
If you get stuck before
you find your way out,
then you "backtrack"
Portion A
to the junction.
if (!conflict(perm, location, i)) { Check if this position conflicts with any previous
queens on the diagonal
perm[location] = i;
usedList[i] = true; 1) mark the queen in this row
solveItRec(perm, location+1, usedList); 2) mark the row as used
usedList[i] = false; 3) solve the next column location
} recursively
} 4) un-mark the row as used, so we
} can get ALL possible valid
} solutions.
Backtracking – 8 queens problem - Analysis
• Another possible brute-force algorithm is generate the permutations of
the numbers 1 through 8 (of which there are 8! = 40,320),
– and uses the elements of each permutation as indices to place a queen on
each row.
– Then it rejects those boards with diagonal attacking positions.
Lecture # 35
Dynamic Programming
Dynamic Programming is a general algorithm design technique
for solving problems defined by or formulated as recurrences with
overlapping sub instances
• Main idea:
- set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the
knapsack
Consider instance defined by first i items and capacity
j (j W).
Let V[i,j] be optimal value of such an instance. Then
max {V[i-1,j], vi + V[i-1,j- wi]} if j- wi 0
{
V[i,j] =
V[i-1,j] if j- wi < 0
Initial conditions: V[0,j] = 0 and V[i,0] = 0
Knapsack Problem by DP (example)
Example: Knapsack of capacity W = 5
item weight value
1 2 $12
2 1 $10
3 3 $20
4 2 $15 capacity j
0 1 2 3 4 5
00 0 0
w1 = 2, v1= 12 1 0 0 12 Backtracing
w2 = 1, v2= 10 2 0 10 12 22 22 22 finds the actual
optimal subset,
w3 = 3, v3= 20 3 0 10 12 22 30 32 i.e. solution.
w4 = 2, v4= 15 4 0 10 15 25 30 37
Knapsack Problem by DP (pseudocode)
Lecture # 36
Traveling Salesman Problem
Lecture # 39
Euclidean Algorithm
m,n Euclidean
gcd(m,n)
Algorithm
integer euclid(pos. integer m, pos. integer n)
x = m, y = n
while(y > 0)
r = x mod y
x=y
y=r
return x
L11 2
Euclidean Algorithm. Example
gcd(33,77):
Step r = x mod y x y
0 - 33 77
L11 3
Euclidean Algorithm. Example
gcd(33,77):
Step r = x mod y x y
0 - 33 77
33 mod 77
1 77 33
= 33
L11 4
Euclidean Algorithm. Example
gcd(33,77):
Step r = x mod y x y
0 - 33 77
33 mod 77
1 77 33
= 33
77 mod 33
2 33 11
= 11
L11 5
Euclidean Algorithm. Example
gcd(33,77):
Step r = x mod y x y
0 - 33 77
33 mod 77
1 77 33
= 33
77 mod 33
2 33 11
= 11
33 mod 11
3 11 0
L11 =0 6
Euclidean Algorithm. Example
gcd(244,117):
Step r = x mod y x y
0 - 244 117
L11 7
Euclidean Algorithm. Example
gcd(244,117):
Step r = x mod y x y
0 - 244 117
1 244 mod 117 = 10 117 10
L11 8
Euclidean Algorithm. Example
gcd(244,117):
Step r = x mod y x y
0 - 244 117
1 244 mod 117 = 10 117 10
2 117 mod 10 = 7 10 7
L11 9
Euclidean Algorithm. Example
gcd(244,117):
Step r = x mod y x y
0 - 244 117
1 244 mod 117 = 10 117 10
2 117 mod 10 = 7 10 7
3 10 mod 7 = 3 7 3
L11 10
Euclidean Algorithm. Example
gcd(244,117):
Step r = x mod y x y
0 - 244 117
1 244 mod 117 = 10 117 10
2 117 mod 10 = 7 10 7
3 10 mod 7 = 3 7 3
4 7 mod 3 = 1 3 1
L11 11
Euclidean Algorithm. Example
gcd(244,117):
Step r = x mod y x y
0 - 244 117
1 244 mod 117 = 10 117 10
2 117 mod 10 = 7 10 7
3 10 mod 7 = 3 7 3
4 7 mod 3 = 1 3 1
5 3 mod 1=0 1 0
By
L11
definition 244 and 117 are rel. prime.
12
Euclidean Algorithm Correctness
L11 13
Optimization Problem
Lecture # 40
NP-Completeness
• P NP (Sure)
• NPC NP (sure)
• P = NP (or P NP, or P NP) ???
• NPC = NP (or NPC NP, or NPC NP) ???
• P NP: one of the deepest, most perplexing
open research problems in (theoretical)
computer science since 1971.
Arguments about P, NP, NPC
NP NPC
(Poly) Reduction Algorithm for B
Decision algorithm for A
Circuit-satisfiability problem.
Discussion on Poly time problems
• (n100) vs. (2n)
– Reasonable to regard a problem of (n100) as
intractable, however, very few practical problem with
(n100).
– Most poly time algorithms require much less.
– Once a poly time algorithm is found, more efficient
algorithm may follow soon.
• Poly time keeps same in many different
computation models, e.g., poly class of serial
random-access machine poly class of abstract
Turing machine poly class of parallel computer
(#processors grows polynomially with input size)
• Poly time problems have nice closure properties
under addition, multiplication and composition.
Encoding impact on complexity
• The problem instance must be represented in a way
the program (or machine) can understand.
• General encoding is “binary representation”.
• Different encoding will result in different
complexities.
• Example: an algorithm, only input is integer k,
running time is (k).
– If k is represented in unary: a string of k 1s, the running
time is (k) = (n) on length-n input, poly on n.
– If k is represented in binary: the input length n = log k
+1, the running time is (k) = (2n), exponential on n.
• Ruling out unary, other encoding methods are same.
Examples of encoding and complexity
• Hamiltonian cycles
– A simple path containing every vertex.
– HAM-CYCLE={<G>: G is a Hamiltonian graph, i.e. containing
Hamiltonian cycle}.
– Suppose n is the length of encoding of G.
– HAM-CYCLE can be considered as a Language after encoding, i.e. a
subset of * where ={0,1}*.
• The naïve algorithm for determining HAM-CYCLE runs in
(m!)=(2m) time, where m is the number of vertices, m
n1/2.
• However, given an ordered sequence of m vertices (called
“certificate”), let you verify whether the sequence is a
Hamiltonian cycle. Very easy. In O(n2) time.
Class NP problems
• For a problem p, given its certificate, the
certificate can be verified in poly time.
• Call this kind of problem an NP one.
• Complement set/class: Co-NP.
– Given a set S (as a universal) and given a subset A
– The complement is that S-A.
– When NP problems are represented as languages
(i.e. a set), we can discuss their complement set,
i.e., Co-NP.
Relation among P, NP and co-NP={L: L NP where L= *-L}
P
NP-completeness and Reducibility
• A problem p is NP-complete if
1. p NP and
2. p'p p for every p' NP.
(if p satisfies 2, then p is said NP-hard.)
Theorem 34.4 (page 986)
if any NP-compete problem is poly-time
solvable, then P=NP. Or say: if any problem in
NP is not poly-time solvable, then no NP-
complete problem is poly-time solvable.
First NP-complete problem—Circuit
Satisfiability (problem definition)
• In summary
– CIRCUIT-SAT belongs to NP, verifiable in poly time.
– CIRCUIT-SAT is NP-hard, every NP problem can be
reduced to CIRCUIT-SAT in poly time.
– Thus CIRCUIT-SAT is NP-complete.
NP-completeness proof basis
• Lemma 34.8 (page 995)
– If X is a problem (class) such that P'p X for some P'
NPC, then X is NP-hard. Moreover, if X NP, then X
NPC.
• Steps to prove X is NP-complete
– Prove X NP.
• Given a certificate, the certificate can be verified in poly time.
– Prove X is NP-hard.
• Select a known NP-complete P'.
• Describe a transformation function f that maps every instance x
of P' into an instance f(x) of X.
• Prove f satisfies that the answer to xP' is YES if and only if the
answer to f(x)X is YES for all instance x P'.
• Prove that the algorithm computing f runs in poly-time.
NPC proof –Formula Satisfiability (SAT)
• SAT definition
– n boolean variables: x1, x2,…, xn.
– M boolean connectives: any boolean function with one
or two inputs and one output, such as ,,,,,…
and
– Parentheses.
• A SAT is satisfiable if there exists an true
assignment which causes to evaluate to 1.
• SAT={< >: is a satifiable boolean formula}.
• The historical honor of the first NP-complete
problem ever shown.
SAT is NP-complete
INCORRECT REDUCTION: = x10= x7 x8 x9=(x1 x2 x4) (x5 x6) (x6 x7)
=(x1 x2 x4) ((x1 x2) x4) (x4 (x1 x2 x4))=….
NPC Proof –3-CNF Satisfiability
• 3-CNF definition
– A literal in a boolean formula is an occurrence of a variable
or its negation.
– CNF (Conjunctive Nornal Form) is a boolean formula
expressed as AND of clauses, each of which is the OR of
one or more literals.
– 3-CNF is a CNF in which each clause has exactly 3 distinct
literals (a literal and its negation are distinct)
• 3-CNF-SAT: whether a given 3-CNF is satiafiable?
3-CNF-SAT is NP-complete
• Proof: 3-CNF-SAT NP. Easy.
– 3-CNF-SAT is NP-hard. (show SAT p3-CNF-SAT)
• Suppose is any boolean formula, Construct a binary ‘parse’ tree,
with literals as leaves and connectives as internal nodes.
• Introduce a variable yi for the output of each internal nodes.
• Rewrite the formula to ' as the AND of the root variable and a
conjunction of clauses describing the operation of each node.
• The result is that in ', each clause has at most three literals.
• Change each clause into conjunctive normal form as follows:
– Construct a true table, (small, at most 8 by 4)
– Write the disjunctive normal form for all true-table items evaluating to 0
– Using DeMorgan law to change to CNF.
• The resulting '' is in CNF but each clause has 3 or less literals.
• Change 1 or 2-literal clause into 3-literal clause as follows:
– If a clause has one literal l, change it to (lpq)(lpq) (lpq)
(lpq).
– If a clause has two literals (l1 l2), change it to (l1 l2 p) (l1 l2 p).
Binary parse tree for =((x1 x2) ((x1 x3) x4))x2
C1=x1x2 x3
CLIQUE is NP-complete
• Prove the above reduction is correct:
– If is satisfiable, then there exists a satisfying assignment,
which makes at least one literal in each clause to evaluate to
1. Pick one this kind of literal in each clause. Then consider
the subgraph V' consisting of the corresponding vertex of
each such literal. For each pair vir,vjs V', where rs. Since
lir,ljs are both evaluated to 1, so lir is not negation of ljs, thus
there is an edge between vir and vjs. So V' is a clique of size k.
– If G has a clique V' of size k, then V' contains exact one
vertex from each triple. Assign all the literals corresponding
to the vertices in V' to 1, and other literals to 1 or 0, then
each clause will be evaluated to 1. So is satisfiable.
• It is easy to see the reduction is in poly time.
• The reduction of an instance of one problem to a
specific instance of the other problem.
Traveling-salesman problem is NPC
• TSP={<G,c,k>:
G=(V,E) is a complete graph,
c is a function from VVZ,
kZ, and G has a traveling salesman
tour with cost at most k.}
• Theorem 34.14: (page 1012)
– TSP is NP-complete.
TSP is NP-complete
• TSP belongs to NP:
– Given a certificate of a sequence of vertices in the tour, the
verifying algorithm checks whether each vertex appears once,
sums up the cost and checks whether at most k. in poly time.
• TSP is NP-hard (show HAM-CYCLEpTSP)
– Given an instance G=(V,E) of HAM-CYCLE, construct a TSP
instance <G',c,0) as follows (in poly time):
• G'=(V,E'), where E'={<i,j>: i,j V and ij} and
• Cost function c is defined as c(i,j)=0 if (i,j) E, 1, otherwise.
– If G has a hamiltonian cycle h, then h is also a tour in G' with
cost at most 0.
– If G' has a tour h' of cost at most 0, then each edge in h' is 0, so
each edge belong to E, so h' is also a hilmitonian cycle in G.
Subset Sum is NPC
• SUNSET-SUM={<S,t>: S is a set of integers
and there exists a S'S such that t=sS's.}
• Theorem 34.15: (page 1014)
– SUBSET-SUM is NP-complete.
SUBSET-SUM is NPC
• SUBSET-SUM belongs to NP.
– Given a certificate S', check whether t is sum of
S' can be finished in poly time.
• SUBSET-SUM is NP-hard (show 3-CNF-
SATpSUBSET-SUM).
SUBSET-SUM is NPC
• Given a 3-CNF formula =C1 C2… Ck with literals x1, x2,…, xn. Construct a SUBSET-
SUM instance as follows:
– Two assumptions: no clause contains both a literal and its negation, and either a literal or
its negation appears in at least one clause.
– The numbers in S are based on 10 and have n+k digits, each digit corresponds to (or is
labeled by) a literal or a clause.
– Target t=1…1||4…4 (n 1’s and k 4’s)
– For each literal xi, create two integers:
• vi=0…01(i)0…0||0…01(l)0…01(w)0…0, where xi appears in Cl,…,Cw.
• vi'=0…01(i)0…0||0…1(m)0……01(p)0…0, where xi appears in Cm,…,Cp. .
• Clearly, vi and vi' can not be equal in right k digits, moreover all vi and vi' in S are distinct.
– For each clause Cj, create two integers:
• sj=0…0||0…01(j)0…0,
• sj'=0…0||0…02(j)0…0.
• all sj and sj' are called “slack number”. Clearly, all sj and sj' in S are distinct.
– Note: the sum of digits in any one digit position is 2 or 6, so when there is no carries when adding any
subset of the above integers.
SUBSET-SUM is NPC
• The above reduction is done in poly time.
• The 3-CNF formula is satisfiable if and only if there is a subset S'
whose sum is t.
– suppose has a satisfying assignment.
• Then for i=1,…,n, if xi=1 in the assignment, then vi is put in S', otherwise, then
vi' is put in S'.
• The digits labeled by literals will sum to 1.
• Moreover, for each digit labeled by a clause Cj and in its three literals, there
may be 1, 2, or 3 assignments to be 1. correspondingly, both sj and sj' or sj', or
sj is added to S' to make the sum of the digit to 4.
• So S' will sum to 1…14…4.
– Suppose there is a S' which sums to 1…14…4. then S' contains exact one
of vi and vi' for i=1,…,n. if vi S', then set xi=1, otherwise, vi' S', then set
xi=0. It can be seen that this assignment makes each clause of to
evaluate to 1. so is satisfiable.