0% found this document useful (0 votes)
2 views5 pages

Algorithims Section 10 Dijkstras Algorithm

Dijkstra's Algorithm is designed to find the shortest path in a weighted graph with non-negative edge weights, starting from a source vertex to all other vertices. It employs a combination of Greedy and Dynamic Programming approaches to update distances based on the minimum cost from the source. The algorithm iteratively selects the nearest unvisited vertex and updates the distances of its adjacent vertices until all vertices are visited.

Uploaded by

nh696831
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views5 pages

Algorithims Section 10 Dijkstras Algorithm

Dijkstra's Algorithm is designed to find the shortest path in a weighted graph with non-negative edge weights, starting from a source vertex to all other vertices. It employs a combination of Greedy and Dynamic Programming approaches to update distances based on the minimum cost from the source. The algorithm iteratively selects the nearest unvisited vertex and updates the distances of its adjacent vertices until all vertices are visited.

Uploaded by

nh696831
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

CS Algorithm Dijkstra's Section

Dijkstra's Algorithm
This algorithm aims to find the shortest-path in a directed or undirected graph
with non-negative edge weights.
Problem Statement
Given a weighted graph G, the objective is to find the shortest path from a given
source vertex to all other vertices of G. The graph has the following
characteristics-
• Set of vertices V
• Set of weighted edges E such that (q,r) denotes an edge between vertices q
and r and cost(q,r) denotes its weight
Dijkstra's Algorithm:
• This is a single-source shortest path algorithm and aims to find solution to
the given problem statement
• This algorithm works for both directed and undirected graphs
• It works only for connected graphs
• The graph should not contain negative edge weights
• The algorithm predominantly follows Greedy approach for finding locally
optimal solution. But, it also uses Dynamic Programming approach for
building globally optimal solution, since the previous solutions are stored
and further added to get final distances from the source vertex
• The main logic of this algorithm is basedon the following formula-
dist[r]=min(dist[r], dist[q]+cost[q][r])
This formula states that distance vertex r, which is adjacent to vertex q, will be
updated if and only if the value of dist[q]+cost[q][r] is less than dist[r].
• dist is a 1-D array which, at every step, keeps track of the shortest distance
from source vertex to all other vertices, and
• cost is a 2-D array, representing the cost adjacency matrix for the graph
• This formula uses both Greedy and Dynamic approaches. The Greedy
approach is used for finding the minimum distance value, whereas the
Dynamic approach is used for combining the previous solutions (dist[q] is
already calculated and is used to calculate dist[r])
Algorithm-
Input Data-
• Cost Adjacency Matrix for Graph G, say cost
• Source vertex, say s
Output Data-
• Spanning tree having shortest path from s to all other vertices in G
Following are the steps used for finding the solution-
Step 1; Set dist[s]=0, S=ϕ // s is the source vertex and S is a 1-D array having all
the visited vertices
Step 2: For all nodes v except s, set dist[v]= ∞
Step 3: find q not in S such that dist[q] is minimum // vertex q should not be
visited
Step 4: add q to S // add vertex q to S since it has now been visited
Step 5: update dist[r] for all r adjacent to q such that r is not in S //vertex r should
not be visited dist[r]=min(dist[r], dist[q]+cost[q][r]) //Greedy and Dynamic
approach
Step 6: Repeat Steps 3 to 5 until all the nodes are in S // repeat till all the vertices
have been visited
Step 7: Print array dist having shortest path from the source vertex u to all other
vertices
Step 8: Exit
example-

Given the above weighted and connected graph and source vertex s, following
steps are used for finding the tree representing shortest path between s and all
other vertices-
Step A- Initialize the distance array (dist) using the following steps of algorithm –
• Step 1- Set dist[s]=0, S=ϕ // u is the source vertex and S is a 1-D array
having all the visited vertices
• Step 2- For all nodes v except s, set dist[v]= ∞
Set of visited vertices (S) S A B C D

0 ∞ ∞ ∞ ∞

Step B- a)Choose the source vertex s as dist[s] is minimum and s is not in S.


Step 3- find q not in S such that dist[q] is minimum // vertex should not be visited
Visit s by adding it to S
Step 4- add q to S // add vertex q to S since it has now been visited
Step c) For all adjacent vertices of s which have not been visited yet (are not in S)
i.e A and C, update the distance array using the following steps of algorithm -
Step 5- update dist[r] for all r adjacent to q such that r is not in S //vertex r should
not be visited dist[r]=min(dist[r], dist[q]+cost[q][r]) //Greedy and Dynamic
approach
dist[A]= min(dist[A], dist[s]+cost(s, A)) = min(∞, 0+9) = 9
dist[C] = min(dist[C], dist[s]+cost(s, C)) = min(∞, 0+5) = 5
Thus dist[] gets updated as follows-
Set of visited vertices (S) S A B C D
[s] 0 9 ∞ 5 ∞
Step C- Repeat Step B by
a. Choosing and visiting vertex C since it has not been visited (not in S) and
dist[C] is minimum
b. Updating the distance array for adjacent vertices of C i.e. A, B and D
Step 6- Repeat Steps 3 to 5 until all the nodes are in S
dist[A]=min(dist[A], dist[C]+cost(C,A)) = min(9, 5+2)= 7
dist[B]= min(dist[B], dist[C]+cost(C,B)) = min(∞, 5+9)= 14
dist[D]= min(dist[D], dist[C]+cost(C,D))= min((∞,5+4)=9
This updates dist[] as follows-
Set of visited vertices (S) S A B C D
[s] 0 9 ∞ 5 ∞
[s,C] 0 7 14 5 9
Continuing on similar lines, Step B gets repeated till all the vertices are visited
(added to S). dist[] also gets updated in every iteration, resulting in the following –
Set of visited vertices (S) S A B C D
[s] 0 9 ∞ 5 ∞
[s,C] 0 7 14 5 9
[s, C, A] 0 7 8 5 9
[s, C, A, B] 0 7 8 5 9
[s, C, A, B, D] 0 7 8 5 9
The last updation of dist[] gives the shortest path values from s to all other
vertices
The resultant shortest path spanning tree for the given graph is as follows-

Note-
• There can be multiple shortest path spanning trees for the same graph
depending on the source vertex
Djikstra's algorithm pseudocode
1. function dijkstra(G, S)
2. for each vertex V in G
3. distance[V] <- infinite
4. previous[V] <- NULL
5. If V != S, add V to Priority Queue Q
6. distance[S] <- 0
7. while Q IS NOT EMPTY
8. U <- Extract MIN from Q
9. for each unvisited neighbour V of U
a. tempDistance <- distance[U] + edge_weight(U, V)
b. if tempDistance < distance[V]
c. distance[V] <- tempDistance
d. previous[V] <- U
10. return distance[], previous[]

You might also like