The basic data structure is a graph without direction. However, considering that there are too many subway stations, if the subway station is the vertex of this graph, the resulting graph is too complex, so the shortest path algorithm is not efficient. So our idea is to keep only the transfer station in the graph, and the normal station between the two transfer stations will degenerate into a side.
Based on this idea, the design data structure is
Line number |
Start the transfer station |
End of Transfer Station |
Middle Normal Station Array |
Where the transfer station and the normal station data structure is the same, are the following structural body
struct{
int number;
int mileage;
}station;
Specifically explain:
int number: After getting subway line information, in order to write code conveniently later, we need to encode all the sites, just use the familiar decimal.
It is important to note that the transfer station is to appear as a vertex in the graph without direction. So all the transfer stations are structured so that they are numbered starting from 0, 0,1,2,3,4......n-1, so that the number of vertices in the graph is n.
After assigning the number to the transfer station, we start assigning the number of the general station, for example, we can reserve a larger space for the transfer station, so that the general station is numbered from 100 onwards.
int mileage: Specific to each subway line, this mileage refers to the current station to the next station distance. Each station on this subway line is stored in the distance to the next station, so that the distance information of the entire Metro line can be kept intact.
From the Beijing subway line, intercept a corner:
Now take this as an example specifically to the data structure we designed. We are like the following array:
Line Line 1 |
-1 |
Princess Tomb |
Apple Orchard |
Gu cheng |
Octagonal Amusement Park |
Babaoshan |
Yuquan Road |
Five Pine trees |
Wan Shou Lu |
Line Line 10 |
Princess Tomb |
Haidian Huang Zhuang |
Western Fishing Platform |
Tsz Shou Temple |
Lane Trench |
Changchun Bridge |
Firearms Battalion |
BA Gou |
Suzhou Street |
-1 means no transfer station
In the concrete implementation, the Princess Tomb represents the transfer station, the data structure for the struct station{int 0,int 5} (assuming the transfer station number from the Princess tomb, from the Princess grave to the Military museum mileage of 5)
The apple orchard stands for the general station, the data structure is struct Station{int 101,int 10} (assuming that the normal station number starts from Apple orchard, the mileage from Apple orchard to the Old City is 10)
According to this data structure, design algorithm idea. Algorithm basis: Using Dijkstra can find the shortest path from one vertex to all other nodes in the non-direction graph.
The user enters the start and end points. Here we use numbers instead. Starting point: int start; End point: int end;
We can be divided into 4 different situations:
1. The starting and ending points are common stations
2. The starting point is the transfer station, the terminal is the ordinary station
3. The starting point is the normal station, the destination is the transfer station
4. The starting and ending points are the transfer stations.
Let's consider the first scenario first.
Now it is assumed that all Metro information has been converted into the data structures defined above, and we have an array of all the information. The algorithm steps are as follows:
Step 1: Traverse the above array according to the start and end numbers, and get the following structure:
what line |
start transfer station |
end transfer station |
... |
start |
... |
What line? |
Start the transfer station |
End of Transfer Station |
...... |
End |
...... |
Use station numbers instead of examples:
Indicates that start is between the 0,1 transfer station and end is between the 3,4 transfer stations.
Step 2: First set the source point is 0, using the Dijkstra algorithm, calculated from 0 to 3, from 0 to 4 the shortest path, for the m1,m2;
Then change the source point to 1, run the Dijkstra algorithm, calculate from 1 to 3, from 1 to 4 the shortest path, for M3,M4;
Step 3: Calculate mileage from start to 0 X1,start to 1 miles x2,end to 3 miles X3,end to 4 miles x4 depending on the other element int mileage in the station structure.
Step 4: For the 4 path 0-->3,0-->4,1-->3,1-->4 in step 2, calculate the mileage from the start station to the terminal.
For path 0-->3, mileage =m1 + x1 + x3;
For path 0-->4, mileage =m2 + x1 + x4;
For path 1-->3, mileage =m3 + x2 + x3;
For path 1-->4, mileage =m4 + x2 + x4;
Select the best route based on the minimum mileage.
Now consider the other three cases, only need to make a little change, such as situation 2, the starting point for the transfer station, then only need to set the source point in step 2 as Strat, only need to run the Dijkstra algorithm to the left and right side of the terminal to the transfer station on the line.
The above steps are under the condition that the diagram has been created. The graph is also created from an array of structures populated with metro information. The number of vertices is the number of transfer stations, the edge of the vertex, according to the above example, there are two sides, from 0 to 1, from 3 to 4,.
Team Project--Preliminary design of Metro information Query route planning module