



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















2 3
2 4
3
1
2
3
4
0 1 0 1
0 0 1 0
0 0 0 0
0 1 1 0
1 2 3 4
1
2
3
4
1
2
3
4
In computer science we often build and use multiple
representations of the same data
For A5 this isn’t really necessary, but in A6 (coming
soon!) you’ll need to work with both explicit graph
representations of the park and with the 2-D form
in order to have a high quality solution
Finding the shortest (min-cost) path in a graph is a problem
that occurs often
Find the shortest route between Ithaca and West Lafayette, IN
Result depends on our notion of cost
Least mileage… or least time… or cheapest
Perhaps, expends the least power in the butterfly while flying fastest
Many “costs” can be represented as edge weights
A butterfly that optimizes to fly in bright sunshine, or to most efficiently
collect a list of flowers, is optimizing over possible path lengths that are
computed using one or perhaps multiple such factors: machine learning
How do we find a shortest path?
Dijkstra’s shortest-path algorithm
7
Dijsktra describes the algorithm in English:
When he designed it in 1956, most people were programming in
assembly language!
Only one high-level language: Fortran, developed by John
Backus at IBM and not quite finished.
No theory of order-of-execution time —topic yet to be developed.
In paper, Dijsktra says, “my solution is preferred to another one
… “the amount of work to be done seems considerably less.”
Dijkstra, E.W. A note on two problems in Connexion with graphs.
Numerische Mathematik 1, 269–271 (1959).
The n (> 0) nodes of a graph numbered 0..n-1.
v
Each edge has a positive weight.
Some node v be selected as the start node.
Use an array L[0..n-1]: for each node w, store in
L[w] the length of the shortest path from v to w.
weight(v1, v2) is the weight of the edge from node v1 to v2.
Calculate length of shortest path from v to each node.
using only red nodes (except for f)
Frontier
Settled
Far off
f
f
(edges leaving the black set and
edges from the blue to the red set
are not shown)
v
using only Settled nodes (except for f).
Theorem. For a node f in F with minimum L value (over nodes in
F), L[f] is the length of the shortest path from v to f.
Frontier
Settled
Far off
f
Theorem about the invariant
v f
g
g
Case 1: v is in S.
Case 2: v is in F. Note that L[v] is 0; it has minimum L value
L[g] ≥ L[f]
When does loop stop? When is
array L completely calculated?
while {
shortest v s path.
S F Far off
shortest v f path using
red nodes (except for f).
For all w, L[w]= ∞; L[v]= 0;
F= { v }; S= { };
Theorem: For a node f in F
with min L value, L[f] is
shortest path length
Loopy question 2:
How is progress toward
termination accomplished?
while {
f= node in F with min L value;
Remove f from F, add it to S;
shortest v s path.
S F Far off
shortest v f path using
red nodes (except for f).
For all w, L[w]= ∞; L[v]= 0;
F= { v }; S= { };
Theorem: For a node f in F
with min L value, L[f] is
shortest path length
f
Loopy question 3:
f
For all w, L[w]= ∞; L[v]= 0;
F= { v }; S= { };
while F ≠ {} {
f= node in F with min L value;
Remove f from F, add it to S;
for each edge (f,w) {
if (L[w] is ∞) add w to F;
if (L[f] + weight (f,w) < L[w])
L[w]= L[f] + weight(f,w);
Integer.MAX_VALUE.
if (L[w] == Integer.MAX_VAL) {
L[w]= L[f] + weight(f,w);
add w to F;
} else L[w]= Math.min(L[w],
L[f] + weight(f,w));
For all w, L[w]= ∞; L[v]= 0;
F= { v };
while F ≠ {} {
f= node in F with min L value;
Remove f from F;
for each edge (f,w) {
if (L[w] == Integer.MAX_VAL) {
L[w]= L[f] + weight(f,w);
add w to F;
else L[w]=
Math.min(L[w], L[f] + weight(f,w));
n nodes, reachable from v. e ≥ n-1 edges
n–1 ≤ e ≤ n*n
O(n)
O(n log n)
O(e)
O(n-1)
O(n log n)
O((e-(n-1)) log n)
O(n)
O(n + e)
outer loop:
n iterations.
Condition
evaluated
n+1 times.
inner loop:
e iterations.
Condition
evaluated
n + e times.
Complete graph: O(n
2
log n). Sparse graph: O(n log n)
O(n)
2.
1.
1
2
4 3
2.
0.
1.
3.
0.
∞
2.
1.
1
2
4 3
2.
0.
1.
3.
0.
∞