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

Algorithm Lecture 12 Dijkstra Algorithm.ppt.Pptx

Dijkstra's Algorithm solves the single-source shortest path problem in graph theory, finding the shortest paths from a source vertex to all other vertices in a weighted graph with nonnegative edge weights. The algorithm maintains a set of vertices with computed distances and updates these distances as shorter paths are found. Its time complexity varies based on implementation, with O(|V|^2) for a simple list and O(|E|log|V|) when using a priority queue for sparse graphs.

Uploaded by

tachbirdewan.bd
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)
92 views

Algorithm Lecture 12 Dijkstra Algorithm.ppt.Pptx

Dijkstra's Algorithm solves the single-source shortest path problem in graph theory, finding the shortest paths from a source vertex to all other vertices in a weighted graph with nonnegative edge weights. The algorithm maintains a set of vertices with computed distances and updates these distances as shorter paths are found. Its time complexity varies based on implementation, with O(|V|^2) for a simple list and O(|E|log|V|) when using a priority queue for sparse graphs.

Uploaded by

tachbirdewan.bd
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

Slide Courtesy: Uwash, UT

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={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
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,
u.d stores an approximation of the distance between v and u.
The algorithm will update a u.d value when it finds a shorter
path from v to u.
• When a vertex u is added to the cloud, its label u.d is equal to
the actual (final) distance between the starting vertex v and
vertex u.
5
Dijkstra pseudocode
DIJKSTRA(G,w, s) RELAX(u, v,w)
1 INITIALIZE-SINGLE-SOURCE(G, s) 1 if v.d > u.d + w(u, v)
2S←∅ 2 v.d ← u.d + w(u, v)
3 Q ← G.V 3 v.π← u
4 while Q != ∅
5 u ← EXTRACT-MIN(Q)
6 S ← S ∪ {u}
7 for each vertex v ∈ Adj[u]
8 do RELAX(u, v,w)

6
Example: Initialization

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.

7
Example: Update neighbors'
distance
0 2
2
A B

4 1 3 10

2 2
∞ C D E ∞

5 8 1 4 6

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

8
Example: Remove vertex with
minimum distance
0 2
2
A B

4 1 3 10

2 2
∞ C D E ∞

5 8 1 4 6

1
F G

∞ ∞

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

9
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

10
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

11
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

12
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

13
Example: Continued...
Pick vertex List with minimum distance (G) 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
Previous distance
6 5
Distance(F) = min (8, 5+1) = 6

14
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

15
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Another Example
Time Complexity: Using List
The simplest implementation of the Dijkstra's algorithm
stores vertices in an ordinary linked list or array
– Good for dense graphs (many edges)

• |V| vertices and |E| edges


• Initialization O(|V|)
• While loop O(|V|)
– Find and remove min distance vertices O(|V|)
• Potentially |E| updates
• Update costs O(1)

Total time O(|V2| + |E|) = O(|V2| )


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

26

You might also like