Sei sulla pagina 1di 15


The standard application of minimum spanning tree is to
solve a problem like phone network design.
If we have a business with several offices; we want to lease
phone lines to connect them up with each other; and the
phone company charges different amounts of money to
connect different pairs of cities.
We want a set of lines that connects all our offices with a
minimum total cost. It should be a minimum spanning tree,
since if a network isn't a tree we can always remove some
edges and save money.

A spanning tree of a graph is any tree that includes every
vertex in the graph.
More formally, a spanning tree of a graph G is a subgraph of
G that is a tree and contains all the vertices of G.
We construct spanning tree whenever we want to find a
simple, cheap and efficient way to connect a set of terminals

(computers, cites, factories, etc.). Spanning trees are

important because of following reasons.

Spanning trees construct a sparse sub graph that tells a lot

about the original graph.

Spanning trees are very important in designing efficient

routing algorithms.

Some hard problems (traveling salesman problem) can be

solved approximately by using spanning trees.

Spanning trees have wide applications in many areas, such

as network design, etc.

A minimum spanning tree (MST) is a spanning tree of minimum

weight. The minimum spanning tree may not be unique, but it is
true if all the edge weights are distinct.
The following is an example of spanning and minimum spanning

There are two algorithms: the Kruskals algorithm and the prims
algorithm to find the minimum spanning tree of an undirected
weighted graph. These algorithms use similar approaches, but
work in very different ways.
Kruskals algorithm is a greedy algorithm. A greedy
algorithm chooses some local optimum (i.e. picking an edge
with the least weight in a MST).
It builds the MST in forest. Initially, each vertex is in its own
tree in forest. Then, algorithm considers each edge in turn,
ordered by increasing weight.

If an edge (u, v) connects two different trees, then (u, v) is

added to the set of edges of the MST, and two trees
connected by an edge (u, v) are merged into a single tree
On the other hand, if an edge (u, v) connects two vertices in
the same tree, then edge (u, v) is discarded.
Therefore it Start with an empty set A, and select at every
stage the smallest edge that has not been chosen or rejected,
regardless of where this edge is situated in graph.
Kruskal's algorithm uses a disjoint-set data structure to
maintain several disjoint sets of elements.
Disjoint-Sets Data Structure

Make-Set (v): Create a new set whose only member is

pointed to by v.

FIND-Set: Returns a pointer to the set containing v.

UNION (u, v): Unites the dynamic sets that contain u and v
into a new set that is union of these two sets.

Let V be the number of vertices and E be the number of edges
and we assume that E V 1.
It takes (E log E) time to sort the edges. The for loop
iterates E number of times, and each iteration involves a
constant number of accesses to the Union-Find disjoint set
data structure on the collection of V items.
Thus each access takes (log V) time, which gives a total of
(E log V) times.
Thus the total running time for the algorithm is evaluated by
adding up all these, which is ((V +E) log V). Since V is not
asymptotically larger than E, the running time can be
expressed as (E log V).

Problems with Kruskal

Kruskals algorithm grows a series of trees which are
Growing a set of disjoint trees may be a problem in some
Kruskal also continues checking edges even when all nodes
are inserted.
Checking the edges can be inefficient in that a graph can
have up V2 edges.
Many of these edges will be creating cycles.
Example: Using kruskals algorithm find the minimum spanning
tree of the following given graph.

Example: Find the MST of the following given graph using kruskals algorithm.



Prims algorithm is another greedy algorithm for minimum
spanning trees. It differs from Kruskals algorithm only in
how it selects the next safe edge to add at each step.
The main idea of Prim's algorithm is similar to that of
Dijkstra's algorithm for finding shortest path in a given

Prim's algorithm has the property that the edges in the set A
always form a single tree. We begin with some vertex u in a
given graph G = (V, E), defining the initial set of vertices A.
In each iteration, we choose a minimum-weight edge (u, v),
connecting a vertex u in the set A to the vertex v outside of
set A. Then vertex v is brought in to A.
This process is repeated until a spanning tree is formed. Like
Kruskal's algorithm, the important fact about MSTs is that we
always choose the smallest-weight edge joining a vertex
inside set A to the one outside the set A. Prim is slightly more
efficient than Kruskal.

How it works

The prims algorithm starts with an arbitrary root vertex r and

grows the tree until it spans all the vertices in V.
Initialize the root vertex with key 0 and other vertices with
key .
The initial step is to put all vertices onto a priority queue
based on a key.
The source vertex has 0 as its key, so it is chosen first and its
adjacency list explored for the smallest weight edge which is
added to the tree.
At all times the smallest weight edge between the included
vertices and the vertices not included in the tree is added.
This process continues until all vertices are removed from the
priority queue.
Tree Growing
Prims algorithm creates the tree by adding one edge at a
time to the current tree. The process can start with a root
vertex r (it can be any vertex).
At any time, the subset of edges A forms a single tree
whereas Kruskals algorithm formed a forest.
We add a single vertex as a leaf to the tree. The process is
illustrated in the following figure.



It takes O (log V) to extract each vertex from the priority

For each incident edge, O (log V) time is spent in decreasing
the key of the neighboring vertex. Thus the total time is O
(log V + deg (u) log V).
The other steps for updation take constant time. So the total
running time is:


V is not asymptotically greater than E, so the running time is (E

log V) which is same as Kruskals algorithm.
Example: Using prims algorithm find the minimum spanning tree
of the following given graph.




Example: Find the MST of the following given graph using Prims algorithm.