Minimum Spanning Tree: Presented By: Hinal Lunagariya
Minimum Spanning Tree: Presented By: Hinal Lunagariya
Presented by:
Hinal Lunagariya
TREE
A
Connected acyclic graph
B C
Tree with n nodes contains
D E F
exactly n-1 edges.
GRAPH
Graph with n nodes contains
less than or equal to n(n-1)/2
edges.
SPANNING TREE...
Suppose you have a connected undirected graph
Connected: every node is reachable from every
other node
Undirected: edges do not have an associated
direction
...then a spanning tree of the graph is a connected
subgraph in which there are no cycles
To keep costs down, you could connect these houses with a
spanning tree (of, for example, power lines)
However, the houses are not all equal distances apart
To reduce costs even further, you could connect the houses
with a minimum-cost spanning tree
Example
A cable company want to connect five villages to their network
which currently extends to the market town of Avonford.
What is the minimum length of cable needed?
Brinleigh 5
Cornwell
3
4
8 6
8
Avonford Fingley Donster
7
5
4
2
Edan
MINIMUM SPANNING TREE
• Kruskal’s algorithm:
Created in 1957 by Joseph Kruskal
• Prim’s algorithm
Created by Robert C. Prim
We model the situation as a network, then the problem is
to find the minimum connector for the network
B 5
C
3
4
8 6
8
A F D
7
5
4
2
E
Kruskal’s Algorithm
List the edges in
order of size:
B 5
C
ED 2
3 AB 3
4 AE 4
6
8 CD 4
BC 5
8
EF 5
A
7 F D CF 6
AF 7
5 BF 8
4 CF 8
2
E
Kruskal’s Algorithm
B 5
C Select the shortest
edge in the network
3
6
4 ED 2
8
8
A D
7 F
5
4
2
E
Kruskal’s Algorithm
B 5
C
Select the next
shortest
3
4 edge which does not
8 6 create a cycle
8 ED 2
A
F D AB 3
7
5
4
2
E
Kruskal’s Algorithm
B 5
C
Select the next
shortest
3
4 edge which does not
6
8 create a cycle
8
ED 2
A
7 F D AB 3
CD 4 (or AE 4)
5
4
2
E
Kruskal’s Algorithm
B 5
C Select the next
shortest edge which
3 does not create a cycle
4
8 6
E
8
A D
7 F
5
A
4
2
E
C
Kruskal’s Algorithm
B 5
C
Select the next shortest
edge which does not
3
4 create a cycle
8 6
ED 2
8 AB 3
A D CD 4
7 F
AE 4
5 BC 5 – forms a cycle
4 EF 5
2
E
Kruskal’s Algorithm
O(a log n)
Prim’s Algorithm
B 5
C Select any vertex
3 A
4
8 6
Select the shortest
8 edge connected to
A D that vertex
7 F
AB 3
5
4
2
E
Prim’s Algorithm
B 5
C
Select the shortest
3 edge connected to
4 any vertex already
6
8 connected.
8
AE 4
A D
7 F
5
4
2
E
Prim’s Algorithm
B 5
C
E
Prim’s Algorithm
B 5
C
Select the shortest
3 edge connected to
4 any vertex already
6
8 connected.
8
DC 4
A D
7 F
5
4
2
E
Prim’s Algorithm
B 5
C
Select the shortest
3 edge connected to
6
4 any vertex already
8
connected.
8
A EF 5
F D
7
5
4
2
E
Prim’s Algorithm
Complexity:
Outer loop: n-1 times
Inner loop: n times
O(n2)
Example
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Solution
8 7
b c d 9
4
2
a 11 14 e
i 4
7 6
10
8
h g f
1 2
Minimum Connector Algorithms