You are on page 1of 9

GRAPH THEORY

ASSIGNMENT - 4
Spanning Tree

DONE BY :
K. R. VIKRAM - CS21B1020
3RD YEAR CSE – NIT PY
Input and Output

• Input : Adjacency Matrix

• Output : Adjacency Matrix of the Spanning Tree


Idea / Description
• Initialization:
• Create an empty set S and add the first vertex to it.
• Create a set V-S with all the other vertices.
• Repeat until V-S is not empty:
• Find the edge with the minimum weight from a vertex in S to a vertex in V-S.
• Add the vertex from V-S with the minimum weighted edge to S.
• Remove this vertex from V-S.
• Add the edge to T.
• Output:
• The final T will be a spanning tree of the graph.
Flowchart
Working Example

GIVEN GRAPH
Let ‘A’ be the source vertex
Iteration 1:
• For each vertex v in S (which is just A for now),
find the edge with the minimum weight from v
to a vertex in V-S.
• In this case, the minimum weighted edge is 2,
from A to B.

Iteration 2:
• For each vertex v in S (which are A and B now),
find the edge with the minimum weight from v
to a vertex in V-S.
• The minimum weighted edges are 3 (from B to
C) and 6 (from A to D). We choose the edge
with weight 3.
Iteration 3:
• For each vertex v in S (which are A, B, and C
now), find the edge with the minimum weight
from v to a vertex in V-S.
• The minimum weighted edge is 5 (from B to
E).

Iteration 4:
• For each vertex v in S (which are A, B, C and E
now), find the edge with the minimum
weight from v to a vertex in V-S.
• The minimum weighted edge is 6 (from A to
D).
Complexities
The time complexity for the given algorithm will be - O(E log V)
Here,
E → the number of Edges and
V → the number of Vertices

The space complexity for the given algorithm will be - O(V)

It uses heap sort to find the minimum weighted edge, so the space complexity
of the priority queue is O(V)
THANK YOU

You might also like