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
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))
Relaxation order: 2.0, 4.5, 4.7, 6.4, 5.9, 5.5, 3.6, 5.1, 3.4, 4.8, 1.1, 5.4, 3.7
distanceFromRoot
attribute.
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
pred(v)
and dist(v)
get updated as the algorithm metaSSSP
runs.
Table #1 | Table #2 | Table #3 | Table #4 | Table #5 | Table #6 | Table #7 |
---|---|---|---|---|---|---|
Ethan | John | Isaac | Grace | James | Graham | Bri |
Talia | Drake | Nathan | Trevor | Jordan | Levi | Josiah |
Kristen | Jack | Claire | Andrew | Logan | Blake | Kevin |
Suppose that \(s = \text{pred}(v) = \text{pred}(u)\), but the path \(s \rightarrow v\) is longer than the path \(s \rightarrow u \rightarrow v\). Which edge is tense?
Suppose that \(s \rightarrow \cdots \rightarrow \text{pred}(\text{pred}(v)) \rightarrow \text{pred}(v) \rightarrow v\) is not a shortest path from \(s\) to \(v\). That is, suppose there is a shorter path \(s \rightarrow u_1 \rightarrow u_2 \rightarrow \cdots \rightarrow u_k \rightarrow v\).
TopologicalSort(G):
Call DFSAll(G) to compute finishing times v.post for all v in G
Return vertices in order of decreasing finishing times
So we can topologically sort the vertices in \(O(V+E)\) time.
Recursive Structure: Compute the length of the shortest path from \(s\) to \(v\).
LengthSP(v):
if v = s
return 0
else
minLength <- Infinity
for all edges uv
tryLength <- LengthSP(u) + w(uv)
if tryLength < minLength
minLength <- tryLength
v[1..n]
be the vertex set in topological order. (s = v[1]
)LSP[1..n]
be the length of the shortest path.LSP[v]
depends on LSP[u]
for \(u \prec v\).DagSSSP(s):
for all vertices v in topological order
if v = s
LSP(v) <- 0
else
LSP(v) <- Infinity
for all edges uv
if LSP(v) > LSP(u) + w(uv)
LSP(v) <- LSP(u) + w(uv)
pred(v) <- u
Secretly, LSP[i]
is the same as dist(v[i])
. See Figure 8.9.
DagSSSP
timeDagSSSP(s):
for all vertices v in topological order // O(V + E)
if v = s
LSP(v) <- 0
else
LSP(v) <- Infinity
for all edges uv // need rev(G) to do this
if LSP(v) > LSP(u) + w(uv)
LSP(v) <- LSP(u) + w(uv)
pred(v) <- u
rev(G)
can be computed in \(O(V+E)\) time.Total Time: \(O(V+E)\)