Basics of shortest paths: Difference between revisions

From Algowiki
Jump to navigation Jump to search
Line 63: Line 63:
## Append <math>(v,w)</math> at the beginning of <math>p</math>.
## Append <math>(v,w)</math> at the beginning of <math>p</math>.


== Shortest-paths arborescence ==
== Constructing shortest-paths arborescence ==


'''Statement:'''
As said [[#Valid distance property|here]], all common algorithms for shortest-paths problems apply a stand update operation. This operation may be extended as follows.


Each node <math>v\in V\setminus\{s\}</math> holds one of its incoming arcs as an additional attribute. At the beginning, this attribute is initialized as void (conforming to <math>d(v)=+\infty</math>). Whenever the standard update operation is applied to some arc <math>(v,w)</math>, <math>(v,w)</math> becomes the incoming arc of <math>w</math>.


'''Extension of shortest-paths algorithms:'''
'''Statement:'''
This modification may be applied to algorithms for shortest-paths problems of all types:
Suppose all cycles have strictly positive lengths. On termination of the shortest-path algorithm, the chosen arcs forms a spanning arborescence rooted at <math>s</math>. For each <math>v\in V</math>, the path in this arborescence is a shortest <math>(s,v)</math>.
# [[Single source single target shortest paths]]
# [[Single source shortest paths]]
# [[All pairs shortest paths]]
 
All common algorithms for shortest-paths problems appöy a basic update step: For an arc <math>(v,w)\in A</math>, it is checked whether <math>d_\ell(w)>d_\ell(v)+\ell(v,w)</math>, and if so, it is set <math>d_\ell(w):=d_\ell(v)+\ell(v,w)</math>.
 
In the extension, each node <math>v\in V</math> holds one of its incoming arcs as an additional attribute. At the beginning, this attribute is initialized as void. When


'''Proof:'''
First we show <math>d(w)=d(v)+\ell(v,w)</math> for each chosen arc <math>(v,w)\in A</math> on termination. Since <math>(v,w)</math> is chosen, <math>(v,w)</math> is the last arc over which <math>d(w)</math> was updated in the algorithm. At this moment, we indeed have <math>d(w)=d(v)+\ell(v,w)</math> As <math>d</math>-values never increase, <math>d(w)<d(v)+\ell(v,w)</math> is not possible on termination. On the other hand, <math>d(w)>d(v)+\ell(v,w)</math> violates the [[#Valid distance property|valid distance property]].


By construction, the node <math>s</math> has indegree zero and all other nodes have indegree one. There cannot be a cycle <math>v_1-v_2-\cdots v_k-v_1</math> because this would imply <math>d(v_1)=d(v_k)+\ell(v_k,v_1)=d(v_{k-1})+\ell(v_{k-1},v_k)+\ell(v_k,v_1)=\cdots=d(v_1)+\ell(v_k,v_1)+\cdots+\ell(v_1,v_2)>d(v_1)</math>.


Whenever the current distance value of a node is updated in a sh
In summary, the claim is proved.

Revision as of 15:52, 1 December 2014

Path lengths and distances

Let [math]\displaystyle{ G=(V,A) }[/math] be a simple directed graph and for each arc [math]\displaystyle{ a\in A }[/math] let [math]\displaystyle{ \ell(a) }[/math] be a real number, the length of [math]\displaystyle{ a }[/math].

  1. The length of an ordinary path (incl. ordinary cycles) is the sum of the lengths of all arcs on this path.
  2. Depending on the context, the length of a generalized path (incl. generalized cycles) is either defined identically to ordinary paths or, alternatively, the lengths of the backward arcs are not added but subtracted.
  3. If the length of an ordinary or generalized cycle is negative, this cycle is called a negative cycle.
  4. For two nodes, [math]\displaystyle{ s,t\in V }[/math]:
    1. A shortest path from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ t }[/math] is an [math]\displaystyle{ (s,t) }[/math]-path with minimum length among all [math]\displaystyle{ (s,t) }[/math]-paths.
    2. The distance from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ t }[/math] is the length of a shortest [math]\displaystyle{ (s,t) }[/math]-path.

Remarks:

  1. In this context, undirected graphs are usually regarded as symmetric directed graphs such that two opposite arcs have the same length.
  2. If there are no negative cycles, the distances from a node to itself is zero because the trivial path with no arcs has length zero.

Subpath property of shortest paths

Statement: For [math]\displaystyle{ s,t\in V }[/math] let [math]\displaystyle{ p }[/math] be a shortest [math]\displaystyle{ (s,t) }[/math]-path. Let [math]\displaystyle{ v,w\in V }[/math] be two nodes on [math]\displaystyle{ p }[/math] such that [math]\displaystyle{ v }[/math] precedes [math]\displaystyle{ w }[/math] on [math]\displaystyle{ p }[/math]. Then the subpath of [math]\displaystyle{ p }[/math] from [math]\displaystyle{ v }[/math] to [math]\displaystyle{ w }[/math] is a shortest [math]\displaystyle{ (v,w) }[/math]-path.

Proof: Let [math]\displaystyle{ p_1 }[/math], [math]\displaystyle{ p_2 }[/math] and [math]\displaystyle{ p_3 }[/math] denote the subpaths of [math]\displaystyle{ p }[/math] from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ v }[/math], from [math]\displaystyle{ v }[/math] to [math]\displaystyle{ w }[/math], and from [math]\displaystyle{ w }[/math] to [math]\displaystyle{ t }[/math], respectively. Suppose for a contradiction that some [math]\displaystyle{ (v,w) }[/math]-path [math]\displaystyle{ p'_2 }[/math] is shorter than [math]\displaystyle{ p_2 }[/math]. Then the concatenation [math]\displaystyle{ p_1+p'_2+p_3 }[/math] would be a shorter [math]\displaystyle{ (s,t) }[/math]-path than [math]\displaystyle{ p }[/math].

Remark: Usually, only subpaths at the beginning of a shortest path are considered. That restricted version is called the prefix property.

Valid distance property

Let [math]\displaystyle{ s\in V }[/math] and for each node [math]\displaystyle{ u\in V }[/math] let [math]\displaystyle{ d_\ell(u) }[/math] denote the distance from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ u }[/math] with respect to the arc lengths [math]\displaystyle{ \ell }[/math].

Statement: For [math]\displaystyle{ (v,w)\in A }[/math], it is [math]\displaystyle{ d_\ell(w)\leq d_\ell(v)+\ell(v,w) }[/math].

Proof: The left-hand side of the inequality is the length of a shortest [math]\displaystyle{ (s,w) }[/math]-path, whereas the right-hand side is the length of some [math]\displaystyle{ (s,w) }[/math]-path (viz. the concatenation of a shortest [math]\displaystyle{ (s,v) }[/math]-path and [math]\displaystyle{ (v,w) }[/math]).

Remark: Node labels [math]\displaystyle{ d }[/math] that fulfill the valid distance property need not be distances.

Standard distance update: In all common shortest-paths algorithms, an attribute [math]\displaystyle{ d_s(v) }[/math] is maintained for each [math]\displaystyle{ v\in V }[/math]. This attribute always holds the length of some [math]\displaystyle{ (s,v) }[/math]-path and should be the true distance from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ v }[/math] on termination. In virtually all standard algorithms, the following update operation is the (only) operation to update [math]\displaystyle{ d }[/math]-values:

  1. Choose some arc [math]\displaystyle{ (v,w)\in A }[/math].
  2. If [math]\displaystyle{ d_s(w)\gt d_s(v)+\ell(v,w) }[/math], then set [math]\displaystyle{ d_s(w):=d_s(v)+\ell(v,w) }[/math].

Distances along shortest paths

Let [math]\displaystyle{ s\in V }[/math] and for each node [math]\displaystyle{ u\in V }[/math] let [math]\displaystyle{ d_\ell(u) }[/math] denote the distance from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ u }[/math] with respect to the arc lengths [math]\displaystyle{ \ell }[/math].

Statement: For an arc [math]\displaystyle{ (v,w) }[/math], it is [math]\displaystyle{ d_\ell(w)=d_\ell(v)+\ell(v,w) }[/math] if, and only if, [math]\displaystyle{ (v,w) }[/math] is the last arc of some shortest [math]\displaystyle{ (s,w) }[/math]-path.

Proof: First consider the case that [math]\displaystyle{ d_\ell(w)=d_\ell(v)+\ell(v,w) }[/math]. Then the concatenation of the shortest path from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ v }[/math] and [math]\displaystyle{ (v,w) }[/math] has length [math]\displaystyle{ d_\ell(w) }[/math] and is thus shortest.

So, consider the case that [math]\displaystyle{ d_\ell(w)\neq d_\ell(v)+\ell(v,w) }[/math]. Due to the valid distance property, it is then [math]\displaystyle{ d_\ell(w)\lt d_\ell(v)+\ell(v,w) }[/math]. Suppose for a contradiction that some shortest [math]\displaystyle{ (s,w) }[/math]-path is of the form [math]\displaystyle{ p+(v,w) }[/math]. Then [math]\displaystyle{ p }[/math] would have length [math]\displaystyle{ d_\ell(w)-\ell(v,w)\lt d_\ell(v) }[/math], which contradicts the definition of [math]\displaystyle{ d_\ell(v) }[/math].

Reconstruction of a shortest path from the distances

Suppose that for each node [math]\displaystyle{ v\in V }[/math] the distance [math]\displaystyle{ d_\ell(v) }[/math] from [math]\displaystyle{ s }[/math] to [math]\displaystyle{ v }[/math] is given. For [math]\displaystyle{ t\in V }[/math], a shortest [math]\displaystyle{ (s,t) }[/math]-path [math]\displaystyle{ p }[/math] can be constructed as follows:

  1. Initialize [math]\displaystyle{ p }[/math] to be empty.
  2. Set [math]\displaystyle{ w:=t }[/math].
  3. While [math]\displaystyle{ w\neq s }[/math]:
    1. Find an arc [math]\displaystyle{ (v,w) }[/math] such that [math]\displaystyle{ d_\ell(w)=d_\ell(v)+\ell(v,w) }[/math].
    2. Append [math]\displaystyle{ (v,w) }[/math] at the beginning of [math]\displaystyle{ p }[/math].

Constructing shortest-paths arborescence

As said here, all common algorithms for shortest-paths problems apply a stand update operation. This operation may be extended as follows.

Each node [math]\displaystyle{ v\in V\setminus\{s\} }[/math] holds one of its incoming arcs as an additional attribute. At the beginning, this attribute is initialized as void (conforming to [math]\displaystyle{ d(v)=+\infty }[/math]). Whenever the standard update operation is applied to some arc [math]\displaystyle{ (v,w) }[/math], [math]\displaystyle{ (v,w) }[/math] becomes the incoming arc of [math]\displaystyle{ w }[/math].

Statement: Suppose all cycles have strictly positive lengths. On termination of the shortest-path algorithm, the chosen arcs forms a spanning arborescence rooted at [math]\displaystyle{ s }[/math]. For each [math]\displaystyle{ v\in V }[/math], the path in this arborescence is a shortest [math]\displaystyle{ (s,v) }[/math].

Proof: First we show [math]\displaystyle{ d(w)=d(v)+\ell(v,w) }[/math] for each chosen arc [math]\displaystyle{ (v,w)\in A }[/math] on termination. Since [math]\displaystyle{ (v,w) }[/math] is chosen, [math]\displaystyle{ (v,w) }[/math] is the last arc over which [math]\displaystyle{ d(w) }[/math] was updated in the algorithm. At this moment, we indeed have [math]\displaystyle{ d(w)=d(v)+\ell(v,w) }[/math] As [math]\displaystyle{ d }[/math]-values never increase, [math]\displaystyle{ d(w)\lt d(v)+\ell(v,w) }[/math] is not possible on termination. On the other hand, [math]\displaystyle{ d(w)\gt d(v)+\ell(v,w) }[/math] violates the valid distance property.

By construction, the node [math]\displaystyle{ s }[/math] has indegree zero and all other nodes have indegree one. There cannot be a cycle [math]\displaystyle{ v_1-v_2-\cdots v_k-v_1 }[/math] because this would imply [math]\displaystyle{ d(v_1)=d(v_k)+\ell(v_k,v_1)=d(v_{k-1})+\ell(v_{k-1},v_k)+\ell(v_k,v_1)=\cdots=d(v_1)+\ell(v_k,v_1)+\cdots+\ell(v_1,v_2)\gt d(v_1) }[/math].

In summary, the claim is proved.