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

Algo 4

DSA Notes EEE , 7 Sem

Uploaded by

Soham Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Algo 4

DSA Notes EEE , 7 Sem

Uploaded by

Soham Lohiya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

29/09/2022

• SPANNING TREE

• MINIMUM SPANNING TREE

• PRIM’S AND KRUSKAL’S ALGORITHM


Spanning tree

A spanning tree of graph G is a subgraph of G which contains all its


vertices and has no cycles.
• A graph can have many spanning trees.

• Spanning tree does not exist if the graph is disconnected.

• nn-2 possible combinations


Minimum spanning tree
• Cost of a spanning tree=sum of edge costs in the tree.
• The spanning tree which has minimum cost is known as Minimum
Spanning Tree(MST)
• A graph can have more than one MST
Examples
for
spanning trees
and minimum spanning
trees
PRIM’S ALGORITHM
PRIM’S ALGORITHM:

Let T be a set of selected edges. Initially T= φ.


V’ be the set of vertices in the MST. Initially V’={1}
Let E be the set of graph edges.

While ( E != empty && |T| != n-1 )

{ let < u,v> be the least cost edge such that


u belongs to V’ and v does not belong to V’
if no such edge exists, break;
E = E - <u,v>
add edge <u,v> to T
add v to V’
}

if ( |T| == n-1 ) T is MST


else graph is not connected (MST cannot be constructed)
E
Graph Edge Edge cost
1-3 2
1-2 1
3-2 1
2-4 1
4-5 2
4-6 2
5-6 1
5-7 1
6-7 1
V’=
Kruskal’s Algorithm
(to find Minimum Spanning Tree)
Kruskal’s algorithm for MST
Kruskal’s Algorithm
Let T be a set of selected edges. Initially T=φ.
Let E be the set of graph edges.
n=number of vertices.
While ( E!=empty AND |T| != n-1 )
{ select a least cost edge from E. Let it be <u,v>
E = E - <u,v>
if ( by adding edge <u,v> , it does not create a cycle in T )
T = T U <u,v> // U=union
}
if ( |T| == n-1 ) T is a minimum spanning tree
else graph is not connected(mst cannot be constructed)
Tracing Kruskal’s algorithm.
T= empty

n=7 (7 vertices in the graph)

E=

Graph Edge Edge cost


1-3 2
1-2 1
3-2 1
2-4 1
|T| = no of elements in T. Initially it is 0.
4-5 2
4-6 2
5-6 1
5-7 1
6-7 1
Let T be a set of selected edges. Initially
T=φ. T= 1-2 3-2
Let E be the set of graph edges.
n=number of vertices. n=7 |T|=0 |T|=2
Similarly, we remove all
While ( E!=empty AND |T| != n-1 ) <u,v>= <3,2>
<1,2> |T|=1
edges with cost =1 and
{ select a least cost edge from E. Let it Graph Edge Edge cost see if they can be
be <u,v> E= added to T. After all
1-3 2 edges with cost 1 is
E = E - <u,v>
1-2 1 removed, remove the
if ( by adding edge <u,v> , edges with cost =2 until
3-2 1
it does not create a cycle in T ) table becomes empty.
2-4 1
T = T U <u,v>
4-5 2
}
4-6 2
if ( |T| == n-1 )
5-6 1
T is a minimum spanning tree
5-7 1
else
6-7 1
graph is not connected
Final Output

The graph that is a result of only these edges will form


one of the minimum spanning trees.

T= 1-2 3-2 2-4 5-6 6-7 4-5


Facts About Prim’s Algorithm:

•This algorithm is for obtaining minimum spanning tree by selecting the adjacent vertices of already selected
vertices.
•Prim’s algorithm initiates with a node.
•It traverses one node more than one time to get the minimum distance.
•Prim’s algorithm gives connected component as well as it works only on connected graph.
•Prim’s algorithm runs faster in dense graphs.
Facts About Kruskal’s Algorithm:

•This algorithm is for obtaining minimum spanning tree but it is not necessary to choose adjacent vertices of
already selected vertices.

•Kruskal’s algorithm initiates with an edge.

•Kruskal’s algorithm selects the edges in a way that the position of the edge is not based on the last step.

•It traverses one node only once.

•Kruskal’s algorithm can generate forest (disconnected components) at any instance as well as it can work on
disconnected components.

•Kruskal’s algorithm runs faster in sparse graphs.

You might also like