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

Graph - Shortest Path - Dijkstras Algorithm PDF

Dijkstra's algorithm is used to find the shortest paths between nodes in a graph. It works by maintaining a priority queue of all nodes and iteratively selecting the node with the lowest distance from the source. The algorithm updates the distances of neighboring nodes if a shorter path is found and adds them to the visited set. This process continues until all nodes have been visited.

Uploaded by

sudhan
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)
62 views

Graph - Shortest Path - Dijkstras Algorithm PDF

Dijkstra's algorithm is used to find the shortest paths between nodes in a graph. It works by maintaining a priority queue of all nodes and iteratively selecting the node with the lowest distance from the source. The algorithm updates the distances of neighboring nodes if a shorter path is found and adds them to the visited set. This process continues until all nodes have been visited.

Uploaded by

sudhan
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/ 26

Graph

P VASUKI
Path
• Vertex w is adjacent to v in a graph iff (v,w) is in
the graph
– Note that this includes the case of an
undirected graph with an edge between v and
w
• A path in a graph is a sequence (vi) such that
vi+1 is adjacent to vi for every i
• A path of n vertices may also be considered as a
sequence of n-1 edges.
• The length of a path generally refers to the
number of edges in the path
Shortest Path

• what the cheapest path is


– from one node to another (the single pair
case)
– from one node to all other nodes (the single
source case)
– from all nodes to all other nodes (the all-pairs
case)
• "shortest path" to mean the cheapest path
– where the cost of a path is the cost of the
edges that compose it
Dijkstra
• Formalized by Edsger Wybe
- May 11, 1930 – August 6, 2002

- Received the 1972 A. M. Turing Award, widely considered


the most prestigious award in computer science.

- The Schlumberger Centennial Chair of Computer Sciences


at The University of Texas at Austin from 1984 until 2000

- Made a strong case against use of the GOTO statement in


programming languages and helped lead to its deprecation.

- Known for his many essays on programming.


Dijikstra’s Algorithm

• Single-Source Shortest Path Problem


• The problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
Dijkstra’s algorithm
• 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)
• return dist

• Courtesy: Mark Allen Weiss - Data Structures and Algorithm


Sample Dijkstra's Shortest Path
Algorithm
• Find shortest path from s to t.
2 24 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44

7
Courtesy: Thanks toNational University of Singapore for this example
Dijkstra's Shortest Path Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }


24
2 3
0 9

s
18
14
∞ 2 6
6

30
∞ 11
4 19

15 5
5
6
20 16

t
7 44
distance label ∞ ∞
8
Dijkstra's Shortest Path
Algorithm
S={ }
delmin PQ = { s, 2, 3, 4, 5, 6, 7, t } ∞

0 24
9 2 3

s 18
14 ∞
2 6

6 ∞ 19
30
15 11 4
5 5
6
20 16

distance label ∞ 44 ∞t
7

9
Dijkstra's Shortest Path Algorithm

S={s}
decrease key
PQ = { 2, 3, 4, 5, 6, 7, t }


X 9
24
2 3
0 9

s
18
14

X 14
2 6
6

30
∞ 11
4 19

15 5
5
6
20 16

t
7 44
distance label ∞ 15
X ∞
10
Dijkstra's Shortest Path Algorithm
S={s}
PQ = { 2, 3, 4, 5, 6,
delmin
7, t } ∞

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6

30
∞ 11
4 19

15 5
5
6
20 16

t
7 44
distance label ∞ 15
X ∞
11
Dijkstra's Shortest Path Algorithm

S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }


X 9
24
2 3
0 9

s
18
14

X 14
2 6
6

30
∞ 11
4 19

15 5
5
6
20 16

t
7 44

∞ 15
X ∞
12
Dijkstra's Shortest Path Algorithm
S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t } decrease key

∞ 33
X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6

30
∞ 11
4 19

15 5
5
6
20 16

t
7 44

∞ 15
X ∞
13
Dijkstra's Shortest Path Algorithm

S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }
∞ 33
X

X 9
24
2 3
0 9
delmin
s
18
14

X 14
2 6
6

30
∞ 11
4 19

15 5
5
6
20 16

t
7 44

∞ 15
X ∞
14
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6 }
32
PQ = { 3, 4, 5, 7, t }
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6
44 ∞
30

X 11
4 19

15 5
5
6
20 16

t
7 44

∞ 15
X ∞
15
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6
44 ∞
30

X 11
4 19

15 5
5
6
20 16

t
7 44

∞ 15
X delmin ∞
16
Dijkstra's Shortest Path Algorithm

S = { s, 2, 6, 7 }
32
PQ = { 3, 4, 5, t }
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6
X 35
44 ∞
30

X 11
4 19

15 5
5
6
20 16

t
7 44
59 ∞
∞ 15
X X
17
Dijkstra's Shortest Path Algorithm
delmin
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6
X 35
44 ∞
30

X 11
4 19

15 5
5
6
20 16

t
7 44
59 ∞
∞ 15
X X
18
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6
X 35
44 X 34 ∞
30

X 11
4 19

15 5
5
6
20 16

t
7 44
X ∞
∞ 15
X 51 59 X
19
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6
X 35
44 X 34 ∞
30

X 11
4 19

15 5
5
6
20 16
delmin

t
7 44
X ∞
∞ 15
X 51 59 X
20
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6 45 ∞
X
X 35
44 X 34
30

X 11
4 19

15 5
5
6
20 16

t
7 44
X ∞
∞ 15
X 50 51
X 59 X
21
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6 45 ∞
X
X 35
44 X 34
30

X 11
4 19

15 5 delmin
5
6
20 16

t
7 44
X ∞
∞ 15
X 50 51
X 59 X
22
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6 45 ∞
X
X 35
44 X 34
30

X 11
4 19

15 5
5
6
20 16

t
7 44
X ∞
∞ 15
X 50 51
X 59 X
23
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6 45 ∞
X
X 35
44 X 34
30

X 11
4 19

15 5
5
6
20 16

t
7 44
delmin X ∞
∞ 15
X 50 51
X 59 X
24
Dijkstra's Shortest Path Algorithm
S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { } 32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6 45 ∞
X
X 35
44 X 34
30

X 11
4 19

15 5
5
6
20 16

t
7 44
X ∞
∞ 15
X 50 51
X 59 X
25
Dijkstra's Shortest Path Algorithm

S = { s, 2, 3, 4, 5, 6, 7, t } 32
PQ = { } ∞ 33
X X

X 9
24
2 3
0 9

s
18
14

X 14
2 6
6 45 ∞
X
X 35
44 X 34
30

X 11
4 19

15 5
5
6
20 16

t
7 44
X ∞
∞ 15
X 50 51
X 59 X
26

You might also like