Sei sulla pagina 1di 29

Spanning Tree

R K Mohapatra
What is A Spanning Tree?
• A spanning tree for an
undirected graph G=(V,E) is a
subgraph of G that is a tree
a
and contains all the vertices
of G
b u e

• Can a graph have more than


one spanning tree? c v f

• Can an unconnected graph d


have a spanning tree?
Minimal Spanning Tree.
• The weight of a subgraph is
the sum of the weights of it 4
a 4
edges. 9
3
b u e

• A minimum spanning tree for 14 2


a weighted graph is a 10
c v 15 f
spanning tree with minimum
weight. 3
8
d

• Can a graph have more then


one minimum spanning tree? Mst T: w( T )=  (u,v)  T w(u,v ) is minimized
Example of a Problem that
Translates into a MST
The Problem
• Several pins of an electronic circuit must be
connected using the least amount of wire.

Modeling the Problem


• The graph is a complete, undirected graph
G = ( V, E ,W ), where V is the set of pins, E is the
set of all possible interconnections between the
pairs of pins and w(e) is the length of the wire
needed to connect the pair of vertices.
• Find a minimum spanning tree.
MST Algorithms
We will show two ways to build a minimum
spanning tree.
• A MST can be grown from the current spanning
tree by adding the nearest vertex and the edge
connecting the nearest vertex to the MST. (Prim‘s
algorithm)

• A MST can be grown from a forest of spanning


trees by adding the smallest edge connecting two
spanning trees. (Kruskal's algorithm)
Notation
• Tree-vertices: in the tree constructed so far
• Non-tree vertices: rest of vertices

Prim’s Selection rule

• Select the minimum weight edge between a tree-


node and a non-tree node and add to the tree
The Prim algorithm Main Idea
Select a vertex to be a tree-node

while (there are non-tree vertices) {


if there is no edge connecting a tree node
with a non-tree node then
return “no spanning tree”

select an edge of minimum weight between a tree


node and a non-tree node

add the selected edge and its new vertex to the tree
}
return tree
5
A B
4 6 2

2 D 3
C

3 1 2
E F
4

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B

C D

E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B

2 D
C

E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B

2 D
C

1
E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B
2

2 D
C

3 1 2
E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B
2

2 D
C

3 1 2
E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B
2

2 D
C

3 1 2
E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B
2

2 D
C

3 1 2
E F

Prim's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

A B
2

2 D
C

3 1 2
E F

Prim's Algorithm
minimum- spanning tree

A B
2

2 D
C

3 1 2
E F

Prim's Algorithm
Kruskal‘s Algorithm

1. Each vertex is in its own cluster

2. Take the edge e with the smallest weight


- if e connects two vertices in different clusters,
then e is added to the MST and the two clusters,
which are connected by e, are merged into a
single cluster
- if e connects two vertices, which are already
in the same cluster, ignore it

3. Continue until n-1 edges were selected


Kruskal's Algorithm
5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3 Can’t be chosen next


(B & F are in same cluster)
3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
A 5 B
4 6 2

C 2 D 3

3 1 2
E F
4

5
A B
4 6 2

2 D 3
C

3 1 2
E F
4
Kruskal's Algorithm
minimum- spanning tree

A B
2

2 D
C

3 1 2
E F

Kruskal's Algorithm

Potrebbero piacerti anche