Computing k shortest paths 2393
Fig. 1 A simple graph to illustrate the definitions
– bornNode: the node that generates the current Water.
– dieNode: the node where the current Water will van-
ish.
– parentSplitIndex: the index indicates which Split
generates the current Water.
– reachTime: the time when a Water arrives at its target
node.
Figure 1 explains the above definitions. The Waters
and Splits can be denoted by using the two formats
w(bornNode, dieN ode, par entS plit Ind ex , reachT ime),
and s( parent Nod e, parentS pl i t Ind ex , splitT ime).The
Waters are shown above edges, and the array under node b
is the Split recorded by node b. We set the water flow speed
by unit. At time 20, there is a Water w(a, b, 1, 20) flowing
from node a along edge (a, b ). At time 22, the water reaches
node b, and b splits; this split is subsequently recorded by
node b;the parent Nod e and par entS pl i tInd ex of the split
are a and 1 (also the bornNode and bornN odeSplitI ndex
of w(a, b, 1, 20)). This
Split generates three Waters. One
is for edge (b, d), i.e., w(b, d, 2, 22).Itspar ent S pli t Ind ex
is 2, which is the position the Split (generated this water)
located in the split record list of node b.
3 The WSA algorithm
3.1 Basic idea
Let G be the problem graph and s the source node. First, let
s split and s will immediately generate new waters for all of
its outgoing edges; each water moves along edges (from tail
to head) with a constant speed (same for all waters). A water
will keep moving until it encounters a graph node and then
vanishes. Next, the encountered node will split and generate
new waters for its outgoing edges. By this means, the waters
keep flowing in the whole graph, just like water flowing along
canals. We record each split to their corresponding nodes to
step back the trace of each water. Clearly, at the first time
a node splits, the shortest path from s to this node has been
found. Similarly, if a node has split k times, we can obtain
the k shortest paths from s to this node. As we are interested
in finding k shortest paths, this requires each node to split at
least k times. In the above process, we can find the k shortest
paths from s to each other node. The paths can be obtained
by tracing the tracks of the waters, which will be described
in the next subsection.
We use an example to illustrate how the above idea works.
As shown in Fig. 2, we are interested in computing two
shortest paths from s
0
to each other node. At the beginning,
t = 0, s
0
splits and creates two waters. At time 1.4, water
w(s
0
, s
1
, 0, 1.4) reaches s
1
, then vanishes. s
1
splits and cre-
ates two waters flowing to s
2
and s
3
, respectively. At time 2.3,
w(s
1
, s
2
, 0, 2.3) reaches s
2
, s
2
splits and creates two waters
flowing to s
1
and s
3
. At time 2.4, w(s
1
, s
3
, 0, 1.4) reaches
s
3
and s
3
splits but creates no water because s
3
has no out-
going edge. At time 2.6, w(s
2
, s
1
, 0, 2.3) arrives at s
1
and
then vanishes, s
1
splits and creates two waters. Notice that
s
1
has split two times and would not split anymore. At time
3.0, w(s
0
, s
2
, 0, 0) reaches s
2
, s
2
splits and creates two water
flowing to s
1
and s
3
. The water flow situation at time 3.5 is
the final split result of the example. Since all nodes except
the source node has split 2 times and thus we can obtain 2
shortest paths from s
0
to each other node by tracing the track
of the water.
From the above representation, the basic process of our
algorithm can be summarized as: the water w arrives at its
dieN ode v, then w vanishes and v splits, then we record this
split record to v and node v creates new waters neww for
each outgoing edge e of v.
3.2 Algorithm implementation
In this subsection, we give a non-parallel i mplementation of
WSA algorithm. We first create two data structures named
Split and wat er . The members of both structures are listed
in Sect. 2.Thewat er Li s t is maintained with its wat er
elements ordered in a non-decreasing way according to
reachTime. The pseudo code of the implementation is
shown in Fig. 3. First, in line 5, s splits and we add this
Split to the split list of s, and new waters are created for
each outgoing edge. We then insert these new waters into
wat er L i st , and set the water speed to 1 by default. In
line 6, the main loop begins. We choose the wat er , rep-
resented by w, which has the smallest reachTime from
wat er L i st and erase it from there. If w.die Node has split
more than k times, the while loop is continued. Otherwise,
let w.dieN ode split and new wat er s are created for each
outgoing edge of w.dieN ode. We set these new wat ers’
parent Nod e and parent Index to w.dieN ode and the size
of the water list of w.
dieN ode, respectively. Then we set
these new wat er s’ di eN ode and reachTime to the corre-
sponding edges’ tail node and w.reachTime, add the corre-
sponding edges’ weight. Next these new Watersare inserted
123