Project 3: Transportation Hub¶
Chapter 1 : Introduction¶
Problem¶
Description¶
For a map of a country, there might be more than one shortest path from the starting city to the destination. For a city that appears in the shortest paths for more than \(k\) times, we call it a transportation hub. Our task is to find the transportation hubs for each query given a map of a country.
Input¶
The first line provides the total number of cities \(n\),the number of roads \(m\),and the threshold for a transportation hub \(k\).
Then we are provided \(m\) lines in the format \(c_1\space c_2\space length\),where \(c_1\) and \(c_2\) are the city indices (from \(0\) to \(n-1\)) of the two ends of the road (two-way roads) and \(length\) is the length of the road.
Then the next line gives a positive integer \(T\), followed by \(T\) lines, each of which gives a pair of source and destination.
Output¶
The output contains \(T\) lines. For each pair of source and destination, list all the transportation hubs on the way in the ascending order in the line.
If there is no transportation hub on the way, print \(None\) in the line.
Sample Input¶
Text Only | |
---|---|
Sample Output¶
Algorithm Analysis¶
Dijkstra Algorithm¶
The core algorithm is called Dijkstra Algorithm.
The common Dijkstra Algorithm uses greedy strategy and is comprised of 4 steps:
Firstly declare an array \(dis\) to store the shortest distance from the source point to each vertex and a set of vertices named \(T\) that have found the shortest path. Initially, the path weight of the origin \(s\) is assigned to 0 (\(dis[s]=0\)). If there are edges \((s,m)\) that can be directly reached for vertex \(s\), set \(dis[m]\) as the weight of the edge, and set the path length of all other vertices (which s cannot directly reach) to infinity.
Secondly, the set \(T\) only has vertices \(s\).Then, select the minimum value from the dis array, which is the shortest path from the source point \(s\) to the corresponding vertex, and add the point to \(T\). Then a vertex is completed.
Thirdly, we need to see if the newly added vertices can reach other vertices and if the path length to reach other points through that vertex is shorter than directly reaching the source point. If so, then replace the values of these vertices in \(dis\).This operation is called Loose Operation.
Lastly, find the minimum value from dis and repeat the Loose Operation until \(T\) contains all the vertices of the graph.
DFS Algorithm¶
Also in this task we use DFS Algorithm to find all smallest distance path.
To obtain a solution to the problem, DFS Algorithm firstly choose a possible scenario to explore forward.
During the exploration process, once it is discovered that the original choice was incorrect or we found one solution, one step back and choose again to continue exploring forward
Repeat this process until all solutions are obtained.
Chapter 2 : Algorithm Specification¶
Step 1 : Apply Dijkstra Algorithm¶
To solve this problem, we only need to make several little changes to the common Dijkstra Algorithm. In the Loose Operation, we also need to consider the situation when the path length to reach other points through that vertex is equal to directly reaching the source point. This means there is probably more than one minimum path. Store the path.
We use heap to find the smallest unknown distance vertex thus optimizing the dijkstra algorithm. Also an array \(dis\) is applied to store the smallest path from starting point to the destination. Pseudo-code is shown below:
Step 2 : Get the path and judge transportation hub¶
Finally we use DFS Algorithm to count the times that the vertix appears in the path and judge whether it is a transportation hub or not.
We will store all possible previous vertex in the smallest distance path in an array \(pre\). Then for every previous vertex \(pre[i]\) in the \(pre\), explore further to get the previous vertices of \(pre[i]\). Dug deeper until we reach the starting point. This means we have found one smallest distance path. Step back to explore further until we have found all smallest distance path.
Lastly use an array \(bucket\) to count and judge whether a vertix is a transportation hub or not. Pseudo-code is shown below:
C++ | |
---|---|
Chapter 3 : Testing Results¶
Test case 1¶
Test case 1 wants to test the graph of 10 vertices and 38 edges with the threshold being 1 and query times being 5.
Input¶
Text Only | |
---|---|
Output¶
Status : Passed
Test case 2¶
Test case 2 wants to test the graph of 20 vertices and 137 edges with the threshold being 2 and query times being 10.
Input¶
Text Only | |
---|---|
Output¶
Status : Passed
Test case 3¶
Test case 3 wants to test the graph of 50 vertices and 1089 edges with the threshold being 2 and query times being 20.
Input¶
Text Only | |
---|---|
Output¶
Text Only | |
---|---|
Status : Passed
Test case 4¶
Test case 4 wants to the graph of 200 vertices and 8558 edges with the threshold being 1 and query times being 100.
Input¶
Text Only | |
---|---|
Output¶
Text Only | |
---|---|
Status : Passed
Test case 5¶
Test case 5 wants to test the graph of 500 vertices and 124750 edges with the threshold being 4 and query times being 500. (Maximum test cases)
Input¶
Text Only | |
---|---|
Output¶
Status : Passed
Chapter 4 : Analysis and Comments¶
Time Complexity¶
For step 1 : Apply Dijkstra Algorithm, the time complexity of find the smallest unknown distance vertex through heap is \(O(log\space n)\). Also we still need to traverse all the vertex, so the time complexity of step 1 is \(O(nlog\space n)\).
For step 2 : Get the path and judge transportation hub. Firstly we need to traverse all the vertex. Then we need to traverse all the vertex's previous vertex in the smallest distance path. This means the time complexity of step 2 is \(O(n*n)=O(n^2)\)
To sum up, the total time complexity of the program is \(O(nlog\space n+T*n^2)=O(T*n^2)\)
Space Complexity¶
The whole program construct:
- Two arrays \(edge\) and \(head\) to store the information of the graphs : \(O(n^2+n)=O(n^2)\)
- A two-dimensional array \(pre\) to store the previous vertex of every vertex in the smallest distance path : \(O(n^2)\)
- A heap to get the smallest unknown distance vertex : \(O(n)\)
- An array \(dis\) to store the minimum distance : \(O(n)\)
- An array \(bucket\) to count the times every vertex in the minimum paths : \(O(n)\)
- An array \(visit\) to mark whether the vertex is in the set \(T\) : \(O(n)\)
To sum up the total space complexity of the program is \(O(n^2)\)
Comments¶
This program successfully complete the task, but the DFS algorithm slow down the whole program, making the optimizing part of the Dijkstra algorithm a bit useless. It can be possibly improved.
Appendix : Source Code¶
Source Code¶
C++ | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
Data Generator¶
Declaration¶
I hereby declare that all the work done in this project titled “Transportation Hub” is of my independent effort.