0% found this document useful (0 votes)
48 views39 pages

Dijkstras Algorithm

Dijkstra's algorithm is used to find the shortest path between any pair of nodes in a weighted graph. It works by iteratively selecting the unvisited node with the lowest distance from the source node and updating the distances of its neighboring nodes. The time complexity of Dijkstra's algorithm is O((V+E)logV) as it involves selecting the minimum distance node from a priority queue in each iteration and updating adjacent nodes. Dijkstra's algorithm finds the single source shortest path and can be applied to any directed or undirected weighted graph.
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)
48 views39 pages

Dijkstras Algorithm

Dijkstra's algorithm is used to find the shortest path between any pair of nodes in a weighted graph. It works by iteratively selecting the unvisited node with the lowest distance from the source node and updating the distances of its neighboring nodes. The time complexity of Dijkstra's algorithm is O((V+E)logV) as it involves selecting the minimum distance node from a priority queue in each iteration and updating adjacent nodes. Dijkstra's algorithm finds the single source shortest path and can be applied to any directed or undirected weighted graph.
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/ 39

Dijkstra's

algorithm
TEAM #2

Carreño Limones André


Cordero Martínez Ileana Angélica
Flores Medina Adán
García Soto Jean Carlo
Maldonado Escobedo Emmanuel Octavio
Mendoza Hernández Carlos Emiliano
Pineda Migranas Angel Uriel
Xinastle Rosas Elioth
Introduction
Shortest path algorithm

Short path algorithms are an important branch of support for the study of complex data
contained in graphs. Thanks to them we can determine paths within the structure of a
graph efficiently and quickly.

The shortest path algorithm is in one of the most important analysis modules of graph
algorithms. This is responsible for detecting within a graph which is the most efficient
route or the shortest distance path between a pair of vertices that make up a graph.
Within this category, the well-known Dijkstra algorithm stands out for its properties.
The shortest path problem is to find the shortest paths
between a given vertex v and all other vertices of the
graph.

Example:The miner's problem


Other algorithms

- K routes of Yen
- Minimum Weight Spanning
Tree
- A*
- Random Walk
Dijkstra
Edsger Wybe Dijkstra was born in 1930 in
Rotterdam, the Netherlands. He was the son of
Wybe Douwe Dijkstra and Brechtje Cornelia
Kruyper. He was a computer scientist,
programmer, software engineer, systems
scientist, and scientific essayist.

He received in 1972 the Turing Award for his


seminal contributions to the development of
structured programming languages.
What is Dijkstra’s Algorithm
Dijkstra's algorithm is an algorithm designed to find the shortest path between any pair of
nodes contained in a weighted graph. This algorithm is primarily used to solve the single
source shortest path problem, where the goal is to find the quickest route from a given vertex
to all other vertices in the graph, an example of this problem applied common daily
occurrences is network routing, GPS navigation or airline scheduling

Although very efficient this algorithm has some restrictions:

○ -Works only for connected weighted graphs


○ -Edges contained in the graph cannot contain a negative weight value
○ -in its simple version, it only provides the value of the shortest path
WHY IS IT IMPORTANT.
Dijkstra's algorithm importance lies in its efficiency and effectiveness in solving the
single-source shortest path problem. Beyond this, this algorithm is the foundation of various
other graph algorithms and data structures, for example, its a key component in algorithms
like A* (An algorithm highly used for pathing in games and robotics) or Bellman-Ford and
Floyd-Warshall, which manage the cases where Dijkstra's algorithm lacks an efficient solution.
BRIEF HISTORY BEHIND THE ALGORITHM
in 1959 Dr Edsger W. Dijkstra published a 3 page article by the title of "A note
on two problems in connection with graphs", In this article Dijkstra
presented two problems and his proposed solution for each one, the first
problem discussed how to create a three with minimum length for n nodes,
for the second problem, he proposed a solution on how to extract the
shortest path between a pair of vertices in a graph, this solution was the
one that put Dijkstra's name in almost every computer science course book,
as it is one of the cornerstones in graph theory and graph traversal.
Dijkstra’s Algorithm

5 steps

1. Create a priority queue for


the nodes in the graph and mark all
of them a as unvisited nodes.
2. Establish an initial node.

3. Set the distance of the starting


node to zero and the distance
of the other nodes to infinity.
4. Visit the neighboring nodes of the 17

current node and calculate the distance


to them. If the value of the distance is
smaller than the one already in that node,
it is updated by the smaller one. After
doing the node is marked as visited and
the neighbor nodes are enqueued.

5. The nodes in the priority queue


are dequeued and step 4 is repeated.
Dijkstra’s Algorithm

Once all the nodes are visited, the minimum route from the
initial node to the other nodes of the graph is obtained.

This can be applied to any directed


or undirected weighted graph.
Time complexity
O((V+E)logV)

The algorithm works by iteratively selecting the vertex with the smallest distance from the
source vertex and updating the cost of its neighboring vertices. The operation of selecting
the vertex with the smallest distance is typically implemented using a priority queue, which
has a time complexity of O(log V) for insertion and extraction of the minimum element. Since
this operation is performed for each vertex in the graph, the overall time complexity of
Dijkstra's algorithm becomes O(V log V).

In addition to the vertex selection, the algorithm also needs to update the value of the
shortest path found to any adjacent vertex of the current vertex, which takes O(1) time for
each edge in the graph. Since the total number of edges is E, the time complexity for this task
is O(E).
Dijkstra's algorithm pseudocode
Example
Previous distance of b Distance from a to b

Distance from a to itself


Dry-running Dijkstra’s algorithm
Find the shortest path from node C to every other node

Exercise and images taken from:


https://www.codingame.com/playgrounds/1608/shortest-paths-with-dijkstras-algorithm/dijkstras-algorithm
1. While Q is not empty, pop the node v, that is not already in S from Q with the
smallest dist(v), In the first run, source node s will be chosen. The array dist is
initialized.

Node Distance
v dist(v)

A ∞
B ∞
s (source) = C 0
D ∞
Q = {A, B, D, E}
E ∞
Current node = C

S = {}
2. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent
node u,

● If dist(v) + w(u,v) < dist(u), there is a new minimal distance found for u
● Otherwise, no updates are made to dist(u)

Node Distance
v dist(v)

A ∞
B 7
s (source) = C 0
D ∞
Q = {A, B, D, E}
E ∞
Current node = C

S = {}
2. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent
node u,

● If dist(v) + w(u,v) < dist(u), there is a new minimal distance found for u
● Otherwise, no updates are made to dist(u)

Node Distance
v dist(v)

A 1
B 7
s (source) = C 0
D ∞
Q = {A, B, D, E, F}
E ∞
Current node = C

S = {}
2. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent
node u,

● If dist(v) + w(u,v) < dist(u), there is a new minimal distance found for u
● Otherwise, no updates are made to dist(u)

Node Distance
v dist(v)

A 1
B 7
s (source) = C 0
D 2
Q = {A, B, D, E}
E ∞
Current node = C

S = {}
3. Add node v to indicate that v has been visited.

Node Distance
v dist(v)

A 1
B 7
s (source) = C 0
D 2
Q = {A, B, D, E}
E ∞
Current node = C

S = {C}
1. While Q is not empty, pop the node v, that is not already in S from Q with the smallest dist(v)

Node Distance
v dist(v)

A 1
B 7
s (source) = C 0
D 2
Q = {B, D, E}
E ∞
Current node = A

S = {C}
2. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent
node u,

● If dist(v) + w(u,v) < dist(u), there is a new minimal distance found for u
● Otherwise, no updates are made to dist(u)

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {B, D, E}
E ∞
Current node = A

S = {C}
3. Add node v to indicate that v has been visited.

1. While Q is not empty, pop the node v, that is not already in S from Q with the smallest dist(v)

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {B, E}
E ∞
Current node = D

S = {C, A}
2. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent
node u,

● If dist(v) + w(u,v) < dist(u), there is a new minimal distance found for u
● Otherwise, no updates are made to dist(u)

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {B, E}
E 9
Current node = D

S = {C, A}
3. Add node v to indicate that v has been visited.

1. While Q is not empty, pop the node v, that is not already in S from Q with the smallest dist(v)

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {E}
E 9
Current node = B

S = {C, A, D}
2. Update dist values of adjacent nodes of the current node v as follows: for each new adjacent
node u,

● If dist(v) + w(u,v) < dist(u), there is a new minimal distance found for u
● Otherwise, no updates are made to dist(u)

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {E}
E 5
Current node = B

S = {C, A, D}
3. Add node v to indicate that v has been visited.

1. While Q is not empty, pop the node v, that is not already in S from Q with the smallest dist(v)

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {}
E 5
Current node = E

S = {C, A, D, B}
The algorithm has visited all nodes in the graph and found the smallest distance to each node. The
array dist now contains the shortest path from source s to every other node.

Node Distance
v dist(v)

A 1
B 4
s (source) = C 0
D 2
Q = {}
E 5
Current node = E

S = {C, A, D, B, E}
Java
Implementation
APPLICATIONS

Public
Transportation
Networks
Network Gaming and
Routing Virtual Reality

Navigation
Systems Logistics and
Transportation
Network Routing Navigation systems

Network routing is the process of


selecting a path through one or Navigation systems use Dijkstra's
more networks. algorithm to calculate the optimal
route between two geographic locations,
Routing principles can be applied to any type taking into account factors such as
of network, from telephone networks to public distance, travel time, and road restrictions.
transportation.
Logistic and
Gaming transportation

In virtual reality games and Dijkstra's algorithm is used for


applications, Dijkstra's algorithm is delivery route optimization, fleet
used to generate optimal and planning and efficient supply chain
realistic paths for characters and management.
enemies in virtual environments.
Conclusions

The Dijkstra algorithm is a fundamental tools in graph theory and it works as basis for other
graphing algorithms and graphing data structures. It is an easy and efficient way to find the
shortest path on a weighted graph. It is also easy to implement on a programming language
and has a lot of useful applications.

However, it is important to use this or another shortest path search algorithm depending on
the characteristics of the case in which it is needed because it has some restrictions as the
inability to handle edges with negative weights.
Sources
● https://dl.ebooksworld.ir/books/Introduction.to.Algorithms.4th.Leiserson.Stein.Rivest.Cormen.MIT.
Press.9780262046305.EBooksWorld.ir.pdf
● https://stackoverflow.com/questions/26547816/understanding-time-complexity-calculation-for-dij
kstra-algorithm
● https://www.researchgate.net/publication/220426527_An_Interview_with_Edsger_W_Dijkstra

You might also like