Minimum Spanning Tree
Presented by:
Hinal Lunagariya
TREE
B
A
FE
C
D
Connected acyclic graph
Tree with n nodes contains
exactly n-1 edges.
GRAPH
Graph with n nodes contains
less than or equal to n(n-1)/2
edges.
A connected,
undirected
graph
Four of the spanning trees of the graph
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
EXAMPLE..
Minimizing costs
Suppose you want to supply a set of houses (say, in a new
subdivision) with:
electric power
water
sewage lines
telephone lines
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
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?
Avonford Fingley
Brinleigh Cornwell
Donster
Edan
2
7
4
5
8 6
4
5
3
8
Example
MINIMUM SPANNING TREE
Let G = (N, A) be a connected, undirected graph where
N is the set of nodes and A is the set of edges. Each
edge has a given nonnegative length. The problem is to
find a subset T of the edges of G such that all the
nodes remain connected when only the edges in T are
used, and the sum of the lengths of the edges in T is as
small as possible possible. Since G is connected, at
least one solution must exist.
Finding Spanning Trees
• There are two basic algorithms for finding minimum-cost
spanning trees, and both are greedy algorithms
• 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
A F
B C
D
E
2
7
4
5
8 6
4
5
3
8
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
List the edges in
order of size:
ED 2
AB 3
AE 4
CD 4
BC 5
EF 5
CF 6
AF 7
BF 8
CF 8
Kruskal’s Algorithm
Select the shortest
edge in the network
ED 2
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest
edge which does not
create a cycle
ED 2
AB 3A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4 (or AE 4)
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next
shortest edge which
does not create a cycle
E
A
C
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Select the next shortest
edge which does not
create a cycle
ED 2
AB 3
CD 4
AE 4
BC 5 – forms a cycle
EF 5
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
All vertices have been
connected.
The solution is
ED 2
AB 3
CD 4
AE 4
EF 5
Total weight of tree:
18
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Kruskal’s Algorithm
Algorithm
function Kruskal (G=(N,A): graph ; length : AR+
):set of edges
{initialisation}
sort A by increasing length
N  the number of nodes in N
T  Ø {will contain the edges of the minimum spanning tree}
initialise n sets, each containing the different element of N
{greedy loop}
repeat
e  {u , v}  shortest edge not yet considered
ucomp  find(u)
vcomp  find(v)
if ucomp ≠ vcomp then
merge(ucomp , vcomp)
T  T Ú {e}
until T contains n-1 edges
return T
Kruskal’s Algorithm: complexity
Sorting loop: O(a log n)
Initialization of components: O(n)
Finding and merging: O(a log n)
O(a log n)
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Select any vertex
A
Select the shortest
edge connected to
that vertex
AB 3
Prim’s Algorithm
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Select the shortest
edge connected to
any vertex already
connected.
AE 4
Prim’s Algorithm
Select the shortest
edge connected to
any vertex already
connected.
ED 2
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Prim’s Algorithm
Select the shortest
edge connected to
any vertex already
connected.
DC 4
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Prim’s Algorithm
Select the shortest
edge connected to
any vertex already
connected.
EF 5A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
Prim’s Algorithm
A
F
B
C
D
E
2
7
4
5
8 6
4
5
3
8
All vertices have been
connected.
The solution is
AB 3
AE 4
ED 2
DC 4
EF 5
Total weight of tree:
18
Prim’s Algorithm
Prim’s Algorithm
function Prim(G = <N,A>: graph ; length : A R+
) : set of
edges
{initialisation}
T  Ø
B  {an arbitrary member of N}
While B ≠ N do
find e = {u , v} of minimum length such
u € B and v € N  B
T T υ {e}
B  B υ {v}
Return T
Complexity:
Outer loop: n-1 times
Inner loop: n times
O(n2
)
a
b
h
c d
e
fg
i
4
8 7
9
10
14
4
2
2
6
1
7
11
8
Example
a
b
h
c d
e
fg
i
4
8 7
9
10
14
4
2
2
6
1
7
11
8
Solution
Minimum Connector Algorithms
Kruskal’s algorithm
1. Select the shortest edge in
a network
2. Select the next shortest
edge which does not create a
cycle
3. Repeat step 2 until all
vertices have been
connected
Prim’s algorithm
1. Select any vertex
2. Select the shortest edge
connected to that vertex
3. Select the shortest edge
connected to any vertex
already connected
4. Repeat step 3 until all
vertices have been
connected
Thank you!!
Minimum spanning tree

Minimum spanning tree

  • 1.
    Minimum Spanning Tree Presentedby: Hinal Lunagariya
  • 2.
    TREE B A FE C D Connected acyclic graph Treewith n nodes contains exactly n-1 edges. GRAPH Graph with n nodes contains less than or equal to n(n-1)/2 edges.
  • 3.
    A connected, undirected graph Four ofthe spanning trees of the graph 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
  • 4.
  • 5.
    Minimizing costs Suppose youwant to supply a set of houses (say, in a new subdivision) with: electric power water sewage lines telephone lines 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
  • 6.
    A cable companywant to connect five villages to their network which currently extends to the market town of Avonford. What is the minimum length of cable needed? Avonford Fingley Brinleigh Cornwell Donster Edan 2 7 4 5 8 6 4 5 3 8 Example
  • 7.
    MINIMUM SPANNING TREE LetG = (N, A) be a connected, undirected graph where N is the set of nodes and A is the set of edges. Each edge has a given nonnegative length. The problem is to find a subset T of the edges of G such that all the nodes remain connected when only the edges in T are used, and the sum of the lengths of the edges in T is as small as possible possible. Since G is connected, at least one solution must exist.
  • 8.
    Finding Spanning Trees •There are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms • Kruskal’s algorithm: Created in 1957 by Joseph Kruskal • Prim’s algorithm Created by Robert C. Prim
  • 9.
    We model thesituation as a network, then the problem is to find the minimum connector for the network A F B C D E 2 7 4 5 8 6 4 5 3 8
  • 10.
    A F B C D E 2 7 4 5 8 6 4 5 3 8 List theedges in order of size: ED 2 AB 3 AE 4 CD 4 BC 5 EF 5 CF 6 AF 7 BF 8 CF 8 Kruskal’s Algorithm
  • 11.
    Select the shortest edgein the network ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 12.
    Select the next shortest edgewhich does not create a cycle ED 2 AB 3A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 13.
    Select the next shortest edgewhich does not create a cycle ED 2 AB 3 CD 4 (or AE 4) A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 14.
    Select the next shortestedge which does not create a cycle E A C A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 15.
    Select the nextshortest edge which does not create a cycle ED 2 AB 3 CD 4 AE 4 BC 5 – forms a cycle EF 5 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 16.
    All vertices havebeen connected. The solution is ED 2 AB 3 CD 4 AE 4 EF 5 Total weight of tree: 18 A F B C D E 2 7 4 5 8 6 4 5 3 8 Kruskal’s Algorithm
  • 17.
    Algorithm function Kruskal (G=(N,A):graph ; length : AR+ ):set of edges {initialisation} sort A by increasing length N  the number of nodes in N T  Ø {will contain the edges of the minimum spanning tree} initialise n sets, each containing the different element of N {greedy loop} repeat e  {u , v}  shortest edge not yet considered ucomp  find(u) vcomp  find(v) if ucomp ≠ vcomp then merge(ucomp , vcomp) T  T Ú {e} until T contains n-1 edges return T
  • 18.
    Kruskal’s Algorithm: complexity Sortingloop: O(a log n) Initialization of components: O(n) Finding and merging: O(a log n) O(a log n)
  • 19.
    A F B C D E 2 7 4 5 8 6 4 5 3 8 Select anyvertex A Select the shortest edge connected to that vertex AB 3 Prim’s Algorithm
  • 20.
    A F B C D E 2 7 4 5 8 6 4 5 3 8 Select theshortest edge connected to any vertex already connected. AE 4 Prim’s Algorithm
  • 21.
    Select the shortest edgeconnected to any vertex already connected. ED 2 A F B C D E 2 7 4 5 8 6 4 5 3 8 Prim’s Algorithm
  • 22.
    Select the shortest edgeconnected to any vertex already connected. DC 4 A F B C D E 2 7 4 5 8 6 4 5 3 8 Prim’s Algorithm
  • 23.
    Select the shortest edgeconnected to any vertex already connected. EF 5A F B C D E 2 7 4 5 8 6 4 5 3 8 Prim’s Algorithm
  • 24.
    A F B C D E 2 7 4 5 8 6 4 5 3 8 All verticeshave been connected. The solution is AB 3 AE 4 ED 2 DC 4 EF 5 Total weight of tree: 18 Prim’s Algorithm
  • 25.
    Prim’s Algorithm function Prim(G= <N,A>: graph ; length : A R+ ) : set of edges {initialisation} T  Ø B  {an arbitrary member of N} While B ≠ N do find e = {u , v} of minimum length such u € B and v € N B T T υ {e} B  B υ {v} Return T Complexity: Outer loop: n-1 times Inner loop: n times O(n2 )
  • 26.
  • 27.
  • 28.
    Minimum Connector Algorithms Kruskal’salgorithm 1. Select the shortest edge in a network 2. Select the next shortest edge which does not create a cycle 3. Repeat step 2 until all vertices have been connected Prim’s algorithm 1. Select any vertex 2. Select the shortest edge connected to that vertex 3. Select the shortest edge connected to any vertex already connected 4. Repeat step 3 until all vertices have been connected
  • 29.