Daa Theory
Daa Theory
Case Study on
A prototype implementation for real life
application of
DIJKSTRA’S ALGORITHM
GROUP MEMBERS
Page |2
ACKNOWLEDGEMENTS
ABSTRACT
Dijkstra's algorithm is an algorithm for finding the shortest
paths between nodes in a graph, which may represent, for
Page |4
LIST OF CONTENTS
Page |5
1) INTRODUCTION………………………………….. 8
3) ALGORITHM……………………………………... 13
4) SOURCE CODE…………………………………… 14
8) CONCLUSION……………………………………... 22
9) REFERENCES............................................................. 23
INTRODUCTION
Dijkstra’s algorithm is mainly used to find the shortest path from a
starting node/point to the target node/point in a weighted graph.
Page |6
A little insight into the algorithm with a simple example is that you
may imagine you are planning to drive back from one state
(California) to your state (Arizona), and definitely want to go thru
the possible shortest path to minimize spending on petrol and save
your time. Another example, you want to come back from school to
home in the shortest possible way. You try to open a map service to
look up the way, it is very likely map service engineers might have
used Dijkstra’s algorithm and other algorithms to find the possible
shortest path. In Dijkstra’s algorithm, that means it tries to find the
shortest paths and build the shortest path tree by avoiding the
edges/ways in our example with largest / longest weights as much as
possible by a greedy approach.
Before we really dive into the details, please try to find the shortest
path from home to school by a greedy approach in the given picture
below. Please make sure your answer is correct and try to analyse
and think about how you have come up with the solution.
Dijkstra's algorithm
Problem statement in shortest path :
Consider the map below. The cities have been selected
and marked from alphabets A to F and every edge has a
Page |7
return cost
P a g e | 12
In a very general and broad case, the time complexity is O(|E| + |V|
²) and space complexity is O(|V|) for the algorithm. You can do
research more on edge cases and the application of the Dijkstra
algorithm for condensed graphs on your own.
ALGORITHM
1) Create a set sptSet (shortest path tree set) that keeps track of vertices included in the
shortest-path tree, i.e., whose minimum distance from the source is calculated and
finalized. Initially, this set is empty.
2) Assign a distance value to all vertices in the input graph. Initialize all distance values as
INFINITE. Assign distance value as 0 for the source vertex so that it is picked first.
3) While sptSet doesn’t include all vertices
….a) Pick a vertex u which is not there in sptSet and has a minimum distance value.
….b) Include u to sptSet.
….c) Update distance value of all adjacent vertices of u. To update the distance values,
iterate through all adjacent vertices. For every adjacent vertex v, if the sum of distance
value of u (from source) and weight of edge u-v, is less than the distance value of v, then
update the distance value of v.
SOURCE CODE
#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10
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;
}
int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
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];
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;
while(count<n-1)
{
mindistance=INFINITY;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
P a g e | 14
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
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);
}
}
Output:
P a g e | 15
Mapping:-
P a g e | 16
When the social networking graph is very small, you can use
standard Dijkstra’s algorithm to find the shortest paths, and
however, when the graph is becoming bigger and bigger, the
standard algorithm takes a few several seconds to count and is very
inefficient.
Telephone Network:-
You may wonder how the algorithm can be applied to this problem.
If we imagine a city to be a graph, the vertices represent the
switching stations, and the edges represent the transmission lines
and the weight of the edges represents bw. So as you can see it can
fall into the category of shortest distance problem, for
which Dijkstra is very applicable.
Flight Agenda:-
Thank you so much for reading, and I hope you learned at least a
little bit of the usefulness of Dijkstra’s algorithm, and where it can
be applied.
2. It can be used to calculate the shortest path between a single node to all
other nodes and a single source node to a single destination node by
stopping the algorithm once the shortest distance is achieved for the
destination node.
3. It only works for directed-, weighted graphs and all edges should have non-
negative values.
P a g e | 21
CONCLUSION
REFERENCES
https://en.wikipedia.org/wiki/Dijkstra
%27s_algorithm
https://www.programiz.com/dsa/dijkstra-
algorithm
“Introduction to Algorithms" by Cormen, Leiserson, Rivest, and
Stein.
https://dl.acm.org/
https://www.researchgate.net/
https://www.interviewbit.com/