You are on page 1of 23

Page |1

CV RAMAN GLOBAL UNIVERSITY


BHUBANESHWAR,ODISHA

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Design And Analysis of Algorithm

Case Study on
A prototype implementation for real life
application of
DIJKSTRA’S ALGORITHM
GROUP MEMBERS
Page |2

NAME REGISTRATION GROUP BRANCH MAIL-ID


NUMBER

PRINCE KUMAR 2201020754 5 CSE(DATA 2201020754@cgu-


SCIENCE) odisha.ac.in

GYANA RANJAN 2201020755 5 CSE(DATA 2201020755@cgu-


SUNDARAY SCIENCE) odisha.ac.in

RAHUL KUMAR 2201020779 5 CSE(DATA 2201020779@cgu-


MISHRA SCIENCE) odisha.ac.in

T.CHARLIE PRIYA 2201020832 5 CSE(DATA 2201020832@cgu-


SCIENCE) odisha.ac.in
Page |3

ACKNOWLEDGEMENTS

We are pleased to acknowledge Dr. MAMATA WAGH for


their invaluable guidance during the course of this project
work. We extend our sincere thanks for their immeasurable
support during our project work.
Above all,we would like to thank the GREAT ALMIGHTY
for always having his blessing on us.

ABSTRACT
 Dijkstra's algorithm is an algorithm for finding the shortest
paths between nodes in a graph, which may represent, for
Page |4

example, road networks. It was conceived by computer


scientist Edsger W. Dijkstra in 1956 and published three years
later.
 The Dijkstra algorithm uses labels that are positive integers or
real numbers, which are totally ordered. It can be generalized
to use any labels that are partially ordered, provided the
subsequent labels (a subsequent label is produced when
traversing an edge) are monotonically non-decreasing. This
generalization is called the generic Dijkstra shortest-path
algorithm.
 Dijkstra thought about the shortest path problem when
working at the Mathematical Center in Amsterdam in 1956 as a
programmer to demonstrate the capabilities of a new
computer called ARMAC. His objective was to choose both a
problem and a solution that non-computing people could
understand. He designed the shortest path algorithm and later
implemented it for ARMAC for a slightly simplified
transportation map of 64 cities in the Netherlands (64, so that 6
bits would be sufficient to encode the city number). A year
later, he came across another problem from hardware
engineers working on the institute's next computer: minimize
the amount of wire needed to connect the pins on the back
panel of the machine. As a solution, he re-discovered the
algorithm known as Prim's minimal spanning tree
algorithm (known earlier to Jarník, and also rediscovered
by Prim). Dijkstra published the algorithm in 1959, two years
after Prim and 29 years after Jarník .

LIST OF CONTENTS
Page |5

1) INTRODUCTION………………………………….. 8

2) EXAMPLE OF DIJKSTRA’S ALGORITHM…….. 9

3) ALGORITHM……………………………………... 13

4) SOURCE CODE…………………………………… 14

5) APPLICATION OF DIJKSTRA’S ALGORITHM… 17

6) ADVANTAGES OF DIJKSTRA’S ALGORITHM... 20

7) LIMITATIONS OF DIJKSTRA’S ALGORITHM..... 21

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

When Dijkstra’s algorithm is applied, it creates a tree of the shortest


path from a starting vertex/source to all the other nodes in the
graph. A few constraints for the application of Dijkstra’s algorithm,
the graph must be a weighted graph with non-negative weighted
edges. It can be either a directed or undirected graph.

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

cost associated with it.


We need to travel from Bengaluru to all other places and
we have to identify what are the shortest paths with
minimal cost from Bengaluru to other destinations.

Solution for shortest path algorithm :


1. Convert problem to its graph equivalent.
Upon conversion, we get the below representation. Note
that the graph is weighted and undirected. All the cities
have been replaced by the alphabets associated with it
and the edges have the cost value (to go from one node to
Page |8

other) displayed on it.

2. Assign cost to vertices.


Assign cost of 0 to source vertex and ∞ (Infinity) to all
other vertices as shown in the image below.
Maintain a list of unvisited vertices. Add all the vertices to
the unvisted list.
Page |9

3. Calculate minimum cost for neighbors of selected


source.
For each neighbor A, C and D of source vertex selected
(B), calculate the cost associated to reach them from B
using the formula. Once this is done, mark the source
vertex as visited (The vertex has been changed to blue to
indicate visited).
Minimum(current cost of neighbor vertex, cost(B)
+edge_value(neighbor,B))
1. For neighbor A: cost = Minimum(∞∞ , 0+3) = 3
2. For neighbor C: cost = Minimum(∞∞ , 0+1) = 1
3. For neighbor D: cost = Minimum(∞∞ , 0+6) = 6
P a g e | 10

4. Select next vertex with smallest cost from the unvisited


list.
Choose the unvisited vertex with minimum cost (here, it
would be C) and consider all its unvisited neighbors (A,E
and D) and calculate the minimum cost for them.
Once this is done, mark C as visited.
Minimum(current cost of neighbor vertex, cost(C)
+edge_value(neighbor,C))

1. For neighbor A: cost = Minimum(3 , 1+2) = 3


2. For neighbor E: cost = Minimum(∞∞, 1+4) = 5
3. For neighbor D: cost = Minimum(6 , 1+4) = 5
Observe that the cost value of node D is updated by the
new minimum cost calculated.

5. Repeat step 4 for all the remaining unvisited nodes.


Repeat step 4 until there are no unvisited nodes left. At
the end of the execution, we will know the shortest paths
from the source vertex B to all the other vertices. The
final state
of the graph would be like below.
P a g e | 11

Pseudo Code of Dijkstra algorithm


Dijkstra_Algorithm(source, G):
"""
parameters: source node--> source, graph--> G
return: List of cost from source to all other nodes--> cost
"""
unvisited_list = [] // List of unvisited verticesvertices
cost = []
cost[source] = 0 // Distance (cost) from source to source will be
0
for each vertex v in G: // Assign cost as INFINITY to all vertices
if v ≠ source
cost[v] = INFINITY
add v to unvisited_list // All nodes pushed to unvisited_list
initially

while unvisited_list is not empty: // Main loop


v = vertex in unvisited_list with min cost[v] // v is the source node
for first iteration
remove v from unvisited_list // Marking node as
visited

for each neighbor u of v: // Assign shorter path cost to


neigbour u
cost_value = Min( cost[u], cost[v] + edge_cost(v, u)]
cost[u] = cost_value // Update cost of
vertex u

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

void dijkstra(int G[MAX][MAX],int n,int startnode);


P a g e | 13

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;
}

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;

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

……I have spent writing about the actual details of the


algorithm Dijkstra more than I actually expected. From now
onward, I will focus on the practical application of Dijkstra’s
algorithm in the real world.

Application of Dijkstra’s Algorithm


The application of Dijkstra’s algorithm has been used widely in
several different aspects of engineering since its first introduction
by computer scientist Edsger W. Dijkstra in 1956.

Mapping:-
P a g e | 16

If you have ever tried to find distance or a path from


one point/location to another point/destination, let us say from one
city to another, or from your location to the nearest gas station, it is
very likely that you have encountered the shortest path problem. In
math terms, it is finding the shortest path between two points in the
graph, for which Dijkstra’s algorithm is very likely to be applicable.

Another example would be that the firefighter department wants to


develop a system that finds the shortest distance between the closest
firefighter department and the house being burnt to avoid the extra
delay. Or logistics companies want to develop a system that finds the
shortest distance between the warehouse and destinations to avoid
extra spending and time. For such systems’ development, Dijkstra’s
algorithm is very applicable.

Social Networking Application:-

In many social networking applications, it is very common to


suggest the lists of the friends that a particular user may know. How
do you think many social media companies implement this feature
efficiently, especially when the system has over a billion users. The
standard Dijkstra algorithm can be applied using the shortest path
between users measured in handshakes or connections between two
users.
P a g e | 17

As far as I know, many social networking applications are based on


six degrees of separation that is the distance measured in the
number of handshakes between any two users and it is quite small.
Even if the average number of friends for users is not big, the
problem of finding the shortest paths between users is very complex
from the view of algorithm complexity.

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.

However, I believe that with a few minor tweaks of Dijkstra‘s


algorithm, the shortest distance between any users in social
networking graph can be found very efficiently. I won’t go into
details since it takes really long explanation. You can do research on
your own. By the way, if you do research on it, I am sure it is going to
be a very interesting research topic.

Telephone Network:-

In a telephone network, each line has a bandwidth, bw. We would


like to route the phone call thru the highest bw. The bandwidth of
the transmission line is the highest frequency that that line can
support. Generally, if the frequency of the signal is higher in a
certain line, the signal is reduced by that line. If we are transmitted a
P a g e | 18

digital signal then the bw represents the highest frequency or the


fastest the signal can change from 0 to 0. Bandwidth represents the
amount of information that can be transmitted by the line.

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:-

A travel agent requests software for making an agenda of flights for


clients. The agent has access to a database with all airports and
flights. Besides the flight number, origin airport, and destination,
the flights have departure and arrival times. Specifically, the agent
wants to determine the earliest arrival time for the destination given
an origin airport and start time.

Here we can assume that graph is a directed graph with certain


directions. The vertices represent the airports, and directed edges
represent edges with two weights, departure time and arrival
time. With a slight modification of the Dijkstra algorithm, the
solution can easily be found.

In the practical world, we can find many different practical


applications of the Dijkstra algorithm, I have mentioned only a few
of them here. I would like to stop giving the long list here since it is
P a g e | 19

getting a little bit boring. In the next article in a few days, I am


planning to go deep into the implementation of the Flight Agenda
application using Dijkstra’s algorithm.

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.

Advantages of Dijkstra’s Algorithm

Discussing some advantages of Dijkstra’s algorithm;

1. One of the main advantages of it is its little complexity which is almost


linear.
P a g e | 20

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

Limitations of Dijkstra’s Algorithm

1. It may not produce accurate output when it is independent on graph having


negative weight.
2. As Dijkstra’s algorithm follows greedy method , it picks the closest node at
each time , which gives the shortest path , but it not might be the fastest way
to solve the problem in computer.
3. Dijkstra's algorithm can consume a considerable amount of memory,
especially for large graphs, because it needs to store information about all
vertices and edges in the graph.
P a g e | 22

CONCLUSION

Among many, we have discussed the Dijkstra algorithm used for


finding the shortest path, however, one of the obstacles while
implementing the algorithm on the internet is to provide a full
representation of the graph to execute the algorithm as an individual
router has a complete outline for all the routers on the internet. We
have seen

 Graphs are used to display connections between


objects, entities or people, they have the main
elements: Nodes and edges.
 Dijkstra’s algorithm enables determining the shortest
path amid one selected node and each other node in a
graph.
 And finally, the steps involved in deploying Dijkstra’s
algorithm.
P a g e | 23

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/

You might also like