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

Dijkstra's Shortest Path Algorithm

The document discusses optimization problems and introduces greedy algorithms as a method for finding optimal solutions through local choices. It explains the greedy choice property and presents Dijkstra's algorithm for solving the single-source shortest path problem in weighted graphs. The algorithm's pseudocode and its application in finding the shortest paths from a source vertex to all other vertices are also detailed.

Uploaded by

areebababar1612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dijkstra's Shortest Path Algorithm

The document discusses optimization problems and introduces greedy algorithms as a method for finding optimal solutions through local choices. It explains the greedy choice property and presents Dijkstra's algorithm for solving the single-source shortest path problem in weighted graphs. The algorithm's pseudocode and its application in finding the shortest paths from a source vertex to all other vertices are also detailed.

Uploaded by

areebababar1612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Analysis of Algorithm

Greedy Algorithm

1
Optimization Problems
• Some problems can have many possible/ feasible solutions
with each solution having a specific cost. We wish to find the
best solution with the optimal cost.
• Maximization problem finds a solution with maximum cost
• Minimization problem finds a solution with minimum cost

• A set of choices must be made in order to arrive at an optimal


(min/max) solution, subject to some constraints.

• Is “Sorting a sequence of numbers” optimization problem?

2
Optimization Problems
• Two common techniques:
• Greedy Algorithms (local)
• Make the greedy choice and THEN
• Solve sub-problem arising after the choice is made
• The choice we make may depend on previous choices,
but not on solutions to sub-problems
• Top down solution, problems decrease in size
• Dynamic Programming (global)
• We make a choice at each step
• The choice depends on solutions to sub-problems
3
• Bottom up solution, smaller to larger sub-problems
Greedy Algorithm
• A greedy algorithm works in phases. At each phase:
• makes the best choice available right now, without regard for future
consequences
• hopes to end up at a global optimum by choosing a local optimum at
each step. For some problem, it works

• Greedy algorithms sometimes works well for optimization problems

• Greedy algorithms tend to be easier to code

• Greedy algorithms frequently used in everyday problem solving


• Choosing a job
• Route finding
• Playing cards 4
Greedy Algorithm
• Greedy choice property: A globally optimal solution can be arrived
at by making a locally optimal (greedy) choice
• Make whatever choice seems best at the moment and then solve the
sub-problem arising after the choice is made
• The choice made by a greedy algorithm may depend on choices so far,
but it cannot depend on any future choices or on the solutions to
sub-problems

5
Single-Source Shortest Path
Problem
• Problem: Given a weighted directed graph G, find the shortest paths
from a source vertex v to all other vertices in the graph.
• “Shortest-path” = minimum weight
• Weight of path is sum of edges
• E.g., a road map: what is the shortest path from Chapel Hill to
Charlottesville?
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 (same as breadth-first search if all weights = 1)

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
Dijkstra's Shortest Path
Algorithm
• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44
Dijkstra's Shortest Path
Algorithm S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }



24
2 3
0 9

s
18
14 ∞ 2 6
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44
distance label ∞ ∞
Dijkstra's Shortest Path
Algorithm
S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin


24
2 3
0 9

s
18
14 ∞ 2 6
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44
distance label ∞ ∞
Dijkstra's Shortest Path
Algorithm S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }

decrease key



X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44
distance label ∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }

delmin


X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44
distance label ∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }



X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44

∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

decrease key

∞ 33
X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44

∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

∞ 33
X

X 9
24
2 3
0 9
delmin
s
18
14 X 14
∞ 6
2
6 ∞
30 ∞ 4 19
11
15 5
5
6
20 16

t
7 44

∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
44
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

∞ 15
X ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
44
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

∞ 15
X delmin ∞
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
X 35
44
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ 59 ∞
X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t } delmin

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
X 35
44
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ 59 ∞
X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ X ∞
51 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 ∞
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16
delmin

t
7 44

X 15
∞ X ∞
51 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5 delmin
5
6
20 16

t
7 44

X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44
delmin 50 51 X ∞
X 59 X
X 15

Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ 50 51 X ∞
X 59 X
Dijkstra's Shortest Path
Algorithm S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }

32
∞ 33
X X

X 9
24
2 3
0 9

s
18
14 X 14
∞ 6
2
6 45 ∞
X
X 35
44 X 34
30 X
∞ 4 19
11
15 5
5
6
20 16

t
7 44

X 15
∞ 50 51 X ∞
X 59 X
Implementations and Running Times
• The simplest implementation is to store vertices in an array or
linked list. This will produce a running time of O(|V|^2 + |E|)

• For sparse graphs, or graphs with very few edges and many
nodes, it can be implemented more efficiently storing the graph
in an adjacency list using a binary heap or priority queue. This
will produce a running time of O((|E|+|V|) log |V|)

Q build-queue extract-min decrease-key total

#operations 1 n = |V| m = |E|


Array O(n) O(n) O(1) O(n2 )
Heap O(n) O(lg n) O(lg n) O(m lg n)
Applications of Dijkstra's Algorithm
- Traffic Information Systems are most prominent use
- Mapping (Map Quest, Google Maps)
- Routing Systems
BACKUP SLIDES
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example
Dijkstra Animated Example

You might also like