Professional Documents
Culture Documents
Chapter 05
Chapter 05
Chapter 05
Graph theory
Pham Quang Dung
Hanoi, 2012
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 1 / 65
Outline
1
Introduction
2
Graph representations
3
Depth-First Search and Breadth-First Search
4
Topological sort
5
Euler and Hamilton cycles
6
Minimum Spanning Tree algorithms
7
Shortest Path algorithms
8
Maximum Flow algorithms
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 2 / 65
Introduction
Many objects in our daily lives can be modeled by graphs
Internets, social networks (facebook), transportation networks,
biological networks, etc.
An graph G is a mathematical object consisting two nites sets,
G = (V, E)
V is the set of vertices
E is the set of edges connecting these vertices
Graphs have many types: directed, undirected, multigraphs, etc.
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 3 / 65
Denitions
An undirected graph G = (V, E)
V = (v
1
, v
2
, . . . , v
n
) is the set of vertices or nodes
E V V is the set of edges (also called undirected edges). E is the
set of unordered pair (u, v) such that u ,= v V
(u, v) E i (v, u) E
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 4 / 65
Denitions
A directed graph G = (V, E)
V = (v
1
, v
2
, . . . , v
n
) is the set of vertices or nodes
E V V is the set of arcs (also called directed edges). E is the set
of ordered pair (u, v) such that u ,= v V
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 5 / 65
Multigraphs
An undirected (directed) multigraph is a graph having multiples edges
(arcs), i.e., edges (arcs) having the same endpoints
Two vertices may be connected by more than one edges (arcs)
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 6 / 65
Denitions
Given a graph G = (V, E), for each (u, v) E, we say u and v are
adjacent
Given an undirected graph G = (V, E)
degree of a vertex v is the number of edges connecting it:
deg(v) = (u, v) [ (u, v) E
Given a directed graph G = V, E)
An incoming arc of a vertex is an arc that enters it
An outgoing arc of a vertex is an arc that leaves it
indegree (outdegree) of a vertex v is the number of its incoming
(outgoing) arcs
deg
+
(v) = (v, u) [ (v, u) E, deg
vV
deg(v)
Theorem
Given a directed graph G = (V, E), we have
vV
deg
+
(v) =
vV
deg
(v) = [E[
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 8 / 65
Denition - Paths, cycles
Given a graph G = (V, E), a path from vertex u to vertex v in G is a
sequence u = x
0
, x
1
, . . . , x
k
= v in which
(x
i
, x
i +1
) E), i = 0, 1, . . . , k 1
u: starting point (node)
v: terminating point
k is the length of the path (i.e., number of its edges)
A cycle is a path such that the starting and terminating nodes are the
same
A path (cycle) is called simple if it contains no repeated edges (arcs)
A path (cycle) is called elementary if it contains no repeated nodes
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 9 / 65
Connectivity
Given an undirected graph G = (V, E). G is called connected if for
any pair (u, v) (u, v V), there exists always a path from u to v in G
Given a directed graph G = (V, E), G is called
weakly connected if the corresponding undirected graph of G (i.e., by
removing orientation on its arcs) is connected
strongly connected if for any pair (u, v) (u, v V), there exists
always a path from u to v in G
Given an undirected graph G = (V, E)
an edge e is called bridge if removing e from G increases the number
of connected components of G
a vertex v is called articulation point if removing it from G increases
the number of connected components of G
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 10 / 65
Connectivity
Theorem
An undirected graph G can be oriented (each edge of G is oriented) to
obtain a strongly connected component i each edge of G lies on at least
one cycle
Proof.
Exercise in class
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 11 / 65
Special graphs
Complete graphs K
n
: undirected graph G = (V, E) in which [V[ = n
and E = (u, v) [ u, v V
Bipartie graphs K
n,m
: undirected graph G = (V, E) in which
V=X Y, X Y = , [X[ = n, [Y[ = m,
E = (u, v) [ u X v Y
Planar graphs: can be drawn on a plane in such a way that edges
intersect only at their common vertices
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 12 / 65
Planar graphs - Euler Polyhedron Formula
Theorem
Given a connected planar graph having n vertices, m edges. The number
of regions divided by G is m n + 2.
Proof.
By induction on vertices (or edges)
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 13 / 65
Planar graphs - Kuratowskis theorem
Denition
A subdivision of a graph G is a new graph obtained by replacing some
edges by paths using new vertices, edges (each edge is replaced by a path)
Theorem
Kuratowski A graph G is planar i it does not contain a subdivision of
K
3,3
or K
5
Proof.
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 14 / 65
Outline
1
Introduction
2
Graph representations
3
Depth-First Search and Breadth-First Search
4
Topological sort
5
Euler and Hamilton cycles
6
Minimum Spanning Tree algorithms
7
Shortest Path algorithms
8
Maximum Flow algorithms
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 15 / 65
Graph representation
Two standard ways to represent a graph G = (V, E)
Adjacency list
Appropriate with sparse graphs
Adj [u] = {v | (u, v) E}, u V
Adjacency matrix
Appropriate with dense graphs
A = (a
ij
)
nn
such that (suppose V = {1, 2, . . . , n})
a
ij
=
1 if (i , j ) E,
0 otherwise
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 16 / 65
Graph representation
In some cases, we can use incidence matrix to represent a directed
graph G = (V, E)
b
ij
=
eE
T
w(e)
where E
T
is the set of edges of T
Find a spanning tree of G such that the total weights on edges is
minimal
Theorem
For any graph G having distinct weights on edges, the MST T of G
satises the following properties
Cut property: For any cut (X, X) of G, T must contain shortest
edges crossing the cut
Cycle property: Let C be a cycle in G, T does not contain the
longest edges in C
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 41 / 65
Minimum Spanning Tree - Proof of Cut property
Proof.
Proof by contradiction
Denote = minw(e) [ e E
Suppose that there exists an edge (x, y) with x X, y X and
w(x, y) = such that T does not contain (x, y)
There exists a path T from x to y in T because T is connected
T must contain an edge (u, v) such that u X v X
(w(u, v) > = w(x, y))
Denote T
(e
1
, e
2
, . . . , e
k1
T
)
Adding e
k
to T
creates a graph G
1
containing a cycle C
There exists an edge e
,= e
k
of C that is not in T, since otherwise T
contains all edges of C and T is thus not a tree
e
k
is in T and not in T
is in T
and not in T
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 45 / 65
Proof of the correctness of the Kruskal algorithm
Proof by contradiction (continue)
Case 1: If w(e
) < w(e):
e
1
, e
2
, . . . , e
k1
and e
are in T
. Thus adding e
to the set
e
1
, . . . , e
k1
does not create any cycle
Hence, the KRUSKAL must select e
instead of e
k
at the k
th
step of
the KRUSKAL during the construction of T (contradiction, case 1
does not happen)
Case 2: w(e
) w(e
k
):
Replacing e
by e
k
in T
having
w(T
) w(T
). Clearly T
(contradiction)
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 46 / 65
Prim algorithm
Algorithm 10: PRIM(G = (V, E))
s select a vertex of V; 1
S V \ {s}; 2
V
T
{s}; 3
E
T
; 4
foreach v V do 5
d(v) w(s, v); 6
near (v) s; 7
while |V
T
| < |V| do 8
v argMin
uS
d(u); 9
S S \ {v}; 10
V
T
V
T
{v}; 11
E
T
E
T
{(v, near (v))}; 12
foreach u S do 13
if d(u) > w(u, v) then 14
d(u) w(u, v); 15
near (u) v; 16
return (V
T
, E
T
); 17
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 47 / 65
Proof of the correctness of the Prim algorithm
Proof by contradiction
Let T is spanning tree computed by the Prim algorithm and T
is a
MST of G having most edges in common with T
Suppose that T is not a MST of G, thus T ,= T
Let V
k
(T) be the set of vertices of T computed at the k
th
iteration
Let e
k
= (u, v) be the rst edge chosen by Prim algorithm at k
th
iteration which is not in T
(u V
k1
(T) and v / V
k1
(T))
Let P be the path from v to u in T
and (e
of T
by e
k
yielding a spanning tree T
having weight
less than or equal to the weight of T
is also a MSTof G but has one more edge (i.e., the edge e
k
) in
common with T than T
(contradiction)
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 48 / 65
Proof of the correctness of the Prim algorithm
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 49 / 65
Outline
1
Introduction
2
Graph representations
3
Depth-First Search and Breadth-First Search
4
Topological sort
5
Euler and Hamilton cycles
6
Minimum Spanning Tree algorithms
7
Shortest Path algorithms
8
Maximum Flow algorithms
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 50 / 65
Shortest path problem
Given a graph G = (V, E), each edge e is associated with a weight
w(e).
Single-source shortest paths problem Find the shortest paths from a
given source node s to all other nodes of G
All-pairs shortest paths problem Find shortest paths between every
pairs of vertices u, v in G
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 51 / 65
Bellman-Ford algorithms
Graph without negative cycles
Algorithm 11: Bellman-Ford(G = (V, E), s)
foreach v V do 1
d(v) w(s, v); 2
p(v) s; 3
d(s) 0; 4
foreach k = 1, . . . , n 2 do 5
foreach v V s do 6
foreach u V do 7
if d(v) > d(u) + w(u, v) then 8
d(v) d(u) + w(u, v); 9
p(v) u; 10
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 52 / 65
Shortest path problem on directed acyclic graphs (DAG)
Given a DAG G = (V, E) and a source node s V. Find shortest
paths from s to all other nodes of G
Algorithm 12: ShortestPathAlgoDAG(G = (V, E), s)
L Topological sort vertices of G; 1
foreach v V do 2
d(v) w(s, v); 3
d(s) 0; 4
foreach v L do 5
foreach u G.Adj [v] do 6
d(u) min(d(u), d(v) + w(v, u)); 7
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 53 / 65
Dijkstra algorithm
Graph without negative edge weights
Algorithm 13: Dijkstra(G = (V, E), s)
foreach x V do 1
d(x) w(s, x); 2
pred(x) s; 3
NonFixed V \ {s}; 4
Fixed {s}; 5
while NonFixed = do 6
(*get the vertex v of NonFixed such that d(v) is minimal*); 7
v argMin
uNonFixed
d(u); 8
NonFixed NonFixed \ {v}; 9
Fixed Fixed {v}; 10
foreach x NonFixed do 11
if d(x) > d(v) + w(v, x) then 12
d(x) d(v) + w(v, x); 13
pred(x) v; 14
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 54 / 65
Proof of the correctness of the Dijkstra algorithm
Proof by induction on [Fixed[
Invariant: At any step, we have d(v) =
min
xFixed
d(x) + w(x, v), v / Fixed (1)
Base case [Fixed[ = 1, trivial
Inductive hypothesis (step k): [Fixed[ k, d(x) is the shortest path
distance from s to x, x Fixed
At step k + 1, we select a node v / Fixed having smallest value of
d(v)
d(v) = d(x) + w(x, v) with x Fixed
Suppose that P is the shortest path from s to v and z is the rst when
traversing from s to v on P that is not in Fixed:
P = s, x
1
, x
2
, . . . , x
k
, z, . . . , v where x
1
, x
2
, . . . , x
k
Fixed
At this time, we have d(z) d(x
k
) + w(x
k
, z) because of invariant (1)
w(P) = d(x
k
) + w(x
k
, z) + L d(z) because L is the weight of a path
from z to v on P which is greater or equal to 0
If d(v) is not the shorest path distance from s to v, then
d(v) > w(P) d(z) (contradiction with the hypothesis that
d(v)=min
x / Fixed
d(x) d(z))
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 55 / 65
All-pairs shortest path - Floyd-Warshall algorithm
Algorithm 14: Floyd-Warshall(G = (V, E))
foreach u V do 1
foreach v V do 2
d(u, v) w(u, v); 3
p(u, v) u; 4
foreach z V do 5
foreach u V do 6
foreach v V do 7
if d(u, v) > d(u, z) + d(z, v) then 8
d(u, v) d(u, z) + d(z, v); 9
p(u, v) p(z, v); 10
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 56 / 65
Outline
1
Introduction
2
Graph representations
3
Depth-First Search and Breadth-First Search
4
Topological sort
5
Euler and Hamilton cycles
6
Minimum Spanning Tree algorithms
7
Shortest Path algorithms
8
Maximum Flow algorithms
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 57 / 65
Maximum Flow problem
Capacitated Networks
G = (V, E, s, t) where V and E are respectively the set fo vertices and
the set of arcs
Each arc (u, v) E is associated with a nonnegative capacity c(u, v)
A source node s: no incoming arcs
A sink node t: no outgoing arcs
A
(v) = u [ (u, v) E, v V s
A
+
(v) = u [ (v, u) E, v V t
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 58 / 65
Network Flow
A ow x on G: each arc (u, v) E has a ow x(u, v) traversing
along it
0 x(u, v) c(u, v), (u, v) E
Flow conservation
vA
(v)
x(v, u) =
vA
+
(u)
x(u, v), u V s, t
Flow value f (x) =
vA
+
(s)
x(s, v) =
vA
(t)
x(v, t)
Objective: nd a ow x on G such that f (x) is maximal
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 59 / 65
Maximum Flow problem - Cut
Given a network G = (V, E, s, t), a s t cut C = (S, T) is a
partition of V such that s S t T
Capacity of s t cut is C(S, T) =
(u,v)ST
c(u, v)
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 60 / 65
Maximum Flow problem - Cut
Lemma
For any ow x and a s t cut (S, T) of a network G = (V, E), we have
f (x) C(S, T)
Proof.
f (x) =
vA
+
(s)
x(s, v)
vA
(s)
x(v, s) (by denition)
=
vS
(
wA
+
(v)
x(v, w)
uA
(v)
x(u, v))
(by ow conservation constraint)
=
vS,uT
x(v, u)
vS,uT
x(u, v)
(by removing duplicate arcs)
vS,uT
c(v, u) = c(S, T) because
vS,uT
x(v, u)
vS,uT
c(v, u) and
vS,uT
x(u, v) 0
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 61 / 65
Residual graph
Given a network G = (V, E, s, t) and a ow x on G. The residual
graph G
x
= (V, E
x
) is dened as follows:
If (v, w) E with x(v, w) = 0, then (v, w) E
x
with weight c(v, w)
If (v, w) E with x(v, w) = c(v, w), then (w, v) E
x
with weight
x(v, w)
If (v, w) E with 0 < x(v, w) < c(v, w), then (v, w) E
x
with
weight c(v, w) x(v, w) and (w, v) E
x
with weight x(v, w)
If we can nd a path P = s = v
0
, v
1
, . . . , v
k
= t on G
x
and denote
the minimum weight of edges on P, then we can construct a ow x
as follows:
x
(u, v) =
vS,uT
x(v, u)
vS,uT
x(u, v) =
vS,uT
x(v, u)
Since (v, u) / E
x
, then x(v, u) = c(v, u) (by construction rule of G
x
)
Hence f (x) = C(S, T)
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 64 / 65
Ford-Fulkerson algorithm
Algorithm 15: FordFulkerson(G = (V, E, s, t))
foreach (u, v) E do 1
x(u, v) 0; 2
while true do 3
G
x
is the residual graph of G w.r.t ow x; 4
P FindPath(G
x
, s, t); 5
if P = NULL then 6
break; 7
x augmentFlow(P, x, G
x
); 8
return x; 9
Pham Quang Dung () Discrete Mathematics Graph theory Hanoi, 2012 65 / 65