Unit 4
Unit 4
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 2
r s n a 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 2 e 8
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
return; 2 0 0
//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 matchingfM=inMG 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.