0% found this document useful (0 votes)
30 views46 pages

Analysis of Algorithms: Dijkstra's Algorithm

Dijkstra's algorithm is a solution to the single-source shortest path problem in graph theory, designed to find the shortest paths from a source vertex to all other vertices in a weighted graph with nonnegative edge weights. The algorithm employs a greedy approach and has applications in various fields such as traffic information systems and epidemiology. Its efficiency can be improved using data structures like priority queues, resulting in a running time of O((|E|+|V|) log |V|).

Uploaded by

jale.cavus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views46 pages

Analysis of Algorithms: Dijkstra's Algorithm

Dijkstra's algorithm is a solution to the single-source shortest path problem in graph theory, designed to find the shortest paths from a source vertex to all other vertices in a weighted graph with nonnegative edge weights. The algorithm employs a greedy approach and has applications in various fields such as traffic information systems and epidemiology. Its efficiency can be improved using data structures like priority queues, resulting in a running time of O((|E|+|V|) log |V|).

Uploaded by

jale.cavus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

1

Analysis of
Algorithms

Dijkstra's
Algorithm

DR. MOHAMMED AL-HUBAISHI


2
Single-Source Shortest Path Problem

Single-Source Shortest Path Problem - The problem of


finding shortest paths from a source vertex v to all other
vertices in the graph.
3
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source shortest path
problem in graph theory.

Works on both directed and undirected graphs. However, all edges must
have nonnegative weights.

Approach: Greedy

Input: Weighted graph G={E,V} and source vertex v∈V, such that all edge
weights are nonnegative

Output: Lengths of shortest paths (or the shortest paths themselves) from
a given source vertex v∈V to all other vertices
4

Dijkstra's algorithm (/ˈdaɪkstrəz/


DYKE-strəz) is an algorithm for finding the
shortest paths between nodes in a
weighted graph, which may represent, for
example, road networks. It was conceived
by computer scientist Edsger W. Dijkstra in
1956 and published three years later
5
undirected unweighted graph
6
undirected weighted graph

► weighted graph 5
5
2
4
3 -2
1 7
2 3
0 1
7
5 -2

6
4 -4
7
directed weighted graph

► weighted graph 5
5
2
4
3 -2
1 7
2 3
0 1
7
5 -2

6
4 -4
8
Dijkstra's algorithm - Pseudocode

dist[s] ←0 (distance to source vertex is zero)


for all v ∈ V–{s}
do dist[v] ←∞ (set all other distances to infinity)
S←∅ (S, the set of visited vertices is initially empty)
Q←V (Q, the queue initially contains all vertices)
while Q ≠∅ (while the queue is not empty)
do u ← mindistance(Q,dist) (select the element of Q with the min. distance)
S←S∪{u} (add u to list of visited vertices)
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then d[v] ←d[u] + w(u, v) (set new value of shortest path)

(if desired, add traceback code)

return dist
https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-greedy-algo-7/
9
C++ code for Dijkstra’s
Algorithm

Dijkstra's Algorithm in C++ | Shortest Path Algorithm | FavTutor

https://favtutor.com/blogs/dijkstras-algorithm-cpp#:~:text=Algori
thm%20for%20Dijkstra's%20in%20C%2B%2B&text=Consider%20sour
ce%20vertex%20as%20current,replace%20it%20otherwise%20ign
ore%20it
10
pseudocode for Dijkstra's Algorithm

● graph represents the input graph.


● source is the source vertex from which
the shortest paths are calculated.
● distances is an array to store the
shortest distances from the source
vertex to all other vertices.
● visited is a set to keep track of visited
vertices.
11
Dijkstra's algorithm
12
Dijkstra's algorithm example on
an undirected graph cont’d

Shortest path: 0 3 4 2
13
path from 0 to 4

https://youtu.be/T8KcNk6cAHo
6
7
4 8
7 3

5
9 4

8
2
6

Shortest path: 0 1 4
14
14
path from 0 to 5

https://youtu.be/T8KcNk6cAHo
6
7
4 8
7 3

5
9 4

8
2
6

Shortest path: 0 1 4 5
17
15
path from 0 to 5

6
7
4 18
7 3

5
9 4

8
2
6

Shortest path: 0 1 2 3 4 5

26
16
path from 0 to 5

6
7
4 -18
7 3

5
9 4

8
2
6

Shortest path: 0 1 4 5

-9
17
Dijkstra's algorithm example on
an undirected graph
18
Dijkstra's algorithm example on
an undirected graph cont’d
19
Dijkstra's algorithm example on
an undirected graph cont’d
20
Dijkstra's algorithm example on
an undirected graph cont’d
21
Dijkstra's algorithm example on
an undirected graph cont’d

Update
If (d[u]+c(u,v)<d[v])
d[v]= d[u]+c(u,v)
22
Dijkstra's algorithm example on
an undirected graph cont’d
23
Dijkstra's algorithm example on
an undirected graph cont’d
24
Dijkstra's algorithm example on
an undirected graph cont’d

Update
If (d[u]
+c(u,v)<d[v])
d[v]= d[u]+c(u,v)
25
Dijkstra's algorithm example on
an undirected graph cont’d
26
Dijkstra's algorithm example on
an undirected graph cont’d
27
Dijkstra's algorithm example on
an undirected graph cont’d

Update
If (d[u]+c(u,v)<d[v])
d[v]= d[u]+c(u,v)
28
Dijkstra's algorithm example on
an undirected graph cont’d
29
Dijkstra's algorithm example on
an undirected graph cont’d
30
path from Previous vertex
31
Dijkstra's algorithm example on
a directed graph
7
4 Update
2
1 If (d[u]+c(u,v)<d[v])
2 d[v]= d[u]+c(u,v)

1 2 6
0 1

4 5

3 5
3
32
Dijkstra's algorithm example on
a directed graph

Update
8 If (d[u]+c(u,v)<d[v])
9
2 ∞ d[v]= d[u]+c(u,v)

2 7 4 9
11
1
2 ∞
1 2 6 V D[V]
0 1
2 2
4 5 3 3
3 5 4 8
3 6
3
∞ 5 6
4
6 9
33
Homework: Dijkstra's algorithm
on a directed graph

Started from node 1


45
1 3
50 2 10

15 35
10 20 30

5 6
4
15 3
34
Dijkstra's algorithm
Selected 2 3 4 5 6
node

4 50 45 10 Max Max

5 50 45 10 25 Max
Start from 1 2 45 45 10 25 Max

3 45 45 10 25 Max

6 45 45 10 25 Max

45
1 3
50 2 10

15 35
10 20 30

5 6
4
15 3
35
Homework: Dijkstra's algorithm
36
Homework: Dijkstra's algorithm
37
Homework: Dijkstra's algorithm

Shortest path: 0 7 6 5 4
38
39
40
Homework: Dijkstra's algorithm
41
42
43
Implementations and Running Times
The simplest implementation is to store vertices in an array
or linked list. This will produce a running time of

O(|V|^2 + |E|)

For sparse graphs, or graphs with very few edges and many
nodes, it can be implemented more efficiently storing the
graph in an adjacency list using a binary heap or priority
queue. This will produce a running time of

O((|E|+|V|) log |V|)


44
Dijkstra's Algorithm - Why use it?

► As mentioned, Dijkstra’s algorithm calculates the shortest path to


every vertex.
► However, it is about as computationally expensive to calculate
the shortest path from vertex u to every vertex using Dijkstra’s as
it is to calculate the shortest path to some particular vertex v.
► Therefore, anytime we want to know the optimal path to some
other vertex from a determined origin, we can use Dijkstra’s
algorithm.
45
Applications of Dijkstra's Algorithm

- Traffic Information Systems are most prominent use


- Mapping (Map Quest, Google Maps)
- Routing Systems
46
Applications of Dijkstra's Algorithm

🞆 Oneparticularly relevant this week:


epidemiology

🞆 Prof.Lauren Meyers (Biology Dept.) uses


networks to model the spread of infectious
diseases and design prevention and response
strategies.

🞆 Vertices represent individuals, and edges their


possible contacts. It is useful to calculate how a
particular individual is connected to others.

🞆 Knowing the shortest path lengths to other


individuals can be a relevant indicator of the
potential of a particular individual to infect
others.

You might also like