ShortBusRide: Dijkstra For Bus Routes
ShortBusRide: Dijkstra For Bus Routes
ShortBusRide: Dijkstra For Bus Routes
James Baker
Eugene Hsu
Roko Kruze
1 Abstra
t:
Trying to nd the bus that one should take to get some where is always a daunting task.
With only maps of bus routes and bus s
hedules one
ould very easily miss the best route
to their destination. The idea behind this model is to be able to nd a path between two
points taking into a
ount transfer times and the time one leaves.
The way in whi
h this model solves the problem is by using Dijkstra's algorithm to solve
the shortest path. Ea
h bus stop will be made up of many sub-stops. Ea
h sub-stop will
represent a bus
oming into the stop or a bus leaving the stop. This will allow Dijkstra's
algorithm to be able to a
ount for transfer times.
Having the problem in a simple graph stru ture allows for algorithms su h as
ShortBusRide
of day. To get around this we allow the sear
h to do look up
alls whi
h will s
an the
time tables instead of doing the edge weight look ups itself. This is explained in further
detail in the implementation and theory se
tion.
3 Implementation details
The implmentation is
overed as follows: rst, a high-level English des
ription of how
we model the bus system, followed by a brief des
ription of the data format and data
stru
tures, and we
on
lude with a pseudo-
ode implementation of our algorithm.
3.1
This se
tion outlines how our model works to give a general sense of how things t
together, but the a
tual implementation details are des
ribed later on.
to start by
on
eptualizing ea
h stop as a vertex, and the edge weights as the ride time
between stops.
We ould then apply Dijkstra's algorithm to the graph and nd the
Stop A
Rt. 1
Stop B
Rt. 2
Stop C
Rt. 2
Rt. 2
Rt. 3
However, this is an over simpli
ation: we ignore the fa
t that as we ride along the route,
the time
hanges, and as the time
hanges, some edge weights between stops be
ome
longer or shorter, appear or disappear. To get around this, at ea
h vertex
al
ulate edge
weights based on what time it is (the departure time plus the travel time) at that vertex.
In other words, edge weights are a fun
tion of path length.
Still, we are over simplifying. At ea
h stop, we have the option of getting o and transfering to another bus. If we were to model ea
h stop as a single vertex, these transfers would
have to be instantaneous and the bus we were transfering to would always be available.
The solution to this is to break up a stop into several smaller verti
ies.
ShortBusRide
Figure 2: Modeling a stop, with a set of "in" and "out" verti
ies, and how stops relate.
Stop B
B1
1B
Stop A
Stop A
In
1A
2A
A1
Out
1A
B2
2B
IN
OUT
B3
3B
B4
4B
1A
A2
2A
IN
OUT
A3
3A
A4
4A
Stop C
2A
C1
...
...
1C
C2
2C
IN
OUT
C3
3C
C4
4C
3.2
Data representation
In this se
tion, we des
ribe how the various pei
es of data t together, both on disk and
in memory.
ShortBusRide
10:30
10:45
11:10
12:50
11:15
11:30
-
11:45
12:00
-
12:30
12:45
11:55
13:35
13:00
13:15
12:25
14:15
The format for the input les will be arranged roughly the equivalent to how the printed
time s
hedules are formated. Ea
h route has its own le with a table in it. Ea
h table
ontains a list of stops in the rst row, and the times that the bus arrives at that stop.
Times where a bus skips that stop are left blank. In this form, we
an read ea
h line from
right to left to determine what a bus's next stop is and what time it will arrive there.
One
aveat worth mentioning is that it's important to separate the dierent dire
tions of a
route. For example, though we think of bus N as being the same bus both northbound and
southbound, it's really two dierent routes. If we don't make this distin
tion, Dijkstra's
algorithm
ould potentially fail to nd an optimal path (returning a sub-optimal one or
failure).
Responsible for loading the datales for all routes, and performing lookups for
a route for a given departure time.
Class Stop
Class Vertex
Keeps a pointer to the previous vertex (to re
onstru
t the path by ba
ktra
king), the time we arrived at the vertex, the stop that this vertex is part of,
whether it's an in or out vertex, and the route that this vertex represents.
3.3
Algorithms
ShortBusRide
3.3.1 Initialization
After we initialize the time s
hedule for all routes, we
an begin
reating the stops and
verti
es of the graph.
table as ne
essary. The task of adding a route that passes through a stop is delegated to
the stop (whi
h in turn adds verti
es to its in and out sets). The task of linking verti
es
is done at the time of the query (due to the fun
tional nature of edge weights).
See
algorithm 1.
4 Theory
Let
G = (V; E )
f(v; w; t) j (v; w) 2
E; t
0g
Let
fun
tion that
omputes the weight of an edge given a
urrent time
Bus systems
an be modeled with su
h graphs. We
an let
t, D(v; w; t)
is a
be the set of (v; w ) su h that there is a dire t bus going from stop
a urrent time
Intuitively,
t.
v
to stop
w.
Given
to stop
w.
This allows the model to a
ount for time dependent fa
tors su
h as tra
density.
The single sour
e shortest paths problem (SSSP) is one of the most
ommonly known
problems in graph theory.
shortest path is from
G.
s,
t,
s,
what the
G,
F,
a sour e
G?
Call this problem the time dependent single sour
e shortest paths problem (TDSSSP).
Of the algorithms to solve the standard SSSP problem, the most
ommonly known is
Dijkstra's algorithm. It is stated as follows.
Dijkstra(G; s)
fg
K= s
d [ s = 0
for all v 2 V K do
if (s; v ) 2 E then
d[v
weight (s; v )
d[v
else
end if
ShortBusRide
end for
while K 6= V do
v
K
vertex in (V
for all
d[w
K)
with minimum
d[v
[v
fw j (v; w) 2 E g do
min fd[v + weight (v; w ); d[w g
end for
end while
We
an make a slight modi
ation to the algorithm to t the time dependent graph
formulation by
hanging
and
hanging
to
weight (s; v )
D(s; v; 0)
d[v
d[v
to
f
f
d[w
d[w
to a
ertain set of fun tions, TD-Dijkstra will solve the TDSSSP problem. We laim that
TD-Dijkstra solves the TDSSSP problem if D has the FIFO (rst in, rst out) property. Basi
ally, the FIFO property states that arriving later at a vertex never yields an
advantage. In the bus system model, the FIFO property holds, sin
e arriving at a stop
earlier
an't make you later to your nal destination. More formally,
property if
j ! i + D(v; w; i) j + D(v; w; j )
8v; w 2 V 8i; j 0:
(1)
TD-
Dijkstra
Proof
and
su h that
the the sour e vertex for TDSSSP. Assume without loss of generality that the start time
t = 0.
Given a vertex
v,
dene
fj
2 E g.
to
(s; v )
Intuitively,
B (v )
is
v.
an be formally dened as follows.
(s; v ) =
minu B (v )
if
if
v=s
v=s
(2)
ShortBusRide
t + D(u; v; t)
is al ulated by minimizing
g.
t + D(u; v; t)
t = (s; u). Thus
(s; v ) =
to
u.
as follows.
if
if
Be ause
has the
we an restate
(s; v )
v=s
v=s
(3)
This implies the optimal substru
ture property; that is, optimal solutions are built from
optimal solutions. Given the above result, the
orre
tness of TD-Dijkstra follows. The
proof mirrors the
orre
tness proof of Dijstrka.
jV j
jV j deletions from the priority queue, ea
h taking O(log jV j) time.
As for updating the values of d[v , this is done at most jE j times (sin
e every edge is visited
at most on
e). Ea
h update potentially requires a priority
hange, whi
h takes O (log jV j)
time. Thus the total time
omplexity of Dijkstra is O (jV j log jV j + jE j log jV j).
Suppose Dijkstra is implemented with adja
en
y lists and priority queues. It does
iterations, leading to
If omputing
D(v; w; t)
is an
O(1)
D(v; w; t) is
O( V log V + X E log E )
j j
j j
j j
j j
an
O(X )
time omplexity.
5 Simpli
ations:
With limitations on getting data from Metro themselves the data used in the model is
very over simplied. This is due to the fa
t that few stops are listed on Metro's web site.
Thus the model will only
onsist of these few bus stops.
In the real world a person
ould ride a bus to a
ertain lo
ation, get o and walk to
another bus stop.
a
ount many variables would have to be taken in, whi
h is far beyond the s
ope of this
problem.
6 Improvements:
Many improvements
an be made to this model. A person may want to nd the latest time
they would leave and still have the same travel time. This would in
lude a ba
ktra
king
sear
h of the graph in order to nd the maximum time in whi
h this would still work.
ShortBusRide
The model
ould also solve for the minimized travel time for any arbitrary time of departure. Thus a person
ould nd the best times to leave during the day to minimize travel
time. However, to solve this problem would mean solving what is probably an NP-hard
algorithm sin
e a bran
h and bound approa
h would have to be taken.
A person may also want to minimize the number of transfers between buses as well. This
ould be a large fa
tor in pi
king a path for a person who is handi
apped. Where getting
on and o the bus is a time
onsuming task.
7 Results
All your results are belong to us.
8 Con
lusion
Our model ro
ks.
ShortBusRide
Algorithm 1 initGraph
begin
TimeShedule t := LoadTimeS
hedule()
Map sMap := CreateEmptyMap()
forea
h Route r in t
forea
h Stop s in r
if sMap[s 6= null
sMap[s = CreateNewStop()
endif
sMap[s.add(r)
endfor
endfor
end
ShortBusRide
Algorithm 2 sear
h
10