1
4
9
6
3
7
5
2
0
8
Dijkstra’s
Algorithm & Prim’s
Algorithm
Understanding the Fundamentals of Shortest Path and
Minimum Spanning Tree
Our Team
• Seerat Fatima [2024-DS-32]
• Aleena Ahmad [2024-DS-9]
• Minahil Samreen [2024-DS-11]
• Amna Naveed [2024-DS-42]
1
3
4 9
7
5
2
0
6
8
Today’s Agenda
1
3
8
9
7
5
0
6
2
Dijestra’s
Algorithm
01
Prim’s
Algorithm
03
Real
World
Example
02
Real
World
Example
04
1
4
9
6
3
7
5
2
0
8
Dijkstra
Algorithm
What is Dijkstra's Algorithm?
• Dijkstra's algorithm finds the shortest path from one
vertex to all other vertices.
• It does so by repeatedly selecting the nearest unvisited
vertex and calculating the distance to all the unvisited
neighboring vertices.
How it works?
• Start from the source
node.
• Assign initial distances
(infinity except for the
source = 0).
• Visit neighbors of the
current node, update
shortest paths.
• Repeat until all nodes
are visited.
Example Problem
A
D
B
C
E
6
1
5
5
1 2
2
Answer
A = 0
B = 1+2=3 (A -> D -> B)
C = 1+1+5=7 (A -> D -> E -> C)
D = 1 (A -> D)
E = 1+1=2 (A -> D -> E)
• GPS navigation systems
• Network routing protocols
• Game AI pathfinding
Where is it used?
1
3
8
9
7
5
0
6
2
1
4
9
6
3
7
5
2
0
8
Real World
Example
Finding the shortest route from Lahore to all other
cities
Lahor
e
Karach
i
Islamaba
d
Multa
n
Peshawa
r
Quett
a
Solution
1.A = 0
2.B = 4 (A -> B)
3.C = 5 (A -> C)
4.D = 4 + 9 = 13 (A -> B -> D)
5.E = 5 + 3 = 8 (A -> C -> E)
6.F = 5 + 3 + 6 = 14 (A -> C -> E -> F)
1
4
9
6
3
7
5
2
0
8
Prim’s
Algorithm
What is Prim's Algorithm?
• Prim's algorithm finds the Minimum Spanning Tree
(MST) in a connected and undirected graph.
• The MST found by Prim's algorithm is the collection of
edges in a graph, that connects all vertices,with a
minimum sum of edge weights.
1
3
6
8
9
7
5
2
0 Fundamental Concepts
A spanning tree is a subgraph of a connected, undirected
graph that includes all the vertices of the graph and is a tree
(i.e., it has no cycles).
Spanning tree
A spanning tree is a subgraph of a connected, undirected
graph that includes all the vertices of the graph and is a tree
(i.e., it has no cycles).
Minimum Spanning
tree
How it works?
• Start with any node.
• Add the smallest edge that
connects an unvisited node.
• Repeat until all nodes are
connected.
Example Problem
Cost of MST = 4 + 2 + 1 + 3 = 10 units.vv
Answer
Cost of MST = 4 + 2 + 1 + 3 = 10 units.
• Network design
• Clustering analysis
• Circuit Design
Where is it used?
1
3
8
9
7
5
0
6
2
1
4
9
6
3
7
5
2
0
8
Real World
Example
Designing a minimum-cost communications
network
Solution
Solution
Its Quiz Time!!!!
Let's see how much you've learned so far!
1
3
4 9
7
5
2
0
6
8
8
1
4
9
6
3
7
5
2
0
Thank
you!

Discrete Mathematics ( Dijkstra Algorithm & Prims Algorithm)