0% found this document useful (0 votes)
84 views31 pages

Dijkstra's Algorithm: Slide Courtesy: Uwash, UT

Dijkstra's algorithm finds the shortest paths from a source vertex to all other vertices in a graph. It works for graphs with non-negative edge weights. The algorithm maintains a distance label for each vertex which is updated if a shorter path is found. It iteratively selects the vertex with the minimum distance label and updates the labels of its neighbors until all vertices are processed. Examples demonstrate how the algorithm progresses by selecting the closest vertex and updating neighboring distances at each step until the shortest paths are determined.

Uploaded by

Absk
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)
84 views31 pages

Dijkstra's Algorithm: Slide Courtesy: Uwash, UT

Dijkstra's algorithm finds the shortest paths from a source vertex to all other vertices in a graph. It works for graphs with non-negative edge weights. The algorithm maintains a distance label for each vertex which is updated if a shorter path is found. It iteratively selects the vertex with the minimum distance label and updates the labels of its neighbors until all vertices are processed. Examples demonstrate how the algorithm progresses by selecting the closest vertex and updating neighboring distances at each step until the shortest paths are determined.

Uploaded by

Absk
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/ 31

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,
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
Dijkstra pseudocode
Dijkstra(v1, v2):
for each vertex v: // Initialization
v's distance := infinity.
v's previous := none.
v1's distance := 0.
List := {all vertices}.

while List is not empty:


v := remove List vertex with minimum distance.
mark v as known.
for each unknown neighbor n of v:
dist := v's distance + edge (v, n)'s weight.

if dist is smaller than n's distance:


n's distance := dist.
n's previous := v.

reconstruct path from v2 back to v1,


following previous pointers.
6
Example: Initialization

Distance(source) = 0 ∞ Distance (all vertices


0 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

Distance(B) = 2 1
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

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

10
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

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

8 5

13
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

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
Correctness :“Cloudy” Proof
When a vertex is added to the cloud, it has shortest
distance to source.

Least cost node v P: Next shortest path from


inside the known cloud

competitor v'

Source

• If the path to v is the next shortest path, the path to v' must be at
least as long. Therefore, any path through v' to v cannot be shorter!
Dijkstra Correctness (2)

27
Dijkstra Correctness (3)

d
[] d
u (s
,)
u (
in
it
ialas
sump
tio
n)
d
(s
,)
yd
(y
,)
u(opt
im a
lsub
st
ruc
tu
re)
d
[y
]d
(y
,)
u (cor
rect
nes
sofd[
y])
d
[y
] (
nonegat
ive
w e
ig
hts
)

• But if d[u] > d[y], the algorithm would have chosen y


(from the Q) to process next, not u -- Contradiction
• Thus d[u] = d(s,u) at time of insertion of u into S, and
Dijkstra's algorithm is correct

28
Dijkstra’s Pseudo Code
• Graph G, weight function w, root s

relaxing
edges

29
Time Complexity: Using List
Time Complexity: Priority Queue

31

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy