0% found this document useful (0 votes)
72 views

Minimum Spanning Tree: Presented By: Hinal Lunagariya

Minimum Spanning Tree is a connected, acyclic subgraph of a connected, undirected graph that connects all the vertices together with the minimum total edge weight or cost. Kruskal's and Prim's algorithms are commonly used to find minimum spanning trees in graphs. Kruskal's algorithm works by sorting the edges by weight and building the spanning tree by adding the shortest edges that do not create cycles, while Prim's algorithm grows a spanning tree from an initial vertex by iteratively adding the shortest edge connecting that tree to another vertex. Both algorithms are greedy algorithms that run in O(E log V) time where E is the number of edges and V is the number of vertices.

Uploaded by

Arbaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Minimum Spanning Tree: Presented By: Hinal Lunagariya

Minimum Spanning Tree is a connected, acyclic subgraph of a connected, undirected graph that connects all the vertices together with the minimum total edge weight or cost. Kruskal's and Prim's algorithms are commonly used to find minimum spanning trees in graphs. Kruskal's algorithm works by sorting the edges by weight and building the spanning tree by adding the shortest edges that do not create cycles, while Prim's algorithm grows a spanning tree from an initial vertex by iteratively adding the shortest edge connecting that tree to another vertex. Both algorithms are greedy algorithms that run in O(E log V) time where E is the number of edges and V is the number of vertices.

Uploaded by

Arbaz Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Minimum Spanning Tree

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

A connected, Four of the spanning trees of the graph


undirected
graph
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
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

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

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

B 5 All vertices have been


C
connected.
3 The solution is
4
8 6
ED 2
8 AB 3
A
F D CD 4
7
AE 4
5 EF 5
4
2
Total weight of tree:
18
E
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)
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

3 Select the shortest


4
8 6 edge connected to
any vertex already
8 connected.
A D
F
7 ED 2
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
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

B 5 All vertices have been


C
connected.
3
4 The solution is
8 6
AB 3
8 AE 4
A
F D ED 2
7
DC 4
5 EF 5
4
2
Total weight of tree:
18
E
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)
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

Kruskal’s algorithm Prim’s algorithm

1. Select the shortest edge in 1. Select any vertex


a network
2. Select the shortest edge
2. Select the next shortest connected to that vertex
edge which does not create a
cycle 3. Select the shortest edge
connected to any vertex
3. Repeat step 2 until all already connected
vertices have been
connected 4. Repeat step 3 until all
vertices have been
connected
Thank you!!

You might also like