san
san

Reputation: 290

How do I make a simple bus route search Engine?

[Not:e user is asking this again at Development of railway enquiry system, how to model Trains, Stations and Stops? ] My Problem Description:

Suppose I have a BUS-123 in ROUTE-1 it will travel through A, B, C, D, E, F, G, H and BUS-321 in ROUTE-2 through D, E, F, X, Y, Z . if someone enters B as a source point and F as a destination point then ROUTE-1 with BUS-123 should display in the result. But if someone enters H as a source and A as destination result should not display, because returning may not always same with one that is traveled. But if a person enters A as a source and Z as destination then BUS-123 with ROUTE-1 and BUS-321 with ROUTE-2 should display.

My Problem is: How do I store that route information in Database? if i store in RDBMS like the following

BUS_NUMBER   ROUTE_NUMBER    VIA_ROUTES
BUS-123      ROUTE-1         A, B, C, D, E, F, G, H
BUS-321      ROUTE-2         D, E, F, X, Y, Z

Then how my search will work. I mean how to search it in a string. And if I store all the VIA_ROUTES in different different columns then how it will be..? Please suggest me with your own technique. It is not urgent but I am planning to make a basic bus route search, so your comment with help is appreciated.

Upvotes: 4

Views: 2317

Answers (3)

Nicholas Carey
Nicholas Carey

Reputation: 74345

I'd model it as a cyclic graph. Each bus stop is represented by a vertice. Each direct connection between two stops is represented by an edge labelled with the route number; consequently, each route is a sequence of connected edges. Make the edges directed, too. Not all routes travelling from stop A to stop B will necessarily also travel from stop B to stop A in the other direction.

Probably want to populate each edge with the estimated travel time, a measure (or measures) of variance for that leg -- at 2am on a Sunday night, the variance might be low, but at 5pm on a Friday evening, it might be very high, and list of departure times as well.

Then its a matter of graph traversal and finding the "least cost" route, however you choose to define "least cost" -- Factors you might want to consider would include:

  • Total travel time
  • Total time spent waiting for the next leg to depart.
  • Wait time at any individual stop.
  • Distance?

One should note that too much wait time is bad (ever spend 40 minutes waiting for a bus in January when it's -10 F?). Too little is bad, too, as it increases the probability of missing a connection, given that buses tend to have a fairly large variability to their schedules since they are highly responsive to fluctuations in local traffic conditions.

That's how I would do it.

I don't believe I'd try to solve it directly in SQL, though.

The model is a good fit for SQL, though. You need the following entities, and then some, since you'll need to represent schedules, etc.:

  • Stop. A Bus stop. The vertices of the graph.
  • Route. A bus route.
  • Segment. The direct link between two stops. The edges of the graph.
  • RouteSegment. An associative entity representing ordered sequence of segments that composes the route.

Upvotes: 4

halfer
halfer

Reputation: 20467

I'd have a route table and a route_part table. The latter would contain a reference to the route, plus an ordinal number for sorting, and a reference to a stop table. Thus, you can store any route.

In terms of searching, if you wish to search for a route between two stops, you could look up the two stops in the route_part table and see if they appear on the same route in any cases (bearing in mind that a route may exist in one direction and not the other).

Upvotes: 0

Cybercartel
Cybercartel

Reputation: 12592

I think the bus_numbers aren't important because you can look them up later. Maybe what you need is to create a 2d matrix with the bus_stops in a big matrix having them all and then use a graph traversing algorithm like dijkstra to find the shortest path from A to B. When you got that you can easily lookup the bus_numbers and show them to the client. Thus I think your database is already very good.

Upvotes: 0

Related Questions