0% found this document useful (0 votes)
26 views

Dijkstra's Algorithm

The document explains Dijkstra's algorithm for finding the shortest path between nodes in a graph. It provides an example of running Dijkstra's algorithm on a directed graph to find the shortest path from vertex s to z and then back from z to s. Dijkstra's algorithm works by keeping track of the shortest known distances from the source node to all other nodes and updating these values if shorter paths are found.

Uploaded by

Bbaraka Alaizer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Dijkstra's Algorithm

The document explains Dijkstra's algorithm for finding the shortest path between nodes in a graph. It provides an example of running Dijkstra's algorithm on a directed graph to find the shortest path from vertex s to z and then back from z to s. Dijkstra's algorithm works by keeping track of the shortest known distances from the source node to all other nodes and updating these values if shorter paths are found.

Uploaded by

Bbaraka Alaizer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

MSIT 5214-01 Algorithms

Title: Dijkstra’s algorithm

Course Instructor: Dr. Mudasir Ashraf

(Oct 19, 2022)


Introduction

Dijkstra's Algorithm begins at the node you specify (the source node) and analyses the

graph to find the shortest path between that node and all other nodes in the graph.

It keeps track of the shortest known distance from each node to the source node and updates

these values if a short path is discovered. When the algorithm finds the shortest path between

the source and another node, it marks that node as visited and adds it to the path. The process

is repeated until all the nodes in the graph are added to the path. In this process, a path

connects the source node to all other nodes by taking the shortest possible path to each node

(Estefania, 2020).

Dijkstra's Algorithm can only be applied to graphs with positive weights. That the

weights of the edges must be added during the process to find the shortest path. If the graph

contains a negative weight, the algorithm will not function properly. When a node is marked

as visited, the current path to that node becomes the shortest path to that node. Negative

weights can change this if the total weight can be decremented after this step (Estefania,

2020).

Write down Dijkstra’s algorithm. Run Dijkstra’s algorithm on following directed

graph. Illustrate with all steps taken in the algorithm by drawing graphs and showing

outcome as explained in the video. Find the shortest path from vertex s to vertex z and

then back from z to s.

(Fiset, 2016)
Dijkstra's Algorithm finds the shortest path in a graph between a given node (called

the "source node") and all other nodes. The weights of the edges are used by this algorithm to

find the path that minimizes the total distance (weight) between the source node and all other

nodes (Fiset, 2016).

This algorithm's main logic is based on below formula

f(dist(u, v)+cost(u))< dist(v), and

dist(u)=dist(u, v)+cost(u),

here u and v are the vertices cost is the value of the vertex, dist is the distance between two

vertices (Fiset, 2016).

From the given graph the shortest path from vertex s to vertex z

Dijkstra's Algorithm will find the shortest path from the vertices S to vertices Z in the

graph. Dijkstra's Algorithm will also include the shortest distance from the S vertex to Z

vertex (Fiset, 2016).

Step1: The shortest path from the source vertex s to the source

vertex s is 0, because source vertex is 0 and distance to all other

vertices from s is ∞ (infinity).

From the Dijkstra's Algorithm principle

f(dist(u, v)+cost(u))< dist(v), and

dist(u)=dist(u, v)+cost(u)

f(dists((0, 0))+0)<0 and dist(s(0))=dist(0, 0)+cost(0)


Shortest distance between vertices s to s is 0.

vertices s (0) t(3) x(9) y(5) z(11)

distance 0, 0

Shortest 0

distance

Visited

vertices

(Fiset, 2016)

Step2: The path between the source vertex s to the next nearby unvisited vertex is y. The

shortest distance form vertices s(0) to y(5) is

f(dist(u, v)+cost(u))< dist(v), and

dist(u)=dist(u, v)+cost(u).

s(0) shortest distance is 0

y(5) distance is 51, so

dist(s(0), y(5)) =0+5 = 5

So 5< ∞, 5 is less than ∞ and

Shortest distance from s(0) to y(5) is 5.


vertices s (0) t(3) x(9) y(5) z(11)

distance 0, 0 0, 5

Shortest 0 5

distance

Visited S s

vertices

(Fiset, 2016)

Step3: The path between the vertex y to the next nearby unvisited vertex is t, the path

between the vertex y to the next nearby unvisited vertex is x, and the path between the vertex

y to the next nearby unvisited vertex is z.

The shortest distance form vertices y(5) to t(3) is

dist(u)=dist(u, v)+cost(u).

f(dist(u, v)+cost(u))< dist(v), and

y(5) shortest distance is 5

t(3) distance is 1, so

dist(y(5), t(3))=5+1=6

So 6 < ∞, 6 is lessthan ∞ and

Shortest distance from s to y(5) to t(3) is 6.


vertices s (0) t(3) x(9) y(5) z(11)

distance 0, 0 5, 1 0, 5

Shortest 0 6 5

distance

Visited S y s

vertices

(Fiset, 2016)

The path between the vertex y to the next nearby unvisited vertex is t. The shortest distance

form vertices y(5) to x(9) is

dist(u)=dist(u, v)+cost(u).

f(dist(u, v)+cost(u))< dist(v), and

y(5) shortest distance is 5

x(9) distance is 4, so

dist(y(5), x(9))=5+4=9

So 9 < ∞, 9 is lessthan ∞ and

Shortest distance from s to y(5) to x(9) is 9.

vertices s (0) t(3) x(9) y(5) z(11)

distance 0, 0 5 ,1 5, 4 0, 5
vertices s (0) t(3) x(9) y(5) z(11)

Shortest 0 6 9 5

distance

Visited s y y S

vertices

(Fiset, 2016)

The path between the vertex y to the next nearby unvisited vertex is z. The shortest distance

form vertices y(5) to z(11) is

dist(u)=dist(u, v)+cost(u).

f(dist(u, v)+cost(u))< dist(v), and

y(5) shortest distance is 5

z(11) distance is 6, so

dist(y(5), z(11))=5+6=11

So 11 < ∞, 11 is lessthan ∞ and

Shortest distance from s to y(5) to z(11) is 11. Visited vertices is y

vertices s (0) t(3) x(9) y(5) z(11)

distance 0, 0 5 ,1 5, 4 0, 5 5, 6
vertices s (0) t(3) x(9) y(5) z(11)

Shortest 0 6 9 5 11

distance

Visited s y Y S y

vertices

So, the shortest distance from the vertices s to z is 11. And the shortest path from the

vertices s to z is s→ y→ z. So, here we find the shortest distance from s to z is 11, and

shortest path is s→ y→ z because remaining all other paths from s to z shortest distance is

more than11 (Fiset, 2016).

From the given graph the shortest path from vertex z to vertex s

Step1: The shortest path from the source vertex z to the source

vertex z is 0, because source vertex is 0 and distance to all other

vertices from s is ∞ (infinity).

From the Dijkstra's Algorithm principle

f(dist(u, v)+cost(u))< dist(v), and

dist(u)=dist(u, v)+cost(u)

f(dists((0, 0))+0)<0 and dist(s(0))=dist(0, 0)+cost(0)

Shortest distance is vertices z to z is 0.

Vertices z (11) t(3) x(9) y(5) s(0)

Distance 0, 0
Vertices z (11) t(3) x(9) y(5) s(0)

Shortest 0

distance

Visited

vertices

(Fiset, 2016)

Step2: The path between the vertex z to the next nearby unvisited vertex is s, the path

between the vertex z to the next nearby unvisited vertex is x.

The shortest distance form vertices z(11) to s(0) is

f(dist(u, v)+cost(u))< dist(v), and

dist(u)=dist(u, v)+cost(u).

distz(11) is 0, and dist s(0) is 3

dist(z(11), s(0))= 0+3=3

So f(dist z((11, 0))+3) < ∞, 3 is lessthan ∞ and

Shortest distance from z to s is 3.

vertices z (11) t(3) x(9) y(5) s(0)

distance 0, 0 0, 3
vertices z (11) t(3) x(9) y(5) s(0)

Shortest 0 3

distance

Visited z

vertices

(Fiset, 2016)

Step3: The path between the vertex z to the next nearby unvisited vertex is x.

The shortest distance form vertices z(11) to x(9) is

f(dist(u, v)+cost(u))< dist(v), and

dist(u)=dist(u, v)+cost(u).

distz(11) is 0, and dist x(9) is 7

dist(z(11), x(9)))= 0+7=7

So f(dist z((11, 9))+7) < ∞, 7 is lessthan ∞ and

Shortest distance from z to s is 7. Visited vertices is z.

vertices z (11) t(3) x(9) y(5) s(0)

distance 0, 0 0, 7 0, 3
vertices z (11) t(3) x(9) y(5) s(0)

Shortest 0 7 3

distance

Visited Z z

vertices

So, here we find the shortest distance from z to s is 3, and shortest path is z→s because

remaining all other paths from z to s shortest distance is more than 3 (Fiset, 2016).

Conclusion

Graphs are used to show connections between objects, entities, or people. They are

made up of two main components: nodes and edges. Dijkstra's algorithm determines the

shortest path between one node and every other node in a graph. The procedures for

deploying this algorithm on the internet are outlined below. Dijkstra's algorithm simple and

effective. This algorithm could be applied to a variety of situations, including IP routing,

phone network routing, geographic maps, and so on. This implementation could be improved

by using concurrent, which allows to analyze the child nodes in parallel, improving

performance (Carlos, 2020).

References:
Estefania C N, (2020). https://www.freecodecamp.org/news/dijkstras-shortest-path-

algorithm-visual-introduction/#:~:text=Dijkstra%27s%20Algorithm%20finds%20the

%20shortest,node%20and%20all%20other%20nodes.

Fiset. W. (2016, May 7).https://www.youtube.com/watch?v=pVfj6mxhdMw

Carlos. H, (2020).
https://medium.com/carlos-hernandez/dijkstras-algorithm-afa09be748bf

You might also like