Bus transfer simple algorithm __ algorithm

Source: Internet
Author: User

Simple Bus transfer algorithm:

Three tables (most simplistic, do not consider fuzzy query, one-way street and other things):
1, the Site table Stop (stop_id,stop_name)
2, route table line (line_id,line_name)
3, Route site table (point line Relational table) Linestops (line_id, stop_id, seq) the SEQ here refers to the order of a site in a line.

Now analyze the algorithm:
1, Direct Line
First of all, according to two site names to obtain the respective IDs of two sites, defined here as Id1,id2
and then query
Select line_id from
(select line_id from linestops where stop_id = id1) A
(select line_id from linestops where stop_id = Id2) B
where a.line_id = b.line_id
To get a direct list of lines
2, one transfer
First of all, according to two site names to obtain the respective IDs of two sites, defined here as Id1,id2
Then search two sites through the direct way to each of the site collection, and finally their intersection is the transfer site we need.
Select stop_id from
(
Select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = id1)
A
(
Select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = id1)
) B
where a.stop_id= b.stop_id
To get the transfer station (there may be multiple or 0), the rest is to show that can reach the transfer station on both sides of the line, which through the front of the direct query can be.
3, two times transfer
First of all, according to two site names to obtain the respective IDs of two sites, defined here as Id1,id2
The central idea of the algorithm is that site 1 is able to have a direct line between all the sites that are reachable through all of the site collection A, and site 2 can b,a and B through all the site collections that arrive directly.
One step at a step:
Site 1 is able to gather all the sites through the direct arrival of a:
Select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = id1)
Site 2 can be reached by direct access to all of the site collection B:
Select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = Id2)
And the direct query is
Select line_id from
(select line_id from linestops where stop_id = id1) C
(select line_id from linestops where stop_id = Id2) D
where c.line_id = d.line_id
We change =id1 and =id2 into in (select ...) A and in (select ...) B
So at the end of our query is
Select line_id from
(select distinct line_id from Linestops where stop_id in "A") C
(select distinct line_id from Linestops where stop_id in "B") D
where c.line_id = d.line_id
Where "a" is
(select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = ID1))
where "B" is
(select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = Id2))
So we found the middle transfer line (maybe a few or 0), each of the listed assumptions is named X-ray, and the next step is to find a direct line from site 1 to any of the x sites, and a direct line from site 2 to any site in X.
So similar to the previous algorithm, we are in the site 1 all reachable sites to find and line x intersect site, and then to find the two points of the line
Select stop_id from
(select distinct stop_id from Linestops where line_id in
(select line_id from linestops where stop_id = ID1)) A
(select stop_id from linestops where line_id = X) B
where a.stop_id = b.stop_id
Find the site, the following is based on a direct query has been resolved to find the line.
Site 2 is similar.
The above algorithm has one advantage, all is the SQL completes the search, therefore the ASP code only needs a few line loops.
But the disadvantage is: slow, after all, may involve hundreds of of SQL queries. And only the simplest SQL method to calculate all the options can be transfer, does not involve the optimal/shortest algorithm. If it is the shortest path, it must use a special structure and algorithm.

In addition:

According to the starting point and end point of the traveler input, determine the starting bus site A and the destination B for the trip to choose. SearchDatabase, query site A and site B have the same car through, if there is one or a few direct lines, through the comparison of the shortest distance of the bus line recommended to the traveler. If not, calculate whether there is a public site C between site A and site B, and transfer from Site C to site B. There are two kinds of situations: (1) If so, it belongs to a transfer. Calculates that there is no same bus pass between site A and public site C, and that there is no same bus pass between site B and public Site C, which is saved to set Y. After comparing the two sets, we can get the bus lines from site A through public site C to site B, compare them in these lines, and select the shortest distance recommended to the traveler. (2) If there is no public site C, there will be a transfer of two times. Save all sites for each bus route through site A to set O, and all sites that pass each line of site B are saved to the set P. Compare these two sets, first take a bus through site A to a certain site D, calculated site D and Site B there is no public site E, if there is a site D, E for transfer site. There may be a variety of options, compared to the shortest recommended distance to the traveler. If no public site e exists, it is not possible to reach site B from site A after two transfers and stop the search calculation.
The best route of bus travel is specific algorithm:
1 Enter the starting site A and destination B;
2) search system database, after the starting site a bus line Save as X (i) (I=1,2,3...,m,m is a positive integer), through the destination station B bus line Save as Y (j) (j=1,2,3,... N,.N is a positive integer);
3) to determine whether there is X (i) =y (j), will satisfy the condition of the deposit Z. If the z=1, then the bus line x (i) is the Y (j) from Site A to site B directly to the optimal line, the output results and end the operation. Z≥1, calculates the distance of each line in Z, selects a line with shortest distance, outputs the result and ends the operation;
4 Search system database, bus line x (i) contains the site to save as O (i,u) (u=1,2,3...,g,g positive integer) bus line y (j) contains the site stored as P (j,v) (V=1,2,3...,h,h is a positive integer);
5) To determine whether there is O (i,u) = P (j,v), will meet the condition of deposit W. If the w=1, then the site O (i,u) is P (j,v) from site A to site B of a transfer site, bus line x (i), Y (j) for the transfer of the optimal route, output results and end the operation. If w≥1, calculate the distance of each transfer route respectively, select a line with shortest distance, output the result and end the operation;
6 Search system database, after the site O (i,u) of the bus line Save as R (k) (K=1,2,3...,p,p is a positive integer), bus line R (k) contains the site to Save as G (K,t) (T=1,2,3...,q,q is a positive integer);
7) To determine whether there is g (k,t) =p (J,V), will meet the conditions of the deposit S. If S=1, then site G (k,t) is P (j,v) from site A to site B of the two transfer site, bus line x (i), R (k), Y (j) for transfer two times the optimal route, the output results and end the operation. If s≥1, calculate the route distance of each transfer two times respectively, select a line with shortest distance, output the result and end the operation;
8 The above steps do not find a suitable bus line, the output "did not find the transfer times not more than two times the optimal bus line", the end of the operation.
In this paper, the optimal route algorithm for bus travel is mainly based on distance. After the transfer scheme is obtained, the time factor can be considered further to find a better transfer scheme, which needs further discussion and research.

One, the bus transfer problem algorithm is divided into two steps:

1, constructs and solves the transfer matrix, obtains the bus transfer plan (namely from the starting point to the destination least transfer frequency, and the transfer site).
There are three forms of implementation:
①, get all the node T-matrices, two nodes directly set to 1, two nodes do not pass or need to transfer set to 0; see "The Best path algorithm for public transportation system"
② obtains the T-matrix of all lines, the intersection of two lines is set to 1, and two lines are disjoint to 0; see the study of bus transfer algorithm based on adjacency matrix (note the algorithm belongs to idealized algorithm)
③ This 2nd step to narrow the range, and then use the first step to obtain the exact result

2, according to the minimum number of transfers, narrowing the scope of the solution to the initial site and target site between the shortest path, and then get the best path.
The shortest path is obtained in the following ways:
①, the simplest way to achieve: if the above 1 of the minimum number of transfers, transfer site can be solved, only need to query the minimum number of transfers, all possible paths in the number of sites with the fewest numbers of lines that is required (the premise, assuming that all the distance between the site is roughly equal);
②, if the above 1 only to find the minimum number of transfers, then need to use algorithms to find the shortest path, the common method is to improve the Dijkstra algorithm, that is, in the Diskstra algorithm to add a minimum transfer algorithm to determine A;
③, in the game design program, often involves the shortest path of the above, the most commonly used algorithm is a * algorithm, see "Game Map Shortest Path search design and implementation" and "a algorithm"
④, K (<=3) gradually short path search algorithm, this algorithm used more abroad, because the algorithm is more troublesome, domestic research is not too much, see "K (≤3) gradually short path search algorithm research", "a new kth shortest path search algorithm" and " Study on the model of transfer ticket clearance in urban rail Transit (master thesis)
⑤, ant algorithm, see "An imitation of Dijkstra ant algorithm"
Among them, the ②③④⑤ steps need to rely on the minimum number of transfer to achieve the goal of reducing complexity.

If the above algorithm can be well implemented, it needs to establish a good data structure

1, data storage format
I asked two people who actually did this, one uses an array to store, and the other is a combination of linked lists and arrays (that is, long linked lists using arrays, others using linked lists), in fact, the reason for these two storage forms, Mainly because these two people in the algorithm focus on different, the previous emphasis on the implementation of the minimum number of transfers, the latter is focused on the implementation of the shortest path.
2, data structure storage, can refer to "Urban public transport network Travel Path Choice computer algorithm research", "GIS based standard public transport basic information System" "Based on GIS bus passenger travel route Choice Model", considering the bus transfer, so usually, close to the site ( The site name is the same but the location is different, the site name is not the same, but the location is very close to the public transport network topology as a node. The selection of the arc segment weights between the
3 and two nodes (to calculate the shortest path required): Typically, the distance between two nodes is replaced, but some people use the bus speed or travel time between these two nodes as weights. The following figure is given by the Shenyang public transport network loop car parameters, based on this parameter and the distance between two sites can be calculated between the travel time (normal). Of course, the more complete the factors considered, the more humane, but the program added a lot of difficulty, such as: Consider "waiting time, site distance, line of heat (to the business district), Time (rush hour, etc.)" and other factors, these factors are difficult to quantify, so, usually, The simplest method uses distance as the weight of the arc segment between two nodes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.