8-Graph
8-Graph
2
TOPICS
Graph Representations
Graph Traversals:
Breadth First Search
Depth First Search
Un-weighted Graphs:
Topological Sort
Strongly Connected Components
Bi-connected Components
Weighted Graphs:
Minimum Spanning Trees
Shortest Paths
Max Flow
Matching
3
GRAPH
REPRESENTATIONS
4
Graph
Graph G = (V, E)
V = V(G) = vertex set of G
E = E(G) = edge set of G ( a set of pairs of vertices of G)
1 2 3
V = { 1, 2, 3, 4, 5, 6 }
1 2 3
V = { 1, 2, 3, 4, 5, 6 }
13 -16
1 2 3
E = {(1,2,13), (1,4,14), (1,5,17),
14 17 21 (2,3,-16), (2,5,21), (3,5,18), (4,5,23)}
18
23
4 5 6 e.g., w(1,5) = w(5,1) = 17.
13 -16
1 2 3
14 E = { (1,2,13), (1,4,14), (1,5,15), (3,2,-16)
14 15 21 (4,5,23), (5,1,14), (5,2,21), (5,3,18)}
18
4 23 5 6 e.g., w(1,5) = 15, w(5,1) = 14.
6
Adjacency Matrix
1 if (i, j) E(G)
A[ i , j] , for i, j V(G).
0 otherwise
1 2 3 4 5 6
1 0 1 0 1 1 0
2 1 0 1 0 1 0
1 2 3 A= 3 0 1 0 0 1 0
4 1 0 0 0 1 0
5 1 1 1 1 0 0
4 5 6 6 0 0 0 0 0 0
1 2 3 4 5 6
1 0 1 0 1 1 0
1 2 3 2 0 0 0 0 0 0
A= 3 0 1 0 0 0 0
4 0 0 0 1 1 0
4 5 6 5 1 1 1 0 0 0
6 0 0 0 0 0 0 7
Weighted Adjacency Matrix
w ( i , j) if (i, j) E(G)
A[ i , j] 0 if i j, (i, j) E(G) , for i, j V(G).
otherwise
1 2 3 4 5 6
1 0 6 9 2
6 8 2 6 0 8 5
1 2 3 A= 3 8 0 -6
-6 4 9 0 4
9 2 5
5 2 5 -6 4 0
4 5 6 6 0
4
1 2 3 4 5 6
1 0 6 9 3
6 8
1 2 3 2 0
A= 3 8 0
7
9 3 5 -6
4 0 4
5 7 5 -6 0
4 4 5 6
6 0
8
(Weighted) Adjacency List Structure
Adj[ i ] { j, w ( i , j) | ( i , j) E ( G ) }, for i V(G).
Adj
6 1 2 6 4 9 5 2
8
1 2 3 2 1 6 3 8 5 5
-6 3 2 8 5 -6
9 2 5
4 1 9 5 4
4 5 6 1 2 2 5 3 -6 4 4
4 5
6
Adj
6 8 1 2 6 4 9 5 3
1 2 3
2
7
9 3 5 -6 4 3 2 8 6 4
4 5 4
4 4 5 6
5 1 7 2 5 3 -6
6 9
The Hand-Shaking Lemma
Vertex vV(G): degree (or valance) , in-degree, out-degree
deg( v ) 2 | E | .
v V ( G )
v V ( G )
indeg( v ) outdeg( v ) | E | .
v V ( G )
10
Adjacency Matrix vs Adjacency List Structure
Adjacency Adjacency
complexity
Matrix List
12
BFS Properties
A simple linear-time graph search algorithm with many applications.
Generalizes the level-order traversal on trees.
G = (V,E) a given directed or undirected graph.
Given a source sV, BFS explores all parts of G that are reachable from s.
Discovers (visits) each vertex u in order of its un-weighted distance d(u) from s.
This distance is the length of the un-weighted shortest path from s to u.
s
s
d(s)=0
p(u)
d(p(u))
u
d(u) = 1+d(p(u))
13
How BFS works
Greedy Choice:
s Scan the oldest
d(s)=0
frontier node.
Q: How?
Scanned nodes (BLACK) A: Maintain
AY) frontier
R nodes
(G
des in a Queue
r no Q
ntie (FIFO
F ro
d(u) u List).
Iteratively
dequeue a vertex u
Unexplored nodes & scan it:
(WHITE) v
visit every
p(v) = u unexplored vertex v
d(v) = 1+d(u) adjacent to u.
14
How BFS works
Greedy Choice:
s Scan the oldest
d(s)=0
frontier node.
Q: How?
Scanned nodes (BLACK) A: Maintain
AY) frontier nodes
R in a Queue Q
(G
des (FIFO List).
r no
ntie Iteratively
F ro
d(u) u dequeue a vertex
u & scan it:
visit every
unexplored
Unexplored nodes vertex v adjacent
v
(WHITE) Enqueueto u.
newly
p(v) = u discovered nodes.
d(v) = 1+d(u)
Frontier advances.
15
BFS Algorithm
Algorithm BFS( G, s )
1. for each vertex v V(G) do § initialize
color[v], d[v], p[v] WHITE, , nil
4. while Q do §
advance frontier
5. u Dequeue(Q) § u = earliest
visited frontier node
6. for each vertex v Adj[u] do § scan u: explore every
edge (u,v)
7. if color[v] = WHITE then do § visit
unexplored adjacent node v
8. color[v], d[v], p[v] GRAY, 1+d[u], u
9. Enqueue(v, Q) § v is a
new frontier node 16
Digraph G: BFS Tree:
0
1
2
s= 11 22 3 1
2 4 5
1
1 44 5 6 2
33 6 7
7 8 9
9
2
3
DONE12453679
SCAN:
Q
9 7 6 3 5 4 2 1
17
Undirected graph G: BFS Tree:
0 1 2 1
s= 1 2 3
2 4 5 7
1
1 4 5 6 2 8 9
3 6
7 8 9
1 2 2
18
BFS Running Time
WHIT GRA BLAC
E
Algorithm BFS( G, s )
Y K
1. for each vertex v V(G) do
• The only way that a node becomes 2. color[v], d[v], p[v] WHITE, ,
gray & hence enters Q is when it was nil
white (lines 2-3-4, 8-9-10). 3. color[s], d[s] GRAY, 0
4. Q ; Enqueue(s, Q)
• So, each node enters Q at most once 5. while Q do
(& only if it was reachable from s). 6. u Dequeue(Q)
7. for each vertex v Adj[u] do
• Thus, a node u is removed from Q 8. if color[v] = WHITE then
at most once (line 6). do
9. color[v], d[v],
• So, a node u is scanned at most once p[v] GRAY, 1+d[u], u
& becomes black (lines 7-13). 10. Enqueue(v, Q)
This takes O(1+|Adj[u]|) time.
11. end-if
• Therefore, the total time is: 12. end-for
13. color[u] BLACK
O V 1 | Adj[ u ] | O V14.
end-while
1 | Adj[ u ] | O ( V E ).
u V ( G ) end uV ( G ) uV ( G )
19
More BFS Properties
FACT 2:
For each edge (u,v) E(G) we have d[v] 1 + d[u].
Furthermore, if G is undirected, then | d[u] – d[v] | 1.
Proof sketch: By the time u becomes black (is scanned), v must have been visited
(if not earlier). …
If G is undirected, then (u,v) = (v,u).
4. Cross-edge: u & v are not ancestor-descendant of each other wrt BFS tree.
20
BFS Tree = Shortest Path Tree rooted at source
FACT 4: For each node v in the BFS tree,
the tree path from root s to v is the un-weighted shortest path from s to v in
G.
(Nodes that are not in the BFS tree are not reachable from s.)
More specifically, we have:
Algorithm PrintPath(v)
p[v] = parent of v in the BFS tree if v = nil then return
= predecessor of v on its shortest path from s PrintPath(p[v])
(nil, if v is not reachable from s), print v
end
d[v] = length of shortest path from s to v in G
( if v is not reachable from s). if v s
0
Proof: Let v be a node reachable from s in G. Then, d[ v ]
1 d[ π[v]] if v s
But that is the solution to the shortest path equations:
0 if v s
d[ v ]
min u {1 d[u] | (u, v) E(G)} if v s
because, by Fact 2: (u,v)E(G), we have d[v] 1+d[u].
(The state of nodes not reachable from s is obvious.) 21
Graph Coloring
G = an undirected graph.
Graph Coloring:
Let k be a positive integer.
G is k-colorable if each node of G can be colored by one of at most k colors such
that for each edge (u,v) E(G), color(u) color(v).
(This problem has long history and many applications.)
1-colorable E(G) =
2-colorable: also easy to detect (see next slides)
3-colorable: is hard to detect (see NP-completeness)
Kn needs n colors.
All planar graphs are 4-colorable.
22
Bipartite Graphs
G = an undirected graph.
Bipartiteness:
G is bipartite if V(G) can be partitioned into two disjoint subsets X and Y such
that for each edge (u,v)E(G), either uX & vY, or vX & uY.
X Y
23
FACT 5: Let G be an undirected graph. The following statements are equivalent:
1. G is bipartite,
2. G is 2-colorable,
3. G has no odd-length cycle,
4. In BFS of G, for every edge (u,v)E(G), we have | d[u] – d[v] | = 1.
These can be tested in O(V+E) time (constructive proof).
Proof:
[(1) (2)]: Let (X,Y) be a bipartition of V(G). Color nodes in X with color 1, and nodes in
Y with color 2.
[(2) (3)]: If G is 2-colorable, then vertices of any cycle have alternate colors.
Hence, the cycle has even length.
[(3) (4)]: By Fact 2 we know that for each edge (u,v), |d[u] – d[v]| 1.
If d[u] = d[v] (i.e., they are at the same BFS tree level), then
edge (u,v) plus the tree paths from each of u and v to their
lowest common ancestor form an odd-length cycle.
u v
A contradiction.
25
DFS Properties
Another simple linear-time graph search algorithm with many applications.
Generalizes the pre-order and post-order traversals on trees.
G = (V,E) a given directed or undirected graph.
DFS creates a forest of node-disjoint trees with non-tree edges between nodes.
Pick an unvisited node as the root of the next DFS tree and advance the search.
Greedy Choice:
within the same DFS tree, advance the search from the most recent visited node.
Recursive DFSvisit(u):
explores nodes in the sub-tree rooted at u
(the nodes reachable from u that are still unexplored at the time u gets visited).
DFS(u) recursive call is active u is an ancestor of the most recent visited node.
26
DFS Algorithm
Algorithm DFS(G)
1. time 0
2. for each vertex u V(G) do color[u], p[u] WHITE, nil
3. for each vertex u V(G) do
if color[u] = WHITE then DFSvisit(u) § u is root of next DFS
tree
end
Procedure DFSvisit(u)
4. d[u] time time + 1 § DFSvisit(u) discovery
time
5. color[u] GRAY § u is visited
6. for each vertex v Adj[u] do § explore edge (u,v)
7. if color[v] = WHITE then do § visit unexplored
adjacent node v
8. p[v] u § (u,v)
is a DFS tree-edge
9. DFSvisit(v) § v is
most recent visited node
10. end-if 27
a b c d
G:
e f g h
[1,12 ] d [13,16]
DFS(G): a
h [14,15]
[2,11 ] b
[3, 4 ] c [5, 8 ] f g
[9,10 ]
G:
g h i j k
[1,16] a e [17,22]
DFS(G):
[2,15]
b f [18,21]
[3,10]
g [11,14]
c
k [19,20]
[4,7] d i h [12,13]
[8,9]
Each edge is traversed twice (once in each direction).
[5,6] j The first traversal of each edge is:
downward if it’s a tree-edge; upward otherwise. 29
DFS Running Time
Algorithm DFS(G) Procedure DFSvisit(u)
1. time 0 4. d[u] time time + 1
2. for each vertex uV(G) do color[u], p[u] WHITE, nil 5. color[u] GRAY
3. for each vertex uV(G) do 6. for each vertex vAdj[u] do
if color[u] = WHITE then DFSvisit(u) 7. if color[v] = WHITE then do
end 8. p[v] u
9. DFSvisit(v)
10. end-if
WHIT GRA BLAC
11. end-for
E Y K 12. color[u] BLACK
13. f[u] time time + 1
end
O V 1 | Adj[ u ] | O V 1 | Adj[ u ] | O ( V E ).
u V ( G ) u V ( G ) u V ( G )
30
The Parenthesis Theorem (PT)
The Parenthesis Theorem (PT):
1. For all nodes u: d[u] < f[u].
[d[u], f[u]] is activation time interval of DFSvisit(u).
2. For every pair of nodes u & v, their time intervals [d[u],f[u]] and [d[v],f[v]] are
either disjoint or one encloses the other (no partial overlap).
3. u is a DFS ancestor of v if and only if
[d[u],f[u]] encloses [d[v],f[v]], that is, d[u] d[v] < f[v] f[u].
Proof: u w
Consider DFS sub-trees Tu and Tv rooted at u & v, respectively.
Tu and Tv are either completely disjoint, or one includes the other.
Their DFSvisit activation time intervals will behave accordingly.
v
In the illustrative figure to the right,
[d[u],f[u]] encloses [d[v],f[v]],
but is disjoint from [d[w],f[w]].
31
The White-Path Theorem (WPT)
The White-Path Theorem (WPT):
Node u is a DFS ancestor of node v if and only if
when u is discovered, there is a path of all white nodes from u to v.
Proof [of ]:
The nodes on the DFS tree path from u to v are all white at the activation time of DFSvisit(u).
v back-edge:
u tree-edge:
color[v] = GRAY
color[v] = WHITE
v
cross-edge:
cross-edge:
color[v] = BLACK
& color[v] = WHITE
d[v] < d[u] Impossible by WPT!33
DFS Finishing Times
DiGraph: state of edge uv at the completion of DFS:
u
f[u] f[v]:
u
back-edge
34
DFS Edge Classification of Undirected Graphs
Undirected Graph: only 2 types of edges:
tree-edges &
back-edges.
v
tree-edge
back-edge
36
TOPOLOGICAL SORT
37
Directed Acyclic Graph (DAG)
a b c d
DAG G:
e f g h
a b d h g c f e
38
Topological Sort Algorithm 1
FACT 1:
A DAG G has at least one vertex with in-degree 0 (& one vertex with out-degree 0).
Proof:
G has no cycles all paths in G have finite length (no node can repeat on a path).
Let P be a longest path in G.
Suppose P starts at node u and ends in node v.
indeg(u) = outdeg(v) = 0 (otherwise, we can extend P to a longer path).
a b c d
indeg(a) = 0
Example DAG: indeg(d) = 0
e f g h outdeg(e) = 0
40
Topological Sort Algorithm 2
In O(V + E) time do a DFS of G with the following modification:
Push nodes into an initially empty stack S when they become black
(i.e., an added line at the end of DFSvisit).
At the end of DFS(G): repeatedly pop(S); nodes come out in topological order.
DAG G: DFS(G):
e
a e
e a b
f
a
f f
c d b b
d
c d S: c
A topological ordering of G:
e f a b d c
41
STRONGLY CONNECTED
COMPONENTS
42
Graph Connectivity
How well is a graph connected?
Connectivity issues arise in communication & transportation networks.
Identify bottlenecks in case parts of the network break down or malfunction.
Fault tolerance in distributed networks.
Digraph Connectivity:
Semi-connectivity:
can every pair of nodes have at least one-way communication?
43
Digraph Strong Connectivity
a b c a b c h
d f d
e e f
g
g
Strongly Connected:
There is a directed path 3 Strongly Connected Components
from any node to any other node.
e e
d f d f
g h i j g h i j
a
DFS(G): b By the WPT
every node of an SCC
c falls within the same DFS tree.
e f
So, each DFS tree is
d the union of one or more
SCC’s
h i
g j How can we disentangle them?
46
• SCC roof = the node x with minimum d[x] among all nodes of the SCC
(nodes d, f, e, a in the example below).
• WPT all nodes of an SCC are DFS descendants of its roof.
• SCC roof x has maximum f[x] among all nodes of the SCC.
a e f d
DFS(GT):
c j g
i
b h
48
a
G:
b c Topological Sort
e of the
d f SCC Component Graph of G:
ac f j dg
e
b i h
g h i j
a e f d
DFS(GT):
c j g
i
b h
49
SCC Algorithm – Kosaraju-Sharir [1978]
Algorithm StronglyConnectedComponents(G)
1. Initialize stack S to empty, and call DFS(G) with the following modifications:
push nodes onto stack S when they finish their DFSvisit calls.
I.e., at the end of the procedure DFSvisit(u) add the statement “Push(u,S)”.
(There is no need to compute d[u] & f[u] values explicitly.)
4. Each DFS-tree of step 3 (plus all edges between its nodes) forms an SCC.
SCCs are discovered in topological order of the SCC Component Graph of G.
end 50
FACT 1: Each DFS tree T of DFS(GT) consists of a single SCC.
Proof: x
FACT 2: In DFS(GT), the DFS trees (i.e., SCCs) are discovered in topological
order of the SCC Component Graph of G.
Proof:
In DFS(GT), each DFS tree is an SCC (by Fact 1).
THEOREM: The algorithm correctly finds the SCCs in topological order of the
SCC Component Graph of G in O(V + E) time.
52
Bi-CONNECTED
COMPONENTS
We must all hang together,
or assuredly we shall hang separately.
Benjamin Franklin
53
Node Connectivity
undirected graph G = (V, E) & integer k 0.
G is k-connected (or k-node-connected):
if removal of fewer than k nodes (& all their incident edges) cannot disconnect G.
FACT 1: G is k-connected if and only if between every pair of its nodes there are
at least - node-disjoint paths (excluding their common end points).
a b
e
c
f
d
FACT 3: xV is an articulation point of G if there are two distinct nodes u & v in
G distinct from x, such that every path between u & v passes through x.
56
Example
a b
c e f
d g
h j k
i
l m n o
a b
c e e f f
d g
k
i
h j k
i i
l m n o
57
Biconnected Components & Articulation Points
FACT 5: Let Gi = (Vi , Ei ) , i = 1..k, be biconnected components of G. Then,
1. Gi is biconnected , for i = 1..k,
2. Vi Vj has at most one node, for i j,
Gi
e1 e2
u x v
58
Biconnected Components & Articulation Points
FACT 5: Let Gi = (Vi , Ei ) , i = 1..k, be biconnected components of G. Then,
1. Gi is biconnected , for i = 1..k,
2. Vi Vj has at most one node, for i j,
Proof of (2): Gi and Gj cannot have more than one point in common, say x and y:
x
Gi Gj
y 59
Biconnected Components & Articulation Points
FACT 5: Let Gi = (Vi , Ei ) , i = 1..k, be biconnected components of G. Then,
1. Gi is biconnected , for i = 1..k,
2. Vi Vj has at most one node, for i j,
Proof of (3): [ ]:
Suppose x is an articulation point of G, lying on every path between u & v.
Consider one such path, and let e1 and e2 be the two edges on the path incident to x.
e1 e2
u x v
Then, e1 e2 .
So, e1 Ei and e2 Ej for some i j.
Therefore, x Vi Vj for some i j.
60
Biconnected Components & Articulation Points
FACT 5: Let Gi = (Vi , Ei ) , i = 1..k, be biconnected components of G. Then,
1. Gi is biconnected , for i = 1..k,
2. Vi Vj has at most one node, for i j,
Proof of (3): [ ]:
x
v
Gi u Gj
61
Apply DFS
DFS is particularly useful in finding the a
biconnected components of a graph, because in
b
the DFS there are no cross edges; only tree- and
back-edges. c
k
a
k l
b c i j l
m
d m n
g
e o
n o
f h
62
Articulation Points & DFS
FACT 6: Node u is an articulation point either 1 or 2 holds:
1. u is a DFS root with at least 2 children, or
2. u is not root, but it has a child v such that there is no back-edge between any
descendant of v (including v) and any ancestor of parent p[u] of u (including p[u]).
root
1 u root 2
Removal of u
disconnects
v w p[u] v & p[u].
u
v
No cross-edges.
Removal of u
disconnects v & w.
63
DFS “low” Computation
Do a DFS and for each node uV(G) compute: root
DFS pre-order numbering of node u.
x
FACT 7:.
64
Articulation Points & DFS “low” numbers
FACT 8: Node u is an articulation point either 1 or 2 holds:
1. u is a DFS root with at least 2 children, or
2. (u is not root &) a child v of u such that low[v] pre[u].
pre low
[1,1] a
[2,1] b
g
[5,4] e [7,2]
low[h] pre[g]
[6,4] f h [8,7]
i
[9,7]
65
Stack up edges
[1,1] a
Stack up each edge (u,v)
at its 1st traversal, i.e., if [2,1] b
v p[u] & pre[v] < pre[u].
Articulation points discovered [3,1] c
in post-order of child-node. [4,2] d
Pop-up the stack g
up to (u,v), inclusive, when [5,4] e [7,2]
u = p(v) & low(v) pre[u].
[6,4] f h [8,7]
top (i, g) [9,7]
(i, g) i
(h, i)
(f, d) POP (f, d) (h, i)
(g,h) (g, h)
(e, f) (e, f) (g, h)
(d, e) (d,e) (d, e) (g, b) (g, b) (g, b)
(c, d) (d, g) (d, g) (d, g)
(c, a) (c, d) (c, d) (c, d) (c, d)
(b, c) (c, a) (c, a) (c, a) (c, a)
(a, b) (b, c) (b, c) (b, c) (b, c)
bottom (a, b) (a, b) (a, b) (a,b) (a, b) 66
BiConnectivity Algorithm – Hopcroft [1972]
Algorithm BiConnectivity (G) § O(V
+ E) time
1. S ; time 0
§ assume no isolated nodes
2. for each vertex u V(G) do pre[u], p[u] 0, nil
3. for each vertex u V(G) do if pre[u] = 0 then SearchBC(u)
end
Procedure SearchBC(u)
4. pre[u] low[u] time time + 1
5. for each vertex v Adj[u] do
6. if v p[u] & pre[v] < pre[u] then Push((u,v), S)
§ 1st traversal of (u,v)
7. if pre[v] = 0 then do
8. p[v] u
§ (u,v) is a tree-edge
9. SearchBC(v)
10. if low[v] pre[u] then PrintBC(u,v)
11. low[u] min { low[u] , low[v] }
12. else if v p[u] then low[u] min { low[u] , pre[v] } § (u,v) is
a back-edge
13. end-for
end 67
MINIMUM
SPANNING TREES
68
What is an undirected tree?
Let T be an undirected graph on n nodes.
The following statements are equivalent:
1. T is a tree.
2. T is connected and acyclic.
3. Between each pair of nodes in T there is a unique simple path.
4. T is connected and has n –1 edges.
5. T is acyclic and has n –1 edges.
6. T is minimally connected: removal of any edge will disconnect T.
7. T is maximally acyclic: addition of any new edge will create a simple cycle.
69
The MST Problem
INPUT: A weighted connected undirected graph G = (V, E, w).
a a
a 3 5 3
5 3
b c b c
b c 2
2 7 4
7 4
4
e d e d
e d 8
8
Minimum spanning tree T Another spanning tree T’
G Cost(T’) = 3+5+4+8 = 20
Cost(T) = 3+2+4+7 = 16
70
MST Preliminaries
Applications:
• Optimum cost network interconnection (connected & acyclic)
• Minimum length wiring to connect n points
• Sub-problem to many other problems
FACT:
• A connected undirected graph has at least one spanning tree (e.g., the DFS tree).
• So, a connected weighted undirected graph has an MST.
Caley’s Theorem: Kn, the complete graph on n nodes, has nn-2 spanning trees.
K4
For example: has 42 = 16 spanning trees.
71
A CUT
• If S is a set and e an element, then S+e denotes S {e}, & S-e denotes S – {e}.
• An edge e is incident to subgraph (or subset of vertices) H,
if exactly one end of e is in H.
• A cut C = (X,Y) is a partitioning of V(G) into two non-empty subsets X and Y.
Cross-edges of C are edges in E(G) that are incident to X (and also to Y).
X Y
72
MST Properties
2 3 4
a b c d a b c d
5 4 3 8 6 3 7
e f g h e f g h
6 2 6
8 7 3 9
4 5 5
i j k l i j k l
9 8 6
G T = MST of G
• T – (f,g) is disconnected.
Its two connected components induce a cut in G.
What is the minimum weight cross-edge of that cut?
What is the range of values for w(f,g) while T remains an MST?
73
A Generic Greedy Strategy
Assume initially all edges of G are white (undecided).
We will iteratively select a white edge and make a permanent greedy decision,
either coloring it blue (accepted) or red (rejected).
This is done according to the following two rules, whichever applies:
Progress: either no white edge remains, or at least one of the two rules applies.
In |E(G)| iterations all edges are colored blue/red. The blue edges form the MST.
There are many MST algorithms, and virtually all of them are specializations of
this generic greedy strategy depending on how the two rules are applied. 74
Generic MST Algorithm
Pre-Cond: input is a connected undirected weighted graph Greedy
G method
LI: Red-Blue-Invariant
Color one more edge red/blue
LI & exit-cond by applying either
PostLoopCond white NO
the Red-Rule or
edge
YES the Blue-Rule,
whichever applies.
blue edges form an MST of G
PostLoopCond
& PostLoopCode return blue edges MP = # white edges
Post-Cond
???
Post-Cond: output is an MST of
75
G
Must prove 3 things:
76
LI & exit-cond & Blue-Rule applied LI
Proof:
• There is an MST T that includes all blue edges and excludes all red edges.
• Suppose Blue-Rule is applied to cut (X,Y) that contains no blue cross-edges,
and minimum weight white edge e of cut (X,Y) is colored blue.
• If e T, then T still satisfies the LI.
• Suppose e T. e’T
• T + e contains a unique simple cycle C.
There must be at least one other (white) edge
e’ TC that crosses the cut (X,Y).
(Why does e’ exist & is white?) eT
So, w(e) w(e’).
X Y
• T’ (T + e) – e’ now satisfies LI:
T’ is also a spanning tree of G.
T’ includes all blue edges (e among them) and excludes all red edges.
Cost(T’) = Cost(T) + w(e) – w(e’) Cost(T).
So, T’ is also an MST. 77
LI & exit-cond & Red-Rule applied LI
Proof:
• There is an MST T that includes all blue edges and excludes all red edges.
• Suppose Red-Rule is applied to cycle C that contains no red edges,
and maximum weight white edge e of C is colored red.
• If eT, then T still satisfies the LI.
• Suppose eT. e’T
• T – e is disconnected and induces a cut (X,Y).
There must be at least one other (white) edge C
e’ C that crosses the cut (X,Y).
(Why does e’ exist & is white?) eT
So, w(e’) w(e).
X Y
• T’ (T + e’) – e now satisfies LI:
T’ is also a spanning tree of G.
T’ includes all blue edges and excludes all red edges (e among them).
Cost(T’) = Cost(T) + w(e’) – w(e) Cost(T).
So, T’ is also an MST. 78
Progress: If there is a white edge, then
at least one of the 2 rules is applicable
Proof:
• Suppose there is a white edge e = (u,v).
• If u & v are connected by a blue path, say P, then Red-rule applies to cycle P+e.
• Otherwise, define: X = the set of nodes connected to u by blue paths.
Y = V(G) – X.
• Cut (X,Y) contains at least one white cross-edge (e) & no blue cross-edges. Why?
So, the Blue-rule can be applied to this cut.
79
Short Bridge
B e
s e
81
Kruskal’s MST Algorithm
e = min weight white edge.
If both ends of e are in the same blue component
then color e red (by the Red-Rule),
else color e blue (e is a short bridge).
82
Algorithm KruskalMST( G )
1. for each vertex v V(G) do MakeSet(v) §
initialize blue forest
2. SORT edges E(G) in non-decreasing order of weight § all edges white
3. T
§ MST blue edges
4 bd 4 8
5 3 6
4 dh
5 ad
5 ei d e f
6 cf 6 2
6 de
7
7 eh 8 3
4 5
8 ce
8 dg
8 hi g h i
9 8
9 gh
84
Prim’s MST Algorithm
B = blue component that contains the selected starting node s.
e = the short bridge of B.
Color e blue.
s bridge(u,B) u
key = - p(u)
B Q
B Q
s bridge(B) u
key = - p(u)
v
u DeleteMin(Q)
key[u] -
B Q
s u
key = - p(u)
v
10. UpHeap(v,Q)
11. O(Eend-if
log V) with standard binary heap
12.Total time =end-for
O(E + V log V) with Fibonacci Heaps (EECS 4101 & [CLRS 20]).
13. end-while 87
Example
2
key = - + key = - +
3
key = 0-
p= a p= b
p = nil
2 3
s= a b c
4 8
5 3 6
key = 3
+ -
45
key = -+ p=b key = -+26
p = ba d e f p = ec
6 2
7
8 3
4 5
key = -+8
p= d g h i
9 8
key = -+35
key = -+47
p = fe
p= de
88
MST Verification
T:
LCA(u,v)
v
u
89
Bibliography: MST
Kruskal [1956] (Borůvka [1926]): O(E log V) time
Prim[1957] (Jarník [1930]): O(E log V) , O(E + V log V) with Fib.Heap
Cheriton-Tarjan [1976] AAW
O(E log log V) time on general graphs,
O(V) time on planar graphs with arbitrary edge weights
[studied in EECS 6114]
Buchsbaum et al. [2006], King [1997]: O(E) time MST verification algorithm.
Euclidean MST of n points in the plane:
O(n log n) time via Voronoi-Delaunay structures [studied in EECS 6114]
P.T.O.
90
Bibliography: MST
A.L. Buchsbaum, L. Georgiadis, H. Kaplan, A. Rogers, R.E. Tarjan, J.R. Westbrook,
“Linear-time pointer-machine algorithms for path evaluation problems on trees and graphs,”
arXiv:cs.DS/0207061v2, October 2006. [An earlier version in STOC’88, pp: 279-288.]
B. Chazelle, “The Soft Heap: an approximate priority queue with optimal error rate,”
J. ACM, 47(6):1012-1027, 2000.
B. Chazelle, “A minimum spanning tree algorithm with inverse Ackermann type complexity,”
J. ACM, 47(6):1028-1047, 2000.
D. Cheriton, R.E. Tarjan “Finding minimum spanning trees,”
SIAM J. Computing (5):725-742, 1976.
H. Kaplan, U. Zwick, “A simpler implementation and analysis of Chazelle’s Soft Heaps,”
SODA, pp:477-485, 2009.
D.R. Karger, P.N. Klein, R.E. Tarjan, “A randomized linear-time algorithm to find minimum
spanning trees,” J. ACM (42):321-328, 1995.
V. King, “A simpler linear time algorithm for minimum spanning tree verification,”
Algorithmica, 18: 263-270, 1997.
S. Pettie, V. Ramachandran “An optimal minimum spanning tree algorithm,”
J. ACM 49(1):16-34, 2002.
91
SHORTEST PATHS
Well, ya turn left by the fire station in the village and take the old post road by
the reservoir and. . . no, that won’t do.
Best to continue straight on by the tar road until you reach the schoolhouse and
then turn left on the road to Bennett’s Lake until. . . no, that won’t work either.
East Millinocket, ya say? Come to think of it, you can’t get there from here.
Robert Bryan and Marshall Dodge,
Bert and I and Other Stories from Down East (1961)
92
The Shortest Path Problem
Given weighted digraph G = (V,E,w) with w: E (possibly < 0).
|
94
Negative Cost Cycles
FACT 1: Suppose node t is reachable from node s.
a shortest path from s to t if no path from s to t contains a negative cost cycle.
If there is any shortest path from s to t, there is one that is simple.
Proof:
If some s-t path contains a negative cycle, we can produce an arbitrarily short s-t path by
repeating the cycle enough times.
If no s-t path contains a negative cycle, we can make any s-t path simple, without increasing
its cost, by removing (or bypassing) the cycles on the path.
In the presence of negative cycles, asking for a simple shortest path is NP-Complete.
95
Shortest Path Trees
Shortest Path Tree T: A compact way to represent single-source shortest paths.
This is a tree rooted at the source s and spans all nodes reachable from s,
all of whose paths are shortest paths in G.
T: g 6
G: g
3 2 3
-2 3 e f 9
e f
4
-1 5 -1 5
10 d
10 d
12
-6 7
b 4 c b 4 c 3
8
13 3
3 a
a
source 0
FACT 3: T is a shortest path tree if and only if for every edge (u,v) of G,
dist(u) + w(u,v) dist(v). [Verifiable in O(V+E) time.]
Proof: dist(u) + w(u,v) < dist(v) the path in T from s to v is not shortest.
Conversely, suppose dist(u) + w(u,v) dist(v) for every edge (u,v) in G.
Let P be any path from s to any node v. By induction on the # edges on P, we
can show d(P) dist(v). Hence, T is a shortest path tree.
dist(v) v
s w(u,v)
dist(u) u
97
Characterizing Shortest Path Trees
T = a subgraph of G that is a tree rooted at source s and spans all nodes reachable from s.
dist(v) = cost of the unique path in T from s to v.
FACT 3: T is a shortest path tree if and only if for every edge (u,v) of G,
dist(u) + w(u,v) dist(v). [Verifiable in O(V+E) time.]
g T: g 6
G:
3 2 3
e
-2 f
3 e f 9
4
-1 5 -1 5
10 d 12 10 d
-6 7
b 4 c b 4 c 3
8 13 3
3
a a
source
0
98
The Recurrence [Bellman 1958]
T = shortest path tree rooted at s.
dist(v) = cost of the unique path in T from s to v.
p(v) = parent of v in T (nil if no parent).
R(s) = the set of nodes in G that are reachable from s.
Initialize:
dist(s) = 0
dist(v) = + v V(G) – {s}
p(v) = nil v V(G)
Labeling Step:
Select an edge (u,v) E(G) such that dist(u) + w(u,v) < dist(v).
Label v: dist(v) dist(u) + w(u,v) ;
p(v) u.
dist(v) v
s w(u,v)
dist(u) u
[CLRS] calls this a Relaxation Step. 100
Labeling Method Invariants
FACT 5:
Ford’s labeling method maintains the following invariants for any vV(G):
(1) If dist(v) is finite, then there is an s-v path of cost dist(v).
(2) If p(v) nil, then dist(p(v)) + w(p(v) ,v) dist(v) remains true,
and is met with equality when the method terminates.
(3) If P is any s-v path, then d(P) dist(v) when Ford’s method terminates.
FACT 6:
(1) When the labeling method terminates,
dist(v) = cost of a shortest s-v path, if v is reachable from s, and
dist(v) = otherwise.
(2) If there is a negative cycle reachable from s, the method never terminates. 101
Tentative Shortest Path Tree Invariants
Define: p0(v) = v, pk+1 (v) = p(pk(v)) for any vertex v and any integer k 0.
That is, p0(v), p1(v), p2(v), … are v, parent of v, grand parent of v, …
FACT 7:
(1) Ford’s labeling method maintains the invariant that
either the edges (p(v) ,v) , for v such that p(v) nil,
form a tree rooted at s, spanning vertices v such that dist(v) < ,
or there is a vertex v such that pk(v) = v for some k > 0.
(2) If at some time during the labeling method
pk(v) = v for some vertex v and integer k > 0,
then the corresponding cycle in G has negative cost.
Proof sketch:
(1) By ind. on # labeling steps. dist(u) – dist(v) + w(u,v) < 0
dist(u)
(2) Consider a labeling step dist(v)
that creates such a cycle
s v u
of parent pointers:
w(u,v) 102
Termination?
FACT 8:
If and when Ford’s labeling method terminates, the parent pointers define
a shortest path tree from the source s to all vertices reachable from s.
103
Labeling & Scanning Method
This first refinement eliminates unnecessary edge examinations.
Maintains a partition of vertices into 3 states: unlabeled, labeled, scanned.
The labeled or scanned vertices are exactly those with finite dist values.
The possible state transitions are:
3 -1 4 3 3
1 1 1
a b a b a b
1 1
0 0 0
(1) Scan a (2) Scan c
(0) Initial
3 1 0 1 0 0
-2 -2 -2 -2 -2 -2
c d c d c d c d
3 -1 -1 -1
1 1 1 1
a b a b b b
a a
1 1 1 1
0 0 0 0
(3) Scan d (4) Scan b (5) Scan c (6) Scan d
106
1. G is a DAG [Topological]
a b d h g c f e
3 -1 4 Topological Order: b, a, c, d. -1 4
1
b a b is not reachable from a. b a
0 0
(0) Initial (1) Scan a
-1 -2 -3 -1 -2 -3
c d c d
-1 -1
b a b a
0 0
(2) Scan c (3) Scan d
FACT 9:
Suppose every edge in G has non-negative weight.
Scanning shortest first maintains the invariant: dist(u) dist(v)
for every scanned vertex u and every non-scanned (labeled/unlabeled) vertex v.
Proof:
This means that vertices are scanned in non-decreasing order by shortest distance
from s, and that a vertex, once scanned, due to non-negativity of edge weights,
cannot become labeled.
dist(u) u
s w(v,u)
dist(v) v
110
Example
0) Initial: 1) Scan a:
g g
3 2 3 2
1 1
e f e f
4
10 0 5 10 0 5
d 12 d 12
8 7 8 7
b 4 c b 4 c
6 3 6 3 3
a a
0 0
3) Scan d: g 2) Scan c: g
4 3 2 9 3 2 15
1 1
e f e f
4 4
10 0 5 10 0 5
d 12 d 12
8 7 8 7
b 4 c b 4 c
6 3 3 6 3 3
a a
0 0 111
Example
7 7
4) Scan e: g 5) Scan g: g
4 3 2 9 4 3 2 9
1 1
e f e f
4 4
10 0 5 10 0 5
d 12 d 12
8 7 8 7
b 4 c b 4 c
14 6 3 3 14 6 3 3
a a
0 0
7
3) Scan d: g 6) Scan f: g
4 3 2 9 4 3 2 9
1 1
e f e f
4 4
10 0 5 10 0 5
d 12 d 12
8 7 8 7
b 4 c b 4 c
6 3 3 14 6 3 3
a a
0 0 112
Example
7 7
4) Scan e: g 5) Scan g: g
4 3 2 9 4 3 2 9
1 1
e f e f
4 4
10 0 5 10 0 5
d 12 d 12
8 7 8 7
b 4 c b 4 c
14 6 3 3 14 6 3 3
a a
0 0
7 7
7) Scan b: g 6) Scan f: g
4 3 2 9 4 3 2 9
1 1
e f e f
4 4
10 0 5 10 0 5
d 12 d 12
DONE
8 7 8 7
b 4 c b 4 c
14 6 3 3 14 6 3 3
a a
0 0 113
Algorithm Dijkstra ( G , sV(G) )
1. for each vertex u V(G) do dist[u] +
2. dist[s] 0 ; p[s] nil
§ first node to be scanned
3. Q ConstructMinHeap(V(G), dist) § dist[u]
= priority(u)
4. while Q do
§ |V| iterations
5. u DeleteMin(Q)
§ O(log V) time § O( |Adj[u]| log V) time
6. for each v Adj[u] do
115
Algorithm BellmanFord ( G , sV(G) ) § O(VE) time
1. for each vertex u V(G) do dist[u] +
2. dist[s] 0 ; p[s] nil
11. for each edge (u,v) E(G), in arbitrary order do § stage |V(G)|
12. if dist[v] > dist[u] + w(u,v) then
13. return there is a negative cycle reachable
from s
end
Exercise: If line 13 detects existence of a negative cycle,
find one such cycle in O(V) time.
116
All-Pairs
Shortest Paths
117
All-Pairs by Single-Source
All-Pairs Shortest Paths: |V| iterations of Single-Source Shortest Paths.
If all edge weights are non-negative, |V| Dijkstra iterations.
Time = O(VE log V) improved to O(VE + V2 log V) by Fibonacci
Heap.
If some edge weights may be negative, |V| Bellman-Ford iterations.
Time = O(V2E).
Edmonds-Karp [1972] & Johnson [1977] found an improved method:
Preprocessing transformation:
make all edge weights non-negative but preserve shortest paths.
118
Edmonds-Karp & Johnson Transformation
1. Start with the given weighted digraph G = (V, E, w).
2. Add to G a new vertex sV and a 0 weight edge from s to each vertex vV.
3. Apply Bellman-Ford to the augmented graph with source s:
either find shortest path distances, denoted r(v), from s to each vV,
or discover a negative cycle in G. In the latter case abort.
0 0 r(d)= -4
r(c) = -2
-2 -2 -2
c d c d
c d 0 0
-1 4 3 -1 4 3 -1 4
3 s s
-1 -1 0 -1
0 a b a b
a b
3 3 3
r(a) = 0
0 r(b)= -1
0
G
Single-source
Augmented G shortest dist in
Augmented G 119
Edmonds-Karp & Johnson Transformation
Transform edge weights: ŵ(u,v) = w(u,v) + r(u) – r(v) for all (u,v) E(G).
r(c) = -2 r(d) = -4 0
-2 c d
c d
5 0 1
G: 3 -1 4
0
-1 a b
a b 2
3 Edge-weight transformed G
r(a) = 0 r(b) = -1
d(k)(u,v) = Minimum cost among all those paths from node u to node v
whose intermediate nodes (excluding u and v) are in {1..k}.
That is, the maximum numbered intermediate node is at most k.
p(k)(u,v) = predecessor of node v on such a path.
121
Floyd-Warshall [1962]
DP: How to go from d(k-1) to d(k), for k = 1,2, …, n?
If d(k-1)(k,k) < 0, then there is a negative cycle & k is the max node of it. Abort.
Otherwise: for all u, v we have:
k ) k d (k-1)
(k-
1) (u , ( k ,v
d )
OSSP holds:
u v
d(k-1)(u,v)
122
Floyd-Warshall [1962]
d(k-1)(k,k) 0 d(k-1)(k,k)
k
d(k)(u,k) = min{ d(k-1)(u,k), d(k-1)(u,k) + d(k-1)(k,k)}
= d(k-1)(u,k)
123
Algorithm FloydWarshall (G) § O(V3) time, O(V2)
space
125
Bibliography: Shortest Paths
R.E. Bellman “On a routing problem,” Quart. Appl. Math. 16: 87-90, 1958.
E.W. Dijkstra, “A note on two problems in connexion with graphs,” Numer. Math.1:269-271, 1959.
Jack Edmonds, R.M. Karp, “Theoretical improvements in algorithmic efficiency for network flow
problems,” J. ACM 19:248-264, 1972.
R.W. Floyd, “Algorithm 97: Shortest path,” CACM 5:345, 1962.
L.R. Ford, Jr., “Network flow theory,” Paper P-923, RAND Corp., Santa Monica, CA., 1956.
L.R. Ford, Jr., D.R. Fulkerson, “Flows in Networks,” Princeton University Press, 1962.
D.B. Johnson, “Efficient algorithms for shortest paths in sparse networks,” JACM 24(1):1-13, 1977.
P. Klein, S. Rao, M. Rauch, S. Subramanian, “Faster shortest-path algorithms for planar graphs,”
26th STOC, pp: 27-37, 1994.
R.E. Tarjan, “A unified approach to path problems,” JACM 28: 577-593, 1981.
M. Thorup, “Undirected single-source shortest paths with positive integer weights in linear time,”
J. ACM 46(3):362-394, 1999.
S. Warshall, “A theorem on Boolean matrices,” JACM 9: 11-12, 1962.
126
MAX FLOW
127
Flow Network
Weighted digraph G = (V, E, c, s, t): s = source node (in-degree = 0)
t = sink node (out-degree = 0)
edge capacity: c(e) > 0 eE
flow
a 2 a 3
2 3 / /3 capacity
1
2/4
s t s t
4
5/
5 3
5 /3
b b 3
Max flow value = 1 + 5 = 3 + 3 = 6
129
Multi-Source Multi-Sink
G t1
s1
a t2
s s2 t
b
t3 super
super
sink
source c d
s3 t4
130
Applications
Electrical circuits
Hydraulic systems
Traffic movements
Telecommunication networks
Freight transportation
Min-cost flow
Graph Matching
Matrix rounding
…
131
A Heuristic: flow composition by s-t path flows
a a a
0/4 0/5 0/4 3/5 2/4 5/5
s 0/3 t s 3/3 t s 3/3 t
0/7 0/7 3/7 0/7 3/7 0/7
b b b
a a a
3 2 2
s t s t + s t
3 +
3 4 4
b b b
a a
2/4 5/5 4/4 5/5
?
s 3/3 t s 1/3 t
7/7 4/7 7/7 6/7
b b
Flow value = 9 Max Flow value = 11132
A Heuristic: flow composition by s-t path flows
a a a
0/4 0/5 0/4 3/5 2/4 5/5
s 0/3 t s 3/3 t s 3/3 t
0/7 0/7 3/7 0/7 3/7 0/7
b b b
a a a
3 2 2
s t s t + s t
3 +
3 4 4
b b b
a
+ a
2/4 5/5 a 4/4 5/5
2 ?
s 3/3 t s 1/3 t
s -2 t
7/7 4/7 7/7 6/7
b b 2 b
Flow value = 9 augmenting path Max Flow value = 11133
Augmenting Path
f = a feasible flow in G
p is an augmenting path with respect to flow f in G if:
p is an s-t path in G if we ignore edge directions,
every forward edge e on p is un-saturated, i.e., f(e) < c(e),
every backward edge e on p carries positive flow, i.e., f(e) > 0.
augmenting path p
s t
134
The Residual Graph G(f)
To help find augmenting paths, we define The Residual Graph G(f):
G(f):
u v
G, f: ĉ(u,v) = c(u,v) – f(u,v)
if > 0
u v
0 f(u,v) c(u,v) u v
ĉ(v,u) = f(u,v)
if > 0
a
4 6
a
4/4 6/6
s 1 2 t
s 2/3 t
4
2
5/7 3/7 5 3
b b
135
An Augmenting Step
There is an augmenting path in G wrt f there is a directed s-t path in G(f).
p = an augmenting path with respect to f in G.
bottleneck capacity of p:
136
Algorithm FordFulkerson( G=(V,E,c,s,t) )
1. for each e E do f(e) 0 § an initial
feasible flow
2. optimal false
3. while not optimal do § each
iteration in O(E) time
4. Construct residual graph G(f)
5. if an s-t path p in G(f)
§ do a graph search in G(f)
6. then augment f along p
7. else optimal true § proof
is coming
8. end-while
Ford-Fulkerson [1956-1962]
9. return f
end
Fundamental Questions:
Termination
Correctness: optimality upon termination
Efficiency: # of augmenting iterations.
137
Example
0/4 4/4
a c a c
0/12 5/12
0 / 15 4 / 15
0/5 1/5
s 0/15 0/8 t s 1/15 1/8 t
a 4/4 c
10/12
G, f: 9 / 15
5/5
s 0/15 6/8 t
8/8 7/7
8 / 18 d
b
4
G(f): a c
9 10
2
6 5 6
s 15 2 t
nodes
reachable 8 10 7
from s b d
8 139
st-CUT
An st-cut is a partitioning of the vertex set V into a subset
X and its complement such that and .
4/4
a c
5/12
4 / 15
1/5 v(f) = 12 = (8 + 1 + 5) – (1 + 1)
s 1/15 1/8 t
7/7 C ( X 2 , X 2 ) 25 = 8 + 5 + 12
8/8 7 / 18
b d
142
Max-Flow Min-Cut Theorem
Proof:
(a) v(f) = (total forward flow) – (total backward flow) s t
(total forward capacity) – 0
= .
(b) At the last iteration of Ford-Fulkerson’s algorithm, let X X
X the set of nodes reachable from s in G(f).
is an st-cut with the following properties:
all its forward cross edges are saturated (f = c) and
all its backward cross edges are empty (f = 0).
2nd line of proof of (a) is now met with =.
143
Max Flow Min Cut
Optimality: For any feasible flow f & any st-cut
f is a max-flow & is a min-cut
if and only if
all forward cross edges of the cut are saturated (f = c), &
all backward cross edges of the cut are empty (f=0).
f=c X
X
t
s
f=0
144
Example
8/9
a c
3 4 2/
/1 /4 2
12
0/3
1/1
s t
0/3
/ 5
5 0
8/
2
8/
8
1
b d
13 / 15
Max-Flow & Min-Cut:
Forward cross edges are saturated.
Backward cross edges are empty.
Max flow value
flow value = 12 + 8 = 20
145
Integrality Theorem:
If all edge capacities are integers, then there is always an all integer max flow,
and the max flow obtained by Ford-Fulkerson’s algorithm has this property.
Proof:
By induction on the # augmenting iterations: f remains an all integer flow.
Basis: Initial flow value is all zero integers.
Induction: Residual capacities ĉ remain all integers
(they are either flow value or edge capacity minus flow
value).
Therefore, the augmenting values D > 0 is also an integer.
146
Termination
Corollary [to the Integrality Theorem]:
If all edge capacities are integers, then Ford-Fulkerson’s algorithm terminates
after at most v* iterations, where v* is the max flow value.
In this case, the running time of the algorithm is O(v* E).
Remark:
If some edge capacities are irrationals, Ford-Fulkerson’s algorithm may never
terminate. It may even converge to a strictly sub-optimal flow! 147
Edmonds-Karp Improvement
Edmonds-Karp [1972]:
Use BFS to pick the shortest augmenting path,
i.e., the s-t path in G(f) with fewest # of edges on it.
Notation:
148
Edmonds-Karp Improvement
FACT 1: df (v) is non-decreasing in each iteration, for all v V.
Proof: Consider an arbitrary augmenting iteration f f’.
Need to show that df’ (v) df (v), vV. We use induction on d = df’ (v).
Basis (d = 0): df’ (s) = 0. Source s is the only possible node.
Induction (d > 0): Suppose df’ (v) = d. Let u = p(v) on the shortest s-v path in G(f’).
Proof: Suppose (u,v) is the augmenting bottleneck edge in G(f) in some iteration i.
So, df (v) = df(u) + 1.
(u,v) is “filled up” and removed from the residual graph.
How can (u,v) become the bottleneck of another augmentation again?
Suppose (u,v) is the augmenting bottleneck edge again in some later iteration j > i.
This can only happen if for some intermediate iteration k, i < k < j.
(v,u) appears on an augmenting path, say in G(f’).
So, df’(u) = df’(v) + 1.
So, each time (u,v) is a bottleneck edge again, d(u) increases by at least 2.
But d(u) |V| – 2, since u V – {s,t}. 150
Edmonds-Karp Improvement
Theorem: Edmonds-Karp Max-Flow algorithm takes O(VE2) time.
151
Algorithm MaxFlowMinCut ( G=(V,E,c,s,t) ) § Ford-
Fulkerson
1. for each e E do f(e) 0 § +
2. optimal false §
Edmonds-Karp
3. while not optimal do
4. Construct residual graph G(f)
5. Do a BFS on G(f) from source s :
X nodes reachable from s in G(f)
6. if t X
7. then p BFS tree path from s to t in G(f)
8. augment flow along p
9. else optimal true
10. end-while
11. return ( max-flow f , min-cut ( ) ) § O(VE2)
time
end
152
Example
0/4 4/4
a c a c
0/12 9/12
0 / 15 9 / 15
0/5 5/5
s 0/15 0/8 t s 0/15 5/8 t
P.T.O. 154
Bibliography: Max Flow
R.K. Ahuja, T.L. Magnanti, J.B. Orlin, “Network Flows: Theory, Algorithms, and Applications,”
Prentice Hall, 1993.
G. Borradaile, P. Klein, “An O(n log n) algorithm for maximum st-flow in a directed planar graph,”
JACM 56(2), 9:1-9:30, 2009.
E.A. Dinits, “Algorithm for solution of a problem of maximum flow in a network with power
estimation,” Soviet Mathematics Doklady, 11(5):1277-1280, 1970.
Jack Edmonds, R.M. Karp, “Theoretical improvements in algorithmic efficiency for network flow
problems,” J. ACM 19:248-264, 1972.
L.R. Ford, Jr., D.R. Fulkerson, “Flows in Networks,” Princeton University Press, 1962.
A.V. Karzanov, “Determining the maximal flow in a network by the method of preflows,” Soviet
Mathematics Doklady, 15:434-437, 1974.
V.M. Melhotra, M.P. Kumar, S.N. Maheshvari, “An O(|V|3) algorithm for finding maximum flows in
networks,” Information Processing Letters, 7(6):277-278, 1978.
C. Papadimitriou, K. Steiglitz, “Combinatorial Optimization: Algorithms and Complexity,”
1st edition Prentice Hall, 1982; 2nd edition, Courier Dover, 1998.
D.D. Sleator, R.E. Tarjan, “A Data Structure for Dynamic Trees,” Journal of Computer and System
Sciences, 26(3):362-391, 1983.
155
GRAPH MATCHING
156
Graph Matching
M E(G) is a matching in undirected graph G
if no two edges in M share a common vertex.
G: a non-bipartite graph:
M2
M1
a perfect matching
G: a bipartite graph:
157
Graph Matching Problems
bipartite graphs max cardinality matching
general (non-bipartite) graphs weighted matching
158
Some Applications
Bipartite Matching:
assign jobs to workers:
maximize employment (unweighted) or profit (weighted).
2 processor scheduling with job conflict constraints:
minimize completion time.
Non-Bipartite Matching:
2 processor scheduling with job precedence constraints:
minimize completion time.
Traveling Salesman Problem.
The Chinese Postman Problem
(MST + Shortest Paths + Min Weight Perfect Matching).
Covering and Packing Problems.
Assign 2n students to n 2-bed dorm-rooms
with student acquaintance-pair constraints.
159
Max Cardinality Bipartite Matching
s
a b c d a b c d
1 2 3 1 2 3
Ford-Fulkerson’s algorithm
takes O(VE) time.
160
Augmenting Paths
Suppose M is a matching in graph G = (V, E).
A node of G is matched if it is incident to some edge in M.
Otherwise, it is exposed.
An alternating path (cycle) with respect to M in G is a path (cycle) such that
the edges along the path (cycle) alternate between being in M and being in E –
M.
An augmenting path is an alternating path between two exposed nodes.
(By necessity, an augmenting path has odd length.)
Augmenting M along an augmenting path P is changing M to
MP = (M – P) (P – M) (i.e., edges in M or in P but not both)
Note that |MP| = |M| + 1.
P
M MP
161
The Augmenting Path Theorem
THEOREM: A matching M in a graph G has maximum cardinality
162
Generic Max-Cardinality Matching Method
Algorithm MaxCardinalityMatching( G = (V,E) )
1. M
2. optimal false
3. while not optimal do
§ O(V) iterations
4. Search for an augmenting path P in G wrt M § This is the key
step
5. if P found
6. then M M P
7. else optimal true
8. end-while
9. return M
end In the following pages we sketch a method to do step 4 in O(VE) time.
This results in an O(V2E)-time max-cardinality matching algorithm.
The fastest known method takes O(V3) time.
163
Matched & Exposed Nodes
FACT 1: If a node becomes matched, it will remain matched.
A u-augmenting-path is an augmenting path where exposed node u is one end of it.
FACT 2: If at some iteration there is no u-augmenting-path,
then there will never be a u-augmenting-path later on.
Proof sketch: Suppose there is no u-augmenting-path in M.
Suppose P is an augmenting path wrt M.
There cannot be a u-augmenting path in MP.
u’
165
Augmenting Path Search as Digraph Search
u v mate[v]
u mate[v]
transformation x
x mate[y]
y mate[y]
Now in the auxiliary digraph G’ = (V, E’), search for a directed path
from a node marked “exposed” (yellow)
to a “target” node (solid black: adjacent to an exposed node). 166
Odd Cycles & Blossoms
Existence of odd length cycles creates further complications:
b c d b c d
a
a
f g h h
e f g
e
G, M: G’:
i j i j
augmenting path directed path
(a, b, c, d, g, h, j, i, f, e) (a, c, g, j, f)
? (a, c, g, h, d, b)
blossom
Blossom: An odd length cycle B with each of its nodes incident to an edge in
MB, except for one node (base of the blossom) that has a mate outside 167
B.
Discovering a Blossom
When search of G’ from u visits a node x’ whose mate x has been visited.
Backtrack up the search tree to lowest common ancestor of (x, x’)
to find base of the blossom.
u
x’ x
x’ = mate[x]
168
Blossom Shrinking
Blossom Shrinking:
When a blossom B is discovered, we shrink it to a new single node called B that
inherits all incident edges to the blossom nodes. This can be done in O(E) time.
b c b c b c
d d d
a a a
f g f
e e B1 e B2
B1 h B2
i j i
u v B w u’
u v B
u’
w 170
Max Cardinality Matching Algorithm
Algorithm MaxCardinalityMatching( G = (V,E) )
1. M
§ matching edges
2. UV
§ untested exposed nodes
3. while U do
§ O(V) iterations
4. remove a node u from U
5. Search for a u-augmenting-path P in G wrt M
using the auxiliary digraph G’ and blossom
shrinking/expansion
6. if P found then do
7. remove the other end node u’ of P from U
8. MMP
9. end-if
10. end-while
THEOREM:
11. return M The above algorithm takes O(V E) time.
2
end
171
Bibliography: Graph Matching
Recommended books in the area:
L. Lovász, M.D. Plummer, “Matching Theory,” Volume 121 of Annals of Discrete Mathematics.
North Holland, 1986.
C. Papadimitriou, K. Steiglitz, “Combinatorial Optimization: Algorithms and Complexity,”
1st edition, Prentice Hall, 1982; or 2 nd edition, Courier Dover, 1998.
E.L. Lawler, “Combinatorial Optimization: Networks and Matroids,” Holt, Rinehart & Winston,
1976. [gives the first O(V3)-time non-bipartite matching algorithm]
Algorithms for bipartite matching have been known for some time:
M. Hall Jr., “An algorithm for distinct representatives,” American Math. Monthly 63:716-717, 1956.
L.R. Ford, Jr., D.R. Fulkerson, “Flows in Networks,” Princeton University Press, 1962.
The simple transformation of bipartite matching to max-flow was first pointed out by
S. Even, R.E. Tarjan, “Network flow and testing graph connectivity,” SIAM J. Computing 4(4):507-
512, 1975.
The first polynomial time algorithm for non-bipartite matching is from
Jack Edmonds, “Paths, trees, flowers,” Canadian Journal of Mathematics, 17:449-467, 1965.
Jack Edmonds, “Matching and a polyhedron with 0-1 vertices,” J. Res. NBS, 69B:125-130, 1965.
[gives an algorithm for the weighted matching problem]
Faster than O(V3)-time algorithm for the geometric version of the problem was first discovered by
P. Vaidya, “Geometry helps in matching,” STOC, pp: 422-425, 1988.
172
Exercises
173
1. Given the adjacency list structure of a graph G = (V, E), give an O(V+E)-time algorithm that
reorders the vertices in each adjacency list in sorted order (say numerically, or alphabetically).
4. [CLRS, Exercise 22.1-6, p. 593] When an adjacency matrix representation is used, most graph
algorithms require time W(V2), but there are some exceptions. Show that determining whether a
digraph G contains a universal sink (a vertex with in-degree |V| – 1 and out-degree 0) can be
determined in time O(V), given the adjacency matrix for G.
5. [CLRS, Exercise 22.1-7, p. 593] The vertex-edge incidence matrix of a digraph G = (V, E)
is a matrix B[V,E] = (bue) such that
1 if edge e leaves vertex u,
b ue 1 if edge e enters vertex u,
0 otherwise .
Describe what the entries of the matrix BB T represent, where BT is the transpose of B. 174
6. [BFS & DFS Example] Consider the example digraph G shown below.
We are given the adjacency list structure of G with each adjacency list alphabetically ordered.
(a) Show the BFS structure of G starting at the source vertex a.
Show the d and p values of each vertex.
(b) Do the same as in part (a), except that start at the source vertex j.
(c) Do a DFS of G (also assume DFS root scanning order is alphabetical).
Show the p values by our drawing convention, as well as the DFS starting and finishing time
stamps d and f for each vertex. Classify tree-, back-, forward-, and cross- edges.
a
G:
b c
e
d f
g h i j
7. [CLRS, Exercise 22.2-5, p. 602] Argue that in a BFS from a given source vertex, the value
d[u] assigned to each vertex u is independent of the order in which the vertices appear in each
adjacency list. However, show that the BFS tree structure (the p values) may depend on the
said ordering.
175
8. [CLRS, Exercise 22.2-6, p. 602] Give an example digraph G = (V, E), a source vertex sV, and
a set of tree edges Ep E such that for each vertex vV, the unique path in the graph
(V, Ep ) from s to v is a shortest path in G, yet the set of edges E p cannot be produced by running
BFS on G, no matter how the vertices are ordered in each adjacency list.
9. [CLRS, Exercise 22.2-8, p. 602] The diameter of an un-weighted undirected tree T = (V, E) is
the largest of all shortest-path distances in the tree. Design and analyze an efficient algorithm to
compute the diameter of a given tree.
10. [CLRS, Exercise 22.2-9, p. 602] We are given a connected undirected graph G = (V, E) .
(a) Give an O(V+E)-time algorithm to compute a path in G that traverses each edge in E exactly
twice, once in each direction. The output should be the sequence of edges along such a path.
(b) Describe how you can find your way out of a maze if you are given a large supply of pennies.
11. [CLRS, Exercise 22.3-7, p. 611] Rewrite the procedure DFS, using a stack to eliminate recursion.
12. [CLRS, Exercise 22.3-8, p. 611] Give a counter-example to the conjecture that if there is a path
from u to v in a digraph G, and if d[u] < d[v] in a DFS of G, then v is a descendant of u in the DFS
forest produced.
13. [CLRS, Exercise 22.3-9, p. 612] Give a counter-example to the conjecture that if there is a path
from u to v in a digraph G, then any DFS must result in d[v] f[u].
14. [CLRS, Exercise 22.3-12, p. 612] Give an O(V+E)-time algorithm that outputs the vertex set of
each connected component of a given undirected graph G = (V, E).
176
15. Tree Broadcast:
The CEO of a large company has an urgent and important message, and she needs to notify every employee by
phone. The company hierarchy can be described by a tree T, rooted at the CEO, in which each other node v has a
parent node u where u is the direct superior officer of v, and v is a direct subordinate of u. To notify everyone of
the message, the CEO first calls each of her direct subordinates, one at a time. As soon as each subordinate gets the
phone call, he or she must notify each of his or her direct subordinates, one at a time. The process continues this
way until everyone has been notified. (Note that each person in this process can only call direct subordinates on the
phone.) We can picture this process as being divided into rounds. In one round, each person who has already
learned the message can call one of his or her direct subordinates on the phone. [Interpret T as a digraph (each edge
directed from parent to child) given by its adjacency list structure. So, Adj[x] is the list of direct subordinates of
employee x.] The number of rounds it takes for everyone to be notified depends on the order in which each
person calls their direct subordinates. For example, in Figure (1) below, it will take only two rounds if A starts by
calling B, but it will take three rounds if A starts by calling D.
a) Consider the example Figure (2). Show an optimum solution by labeling each edge by its round number.
What is the required minimum number of rounds?
b) Define MinRounds(x) to be the minimum number of rounds it takes, counted from the time employee x learns
the message, to broadcast the message to all subordinates of x (i.e., all nodes in the subtree rooted at x).
Develop a recurrence that expresses MinRounds(x) in terms of that of its direct subordinates MinRounds(y),
yAdj[x]. [Hint: In Figure (3) suppose MinRounds(B) = 15, MinRounds(C) = 16, MinRounds(D) = 15. In what order should
A call its direct subordinates B, C, D? What is MinRounds(A)?]
c) Using part (b), design an efficient algorithm that takes as input a broadcast tree T (rooted at root[T]) given by
its adjacency list structure, and outputs the minimum number of rounds needed to broadcast a message in T.
d) Analyze the worst-case time complexity of your algorithm in (c) as a function of n (# nodes in T).
CEO A
A
B C D
B D
17. A Hamiltonian path in a directed graph G=(V,E) is a directed simple path that visits each vertex
of G exactly once. (Note that we are referring to a Hamiltonian path, not a cycle.) In general, the
problem of deciding whether a given digraph has a Hamiltonian path is known to be NP-complete.
(a) Does the graph shown in Fig (b) below contain a Hamiltonian path?
If yes, show one by highlighting the edges on the path.
(b) If the graph G happens to be a DAG, how does finding a topological sort of G help in
finding a Hamiltonian path of G?
m n o p
q r s
t u v w
Fig (b)
x y z
Fig (a)
178
18. [CLRS, Exercise 22.4-2, p. 614] Give a linear-time algorithm that takes as input a DAG G and two
vertices s and t, and returns the number of paths from s to t in G. For example, in the digraph in
Fig (a) on the previous page, there are exactly 4 paths from vertex p to vertex v: pov, poryv, posryv,
psryv. (Your algorithm needs to give the number of paths, not list them.)
19. [CLRS, Exercise 22.4-3, p. 615] Give an algorithm that determines whether or not a given
undirected graph G = (V, E) contains a cycle. Your algorithm should run in O(V) time,
independent of |E|.
20. [CLRS, Exercise 22.4-4, p. 615] Prove or disprove: If a digraph G contains cycles, then
TopologicalSort(G) produces a vertex ordering that minimizes the number of “bad” edges that are
inconsistent with the ordering produced.
21. [CLRS, Exercise 22.4-5, p. 615] Another way to perform topological sorting on a DAG G = (V, E) is
to repeatedly find a vertex of in-degree 0, output it, and remove it and all its outgoing edges from the
graph. Explain how to implement this idea so that it runs in time O(V+E).
What happens to the algorithm if G has cycles?
22. [CLRS, Problem 22-3, p. 623] An Euler tour of a connected graph G = (V, E) is a cycle that
traverses each edge of G exactly once, although it may visit a vertex more than once.
G is Eulerian if it has an Euler tour.
(a) Show that a connected digraph G is Eulerian if and only if in-degree(v) = out-degree(v) vV.
(b) Show that a connected undirected graph G is Eulerian if and only if degree(v) is even vV.
(c) Describe an O(E)-time algorithm to find an Euler tour of G if one exists, once assuming G is
directed, and once assuming G is undirected.
[Hint: Merge edge-disjoint cycles.]
179
23. The algorithm below is purported to find an Euler tour in a given Eulerian undirected graph G.
For example, if the figure below is the DFS structure after step 1, then in step 3 the algorithm will
produce the cycle a, g, d, f, e, d, a, c, b, a. This is indeed an Euler tour of the graph.
Give a counter-example to show that the above algorithm is not always capable of finding an
Euler tour, even though there exists one. Therefore, the algorithm does not work!
[Hint: the smallest counter-example has less than 10 vertices.]
a
1 d
b 2 4
g 7
e
c 5
3 f
6 180
24. Show how the Strongly Connected Components (SCC) algorithm as presented in this lecture slide
works on the graph shown below. (Assume adjacency lists are alphabetically ordered.)
(a) Show the stack and the DFS-structure after step 1.
(b) Show the DFS-structure of the transpose graph G T after step 3.
(c) Show the SCC Component graph of G.
25. [CLRS, Exercise 22.5-5, p. 620] Give an O(V+E)-time algorithm to compute the SCC
Component graph of a digraph G = (V, E). Make sure that there is at most one edge between two
vertices in the component graph that your algorithm produces.
26. We say a digraph is weakly connected if its undirected version (i.e., ignoring the directions on the
edges) is a connected undirected graph.
Let G be a digraph in which for every vertex v, indegree(v) = outdegree(v). Then show that G is
strongly connected if and only if it is weakly connected.
27. [CLRS, Exercise 22.5-6, p. 621] Given a digraph G = (V, E), explain how to create another
digraph G’ = (V, E’) such that
(i) The strongly connected components of G and G’ form the same vertex partition of
V,
(ii) G’ has the same SCC Component graph as G, and
(iii) E’ is as small as possible. [Note: E’ may not necessarily be a subset of E.]
Describe an efficient algorithm to compute G’. a
b c
e
d f
g h i j 181
28. Treasure Hunt:
You are given a directed graph G = (V , E), where each node vV in the graph has a certain
amount of money m(v) > 0 sitting on it. You are also given vertices s, t V . The goal is to start at
s and follow a directed path in G to reach t, picking up as much money along the way as you
can. You may visit the same node more than once, but once you have picked up the money at
that node, it is gone. Define M(G, m, s, t) to be the maximum money that can be picked up on
any path in G from s to t (paths need not be simple).
a) Show M(G, m, s, t) and a corresponding optimum st-path for the instance shown in the
Figure below.
b) Design and analyze an efficient algorithm to compute M(G, m, s, t).
(You are not required to compute the optimum st-path.)
For full credit, your algorithm should run in time O( |V| + |E| ). [Hint: think about SCC.]
5
9 4 3
9 4 3 5
2
1 2 6
s 1 2 6 2 t
3
2 4 8
2 4 3 8
182
29. The City Hall has decided to convert every down-town street to a one-way street without
restricting traffic accessibility, i.e., we should still be able to drive from any city block to any
other city block without ever leaving the down-town area.
The problem is how to assign one-way directions to the down-town streets to achieve the City
Hall's objective.
This can be formulated as a graph problem as follows: We are given an undirected connected
graph G = (V, E). We want to assign a (one way) direction to each edge of G in such a way that
the resulting directed graph G’ = (V , E’ ) is strongly connected.
In this case we say G’ is a strong orientation of G.
We say G is strongly orientable, if it has at least one strong orientation.
(a) The graph shown below is strongly orientable. Show one of its strong orientations by
assigning an appropriate direction to each of its edges.
(b) Show that a connected undirected graph G is strongly orientable if and only if it has no
bridges. A bridge is an edge whose removal will disconnect the graph.
[Note: make sure to prove both directions of the “if and only if”.]
(c) Using DFS and part (b), design and analyze an efficient algorithm to construct a strong
orientation of a given undirected graph G if there is one; otherwise, report that no strong
orientation of G exists, and as a witness output a bridge in G.
Justify the correctness and time complexity of your algorithm.
a b c d e
f g h i
j l m n
k
183
30. [CLRS, Exercise 22.5-7, p. 621] A digraph G = (V, E) is said to be semi-connected if, for all
pairs of vertices u, v V, at least one of u or v is reachable from the other.
Design and analyze an efficient algorithm to determine whether or not G is semi-connected.
31. Given a digraph G = (V, E) and three of its vertices x, y, z V, give an efficient algorithm that
determines whether or not G has a cycle that includes vertices x and y but excludes vertex z.
32. Trace the BiConnectivity algorithm on the example graph G shown below.
Show the articulation points and the biconnected components of G.
33. Let G = (V, E) be a biconnected graph. Let (v,w)E. Start with the graph G’ = ( {v,w}, {(v,w)}).
A petal is a simple path in G whose two end nodes are its only nodes that are also in G’.
Describe an on-line algorithm AddPetal(s,t), where s is a node of G’ and (s,t) is an edge of G not
in G’. AddPetal(s,t) finds a petal starting with edge (s,t), and adds the nodes and edges of that petal
to G’. The execution of any on-line sequence of AddPetal operations should take O(E) time.
a b c d e f
g h i j k l
m n o p q r
184
34. Biconnectification Problem: given a connected undirected graph G, add a minimum number of
new edges to G to make it biconnected.
Let us first consider the simpler case when G is a tree. Let L be the set of leaves of G.
(a) Show that in this case there is always a solution where the added edges are between nodes in L.
(b) Show that there is a feasible solution where the added edges form a spanning tree of L.
Hence, the optimum number of new edges needed is at most |L| – 1.
(c) Can you characterize the optimum solution?
Now consider the general case. Suppose the biconnected components of G are
Gi , for i = 1..k, and let A denote the set of articulation points of G.
Define the skeleton graph Ĝ of G as follows:
V(Ĝ) = {Gi | i = 1..k} A
E(Ĝ) = {(Gi , x) | xA V(Gi)}
(See the illustrative figure below. How many new edges need to be added?)
(e) Design & analyze an efficient algorithm to solve the problem using (a-d) above.
x
Ĝ: x G3
G: G3
G2 z G2 z
y G4 y G4
G1 G5
G5
G6 G1
G6 185
35. Consider an arbitrary iteration of the generic MST algorithm that uses the Red and Blue Rules with
the Red-Blue Invariant. The set B of blue edges at that point forms a forest of one or more blue
components. By the Red-Blue Invariant we know that there is an MST T that contains B. Assume
we still have more than one blue component. Form a cut C by partitioning these blue components
into two non-empty subsets. Consider an arbitrary edge e of T that crosses the cut C. Give a
counter-example to the conjecture that e must be a min-weight cross edge of the cut C.
36. Is the following divide-&-conquer algorithm guaranteed to find a MST of a given graph G? :
Divide V(G) in two arbitrary subsets of roughly equal size X and Y. Let G’ and G’’ be the
subgraphs of G induced by X and Y, respectively. Recursively find an MST T’ of G’ and an MST
T’’ of G’’. Let e be the minimum weight edge of G incident to X and to Y.
Return T = T’ T” {e} as the MST of G.
37. [CLRS, Exercise 23.1-4, p. 629] Give a simple example of a graph G such that the edge set
{ eE(G) | e is a minimum-weight cross edge of some cut in G} does not form a MST of G.
38. [CLRS, Exercise 23.1-6, p. 630] Show that a graph G has a unique MST if, for every cut of G,
there is a unique minimum-weight crossing edge of the cut.
Show that the converse is not true by giving a counter-example.
39. [CLRS, Exercise 23.1-9, p. 630] Let T be a MST of G, and let V’ be a subset of V(G). Let T’ be the
subgraph of T induced by V’, and let G’ be the subgraph of G induced by V’. Show that if T’ is
connected, then T’ is a MST of G’.
3 8 11 9 16 4 6
f g 8 17 i j
19 h 2
9 3 4 7 18 19 8
k 4 l 6 m 14 n 4 o
41. We want to explore some MST properties of the graph shown above.
(a) Show the sequence of edges in the order that they are added to the MST using Kruskal's
algorithm. Highlight the chosen MST edges in the graph, and indicate in what order they
are added to the MST (e.g., by labeling them 1, 2, 3, ...).
(b) Show the sequence of edges in the order that they are added to the MST using Prim's
algorithm, starting from vertex a.
(c) Suppose we decide to change the weight of edge (a, b) to any real number from - to +.
What is the interval of weight values for this edge, for which the spanning tree in part (a)
above (as a set of edges) remains an MST of the new graph. Explain your answer.
(d) Suppose we decide to change the weight of edge (i, j) to any real number from - to +.
What is the interval of weight values for this edge, for which the spanning tree in part (a)
above remains an MST of the graph. Explain your answer.
42. Let T be a MST of G = (V, E, w). Suppose the weight w(e) of one of the edges eE is altered. (a)
Characterize the complete real valued range for w(e) for which T still remains a MST of G.
Consider two cases depending on whether e T or e T.
(b) Using the characterization in part (a), describe an algorithm to update the MST T if the
weight w(e) of a given edge eE changes to a new given value w’.
187
43. [CLRS, Problem 23-3, p. 640] Bottleneck Spanning Tree (BST) Problem:
Let T be any spanning tree of a given weighted connected undirected graph G.
The bottleneck value of T is defined to be the maximum edge weight among edges of T.
We say T is a bottleneck spanning tree (BST) of G if bottleneck value of T is minimum among all
spanning trees of G. The Bottleneck Spanning Tree Problem is to compute a BST of G.
(a) Show a graph G with two of its bottleneck spanning trees such that only one of them is a
minimum spanning tree.
(b) Show that any minimum spanning tree is always a bottleneck spanning tree.
[This shows that finding a bottleneck spanning tree is no harder than finding a minimum
spanning tree. In the remaining parts, we will show that one can be found in linear time.]
(c) Give a linear-time algorithm that, given a graph G and a real number b, determines whether
bottleneck value of a BST of G is at most b. (Note: no BST of G is given.)
[Hint: consider the edges of G that have weight at most b. Do they contain a spanning tree?]
(d) Use your algorithm for part (c) as a subroutine to design a linear-time algorithm for the
Bottleneck Spanning Tree Problem. [Hint: use prune-&-search and the hint for part (c)]
44. Let T be an arbitrary spanning tree of a weighted connected undirected graph G = (V, E, w).
Let L(T) = e1, e2, …, en-1 be the list of edges in T sorted in non-decreasing order of weight, that
is, w(e1) w(e2) … w(en-1), where n = |V(G)|.
Let T’ be another spanning tree of G with L(T’) = e’1, e’2, …, e’n-1 sorted in the same manner.
(a) Prove that if T is a MST of G, then w(ek) w(e’k), for all k = 1..n-1.
(b) Show that if T and T’ are both MSTs of G, then w(e k) = w(e’k), for all k = 1..n-1. 188
45. Let us say a connected weighted undirected graph G = (V, E, w) is a near-tree if |E| |V| + 8.
Design and analyze an algorithm with running time O(V) that takes a near-tree G and returns a
MST of G. (You may assume that all the edge weights are distinct.)
46. Suppose you are given a directed graph G = (V, E) with arbitrary real valued costs on its edges
and a sink vertex tV. Edge costs may be negative, zero, or positive. Assume that you are also
given finite values d(v) for all vV. Someone claims that, for each vertex vV, the quantity d(v)
is the cost of the shortest path from vertex v to the sink t.
(a) Give a linear-time algorithm that verifies whether this claim is correct.
47. Show that the single source longest paths in a weighted DAG can be computed in linear time.
48. A tournament is a directed graph formed by taking the complete undirected graph and assigning
arbitrary directions on the edges, i.e. a graph G = (V,E) such that for each u,vV, exactly one of
(u,v) or (v,u) is in E. Show that every tournament has a Hamiltonian path, that is, a path that visits
every vertex exactly once.
189
49. Color Constrained Single Source Shortest Paths:
Design and analyze an efficient algorithm for the following problem:
Input: A digraph G = (V, E) in which each node vV is allocated a color c(v){red,blue};
and a source node sV.
Output: For each node vV, find a path from s to v that contains the minimum number of red
nodes (including s and v).
51. [CLRS, Exercises 24.3-8 & 24.3-9, p. 664] Modify Dijkstra's algorithm so that it runs faster when
all the edge weights are restricted to being in the integer range [0..W], where W is a given positive
integer.
(a) Give a O(W V + E ) time algorithm.
(b) Give a O((V+E) log W ) time algorithm. [Hint: implement the priority queue by buckets.]
52. We are given the adjacency matrix of an un-weighted directed graph G = (V, E). For each pair of
vertices s,tV, compute the number of simple paths in G from s to t.
190
53. [CLRS, Exercise 24.3-6, p. 663] We are given a digraph G = (V, E) on which each edge (u,v)
E has an associated value r(u,v), which is a real number in the range 0 r(u,v) 1 that
represents the reliability of a communication channel from vertex u to vertex v. We interpret
r(u,v) as the probability that the channel from u to v will not fail, and we assume that these
probabilities are independent. Thus, the reliability of a path is the product of the reliabilities of
its edges. Give an efficient algorithm to find the most reliable path between two given vertices.
54. [CLRS, Exercise 25.3-4, p. 705] Suppose instead of the Edmonds-Karp & Johnson’s edge
weight transformation, we use the following simpler and faster strategy:
Let w* = min { w(u,v) | (u,v) E(G) }.
Define ŵ(u,v) = w(u,v) – w* for all edges (u,v) E(G).
What is wrong with this edge weight transformation?
191
56. Single-source bottleneck paths problem (SSBPP): Given a weighted digraph G =(V, E, w),
define the bottleneck cost of a path to be the maximum of the weights of the edges on the path.
The optimum bottleneck path from a vertex s to a vertex t is a path from s to t in G with
minimum bottleneck cost (i.e., the heaviest edge on the path is as light as possible). The problem
is: given a source vertex sV find the optimum bottleneck paths from s to each vertex in G.
(a) Consider the example digraph shown below.
(i) What is the bottleneck cost of the path ( a, e, f )?
(ii) What is the optimum bottleneck path from vertex a to f?
(iii) What is the bottleneck cost of that path?
(b) Describe how to modify the initialization and the labeling step of Ford's single-source
shortest paths method for the solution of SSBPP.
(c) Would the existence of negative cost edges create any complication in part (b)? Explain.
(d) Describe an algorithm that solves SSBPP in O(V+E) time if G is a DAG.
(e) Design and analyze an efficient algorithm that solves SSBPP on a general digraph.
[Hint: Modify Dijkstra's algorithm.]
a 2 b 5 c
7 8 4 3 6
d 3 e 4 f
192
57. [CLRS, Exercise 25.2-1, p. 699] Run the Floyd-Warshall algorithm on the weighted digraph
shown below. Show the d and p matrices after each iteration of the outer k-loop.
a 1 b 4 c
5 10
-4 7 -8
4 -1
d e f
3
58. Transitive Closure: The problem is to compute the transitive closure of a digraph G = (V, E), given
its adjacency list structure.
(a) Describe an O(VE) time algorithm assuming G is a DAG.
(b) Describe an O(VE) time algorithm for a general digraph G.
[Hint: compute strongly connected components first.]
59. Even length paths: We are given the 0/1 adjacency matrix A[V, V] of an n vertex directed graph G
= (V, E). Assume all diagonal entries of A are 0 (i.e., G has no self-loops).
We want to compute the 0/1 matrix P[V, V], such that for all vertices u and v in G,
P[u,v] = 1 if and only if there is a path of even length from vertex u to v in G, where the length of a
path is the number of edges on it. Design and analyze an efficient algorithm for this problem.
[Hint: also think about odd length paths.]
193
60. [CLRS, Problem 24-2, p. 678] Nesting Boxes
A d-dimensional box with dimensions (x 1, x2, …, xd) nests within another box with dimensions
(y1, y2, …, yd) if there exists a permutation p[1..d] such that xp(1) y1, xp(2) y2, … , xp(d) yd.
(a) Argue that nesting relation is transitive.
(b) Describe an efficient method to determine whether or not one box nests inside another.
(c) Suppose that you are given a set of n d-dimensional boxes {B(1), B(2), …, B(n)}.
Describe an efficient algorithm to determine the longest sequence B(i1), B(i2), …, B(ik) of
boxes such that B(ij) nests within B(ij+1) for j = 1, 2, …, k-1.
Express the running time of your algorithm in terms of n and d.
(a) Design and analyze an efficient algorithm to determine whether or not there exists a
sequence of currencies c(i1), c(i2), …, c(ik) such that
(b) Design and analyze an efficient algorithm to print out such a sequence if one exists.
194
62. [CLRS, Exercise 26.2-11, p. 731] Edge Connectivity
The edge connectivity of an undirected graph is the minimum number k of edges that must be
removed to disconnect the graph. For example, the edge connectivity of a tree is 1, and the edge
connectivity of a cyclic chain of vertices is 2. Show how the edge connectivity of an undirected
graph G = (V, E) can be determined by running a maximum flow algorithm on at most |V| flow
networks, each having O(V) vertices and O(E) edges.
63. Max Flow Example: Consider the flow network shown below with source s and terminal t. The
labels on edges are of the form “f / c” where f is the flow and c is the capacity of the edge. This
flow is feasible.
Start from this given feasible flow and augment it to a max-flow using augmenting
iterations of Ford-Fulkerson's max-flow-min-cut algorithm.
(In each iteration you are free to choose any augmenting path.)
(a) Clearly show each augmentation stage of the algorithm.
(b) Show the final max-flow. What is the net value of the max-flow from s to t?
(c) Show the minimum st-cut. What is the min-cut capacity?
a 1/4 b 5/6 c
4/9 5/5
2/5 2/4
1/4 1/7
3/8 2/12
d e f
7/8 3/5
195
64. Max Flow Min Cut:
a) Below we have an instance of a flow network with the source s and terminal t and edge capacities shown.
Show the max flow and the min cut on the same figure.
Also give the value of that flow and the capacity of that st-cut.
Now answer parts (b) – (d) below for a general Flow Network G = (V, E, c, s, t):
b) Given a flow f for a flow network G, how would you algorithmically determine whether f is a feasible flow
for G in O( |V| + |E| ) time?
c) Given a feasible flow f for a flow network G, how would you algorithmically determine whether f is a max
flow for G in O( |V| + |E| ) time?
d) Given a max flow f for a flow network G, how would you algorithmically determine a min capacity st-cut
for G in O( |V| + |E| ) time?
9
a b
a b
10 3
8
6 t
5 4
s
s t
15
9
c d
c d
7
196
65. [CLRS, Problem 26-1, p. 760] Escape Problem
An nn grid is an undirected graph consisting of n rows and n columns of vertices, as shown
below. We denote the vertex in row i and column j by (i,j). All vertices in a grid have exactly 4
neighbors, except for the boundary vertices, which are points (i,j) for which i=1, i=n, j=1 or j=n.
Given m n2 starting points (x1 , y1), (x2 , y2), …, (xm , ym) in the grid, the escape
problem is to determine whether or not there are m vertex-disjoint paths from the starting points
to any m different points on the boundary. For example, the grid in Fig (a) below has an escape,
but the one in Fig (b) does not.
(a) Consider a flow network in which vertices, as well as edges, have capacities. That is, the
total positive flow entering any given vertex is subject to a capacity constraint. Show that
determining the maximum flow in a network with edge and vertex capacities can be reduced
(a) (b)
197
66. [CLRS, Problem 26-2, p. 761] Minimum Path Cover
A path cover of a digraph G = (V, E) is a set P of vertex-disjoint paths such that every vertex in V
is included in exactly one path in P. Paths may start and end anywhere, and they may be of any
length, including 0. A minimum path cover of G is a path cover containing the fewest possible
paths.
(a) Give an efficient algorithm to find a minimum path cover of a DAG G = (V, E).
[Hint: Construct the graph G’ = (V’, E’), where
V’ = {s, t} {xu | uV } {yv | vV } ,
E’ = {(s , xu) | uV } {(yv , t) | vV } {(xu , yv) | (u,v)E } ,
and run a max flow algorithm.]
(b) Does your algorithm work for digraphs that contain cycles? Explain.
198
68. [CLRS, Exercise 26.3-4, p. 735] Perfect Matching
A perfect matching is a matching in which every vertex is matched. Let G = (V, E) be an
undirected bipartite graph with vertex bipartition V = L R, where |L| = |R|. For any X V,
define the neighborhood of X as
N(X) = { y V | (x,y) E for some x X} ,
that is, the set of vertices adjacent to some member of X. Prove Hall’s Theorem: there exists a
perfect matching in G if and only if |A| |N(A)| for every subset A L.
69. A Dancing Problem: There are n boys and n girls. Each boy knows exactly k girls, and each girl
knows exactly k boys, for some positive integer k.
Assume “knowing” is symmetric: boy b knows girl g g knows b.
We can represent this by the bipartite acquaintance graph G, in which each boy and each girl is
represented by a vertex, and there is an edge (b,g) in the graph between boy b and girl g if and
only if b and g know each other. We have
vV(G): degree(v) = k.
(b) Show that E(G) is the disjoint union of exactly k perfect matchings.
Interpretation: We can have k dancing rounds, where in each round every one dances with a
dancing partner of the opposite sex that he/she knows, and no one dances with the same
partner in two different rounds.
d e
f g h i
j k l m n
o p q
70. Does the matching shown in the graph above have maximum cardinality?
If not, augment it to a max cardinality matching.
(a) You may find augmenting paths by visual inspection.
(b) Now find augmenting paths using BFS in the auxiliary digraph
and blossom shrinking & expansion.
72. A node cover C of a graph G = (V, E) is any subset of V such that every edge in E is incident to
some node in C. Suppose G is a bipartite graph, M is a maximum cardinality matching, and C is
a minimum cardinality node cover of G. Show that |M| = |C|.
200
73. Perfect Matching: Reduction from general graphs to degree 3 graphs
Consider the following reduction of the matching problem from general graphs to degree 3 graphs.
Given a graph G with n nodes, construct a graph G’ as follows. For every node i of G take the
smallest complete binary tree with at least n leaves and insert a node in the middle of every edge; let
Ti be the resulting tree. The graph G’ has a tree T i for every node i of G, and for every edge (i, j) of
G, G’ has an edge connecting the jth leaf of Ti to the ith leaf of Tj .
a) Show that, for any i, Ti minus any one of its leaf nodes contains a perfect matching.
b) Show that G has a perfect matching if and only if G’ has a perfect matching.
c) Verify that the above reduction takes polynomial time.
75. If G = (V, E) is a graph with V = {1,2, …, n}, let us define the Tutte matrix of G, T(G), to be the
nn matrix defined as follows:
xij if (i, j) E & i j
T (G ) ij xij if (i, j) E & i j
0 otherwise
where xij’s are indeterminates (i.e., variables).
Show that G has a complete matching (i.e., a matching with at most one unmatched node)
if and only if det(T(G)) 0. 201
76. An undirected graph is called d-regular if all its vertices have degree d.
a) Show that any d-regular bipartite graph with has a perfect matching.
b) Show a 3-regular graph that has no perfect marching.
77. Let J = {J1 , J2 , …, Jn} be a set of jobs to be executed on two processors with all jobs requiring the
same amount of time (say 1). Suppose that there is a directed acyclic graph P = (J, E), called the
precedence graph, such that if (J1 , J2)E, then J1 must be executed before J2.
The Two-Processor Scheduling Problem with Precedence Constraints is:
Given P, find an optimal schedule; that is, a function S: J {1,2, …, T} such that
(i) For all t T, |{Jk J : S(Jk) = t }| 2,
(ii) if (J1 , J2)E then S(J1) < S(J2) ,
(iii) T is as small as possible.
a) Consider the undirected graph GP = (J, EP), where (J1 , J2)EP if and only if there is no path
from J1 to J2 , nor one from J2 to J1 , in P. Suppose that M is a maximum cardinality matching
of GP. Show that the smallest T achievable must obey
T |J| – |M|.
b) Show that there is always a schedule with T = |J| – |M|.
c) Give an efficient algorithm for finding the optimal schedule.
202
END
203