0% found this document useful (0 votes)
34 views27 pages

Lecture 04 Dijkstra

Dijkstra's algorithm finds the shortest paths from a single source vertex to all other vertices in a weighted graph. It works by maintaining a set S of vertices whose final shortest path from the source is known. It iteratively selects the vertex in the graph not in S with the minimum distance estimate, inserts it into S, and relaxes all edges leaving that vertex to update distance estimates. This process continues until all vertices have been processed.

Uploaded by

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

Lecture 04 Dijkstra

Dijkstra's algorithm finds the shortest paths from a single source vertex to all other vertices in a weighted graph. It works by maintaining a set S of vertices whose final shortest path from the source is known. It iteratively selects the vertex in the graph not in S with the minimum distance estimate, inserts it into S, and relaxes all edges leaving that vertex to update distance estimates. This process continues until all vertices have been processed.

Uploaded by

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

Single Source Shortest Path

Dijkstra’s Algorithm

Dr. Md Masbaul Alam Polash


Associate Professor
Computer Science and Engineering
Jagannath University
 What is shortest path ?
 shortest length between two vertices for an
unweighted graph:
 smallest cost between two vertices for a weighted
graph:
B 210 B

A A
450
60 190

C unweighted C weighted
graph graph
200 130
D D
E E
Weighted Shortest-Path Problems
G = (V, E) weighted directed graph
w: ER weight function
Weight of a path p = <v0, v1,. . ., vn>

n
w( p)   w(vi 1v)
Shortest path weight fromi u1 to v

 p

Shortest
 (upath min{wu( to
, v)  from u  Any
p) :v: v} path
if a u,from
v pathuexits
to v with w(p) = (u,v)
[v] - predecessor  of von a path otherwise


Shortest-Path Problems
 Shortest-Path problems
 Single-source. Find a shortest path from a given source to
each of the vertices
 Single-pair. Given two vertices, find a shortest path
between them. Solution to single-source problem solves
this problem efficiently, too.
 All-pairs. Find shortest-paths for every pair of vertices.
Dynamic programming algorithm.
 Unweighted shortest-paths – BFS.
 What is the problem ?
Find the shortest path from a vertex v to every
other vertex in a graph.

 Some algorithms for problem:


 Dijkstra (single source shortest paths )
 Bellman and Ford (single source shortest paths, for
negative weights)
Dijkstra’s algorithm

 What is the lowest-cost path between two


vertices in a weighted graph?
 Only works on graphs all of whose weights are
positive
 Works in a similar way to the unweighted
algorithm, except we possibly adjust the
values in the vertices more than once
INITIALIZE-SINGLE-SOURCE(G, s)
1. for each vertex v V(G)
2. do d[v] 
3. [v]  nil
4. d[s]  0
Relaxation
 For each vertex in the graph, we maintain d[v], the
shortest path estimate, initialized to at start
 Relaxing an edge (u,v) means testing whether we can
improve the shortest path to v found so far by going
through u Relax (u,v,w)
u v u v if d[v]>d[u]+w(u,v)then
2 2
    d[v]  d[u]+w(u,v)
[v]  u
Relax(u,v) Relax(u,v)

 2   2 
u v u v
EXAMPLE 2

s s 

Relax

 
   
u v
u v
EXAMPLE 3

s s 

Relax

 
   
u v
u v
Dijkstra’s Algorithm
 Approach
 maintain a set S of vertices whose final shortest
path weights from the source s have been
determined.
 repeat
 select vertex from V-S with the minimum shortest path
estimate
 insert u in S

 relax all edges leaving u


Relaxation:
 The process of relaxing an edge (u,v) consists of testing
whether we can improve the shortest path to v found so far
by going through u and if so, updating d[v] and  [v].

 RELAX(u,v,w)
 if d[v] > d[u]+w(u,v)
 then d[v]   d[u]+w(u,v)

  [v]  u
Continue:
 DIJKSTRA(G,w,s):
 INITIALIZE-SINGLE-SOURCE(G,s)
 S   

 Q V[G]

 while Q  
 do u  EXTRACT -MIN(Q)

 S  S {u}

 for each vertex v  Adj[u]


 do RELAX(u,v,w)
Dijkstra’s Pseudo Code
 Graph G, weight function w, source s

Initialization

relaxing
edges
Dijkstra’s
u
Example
v u v
1 1
10   10  
9 9
s  2 3 s  2 3
4 6 4 6
7 7
5 5
 2   2 
x y x y
u v u v
1 1
10   10  
9 9
2 3 s  2 3
s  4 6 7
4 6
7
5 5
   2 
2
x y x y
Dijkstra’s Example
u v u v
1 1
10   10  
9 9
 2 3  2 3
4 6 4 6
7 7
5 5
 2   2 
x y x y
Another Example

Distance(source) = 0 0 ∞ Distance (all vertices


2 but source) = ∞
A B

4 1 3 10

2 2 ∞
∞ C D E

5 8 ∞ 4 6

F G
1
∞ ∞

Pick vertex in List with minimum distance.

17
Example: Update neighbors' distance

0 2
2
A B

4 1 3 10

∞ C
2
D
2
E ∞

5 8 1 4 6

1
Distance(B) = 2 F G
Distance(D) = 1
∞ ∞

18
Example: Remove vertex with minimum distance

0 2
2
A B

4 1 3 10

∞ C
2
D
2
E ∞

5 8 1 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance, i.e., D

19
Example: Update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
Distance(C) = 1 + 2 = 3 F G
Distance(E) = 1 + 2 = 3
Distance(F) = 1 + 8 = 9 9 5
Distance(G) = 1 + 4 = 5

20
Example: Continued...
Pick vertex in List with minimum distance (B) and update neighbors
0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6
Note : distance(D) not
1
F G updated since D is
already known and
9 5 distance(E) not updated
since it is larger than
previously computed

21
Example: Continued...
Pick vertex List with minimum distance (E) and update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G
No updating
9 5

22
Example: Continued...
Pick vertex List with minimum distance (C) and update neighbors

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
Distance(F) = 3 + 5 = 8 F G

8 5

23
Example: Continued...
Pick vertex List with minimum distance (G) and update neighbors

0 2
2
A B

1 3 10
4

2 2
3 C D E 3

5 8 1 4 6

1
F G
Previous distance
6 5
Distance(F) = min (8, 5+1) = 6

24
Example (end)

0 2
2
A B

4 1 3 10

2 2
3 C D E 3

5 8 1 4 6

1
F G

6 5
Pick vertex not in S with lowest cost (F) and update neighbors

25
Try Yourself
Thank You

You might also like