Professional Documents
Culture Documents
C++)
Chapter8: Graphs
http://www.fpt.edu.vn/ 1
Graph representation
a b c
a b c d e f
a 0 0 0 0 1 0
adjacency matrix
d e f b 0 0 0 1 0 1
a e c 0 0 0 0 0 0
d 0 1 0 0 1 1
b d f e 1 0 0 1 0 0
f 0 1 0 1 0 0
c
ae bd bf de df
d b e f a 1 0 0 0 0
incidence matrix
b 0 1 1 0 0
e a d c 0 0 0 0 0
d 0 1 0 1 1
f b d
e 1 0 0 1 0
adjacency list
f 0 0 1 0 1
http://www.fpt.edu.vn/ 2
Depth-first search: simple graph
DFS(v)
a(0)
(1) b c(2)
(0) (0)
(7)
num(v) = i++;
for all vertices u adjacent to v
if num(u) is 0 d(4) e f
(0) (0)
(3) (8)
(0)
attach edge(uv) to edges;
DFS(u);
g h
(5)
(0) (6)
(0)
depthFirstSearch()
for all vertices v
num(v) = 0;
edges = null;
i = 1;
while there is a vertex v such that num(v) is
0
DFS(v);
output edges;
http://www.fpt.edu.vn/ 3
Depth-first search: digraph
DFS(v)
num(v) = i++; a(0)
(1) B c(2)
(0) (0)
(6)
for all vertices u adjacent to v
if num(u) is 0 d e f
attach edge(uv) to edges; (0)
(5) (0)
(3) (0)
(7)
DFS(u);
g h
depthFirstSearch() (4)
(0) (8)
(0)
for all vertices v
num(v) = 0;
edges = null;
i = 1;
while there is a vertex v such that num(v) is
0
DFS(v);
output edges;
http://www.fpt.edu.vn/ 4
Shortest paths: the Dijkstra algorithm
DijkstraAlgorithm(weighted simple digraph, vertex first)
for all vertices v
currDist(v) = ;
currDist(first) = 0;
toBeChecked = all vertices;
while toBeChecked is not empty
v = a vertex in toBeChecked with minimal currDist(v);
remove v from toBeChecked;
for all vertices u adjacent to v
if currDist(u) > currDist(v) + weight(edge(vu))
currDist(u) = currDist(v) + weight(edge(vu));
predecessor(u) = v;
http://www.fpt.edu.vn/ 5
The Dijkstra algorithm: example
predecesso current distance
2r (a,2) 3 (b,5)
a b c
6 3 1 1
d 1 e f 2
(a,6)
(b,5)
(e,4) (b,3) (e,5)
iteration: init 1 2 3 4 5 6
active vertex v: a b e d c f
a 0
b 2
c 5 5 5
d 6 5 4
e 3
f 5 5 5
http://www.fpt.edu.vn/ 6
Shortest paths: the Ford algorithm
FordAlgorithm(weighted simple digraph, vertex first)
for all vertices v
currDist(v) = ;
currDist(first) = 0;
while there is an edge(vu) such that
currDist(u) > currDist(v) + weight(edge(vu))
currDist(u) = currDist(v) + weight(edge(vu));
http://www.fpt.edu.vn/ 7
The Ford algorithm: example
2 (a,2) 5 (b,7)
a b c
2 2 -3 9
d 4 e f 5
(a,2) (a,2)
(b,-1)
(g,-2) (e,4)
(e,3)
1
-5 iteration: init 1 2
g a 0
(d,3)
b 2
c 7
edges: ab ad ae bc be ce de dg ef ge d 2
e 2 -1 -2
f 4 3
g 3
http://www.fpt.edu.vn/ 8
Generic label correcting algorithm
labelCorrectingAlgorithm(weighted simple digraph,
vertex first)
for all vertices v
currDist(v) = ;
currDist(first) = 0;
toBeChecked = {first};
while toBeChecked is not empty
v = a vertex in toBeChecked;
remove v from toBeChecked;
for all vertices u adjacent to v
if currDist(u) > currDist(v) + weight(edge(vu))
currDist(u) = currDist(v) + weight(edge(vu));
predecessor(u) = v;
add u to toBeChecked if it is not there;
http://www.fpt.edu.vn/ 9
Label correcting algorithm with queue
iter: init 1 2 3 4 5 6 7 8 9
v:
queue: a ba b d e c g f f
e f
d d e c g f
e e
2 (a,2) 5 (b,7) e e c gf f
a b c c g
2 2 -3 9
a 0
d 4 e f 5 (e,4)
(e,3) b
(a,2) (a,2)
(g,-2)
(b,-1) 2
c 7
1
-5 d 2
g
e 2 -1 -2
(d,3)
f 4 3
g 3
http://www.fpt.edu.vn/ 10
Cycle detection: simple graph
cycleDetectionDFS(v)
num(v) = i++; (a,2)
(g,2)
(0)
(a,0)
for all vertices u adjacent to v a(0)
(1) b c (0)
if num(u) is 0
pred(u) = v; d(e,4)
(0)
(e,0)
e f (b,3)
(0)
(b,0) (0)
cycleDetectionDFS(u);
else if u ≠ pred(v)
pred(u) = v; g h
(d,5)
(d,0)
(0) (0)
cycle detected;
http://www.fpt.edu.vn/ 11
The Kruskal algorithm
KruskalAlgorithm(weighted connected undirected graph)
tree = null;
edges = sequence of all edges of graph sorted by weight;
for (i = 1; i |E| and |tree| < |V| 1; i++)
if ei from edges does not form a circuit with edges in tree
add ei to tree;
sorted edges: ab ad bd df ce ef de bf bc cd
4
a b4 c 9 a b c
6 8 13 6
5 5
7 7
d e 8 d e
6 6
f 7 7
f
http://www.fpt.edu.vn/ 12
The Dijkstra method
DijkstraMethod(weighted connected undirected graph)
tree = null;
edges = an unsorted sequence of all edges of graph;
for i = 1 to |E|
add ei to tree;
if there is a cycle in tree
remove an edge with maximum weight from this only cycle;
edges: ab ad bc bd bf cd ce de df ef
4 9
a b4 c 9 a b c
6 8 13 6 8 13
5 5
7 7
d e 8 d 8 e
6 6
f 7 7
f
http://www.fpt.edu.vn/ 13
Topological sort
(2,0)
(2,5)
(0,0) (7,6)
(7,0)
(0,0)
a(0,0)
(1,0)
(1,8)b c
TS(v)
num(v) = i++; d(0,0)
(4,0)e
(4,3) f(0,0)
(3,4)
(3,0) (8,0)
(8,7)
(0,0)
for all vertices u adjacent to v
if num(u) is 0
TS(u); g h
(5,2)
(0,0)
(5,0) (0,0)
(6,0)
(6,1)
else if TSNum(u) is 0
error; // a cycle detected;
TSNum(v) = j++;// after processing all successors of v,
// assign to v a number larger than output:
// assigned to any of its successors; a, f, c, b, e, d, g, h
topologicalSorting(digraph)
for all vertices v
num(v) = TSNum(v) = 0;
i = j = 1;
while there is a vertex v such that num(v) is 0
TS(v);
output vertices according to their TSNum's;
http://www.fpt.edu.vn/ 14
Maximum flows: the Ford-Fulkerson algorithm
augmentPath(network with source s and sink t)
for each edge e in the path from s to t
if forward(e)
f(e) += flow(t);
else f(e) –= flow(t);
http://www.fpt.edu.vn/ 15
The Ford-Fulkerson algorithm with stack: example
capacity flow
labeled: sa d
bc
labeled: sa d
bc
http://www.fpt.edu.vn/ 16
The Ford-Fulkerson algorithm with stack: example (cont.)
a (2b,0) (3 a(s,4) (2,2
0) b(s,1)
,0) ,3) , 0
2)
(4 (5,4) (4 5,4)
(null,) (
s (1,0) t ) (1,1) s t (a,2)
0
(2 (5, (2
,0 , 1) ,0
)
c) d (1 c
(s,2)
labeled: sa b c
labeled: a
s bc
http://www.fpt.edu.vn/ 17
Maximum flows of minimum cost: the Dijkstra algorithm
modifiedDijkstraAlgorithm(network, s, t)
for all vertices u
f(u) = 0;
cost(u) = ;
set flows of all edges to 0;
label(s) = (null,,0);
labeled = null;
while (true)
v = a vertex not in labeled with minimal cost(v);
if v == t
if cost(t) == // no path from s to t can be found;
return failure;
else return success;
add v to labeled;
for all vertices u not in labeled and adjacent to v
if forward(edge(vu)) and slack(edge(vu)) > 0 and cost(v) + cost(vu) < cost(u)
label(u) = (v+, min(flow(v), slack(edge(vu)), cost(v) + cost(vu));
else if backward(edge(vu)) and f(edge(uv)) > 0 and cost(v) cost(uv) < cost(u)
label(u) = (v, min(flow(v), f(edge(uv)), cost(v) cost(uv));
http://www.fpt.edu.vn/ 18
The modified Dijkstra algorithm: example
capacity flow cost
http://www.fpt.edu.vn/ 19
The modified Dijkstra algorithm: example ( cont.)
capacity flow cost
(s,4,3)
a) (2b,0,3) (3
,2, a (2,0,3) b (s,5,3)
(3
, 3 1) ) ,32,
4, 0 ,3) 0, 3 1)
( (5, 0 ,
(4 (5,1 0,3)
s (1,0,5) t (1,0,2) s (1,0,5) (1,0,2) t (b,1,4)
(a,2,6)
(2 1) ) )
,2 2, , 2 ,1
,1 , ,0 ,2
c) d(5 (1 (5
c d
(a,1,8)
(b,2,2) (b,1,5)
iteration init 1 2 3 4 5
active vertex v: s a b c t
s 0
a 3 backward
b 3 3 edge
c 8 2
d 5 5
t 6 4 4
http://www.fpt.edu.vn/ 20
The modified Dijkstra algorithm: example (cont.)
capacity flow cost
http://www.fpt.edu.vn/ 21
The modified Dijkstra algorithm: example (cont.)
capacity flow cost
(s,2,3)
a) (2b,2,3) (3
,3, a b (s,4,3)
, 2 ,3 1) 3)
(4 (5,1,3) 2,
,
(4 (5,2 1,3)
s (1,0,5) t (1,0,2) s (1,0,5) (1,0,2)
(1,1,2) t (d,1,7)
(2 1)
,2
,1 , 2, 0, 2) ,1 )
2)
c) d(5 (1
,
(5,2 1
,0,
c
(a,1,8)
(b,2,2) d (1
(b,1,5)
iteration init 1 2 3 4 5 6
active vertex v: s a b c d t
s 0
a 3
b 3 3
c 8 2
d 5 5
t 7
http://www.fpt.edu.vn/ 22
The modified Dijkstra algorithm: example (cont.)
capacity flow cost
(s,2,3)
a) (2b,2,3) (3
,3, a b (s,3,3)
, 2 ,3 1) 3)
(4 (5,2,3) ,2, )
s (4 (5,2,3
(1,0,5) t (1,1,2) s (1,0,5)
(2 1)
,2
,1 , 2, 1,2) ,1 )
c) d(5 (1
,
(5,2
c
(a,1,8)
(b,2,2)
iteration init 1 2 3 4 5 6
active vertex v: s a b c d t
s 0
a 3
b 3 3
c 8 2
d
t
http://www.fpt.edu.vn/ 23
The modified Dijkstra algorithm: example (cont.)
capacity flow cost
http://www.fpt.edu.vn/ 24
Maximum matching: algorithm
findMaximumMatching(bipartite graph)
for all unmatched vertices v
set level of all vertices to 0;
set parent of all vertices to null;
level(v) = 1;
last = null;
clear queue;
enqueue(v);
while queue is not empty and last is null
v = dequeue();
if level(v) is an odd number
for all vertices u adjacent to v such that level(u) is 0
if u is unmatched // the end of an augmenting
parent(u) = v; // path is found;
last = u; // this also allows to exit the while loop;
break; // exit the for loop;
else if u is matched but not with v
parent(u) = v;
level(u) = level(v)+1;
enqueue(u);
else // if level(v) is an even number
enqueue(vertex u matched with v);
parent(u) = v;
level(u) = level(v)+1;
if last is not null // augment matching by updating the augmenting path;
for (u = last; u is not null; u = parent(parent(u)))
matchedWith(u) = parent(u);
matchedWith(parent(u)) = u;
http://www.fpt.edu.vn/ 25
Maximum matching: example
p q r s t
a b c d e
init a p q r s t
v a
a b c d e
queue a
p
init p q r s t
b
v b
q a b c d e
queue b
init p q r s t
c
v c
s a b c d e
queue c
http://www.fpt.edu.vn/ 26
Maximum matching: example (cont.)
p q r s t
a b c d e
init p q r s t
d
v d q b p a
q a b c d e
queue d q b p a
e
b
r
init p q r s t
p
v e r a p t a
a b c d e
queue e r a p t b a
t b p t
r
b
http://www.fpt.edu.vn/ 27
Q&A
http://www.fpt.edu.vn/ 28