2 4-GraphSpanningTree
2 4-GraphSpanningTree
PART 1
COMBINATORIAL THEORY
(Lý thuyết tổ hợp)
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
Contents
1. Tree and its properties
2. Spanning tree
3. The minimal spanning tree
4
1. Tree and its properties
A tree is an undirected connected graph with no cycles.
Example 1. Which of the graphs are trees?
5
1. Tree and its properties
Forest: Graph containing no cycles that are not connected, but each
connected component is a tree.
T1 T2
T3
6
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.
7
Contents
1. Tree and its properties
2. Spanning tree
3. The minimal spanning tree
8
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
b c a
a c c a b
K3 3 spanning trees of K3
16 spanning trees of K4
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
1. Tree and its properties
2. Spanning tree
3. The minimal spanning tree
13
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
f b
1
1 g The weight of the
4 3 7 spanning tree = 14
c 4 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.
Example: An undirected connected weighted graph with two minimal spanning
trees, both of weight 14
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
As the number of spanning trees of G is very large (see Cayley's theorem),
we could not solve this problem by brute force.
Applications of Minimum Spanning Trees: an example
Network design: telephone, electrical, hydraulic, TV cable, computer, road.
• Phone network design. You have a business with several offices; you want to lease
phone lines to connect them up with each other; and the phone company charges
different amounts of money to connect different pairs of cities. You want a set of
lines that connects all your offices with a minimum total cost. It should be a
spanning tree, since if a network isn’t a tree you can always remove some edges and
save money.
• In the design of electronic circuits, it is often necessary to connect pins by wiring
them together:
• To interconnect n pins, we can use n-1 wires,
each connecting 2 pins.
• We want to minimize the total length of the
wires.
• Minimum Spanning Trees can be used to
model this problem.
Applications of Minimum Spanning Trees: an example
Suppose we want to build a railway system that connects n cities so that
passengers can travel between any two cities and the total cost of
construction must be minimal.
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
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
How to find the “safe” edge ???:
The criteria to choose an edge at each step decides the process of two following
minimum spanning tree algorithms (both use greedy strategies):
1. Kruskal
2. PRIM
How to find the “safe” edge?
T : set of edges of some spanning tree
Initialize: T = Æ
Kruskal algorithm
v T is forest.
v The “safe” edge added to T at each iteration is the edge with smallest
weight among edges connecting its connected components.
Prim algorithm
v T is tree.
v 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
T = T È{(u, v)} Joseph Kruskal
return T (1928 - ~)
Kruskal algorithm:
v T is forest (empty).
v 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)
PRIM algorithm:
v T is tree (initialize: T has one vertex).
v 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:
v T is tree (initialize: T has one vertex).
v 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:
v T is tree (initialize: T has one vertex).
v 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:
v T is tree (initialize: T has one vertex).
v 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:
v T is tree (initialize: T has one vertex).
v 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:
v T is tree (initialize: T has one vertex).
v 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 vÎV(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
near[v] = w5 2 wj
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=2; k<=n;k++) 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;
}
Computation time: O(|V| 2)