GRAPH ALGORITHMS
❖ To find the minimum spanning tree of a
graph G we have two algorithms:
• Kruskal’s Algorithm
• Prims Algorithm
❖To find the shortest path between vertices
in a graph
• Dijkstra’s Algorithm
Kruskal’s Algorithm
• Consider the weighted graph given below
A 1 B 6 c
3 5 4
1 2
F 1 E 4 D
Step1: List all the edges of the graph in the increasing
order of their weights.
edge Weights
AB 1
BE 1
FE 1
CD 2
AF 3
EC 4
FB 5
BC 6
Step 2: Select the edge with the smallest weight
to be the branch of the spanning tree
Step 3: Select the next smallest weight edge
such that it doesn’t make a circuit with the
previously selected edges.
Step 4: Continue the process untill (n-1) edges
have been selected to form the spanning tree.
Draw the spanning tree
Find the minimum spanning tree for
the given graph using Kruskals
Algorithm
Prims Algorithm
• Step 1: Draw n isolated vertices and label
them as 𝑣1 , 𝑣2 , 𝑣3 , … … 𝑣𝑛
• Step 2: Tabulate the weights of the edges in an
𝑛 𝑋 𝑛 matrix.
• Step 3: Set the weights of non existing edges
as ∞.
• Step 4: Start from vertex and connect it to its
nearest neighbour(shortest distance
neighbour)
• Step 5: Continue this process untill (𝑛 − 1)
edges have been selected to form a tree.
• Example:
A 1 B 6 c
3 5 4
1 2
F 1 E 4 D
• Step 1: mark all the vertices from 𝐴 to 𝐹
A B c
F E D
• Steps 2 & 3 : Form the matrix
− 1 ∞ ∞ ∞ 3
1 − 6 ∞ 1 5
∞ 6 − 2 4 ∞
∞ ∞ 2 − 4 ∞
∞ 1 4 4 − 1
3 5 ∞ ∞ 1 −
• Step 4: Start from a vertex and connect it to its
nearest neighbour(shortest distance
neighbour)
A B c
F E D
Draw the spanning tree.