Part2 4GraphSpanningTree
Part2 4GraphSpanningTree
Discrete Mathematics
PART 2
GRAPH THEORY
(Lý thuyết đồ thị) 2
Content of Part 2
Chapter 1. Fundamental concepts
Chapter 2. Graph representation
Chapter 3. Graph Traversal
Chapter 4. Tree and Spanning tree
Chapter 5. Shortest path problem
Chapter 6. Maximum flow problem
Chapter 4
Tree and Spanning Tree
5
1. Tree and its properties
A tree is an undirected connected graph with no cycles.
Example 1. Which of the graphs are trees?
T1 T2
T3
Forest F consists of 3 trees: T1, T2,, T3
7
Example
Forest: Graph containing no cycles that are not connected, but
each connected component is a tree.
a b
Forest
c d
Not connected not tree
Consists of 2 trees: {a,f} and {c,e,b,d}
e f
8
1. Tree and its properties
Theorem. Given an undirected graph G = (V,E), the following conditions are
equivalent:
(1) G is a connected graph with no cycles. (Thus G is a tree by the above
definition).
(2) For every two vertices u, v V, there exists exactly one simple path from u
to v.
(3) G is connected, and removing any edge from G disconnects it (each edge of
G is a bridge).
(4) G has no cycles, and adding any edge to G gives rise to a cycle. (Thus G is a
maximal acyclic graph).
(5) G is connected and |E| = |V | − 1.
9
Contents
10
2.The spanning tree
Let G=(V, E) be an undirected connected graph with vertex set V.
Tree T=(V,F) where F E is called spanning tree of G
b c b c b c
a d a d a d
e e e
G Spanning tree T1 Spanning tree T2
Arthur Cayley
(A complete graph is a simple undirected graph in which (1821 – 1895)
every pair of distinct vertices is connected by a unique edge)
b a b c
b c a
a c c a b
K3 3 spanning trees of K3
16 spanning trees of K4
Theorem (Cayley). A complete graph Kn has nn-2 spanning trees.
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
1 2 1 2 1 2 1 2
3 4 3 4 3 4 3 4
Contents
15
Weighted Graphs and Minimum Spanning Trees
Let G=(V, E) be an undirected connected graph with vertex set V:
• For each edge (u,v) in E, we have a weight w(u,v) specifying the cost (length
of edge) to connect u and v.
A minimum spanning tree for G is a spanning tree T which has the smallest
weight 7 a
2 2 d 5
5 4 The weight of the
f b
1
1 g spanning tree = 14
4 3 7
4
c e
Weighted Graphs and Minimum Spanning Trees
Every undirected connected weighted graph G has a minimum spanning tree. Since
G has only a finite number of spanning trees, one of them must have minimum
weight.
Note: a given undirected connected weighted may have more than one minimum
spanning tree.
a 7 a 7
2 2 d 2 2 d
5 5
f 5 4 f 5 4
b 1 g b 1 g
1 3 2 1 3
2 7
c 4 e 7 c 4 e
It is clear that it corresponds to a graph where vertices are the cities and
the edges are the railroads connecting the cities, the length on each edge
is the cost of building a railway connecting the two cities.
Generic-MST(G, c)
T =
//T is the subset edges of some minimum spanning tree
while T is not the spanning tree do
Finding edge (u, v) is “safe” edge for T
T = T {(u, v)}
return T
Set T is always a subset of edges of some minimum spanning tree. This property is
called the invariant Property.
An edge (u,v) is a safe edge for T if adding the edge to T does not destroy the
invariant.
General scheme of the algorithm to find MST
Initialize: The minimum spanning tree T =
Each step of the algorithm: one edge e which is the “safe” edge is chosen, subject only
to the restriction that if adding edge e into T then T is still a tree (no cycle is created).
Generic-MST(G, c)
T =
//T is the subset edges of some minimum spanning tree
while T is not the spanning tree do
Finding edge (u, v) is “safe” edge for T
T = T {(u, v)}
return T
T ={(a,b), (i, c), (c, f), (h, g)} T = {(a,b), (h,g), (g,f), (i, c), (c, f)}
Cut (S, V-S) repects T Cut (S, V-S) repects T
Light edge crossing the cut is the “safe” edge
Theorem.
• Let G = (V, E) be a connected, undirected graph with a real-valued weight
function w defined on E.
• Let T be a subset of E that is included in some minimum spanning tree for G,
• Let (S, V−S) be any cut of G that respects T,
[Note carefully: Except for the dashed edge (u, v), all edges shown are in
Topt. T is some subset of the edges of Topt, but T cannot contain any edges
that cross the cut (S, V − S), since this cut respects T. Shaded edges are
the path p.]
Proof: Let Topt be an MST that includes T.
• If Topt contains an edge (u, v), we are done.
• So now assume that Topt does not contain (u, v). We will construct a different MST that
include T = T {(u, v)}.
– Recall: a tree has unique simple path between each pair of vertices. Since Topt is an MST,
it contains a unique path p between u and v. Path p must cross the cut (S, V − S) at least
once. Let (x, y) be an edge of p that crosses the cut. From how we chose (u, v), must
have w(u, v) < w(x, y).
– Since the cut respects T, edge (x, y) is not in T. To form from Topt:
• Remove the edge (x, y). Breaks T into two components.
• Add edge (u, v). Reconnects.
So = Topt − {(x, y)} {(u, v)} is a spanning tree.
w( ) = w(Topt) − w(x, y) + w(u, v)
≤ w(Topt), since w(u, v) ≤ w(x, y).
Since is a spanning tree, w( ) ≤ w(Topt), and Topt is an MST, then must be an MST.
• We need to show that edge (u, v) is safe for the set T:
– T Topt and (x, y) T T
– T {(u, v)}
• Since is an MST, edge (u, v) is safe for the set T. This completes the proof.
How to find the “safe” edge?
T : set of edges of some spanning tree
Initialize: T =
Kruskal algorithm
T is forest.
The “safe” edge added to T at each iteration is the edge with smallest
weight among edges connecting its connected components.
Prim algorithm
T is tree.
The “safe” edge added to T at each iteration is the edge with smallest weight
among edges connecting the tree T to other vertex not in the tree.
Kruskal algorithm
Generic-MST(G, c)
T =
//T is the subset edges of a minimum spanning tree
while T is not the spanning tree do
Finding edge (u, v) is “safe” edge for T Joseph Kruskal
T = T {(u, v)} (1928 - ~)
return T
Kruskal algorithm:
T is forest (empty).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting its connected components.
void Kruskal ( )
{
Sort m edges of the graph e1,e2, . . . , em in ascending order of weight;
T = ; // T: set of edges of the minimum spanning tree
for (i = 1; i <=m; i++)
if (T {ei} does not contain cycle)
T = T {ei};
}
Kruskal algorithm: an example
7
a
2 2 Cycle (e,b,d,e)
d 5
Cycle (a,b,f,a)
5 4
f b 1 g
4 1 3
Cycle (a,b,c,f,a)
7
c e
4
Cycle (b,c,e,b)
a
a 7
d
2 2 d 5
5 4
f f b 1 g g
4 1 b 3
7
c c e e
4
35
Kruskal – Example
a
b a 7
d
2 2 d 5
b 5 4
f f b 1 g g
4 1 b 3
7
bc c e e
4 d
36
Kruskal – Example
b a 7
b d
2 2 Cycle
d 5
Cycle
b 5 4 b
f b 1 g g
4 1 b 3
Cycle
7
b c e b
4 d |ET| = n-1
Cycle as vertex c and e belongs to the same Length of MST: 14
connected component
37
Improved implementation: Computation time
• Time to determine whether 2 vertices i, j belong to the same connected
component: First(i) = First(j) : O(1) for each i, j. There are m edges
Total: O(m).
• Time to connect 2 connected components S and Q, where |S| |Q|:
– O(1) for each vertex of Q (the one with smaller number of vertices)
– Each vertex i in smaller connected component: connect log n times as
maximum. (As the number vertices of connected component containing
i is doubled after each connection.)
Total time to connect over all algorithm: O(n log n).
• Computation time:
O(mlogm + m + n log n).
Improved implementation: Computation time
void Kruskal ( )
{
Sort m edges of the graph e1,e2, . . . , em in ascending order of weight;
T = ; // T: set of edges of the minimum spanning tree
for (i = 1; i <=m; i++)
if (T {ei} does not contain cycle)
T = T {ei}; Time to connect 2 connected components S and Q, where |S| |Q|:
• O(1) for each vertex of Q (the one with smaller number of vertices)
} • Each vertex i in smaller connected component: connect log n times as maximum.
(As the number vertices of connected component containing i is doubled after
each connection.)
Total time to connect over all algorithm: O(n log n).
PRIM algorithm:
T is tree (initialize: T has one vertex).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting a vertex of T to other vertex not in T
PRIM algorithm: an example
7
a
2 2 d 5 select
5 4
f b 1 g
4 1 3
7
c e
4
Edges could be selected
PRIM algorithm:
T is tree (initialize: T has one vertex).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting a vertex of T to other vertex not in T
PRIM algorithm: an example
7
a
2 2 d 5
5 4
f b 1 g
4 1 3
7
c e
4
PRIM algorithm:
T is tree (initialize: T has one vertex).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting a vertex of T to other vertex not in T
PRIM algorithm: an example
7
a
2 2 d 5
5 4
f b 1 g
4 1 3
7
c e
4
PRIM algorithm:
T is tree (initialize: T has one vertex).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting a vertex of T to other vertex not in T
PRIM algorithm: an example
7
a
2 2 d 5
5 4
f b 1 g
4 1 3
7
c e
4
PRIM algorithm:
T is tree (initialize: T has one vertex).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting a vertex of T to other vertex not in T
PRIM algorithm: an example
7
a
2 2 d 5
5 4
f b 1 g
4 1 3
7
c e
4
PRIM algorithm:
T is tree (initialize: T has one vertex).
The “safe” edge included in T at each iteration is the edge with smallest weight
among edges connecting a vertex of T to other vertex not in T
PRIM algorithm: an example
7
a
2 2 d 5
5 4
f b 1 g
4 1 3
7
c e
4
Minimum spanning tree with edges: (g,d), (d,e), (e,b), (b,c), (b,a), (a,f)
The weight: 14 5+1+3+1+2+2 = 14
PRIM algorithm
void Prim(G, C) //G: graph; C: weight matrix
{
Select an arbitrary vertex r V; Tree T with only one vertex r
Initialize: tree T=(V(T), E(T)) where V(T)= {r}and E(T)=;
while (T has < n vertices)Among edges connecting a vertex of T to other vertex
{ not in T: find the edge with minimum weight
(u, v) is the minimum weight where u V(T) and vV(G) – V(T)
E(T) E(T) { (u, v) };
V(T) V(T) { v } add vertex v and edge (u,v) to tree T
}
}
PRIM: Implementation
• Graph with weight matrix C = {c[i,j], i, j = 1, 2,..., n}.
• At each iteration: to select quickly a vertex and an edge to add to spanning
tree, vertices of graph are labeled:
Label of a vertex v V \ V(T) in the form [d[v], near[v]] :
– d[v] :use to record the distance from vertex v to vertex set V(T):
d[v] := min{ c[v, w] : w V(T) } ( = c[v, z])
Edge with minimum edge among edges connecting vertex v to other vertex of V(T)
– near[v] := z record the vertex of T that is nearest to vertex v
12 w1 w2
v wi
5
d(v) = 2 w3 w4
2 wj
near[v] = w5
w5 V(T)
void Prim ( ) {
// Initialize:
V(T) = { r }; E(T) = ;
d[r] = 0; near[r] = r;
for v V \ V (T)
{
d[v] = c[r,v]; near[v] = r;
} Prepare data for finding “safe” edge process
// Iteration: d[v]: the edge with minimum weight connecting vertex v (not yet in
for k in range (2, n+1) the spanning tree T to other vertex of T
{
Find v V \ V(T) satisfying: d[v] = min { d[i] : i V \ V(T) };
V(T) = V(T) { v }; E(T) = E(T) { ( v, near[v] ) } ;
for v’ V \ V(T)
if (d[v’] > c[v,v’]) Because we have just changed the spanning
{ tree T : vertex v has just been added to T
d[v’] = c[v,v’] ; near[v’] = v; Need to update label of vertices not yet in
} T if necessary
}
T is the minimum spanning tree; 2)
}
Computation time: O(|V|
PRIM: an example
Find the minimum spanning tree of the graph
7
a
2 2 d 5
5 4
f b 1 g
4 1 3
7
c e
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
a 7
d
2 2 5
f
5 b 4
1 g
4 1 3
c e 7
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
a 7
d
2 2 5
f
5 b 4
1 g
4 1 3
c e 7
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
a 7
d
2 2 5
f
5 b 4
1 g
4 1 3
c e 7
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
a 7
d
2 2 5
f
5 b 4
1 g
4 1 3
c e 7
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
a 7
d
2 2 5
f
5 b 4
1 g
4 1 3
c e 7
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
[2, b] - - - - [4, c] - g, d, e, b, c
a 7
d
2 2 5
f
5 b 4
1 g
4 1 3
c e 7
4
Vertex a Vertex b Vertex c Vertex d Vertex e Vertex f Vertex g V(T)
[2, b] - - - - [4, c] - g, d, e, b, c
- - - - - [2, a] - g, d, e, b, c, a
g, d, e, b, c, a, f
a 7
Minimum spanning tree edges: 2 d
2 5
Weight : 14
f 5 b 4
1 g
4 1 3
c e 7
4