Order of addition: KH KJ JI IF FC FG FE GD DB DA
Use a priority queue that does DecreaseKey
, Insert
, and ExtractMin
each in \(O(\log V)\) time.
Jarník(G):
pick random vertex v
T <- graph containing just v
Insert all neighboring edges into priority queue (key = weight)
while the priority queue is not empty:
ExtractMin edge uv
if uv is unmarked and u and v are not both in T:
mark uv
add uv to T
for each unmarked edge neighboring u and v
Insert edge (key = weight)
ExtractMin
and Insert
’s.JarnikTree(G, start_vertex):
tree = Tree()
active_edges = PriorityQueue()
add G[start_vertex].edges to active_edges (sorted by edge weight)
while tree.node_count != G.node_count:
safe = pop from active_edges
if G[safe] in tree: continue
add G[safe] to tree
add G[safe].edges to active_edges (sorted by edge weight)
return tree
A B C D E F G H I J K
A 0 7 0 3.2 0 0 0 0 0 0 0
B 7 0 8.9 3 0 0 0 0 0 0 0
C 0 8.9 0 9 6 2 0 0 0 0 0
D 3.2 3 9 0 0 5 4.1 0 0 0 0
E 0 0 6 0 0 4 0 5.7 0 0 0
F 0 0 2 5 4 0 2.6 0 3.7 0 0
G 0 0 0 4.1 0 2.6 0 0 0 7.5 0
H 0 0 0 0 5.7 0 0 0 8 0 4.7
I 0 0 0 0 0 3.7 0 8 0 1 6.2
J 0 0 0 0 0 0 7.5 0 1 0 5.3
K 0 0 0 0 0 0 0 4.7 6.2 5.3 0
// s is starting vertex
// M is adjancency matrix
Jarnik(M[V,V],s):
T <-({s},∅)
neighbors <- []
neighbors.add(s)
while neighbors is not empty
for each e ∈ neighbors
min <- infinity
for i <- 1 to |V|
if M[e,i] != 0 and M[e,i] < min
min <- M[e,i]
newNeighbor <- i
add newNeighbor to T
neighbors.add(newNeighbor)
return T
Time: \(O(V^3)\)
We find the shortest path by updating the tentative shortest paths until we can’t make them any better.
An edge \(u\rightarrow v\) is called tense if \(\text{dist}(u) + w(u\rightarrow v) < \text{dist}(v)\).
InitSSSP(s): Relax(uv):
dist(s) <- 0 dist(v) <- dist(u) + w(uv)
pred(s) <- Null pred(v) <- u
for all vertices v != s
dist(v) <- Infinity
pred(v) ← Null
MetaSSSP(s):
InitSSSP(s)
while there is at least one tense edge // NOTE: need to specify how to check
Relax any tense edge // NOTE: need to specify how to choose
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
Kristen | Claire | Trevor | Levi | Andrew | Drake | Jordan |
Jack | Logan | Talia | Isaac | Kevin | Blake | Josiah |
Graham | Grace | John | James | Bri | Nathan | Ethan |
Starting at the leftmost vertex \(s\), apply repeated Relax
ings to produce the SSSP tree for the given graph. Make note of a relaxing that you do where none of the distances are \(\infty\).
Use a priority queue that does DecreaseKey
, Insert
, and ExtractMin
each in \(O(V)\) time.
DijkstraSSSP(s):
InitSSSP(s)
for all vertices v
Insert(v, dist(v))
while the priority queue is not empty
u <- ExtractMin()
for all edges uv
if uv is tense
Relax(uv)
DecreaseKey(v, dist(v))
(See Figure 8.12)
Step through DijkstraSSSP
on the Jamboard. Keep track of each Relax
call (which edge gets relaxed, and in which order).
DijkstraSSSP(s):
InitSSSP(s)
for all vertices v
Insert(v, dist(v))
while the priority queue is not empty
u <- ExtractMin()
for all edges uv
if uv is tense
Relax(uv)
DecreaseKey(v, dist(v))
DecreaseKey
, Insert
, and ExtractMin
are all \(O(\log V)\).DecreaseKey
, at most \(V\) Insert
, and ExtractMin
.dist[]
to store the distances for each vertex.BellmanFordSSSP(s)
dist[s] <- 0
for every vertex v != s
dist[v] <- Infinity
for i <- 1 to V − 1
for every edge uv
if dist[v] > dist[u] + w(uv)
dist[v] <- dist[u] + w(uv)
Time: \(O(VE)\)