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

Dijkstra's Algorithm

Dijkstra's algorithm finds the shortest path between any two vertices in a graph. It uses a greedy approach to find the single source shortest path from a starting vertex to all other vertices. The algorithm works for both directed and undirected graphs, as long as all edge weights are non-negative. It maintains a priority queue to iteratively select the vertex with the minimum distance from the source and updates the distances of its neighbors.

Uploaded by

harshini alapati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Dijkstra's Algorithm

Dijkstra's algorithm finds the shortest path between any two vertices in a graph. It uses a greedy approach to find the single source shortest path from a starting vertex to all other vertices. The algorithm works for both directed and undirected graphs, as long as all edge weights are non-negative. It maintains a priority queue to iteratively select the vertex with the minimum distance from the source and updates the distances of its neighbors.

Uploaded by

harshini alapati
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Dijkstra’s Algorithm

A.Harshini K.Sanjana
221IT006 221IT041
Dijkstra’s:

Dijkstra's algorithm, named after Dutchcomputer scientist EdsgerW. Dijkstra.

It Allows us to Find the shortest path between any two vertices of a graph.

Works on both Directed and Undirected graph.However,All edges must have


Non Negative Weights.

It differs from the minimum spanning tree because the shortest distance
between two vertices might not include all vertices of the graph.
HOW IT WORKS?
Approach:Greedy (To find single source shortest path)

Input:weighted graph G={E,V} and source vertex v belongs to V,such that


all edge weights are non negative

Output:Lengths of shortest path from a given source to all vertices

Time complexity:O(V^2)

Space Complexity:O(v)
Example:
A student wants to go from home to school in the
shortest possible way. She knows some roads are
heavily congested and difficult to use. In Dijkstra's
algorithm, this means the edge has a large
weight/cost and the shortest path tree found by
the algorithm will try to avoid edges with larger
weights. If the student looks up directions using a
map service, it is likely they may use Dijkstra's
algorithm.

For Example: The shortest path, which could be


found using Dijkstra's algorithm, is
1 (HOME)-->3-->6-->5(School)
Pseudocode
Add a littfunction Dijkstra(Graph, source):
dist[] // Initialize distances from source to all vertices
parent[] // Initialize array to track shortest path parent nodes
priority_queue pq // Create a priority queue to manage vertices
// Initialize distances and parent nodes

for each vertex v in Graph:


dist[v] = INFINITY
parent[v] = NULL
dist[source] = 0
pq.push({source, 0}) // Push the source vertex with distance 0 into the priority queue
while pq is not empty:
u = pq.pop() // Extract vertex with minimum distance
for each neighbor v of u:
alt = dist[u] + weight(u, v)
if alt < dist[v]:
dist[v] = alt
parent[v] = u
pq.push({v, dist[v]})

return dist[] // Return the array containing distances from source to all vertices
Thank You...

You might also like