Design and implementation of route planning system - data structure final homework (picture)

It was an ordinary data structure final assignment in the first semester of my sophomore year, and DDL was during the Spring Festival, so it didn't sound very ordinary. The question requirements and experimental report are directly integrated below, and the operation manual is omitted.

1. Task Restatement

Construct a route map for a given XM Airlines based on its existing route data. On this basis, a simple route planning system is designed. The system can return flight route solutions (such as flight ID sequence table) according to different needs put forward by users. For route data, see the "flflight-data.csv'' file in the attachment area of ​​the experimental operation. As shown in the figure, there are a total of 2346 routes, involving 79 airports, 142 aircraft, and 514 flights.

 2. Experimental purpose

Use the basic knowledge of graph structure, adjacency matrix and adjacency list to construct a route graph for a given XM airline based on the company's existing route data. On this basis, a simple route planning system is designed. The system can return flight route solutions based on different needs put forward by users.

3. Function introduction

  1. Complete a depth-first traversal starting from any airport and output all reachable airport numbers and paths. The airports passed in all main paths cannot be repeated.
  2. Complete the breadth-first traversal starting from any airport and output all reachable airport numbers and paths. The airports passed in all main paths cannot be repeated.
  3. For direct flights or 1 transfer only, determine the connectivity of any two airports and provide a Flight ID sequence table.
  4. Find the shortest flight time (including transfer layover time) between any two airports. Output Flight ID, total flight time, total transfer time, departure time, and arrival time.
  5. Given the requirements for takeoff or landing time, find multiple alternative routes to any two airports via direct flight or one transfer.
  6. Given the requirements for takeoff or landing time, find the lowest airfare and corresponding route between any two airports via direct flight or one transfer.

 4. Overall idea

The overall program consists of 7 main header files and a main function: flight_reader reads the csv file data, stores it in the corresponding structure and modifies the time format; dfs_and_bfs uses C++ stack operations to implement depth-first search, and uses C++ The queue operation implements breadth-first search; find_all_flight performs a depth-first search of all paths based on the input starting point and end point, outputs the direct or one-stop flight list to the result.txt file, and stores the path in the specified array; count_fares and count_time use already The stored all_route path array information is used to calculate the time and cost required for each path to select the optimal path.

5. Detailed design and key codes

(1) Function points 1 and 2 (DFS/BFS)

After reading the file, each flight information is stored in the structure, and the content of the structure is used as the data content of each information block. DFS uses a stack operation to determine whether two airports are connected. Since there is no need to choose the shortest time or path, during the judgment process, all airports that depart from airport B to C are later than the time from airport A to airport B. Among the routes, the earliest time is selected as the departure time, thereby simplifying multiple sets of route data between the two airports into only one optimal route data, thereby realizing DFS. For example, the two paths from B to C in the figure both meet the time requirements, then the path with the earliest departure time is selected as the optimal path.

Since the time sequence requirement needs to be met when searching downward, the program uses the two-dimensional array time_char[ROUTE_NO][CHAR_LEN] to store the arrival time string of the airport at the ROUTE_NO position of the current search path, so that the search for the top node of the stack is When the stack is empty, it is convenient to obtain the time information of the remaining nodes that have not been popped off the stack.

The basic ideas of depth-first traversal and breadth-first traversal are the same as the puppet function used to determine whether there is connectivity. DFS uses the stack for auxiliary operations, and BFS uses the queue for auxiliary operations.

Figure 1 Judgment function for whether two airports are connected

Figure 2 Depth-first search function-stack operation

(2) Function point 3 (all routes with one direct flight or one transfer)

Since the function point below has restrictions on departure and arrival time requirements, in the deep search function of function point 3, the program adds time limit parameters in advance, and when the main function calls function 3, the function parameters are assigned the value "000000000000" and "999999999999" to ensure that all departure times are within this range to eliminate time constraints.

After function point 3, accurate flight IDs need to be output. It can be said that function points 1 and 2 are all about in-depth searches for the connectivity of airport nodes, while after function point 3, they are used to search for different routes (including different airport nodes and different flight times). ) for deep search of connectivity. Since the route contains all paths between the airport node and the two airport nodes (faf and i), in the DFS process, in addition to faf_stack, which stores the airport node information, an auxiliary stack help_stack is also introduced to store the route between the two airport nodes. serial number. Each time you search one level deeper, you are essentially traversing all paths. If they are connected, the airport number is put into the left stack (as shown below), and the route number is put into the right stack to complete one level of depth traversal.

 

The conditions for the two routes to be connected are 1) the two airports are connected 2) the time sequence is consistent 3) the path has not been visited 4) the node airport does not appear in the path currently being searched, the program uses the three-dimensional array faf_visited[ DEP_ID][ARR_ID][BRANCH_NO] determines whether the path has been visited. The first two dimensions represent the path from the airport number DEP to the airport number ARR. BRANCH_NO represents the path number between the two airports, so as to be accurate to the specific path ID. When determining whether the airport appears in the path being searched, the program uses the save array to temporarily store the information in the stack, and also uses a similar method when printing the path.

      

Figure 3 Function to determine whether the airport appears repeatedly in the path Figure 4 Schematic diagram of the path

When the continued search result at the top of the stack is empty, that is, when there is no path that meets the requirements that can be pushed onto the stack, the top of the stack is popped out, including the main stack (which stores the airport node number) and the auxiliary stack (which stores the path number). Since there are multiple paths in the route directed graph, for example, there are two paths from B to E in Figure 4: B->D->E and B->C->D->E. If you first search for BDE path, the path between DE will be marked. If this mark is not deleted when D is de-stacked, during subsequent searches, since the path has been marked, the BCDE path will no longer be searched, resulting in an error. Therefore, when the program de-stacks each top node, it deletes the marks between the de-stack node and all other connected nodes.

Figure 5 Delete the path markers of the unstack node and other nodes

When searching, since the question limits one transfer or direct flight, the size of the stack storing the path must not exceed 3, that is, the starting point, transfer station, and end point. Therefore, when searching, the path length is controlled by limiting the number of elements pushed into the stack. Overall The search function is as follows.

 

 

 

Figure 6 Function point 3 (2 in the question) deep search code

(3) Function point 4 (minimum flight time without time limit)

Store all the paths obtained in function point 3 into an array, traverse all paths, and use the up and down arrays to calculate the overall flight time, transfer time, etc.

                                                         Figure 7 Key code for time calculation

(4) Function point 5 (all paths with time limits)

Since the departure time is limited, the program passes the input restrictions (no earlier than and no later than) as parameters to the deep search function. In the statement to determine whether to push it onto the stack, add a condition: for the first route, if the departure time is within If it is within the allowed range, it will be pushed onto the stack. If it is not within the allowed range, it will not be pushed onto the stack.

 Figure 8 Take-off time condition restrictions

(5) Function point 6 (minimum cost with time limit)

Based on function point 5, store the path information in the path array, traverse all paths, and find the minimum cost.

 Figure 9 Key code for cost calculation

(6) Main function (user-oriented selection)

6. Display of running results

Function point 1

Input: 3

Output: All deep search paths starting from airport 3

 

 

Function point 2

Input: 3

Output: BFS traversal order

Function point 3

Enter: 48 49

Output: All paths from 48 to 49 (direct flights or one transfer) are written into a file (uploaded as an attachment)

 

Function point 4 

Enter: 49 50

Output: route ID, departure time, flight time, arrival time, accumulated time

  

Note: The result has been calculated, and the total time is 200 minutes, which is the same as the case. During the debugging process, the following answers were also found: the total time is 200 minutes, the transfer is 5 minutes, and the flight is 195 minutes.

Function point 5

Input: 49 50 201705070000 201705091000

Output: All routes from 49 to 50 that take off within this time range (direct flight or one transfer), and write a document (the document is attached to the job folder).

 

Function point 6

Input: 50 25 201705051200 201705051400

Output: Minimum cost among all 50 to 25 paths taking off within this time range

 

The final grade was quite good. Just looking at this big assignment, I got 90 points~

Guess you like

Origin blog.csdn.net/weixin_62724756/article/details/129340083