Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Dijkstra Algorithm class

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)
5 views

Dijkstra Algorithm class

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

Dijkstra’s Algorithm

1
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.
Applications
- Maps (Map Quest, Google Maps)
- Routing Systems
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.

Input: Weighted graph G={V,E} 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
Approach
• The algorithm computes for each vertex u the distance to u from the
start vertex v, that is, the weight of a shortest path between v and u.
• The algorithm keeps track of the set of vertices for which the
distance has been computed, called the cloud C
• Every vertex has a label D associated with it. For any vertex u, D[u]
stores an approximation of the distance between v and u. The
algorithm will update a D[u] value when it finds a shorter path from
v to u.
• When a vertex u is added to the cloud, its label D[u] is equal to the
actual (final) distance between the starting vertex v and vertex u.

5
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
Example 2
Ex 2 Sol
Example:
Distance(source) = 0 0 ∞ Distance (all vertices
A
2
B but source) = ∞

4 1 3 10

2 2
∞ C D E ∞

5 8 ∞ 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance.


24
Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s) (V)
2. S← 
3. Q ← V[G] O(V) build min-heap
4. while Q   Executed O(V) times
O(lgV) O(VlgV)
5. do u ← EXTRACT-MIN(Q)
6. S ← S  {u}
7. for each vertex v  Adj[u] O(E) times
(total) O(ElgV)
8. do RELAX(u, v, w)
9. Update Q (DECREASE_KEY) O(lgV)

25
Running time: O(VlgV + ElgV) = O(ElgV)
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)
Time Complexity: Priority Queue
For sparse graphs, (i.e. graphs with much less than |V2| edges)
Dijkstra's implemented more efficiently by priority queue

• Initialization O(|V|) using O(|V|) buildHeap


• While loop O(|V|)
• Find and remove min distance vertices O(log |V|) using O(log |V|)
deleteMin

• Potentially |E| updates


• Update costs O(log |V|) using decreaseKey

Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|)


• |V| = O(|E|) assuming a connected graph
27

You might also like