|
| 1 | +# Reconstruct Itinerary |
| 2 | + |
| 3 | +Given a list of airline tickets where tickets[i] = [fromi, toi] represent a departure airport and an arrival airport of |
| 4 | +a single flight, reconstruct the itinerary in the correct order and return it. |
| 5 | + |
| 6 | +The person who owns these tickets always starts their journey from "JFK". Therefore, the itinerary must begin with "JFK". |
| 7 | +If there are multiple valid itineraries, you should prioritize the one with the smallest lexical order when considering |
| 8 | +a single string. |
| 9 | + |
| 10 | +> Lexicographical order is a way of sorting similar to how words are arranged in a dictionary. It compares items |
| 11 | +> character by character, based on their order in the alphabet or numerical value. |
| 12 | +
|
| 13 | +- For example, the itinerary ["JFK", "EDU"] has a smaller lexical order than ["JFK", "EDX"]. |
| 14 | + |
| 15 | +> Note: You may assume all tickets form at least one valid itinerary. You must use all the tickets exactly once. |
| 16 | +
|
| 17 | +## Constraints |
| 18 | + |
| 19 | +- 1 <= `tickets.length` <= 300 |
| 20 | +- `tickets[i].length` == 2 |
| 21 | +- `fromi.length` == 3 |
| 22 | +- `toi.length` == 3 |
| 23 | +- `fromi` != `toi` |
| 24 | +- `fromi` and `toi` consist of upper case English letters |
| 25 | + |
| 26 | +## Examples |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +## Related Topics |
| 33 | + |
| 34 | +- Depth-First Search |
| 35 | +- Graph |
| 36 | +- Eulerian Circuit |
| 37 | + |
| 38 | +## Solution |
| 39 | + |
| 40 | +The algorithm uses __Hierholzer’s algorithm__ to reconstruct travel itineraries from a list of airline tickets. This |
| 41 | +problem is like finding an __Eulerian path__ but with a fixed starting point, “JFK”. Hierholzer’s algorithm is great for |
| 42 | +finding Eulerian paths and cycles, which is why we use it here. |
| 43 | + |
| 44 | +> Hierholzer's algorithm is a method for finding an Eulerian circuit (a cycle that visits every edge exactly once) in a |
| 45 | +> graph. It starts from any vertex and follows edges until it returns to the starting vertex, forming a cycle. If there |
| 46 | +> are any unvisited edges, it starts a new cycle from a vertex on the existing cycle that has unvisited edges and merges |
| 47 | +> the cycles. The process continues until all edges are visited. |
| 48 | +
|
| 49 | +> An Eulerian path is a trail in a graph that visits every edge exactly once. An Eulerian path can exist only if exactly |
| 50 | +> zero or two vertices have an odd degree. If there are exactly zero vertices with an odd degree, the path can form a |
| 51 | +> circuit (Eulerian circuit), where the starting and ending points are the same. If there are exactly two vertices with |
| 52 | +> an odd degree, the path starts at one of these vertices and ends at the other. |
| 53 | +
|
| 54 | +The algorithm starts by arranging the destinations in reverse lexicographical order to ensure we always choose the |
| 55 | +smallest destination first. It then uses depth-first search (DFS) starting from “JFK” to navigate the flights. As it |
| 56 | +explores each flight path, it builds the itinerary by appending each visited airport when there are no more destinations |
| 57 | +to visit from that airport. Since the airports are added in reverse order during this process, the final step is to |
| 58 | +reverse the list to get the correct itinerary. |
| 59 | + |
| 60 | +The basic algorithm to solve this problem will be: |
| 61 | + |
| 62 | +1. Create a dictionary, `flight_map`, to store the flight information. Each key represents an airport; its corresponding |
| 63 | + value is a list of destinations from that airport. |
| 64 | +2. Initialize an empty list, result, to store the reconstructed itinerary. |
| 65 | +3. Sort the destinations lexicographically in reverse order to ensure that the smallest destination is chosen first. |
| 66 | +4. Perform DFS traversal starting from the airport "JFK". |
| 67 | + - Get the list of destinations for the current airport from flight_map. |
| 68 | + - While there are destinations available: |
| 69 | + - Pop the next_destination from destinations. |
| 70 | + - Recursively explore all available flights starting from the popped next_destination, until all possible flights |
| 71 | + have been considered. |
| 72 | + - Append the current airport to the result list. |
| 73 | +5. Return the result list in reverse order to ensure the itinerary starts from the initial airport, "JFK", and proceeds |
| 74 | + through the subsequent airports in the correct order. |
| 75 | + |
| 76 | +Let’s look at the following illustration to get a better understanding of the solution: |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +### Time Complexity |
| 95 | + |
| 96 | +Each edge (flight) is traversed once during the DFS process in the algorithm, resulting in a complexity proportional to |
| 97 | +the number of edges, ∣E∣. |
| 98 | +Before DFS, the outgoing edges for each airport must be sorted. The sorting operation’s complexity depends on the input |
| 99 | +graph’s structure. |
| 100 | +In the worst-case scenario, such as a highly unbalanced graph (e.g., star-shaped), where one airport (e.g., JFK) |
| 101 | +dominates the majority of flights, the sorting operation on this airport becomes highly expensive, possibly reaching N log N |
| 102 | +complexity where `N = |E|/2` In a more balanced or average scenario, where each airport has a roughly equal number of |
| 103 | +outgoing flights, the sorting operation complexity remains O(N log N) where N represents half of the total number of |
| 104 | +edges divided by twice the number of airports O(|E|/2|V|). Thus, the algorithm’s overall complexity is O(|E|log|E/2|), |
| 105 | +emphasizing the significance of the sorting operation in determining its performance. |
| 106 | + |
| 107 | +### Space Complexity |
| 108 | + |
| 109 | +The space complexity is O(∣V∣+∣E∣), where ∣V∣ is the number of airports and ∣E∣ is the number of flights. |
0 commit comments