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

Computer Networks Pptsss

Dijkstra's algorithm is used to find the shortest paths from a single source node to all other nodes in a graph. It works for both directed and undirected graphs as long as all edge weights are non-negative. The algorithm uses a greedy approach, maintaining a set of visited nodes and iteratively selecting the unvisited node with the lowest distance from the source. It updates the distances of neighboring nodes if a shorter path is found.

Uploaded by

Jhansi Reddy
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)
41 views

Computer Networks Pptsss

Dijkstra's algorithm is used to find the shortest paths from a single source node to all other nodes in a graph. It works for both directed and undirected graphs as long as all edge weights are non-negative. The algorithm uses a greedy approach, maintaining a set of visited nodes and iteratively selecting the unvisited node with the lowest distance from the source. It updates the distances of neighboring nodes if a shorter path is found.

Uploaded by

Jhansi Reddy
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/ 26

Dijkstra’s Algorithm

Dijkstra’s Algorithm
• Single Source Multiple Destination
Shortest Path Algorithm
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.
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
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
Requirements
• Works with directed and undirected graphs
• Works with weighted and unweighted graphs
• Rare type of algorithm 
A greedy algorithm that produces an
optimal solution
Walk-Through
2
Initialize array
3
5 F C K dv pv
10
A 7
4
3 A F  
8
18 B F  
4
9
B D C F  
10
H 9 25 D F  
2
3 E F  
G 7
E F F  
G F  
H F  
2
Start with G
3
5 F C K dv pv
10
A 7
4
3 A
8
18 B
4
9
B D C
10
H 9 25 D
2
3 E
G 7
E F
G T 0 
H
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
3 A
8
18 B
4
9
B D C
10
H 9 25 D 2 G
2
3 E
G 7
E F
G T 0 
H 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
3 A
8
18 B
4
9
B D C
10
H 9 25 D T 2 G
2
3 E
G 7
E F
G T 0 
H 3 G
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
3 A
8
18 B
4
9
B D C
10
H 9 25 D T 2 G
2
3 E 27 D
G 7
E F 20 D
G T 0 
H 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
3 A
8
18 B
4
9
B D C
10
H 9 25 D T 2 G
2
3 E 27 D
G 7
E F 20 D
G T 0 
H T 3 G
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
3 A 7 H
8
18 B 12 H
4
9
B D C
10
H 9 25 D T 2 G
2
3 E 27 D
G 7
E F 20 D
G T 0 
H T 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B 12 H
4
9
B D C
10
H 9 25 D T 2 G
2
3 E 27 D
G 7
E F 20 D
G T 0 
H T 3 G
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B 12 H
4
9
B D C
10
H 9 25 D T 2 G
2
3 E 27 D
G 7
E F 17 A
G T 0 
H T 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B T 12 H
4
9
B D C
10
H 9 25 D T 2 G
2
3 E 27 D
G 7
E F 17 A
G T 0 
H T 3 G
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B T 12 H
4
9
B D C 16 B
10
H 9 25 D T 2 G
2
3 E 22 B
G 7
E F 17 A
G T 0 
H T 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B T 12 H
4
9
B D C T 16 B
10
H 9 25 D T 2 G
2
3 E 22 B
G 7
E F 17 A
G T 0 
H T 3 G
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B T 12 H
4
9
B D C T 16 B
10
H 9 25 D T 2 G
2
3 E 22 B
G 7
E F 17 A
G T 0 
H T 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
3 A T 7 H
8
18 B T 12 H
4
9
B D C T 16 B
10
H 9 25 D T 2 G
2
3 E 22 B
G 7
E F T 17 A
G T 0 
H T 3 G
2
Update unselected nodes
3
5 F C K dv pv
10
A 7
4
A T 7 H
8
18 B T 12 H
4
9
B D C T 16 B
10
H 9 25 D T 2 G
2
3 E 19 F
G 7
E F T 17 A
G T 0 
H T 3 G
2
Select minimum distance
3
5 F C K dv pv
10
A 7
4
A T 7 H
8
18 B T 12 H
4
9
B D C T 16 B
10
H 9 25 D T 2 G
2
3 E T 19 F
G 7
E F T 17 A
G T 0 
H T 3 G

Done
Order of Complexity
• Analysis
– findMin() takes O(V) time
– outer loop iterates (V-1) times
 O(V2) time
• Optimal for dense graphs, i.e., |E| = O(V 2)
• Suboptimal for sparse graphs, i.e., |E| =
O(V)
Order of Complexity
• If the graph is sparse, i.e., |E| = O(V)
– maintain distances in a priority queue
– insert new (shorter) distance produced by line
10 of Figure 9.32
 O(|E| log |V|) complexity
Negative Edge Weights
• Read § 9.3.3
• Dijkstra’s algorithm as shown in Figure
9.32 does not work! Why?
Acyclic Graphs
• Read § 9.3.4
• Combine topological sort with Dijkstra’s
algorithm
All-Pairs Shortest Paths
• One option: run Dijktra’s algorithm |V|
times  O(V3) time
• A more efficient O(V3) time algorithm is
discussed in Chapter 10

You might also like