You are on page 1of 22

School of Computing Science and Engineering

Course Code: BTCS2402 (PR) Course Name: Analysis & Design of Algorithms Lab

Dijkstra's algorithm for Shortest


Path Routing

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Single-Source Shortest
Course Code: BTCS2402 (PR)
Path Problem
Course Name: Design & Analysis of Algorithms Lab

Single-Source Shortest Path Problem - The problem


of finding shortest paths from a source vertex v to all
other vertices in the graph.

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra's algorithm
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

Dijkstra's algorithm - is a solution to the single-source


shortest path problem in graph theory. 
 
Works on both directed and undirected graphs. However, all
edges must have nonnegative weights.

Approach: Greedy

Input: Weighted graph G={E,V} and source vertex v∈V, such


that all edge weights are nonnegative
 
Output: Lengths of shortest paths (or the shortest paths
themselves) from a given source vertex v∈V to all other
vertices

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra's algorithm - Name:
Course Code: BTCS2402 (PR)
Course Pseudocode
Design & Analysis of Algorithms Lab

dist[s] ←0 (distance to source vertex is zero)


for  all v ∈ V–{s}
        do  dist[v] ←∞ (set all other distances to infinity)
S←∅ (S, the set of visited vertices is initially empty)
Q←V  (Q, the queue initially contains all vertices)              
while Q ≠∅ (while the queue is not empty)
do   u ← mindistance(Q,dist) (select the element of Q with the min. distance)
      S←S∪{u} (add u to list of visited vertices)
       for all v ∈ neighbors[u]
              do  if   dist[v] > dist[u] + w(u, v) (if new shortest path found)
                         then      d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra Animated Example
Course Code: BTCS2402 (PR)
Course Name: Design & Analysis of Algorithms Lab

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Dijkstra's AlgorithmCourse
- Why
Course Code: BTCS2402 (PR)
It Works
Name: Design & Analysis of Algorithms Lab

• As with all greedy algorithms, we need to make sure that


it is a correct algorithm (e.g., it always returns the right
solution if it is given correct input).

• A formal proof would take longer than this presentation,


but we can understand how the argument works intuitively.

• If you can’t sleep unless you see a proof, see the second
reference or ask us where you can find it.

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Applications of Dijkstra's
Course Code: BTCS2402 (PR)
Algorithm
Course Name: Design & Analysis of Algorithms Lab
- Traffic Information Systems are most prominent use 
- Mapping (Map Quest, Google Maps)
- Routing Systems

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Course Code: BTCS2402 (PR) Course Name: Design & Analysis of Algorithms Lab
•#include<stdio.h>
•#include<conio.h>
•#define INFINITY 9999
•#define MAX 10
• 
•void dijkstra(int G[MAX][MAX],int n,int startnode);
• 
•int main()
•{
•int G[MAX][MAX],i,j,n,u;
•printf("Enter no. of vertices:");
•scanf("%d",&n);
•printf("\nEnter the adjacency matrix:\n");
•for(i=0;i<n;i++)
•for(j=0;j<n;j++)
•scanf("%d",&G[i][j]);
•printf("\nEnter the starting node:");
•scanf("%d",&u);
•dijkstra(G,n,u);
•return 0;
•}

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Course Code: BTCS2402 (PR) Course Name: Design & Analysis of Algorithms Lab
•void dijkstra(int G[MAX][MAX],int n,int startnode)
•{
• 
•int cost[MAX][MAX],distance[MAX],pred[MAX];
•int visited[MAX],count,mindistance,nextnode,i,j;
•//pred[] stores the predecessor of each node
•//count gives the number of nodes seen so far
•//create the cost matrix
•for(i=0;i<n;i++)
•for(j=0;j<n;j++)
•if(G[i][j]==0)
•cost[i][j]=INFINITY;
•else
•cost[i][j]=G[i][j];
•//initialize pred[],distance[] and visited[]
•for(i=0;i<n;i++)
•{
•distance[i]=cost[startnode][i];
•pred[i]=startnode;
•visited[i]=0;
•}
•distance[startnode]=0;
•visited[startnode]=1;
•count=1;

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Course Code: BTCS2402 (PR) Course Name: Design & Analysis of Algorithms Lab
•while(count<n-1)
•{
•mindistance=INFINITY;
•//nextnode gives the node at minimum distance
•for(i=0;i<n;i++)
•if(distance[i]<mindistance&&!visited[i])
•{
•mindistance=distance[i];
•nextnode=i;
•}
•//check if a better path exists through nextnode
•visited[nextnode]=1;
•for(i=0;i<n;i++)
•if(!visited[i])
•if(mindistance+cost[nextnode][i]<distance[i])
•{
•distance[i]=mindistance+cost[nextnode][i];
•pred[i]=nextnode;
•}
•count++;
•}
• 

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
Course Code: BTCS2402 (PR) Course Name: Design & Analysis of Algorithms Lab
• //print the path and distance of each node
• for(i=0;i<n;i++)
• if(i!=startnode)
•{
• printf("\nDistance of node%d=%d",i,distance[i]);
• printf("\nPath=%d",i);
• j=i;
• do
•{
• j=pred[j];
• printf("<-%d",j);
• }while(j!=startnode);
• }}

       Faculty Name: Dr.Gambhir Singh Name: B.Tech CSE         


School of Computing Science and Engineering
References
Course Code: BTCS2402 (PR) Course Name: Design & Analysis of Algorithms Lab

• Dijkstra’s original paper:


E. W. Dijkstra. (1959) A Note on Two Problems in Connection with Graphs. Numerische Mathematik, 1. 269-
271.
• MIT OpenCourseware, 6.046J Introduction to Algorithms.
< http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-046JFall-2005/CourseHome/
> Accessed 4/25/09
• Meyers, L.A. (2007) Contact network epidemiology: Bond percolation applied to infectious disease prediction
and control. Bulletin of the American Mathematical Society 44: 63-86.
• Department of Mathematics, University of Melbourne. Dijkstra’s Algorithm.
<http://www.ms.unimelb.edu.au/~moshe/620-261/dijkstra/dijkstra.html > Accessed 4/25/09

You might also like