Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Data Structures Mini Project


Project Title: Dijkstra’s Algorithm

Presented By

Mr. Ajith K USN: 1BY19IS013


Mr. Akul Srinidhi USN: 1BY19IS019
Mr. D. S. Nesar Reddy USN: 1BY19IS047
Mr. Gagan Kumar D. K USN: 1BY19IS058

Under the guidance of


Mrs. Mahalakshmi S, Associate Professor
Dept. of ISE, BMSIT&M
05-02-2021 1
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Introduction
• The algorithm was developed by a Dutch computer scientist Edsger W. Dijkstra in 1956.

• It is used to find the shortest path between a node/vertex (source node) to any (or every)
other nodes/vertices (destination nodes) in a graph.

• A graph is basically an interconnection of nodes connected by edges.

• This algorithm is sometimes referred to as Single Source Shortest Path Algorithm due
to its nature of implementation.

05-02-2021 2
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Basics of Dijkstra’s Algorithm


• Dijkstra's Algorithm basically starts at the node that you choose (the source node) and it
analyzes the graph to find the shortest path between that node and all the other nodes in
the graph.

• The algorithm keeps track of the currently known shortest distance from each node to the
source node and it updates these values if it finds a shorter path.

• Once the algorithm has found the shortest path between the source node and another
node, that node is marked as "visited" and added to the path.

• The process continues until all the nodes in the graph have been added to the path. This
way, we have a path that connects the source node to all other nodes following the
shortest path possible to reach each node.

05-02-2021 3
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

• Dijkstra's Algorithm can only work with graphs that have positive weights. This is
because, during the process, the weights of the edges have to be added to find the
shortest path.

• If there is a negative weight in the graph, then the algorithm will not work properly.
Once a node has been marked as "visited", the current path to that node is marked as the
shortest path to reach that node. And negative weights can alter this if the total weight
can be decremented after this step has occurred.

• Dijkstra’s Algorithm can be used for undirected graphs as well as directed graph.

05-02-2021 4
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Explanation
• In order to find the shortest path, Dijkstra’s algorithm mainly allocates a “cost” value
taken to reach the destination vertex from the source vertex. The “cost” can be mapped
to disance, money or time taken to reach from source to a destination.

05-02-2021 5
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Steps to be followed for solving using Dijkstra’s algorithm:

1. Convert any problem to its graph equivalent representation.

2. Maintain a list of unvisited vertices. Assign a vertex as “source” and also allocate a
maximum possible cost (infinity) to every other vertex. The cost of the source to itself
will be zero as it actually takes nothing to go to itself.

3. In every step of the algorithm, it tries to minimize the cost for each vertex.

4. For every unvisited neighbor (V2, V3) of the current vertex (V1) calculate the new cost
from V1.

5. The new cost of V2 is calculated as :


Minimum( existing cost of V2 , (sum of cost of V1 + the cost of edge from V1 to V2) )

6. When all the neighbors of the current node are visited and cost has been calculated,
mark the current node V1 as visited and remove it from the unvisited list.

7. Select next vertex with smallest cost from the unvisited list and repeat from step 4.
6
8. The algorithm finally ends when there are no unvisited nodes left.
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Example
Consider the map below. The cities have been selected and marked from alphabets A
to F and every edge has a 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.

4
4

05-02-2021 7
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
1. Convert problem to its graph equivalent.
Upon conversion, we get the below representation. 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
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.
Maintain a list of unvisited vertices. Add all the vertices to
the unvisted .

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
Minimum(current cost of neighbor vertex,
cost(B)+edge_value(neighbor,B)) For neighbor A: cost =
Minimum(∞ , 0+3) = 3
For neighbor C: cost = Minimum(∞ , 0+1) = 1 8
For neighbor D: cost = Minimum(∞ , 0+6) = 6
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
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)) For neighbor A: cost =
Minimum(3 , 1+2) = 3
For neighbor E: cost = Minimum(∞, 1+4) = 5
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.

05-02-2021 9
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

Applications
• IP routing to find Open shortest Path First: Open Shortest Path First (OSPF) is a link-
state routing protocol that is used to find the best path between the source and the
destination router using its own Shortest Path First. Dijkstra’s algorithm is widely used
in the routing protocols required by the routers to update their forwarding table. The
algorithm provides the shortest cost path from the source router to other routers in the
network.

• Social Networking Applications: In many applications we might have seen the app
suggests the list of friends that a particular user may know. The Dijkstra algorithm can
be applied using the shortest path between users measured through connections among
them.

• It is also used by Geographic Information System (GIS), such as Google


Maps Dijkstra’s Algorithm is used to find the minimum distance between two locations
along the path.

05-02-2021 10
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

• Robotic Path: The drones/robots which are automated and are used to deliver the
packages to a specific location or used for a task are loaded with this algorithm module
so that when the source and destination is known, the robot/drone moves in the ordered
direction by following the shortest path to keep delivering the package in a minimum
amount of time.

05-02-2021 11
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT
Conclusion
Dijkstra’s Algorithm is basically used find the shortest path between a given node (which
is called the "source node") and all other nodes in a graph.

05-02-2021 12
BMS INSTITUTE OF TECHNOLOGY AND MANAGEMENT

05-02-2021 13

You might also like