How do I make a simple bus route search Engine?

后端 未结 3 2087
挽巷
挽巷 2020-12-30 16:29

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

Suppos

3条回答
  •  醉梦人生
    2020-12-30 17:04

    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.

提交回复
热议问题