Anda di halaman 1dari 43

# Minimum Spanning Tree

## Minimum Spanning Tree: Applications

The Department of Highways wishes to build enough new roads so that
the five towns in a certain states will all be connected to one another
cither directly or via another town.
The cost of constructing a highway between each pair of towns is known
Minimum Spanning Tree: Applications
We may represent this problem as a graph by letting each town
correspond to a vertex, and each possible highway to be
constructed correspond to an edge joining two vertices (towns).

## Associate a weight to each edge that is equal to the cost of

constructing the corresponding highway.

## • Deciding which highways to build can be viewed as the problem of constructing

a minimum-cost spanning tree for the corresponding graph. This follows since
the edges of any spanning tree will connect each vertex (town) with every other
vertex (town). Moreover, the minimum-weight spanning tree will represent the
set of new highways of minimum total
Minimum Spanning Tree
Given a connected, undirected graph, a spanning tree of that graph is a
subgraph that is a tree and connects all the vertices together. A single graph
can have many different spanning trees.
Minimum Spanning Tree: Applications
(i) Logistics
(ii) Route Planning
(iii) Accessibility Planning
Minimum Spanning Tree
Given a connected, undirected graph, a spanning tree of that graph is a
subgraph that is a tree and connects all the vertices together. A single graph
can have many different spanning trees.

## We can also assign a weight to each edge, which is a number representing

how unfavourable it is, and use this to assign a weight to a spanning tree by
computing the sum of the weights of the edges in that spanning tree. A
minimum spanning tree (MST) or minimum weight spanning tree is then a
spanning tree with weight less than or equal to the weight of every other
spanning tree.

## More generally, any undirected graph (not necessarily connected) has a

minimum spanning forest, which is a union of minimum spanning trees for its
connected components.
Minimum Spanning Tree
Given a connected, undirected graph, a spanning tree of that graph is a
subgraph that is a tree and connects all the vertices together. A single graph
can have many different spanning trees.

## We can also assign a weight to each edge, which is a number representing

how unfavourable it is, and use this to assign a weight to a spanning tree by
computing the sum of the weights of the edges in that spanning tree. A
minimum spanning tree (MST) or minimum weight spanning tree is then a
spanning tree with weight less than or equal to the weight of every other
spanning tree.

## More generally, any undirected graph (not necessarily connected) has a

minimum spanning forest, which is a union of minimum spanning trees for its
connected components.
What is Tree ?
Definition: A tree is a connected undirected graph with no simple
circuits.
• Since a tree cannot have a simple circuit, a tree cannot
contain multiple edges or loops.
• Therefore, any tree must be a simple graph.
• Theorem: An undirected graph is a tree if and only if there is a
unique simple path between any of its vertices.
Trees
Spanning Tree
Spanning Trees
A spanning tree of a graph is just a subgraph
that contains all the vertices and is a tree.
A graph may have many spanning trees
Spanning Tree Properties
Spanning Tree properties:
On a connected graph G=(V, E), a spanning
tree:
• is a connected subgraph
• acyclic
• is a tree (|E| = |V| - 1)
• contains all vertices of G (spanning)
Spanning Tree Properties
Spanning Tree properties:
On a connected graph G=(V, E), a spanning
tree:
• is a connected subgraph
• acyclic
• is a tree (|E| = |V| - 1)
• contains all vertices of G (spanning)
Spanning Trees
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
• Total of 16 different spanning trees for the graph below
Minimum Spanning Tree
Minimum Weighted Spanning Tree
Minimum weight spanning tree (MST)
On a weighted graph, A MST:
• connects all vertices through edges with least
weights.
• has w(T) ≤ w(T’) for every other spanning tree T’
in G
• Adding one edge not in MST will create a cycle
Finding Minimum Spanning Tree
Finding Minimum Spanning Tree Algorithms :
• Kruskal’s and
• Prim’s
Kruskal Algorithm
Kruskal’s algorithm

Edge first

## 1. Arrange all edges in a list (L) in non-decreasing order

2. Select edges from L, and include that in set T, avoid cycle.
3. Repeat 3 until T becomes a tree that covers all vertices
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Kruskal Algorithm (Contd..)
Prim’s Algorithm
1. Start form any arbitrary vertex
2. Find the edge that has minimum weight form all
known vertices
3. Stop when the tree covers all vertices
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm (Contd..)
Prim’s Algorithm More Complex..
Prim’s Algorithm More Complex..
Prim’s Algorithm More Complex..
Prim’s Algorithm More Complex..
Prim’s Algorithm More Complex..
Kruskal Vs Prim’s Algorithm

## • Both have the same output -- MST

• Kruskal’s begins with forest and merge into a tree
• Prim’s always stays as a tree
• If you don’t know all the weight on edges-- use
Prim’s algorithm
• If you only need partial solution on the graph- use Prim’s
algorithm
Kruskal Vs Prim’s Algorithm
Complexity
Kruskal: O(NlogN)
comparison sort for edges
Prim: O(NlogN)
search the least weight edge for every
vertices
Kruskal Vs Prim’s Algorithm
Kruskal’s has better running times if the number
of edges is low, while Prim’s has a better running
time if both the number of edges and the number
of nodes are low.

## So, of course, the best algorithm depends on the

graph and if you want to bear the cost of complex
data structures.