dijkstra's algorithm
an algorithm that finds the minimum distances from a given "origin" vertex to all the other vertices in a weighted graph, in other words, it generates a shortest-path tree for a given vertex.

the method
is the building block of dijkstra's algorithm, it checks whether the path we've taken to get to the vertex we're currently at is shorter than a past path we've taken (the length of the past path at that time would be stored in
,
being the distance array and
being the vertex we've arrived at), and resets the value of
and
accordingly. in this note i refer to this as a relaxing step.
let
be a non-negatively weighted graph. assume that dijkstra is being applied to
starting at some vertex
. at each step, for every vertex
, if
then in
there exists a path
from
to
and:
is true for all
at each step of the dijkstra algorithm, if during one step
, the value of
stays the same and doesnt change again during the execution of the algorithm
-
- if
then
is the second-to-last vertex in the path
let
be a series of relaxing steps that were applied during the execution of the algorithm. the proof is done by induction on
(index of relaxing steps)
base step (for
): before the application of
,
, and for every other vertex
,
and so the claim holds true (in this case)
[[blk:1673212044][induction step]]: induction hypothesis: assume the claim is true after the execution of
, and let
. for each vertex
,
doesnt change, and the claim follows trivially from the induction hypothesis. if
is an empty relaxing step, the claim is also true for
. assume
isnt an empty step, in this case, after the relaxing step
is executed,
and
, according to the induction hypothesis, there is a path from
to
whose length is
and it follows that there is a path from
to
whose length is
, and the last edge in that path is
.
base step (for
[[blk:1673212044][induction step]]: induction hypothesis: assume the claim is true after the execution of
according to lem-dijk-1, for each vertex
and at each step of the algorithm, if
,
is the length of some path from
to
. from the code it follows that the value of
changes only by the relaxing method, and so this value can only get smaller, the correctness of the entailment follows from the definition of
being the minimal path from
to
.
let
be a non-negatively weighted graph, and let
be a minimal path in
, then:
-
for each
- if executing a series of relaxing steps
along
, such that
then
proof for claim 1: we denote
.
for all
and it follows that
. every subpath of a minimal path is a minimal path and so
for each
proof for claim 2: left unwritten
proof for claim 2: left unwritten
main lemma for proof of correctness:
let
be a non-negatively weighted graph. upon applying dijkstra to
, starting at
, each vertex
, by the time it gets removed from
, would have had its demands met.
let
the proof is by induction on the removal of vertices from
base step: after the initialization
and for each vertex
,
, its true that
. the first vertex that gets removed from
is
. since the weights of all the edges arent negative,
, and so
meets the first demand. the second demand of the vertex is empty as it should be (
).
[[blk:1673212044][induction step]]: induction hypothesis: assume the demands are met for each of the vertex that were removed from
in the timestamps
and let
be the vertex that is removed from
at the timestamp
. we have to prove that at the timestamp
the demands of the vertex
are met. given that every vertex in
is reachable from
. let
be a minimal path from
to
. assume that
. let
be the last vertex in
that was removed from
before
. according to the induction hypothesis, by the time of removal of
from
, we know
. right after, relaxing steps are applied from
to its neighbors, one of which is
. the path
is minimal. according to lem-dijk-2, after the execution of this relaxing step, we know
, consider the following cases:
case 1:
, when
is removed from
, the algorithm applies a relaxing step to each of
's neighbors, one of which is
, according to lem-dijk-2, after the execution of
, we know
and
is the second-to-last vertex in the minimal path from
to
.
case 2:
, this happens when
is a minimal path from
to
, this might happen when the weights of the edges leading up to
are 0, which would give other vertices the same distance from the origin as
, and so the algorithm could choose to relax some edges before others, and the "natural" order of traversal could change, but because we know
it follows that
. let
be the vertex that "discovers"
. according to the code
exists in
before
. according to the induction hypothesis the demands for
would already have been met, it follows that after the last relaxing step the demands for
would be met.
base step: after the initialization
[[blk:1673212044][induction step]]: induction hypothesis: assume the demands are met for each of the vertex that were removed from
case 1:
case 2:
[[blk:1667592363][correctness]] of dijkstra:
assume dijkstra is applied to a non-negatively weighted graph
and an origin vertex
. the demands of every vertex
are met. this follows from lem-dijk-3
assume dijkstra is applied to a non-negatively weighted graph
the set of edges
forms a distance tree
for each vertex
, in
there is a path from the origin
to
. it follows that
is connected. in
there is exactly
and so
is a tree. we've already seen that for all
, the only path in
from
to
is a minimal path in
, i.e. 
the cost of initialization (using
) is
the main loop (in
) executes
times, on each iteration it chooses a minimal vertex and improves on all of its neighbors, if
isnt a directed graph, then during the relaxing step we test each edge twice, once in each direction. if
is a directed graph, each edge is tested once. so the cost of all relaxing steps together is
. picking the minimal vertex using the straightforward method also takes
steps. the process of picking the minimal vertex is repeated
times so the total cost is
for non-dense graphs, we can improve the time complexity by turning
into a min heap, picking a minimal vertex would take
. the cost of all picks would be
, on each of the
relaxing steps we would need to do a correction in the heap, the cost of all corrections would be
, so the total cost would be 
the main loop (in
for non-dense graphs, we can improve the time complexity by turning
given a weighted graph
that represents a town's junctions/roads
given
is a subset of junctions with streetlights
given
, propose an algorithm that given
would find the length of the shortest paths from
to each of the vertices in the graph, such that each path wouldnt contain more than 2 junctions with streetlights
complexity of this algorithm is 
given
given
assume we're given a graph
with real weights (allows negative weights) and we wanted to find the distances between the vertices, we can do the following
- find the edge
that has the lowest weight in
- if
, just apply
- otherwise, build a new graph
, such that
and
- run
, which would be a modified version of dijkstra that also stores the actual length (number of edges) of the shortest path between
and any other vertex
- return
, such that
is the output from the modified dijkstra algorithm, and
is the number of edges in the shortest path between
and
.
given an undirected graph
, let
, we define
, we want to write an algorithm that find
.
- define a new graph
- run
- return
let
be a directed non-negatively weighted graph. a vertex is called origin iff
and pit iff
. for every vertex we define the length of the shortest path that starts from some origin, goes through
and ends in some pit as
propose an algorithm that finds
for each vertex in the graph
propose an algorithm that finds
- define a graph
such that
,
,
- run
- define a graph
such that
,
,
- run
- return
for each