Reconstruct Itinerary

Reconstruct Itinerary – Reconstruct Flight Path Problem

You are given a list of flight tickets, where each ticket is represented as tickets [i] = [from_i, to_i]. Here, from_i and to_i are three-letter codes for the source and destination airports.

Your task is to reconstruct the complete flight itinerary in order and return it.

  • All tickets start from the “JFK” airport.
  • Each ticket is used exactly once.
  • If there are multiple valid itineraries, return the one that comes first in lexicographical order.
Reconstruct Itinerary example

Examples related to Reconstruct Itinerary - Reconstruct Flight Path Problem

Example 1:

Reconstruct Itinerary problem 1

Example 2:

Reconstruct Itinerary problem 2

Constraints:

  • 1 <= tickets.length <= 300
  • from_i != to_i

Hints to solve Reconstruct Flight Path Problem

Hint 1:

  • Think of this problem as a graph where airports are the nodes and tickets are the edges.
  • Since you need to use all the tickets exactly once, can you figure out a way to visit every edge of the graph?
  • Also, how can you make sure the flight path follows the smallest alphabetical order?

Hint 2:

  • We can create an adjacency list from the tickets to represent the graph. Then, we use DFS to build the itinerary.
  • To ensure the smallest alphabetical order, we sort the list of destinations for each airport.
  • Sorting ensures that during DFS, we visit the airport with the smallest name first.

Hint 3:

  • Simple DFS might not be efficient since it can take O(E * V) time, where E is the number of tickets and V is the number of airports.
  • Naive approach would involve removing a ticket, exploring, and then adding it back if needed.
  • Can you think of a better approach? Maybe a modified version of DFS could help.

Hint 4:

  • You can use Hierholzer’s algorithm, a technique for finding Eulerian paths.
  • In this approach, instead of adding airports to the result immediately, you visit all its neighbors first (post-order traversal).
  • Once all DFS calls are done, you reverse the collected path to get the correct itinerary.

Methods to Solve Reconstruct Itinerary - Reconstruct Flight Path Problem

There are mainly 3 approach to solve Reconstruct Flight Path Problem problem:

  1. Depth First Search Method
  2. Hierholzer’s Algorithm (Recursion)
  3. Hierholzer’s Algorithm (Iteration)

1. Depth First Search Method

  • This method involves performing a depth-first traversal of the graph to build the flight path.

  • Starting from the given departure point, you recursively explore each destination, marking routes as you go to reconstruct the path in the correct order.

  • Time complexity: O(E x V)
  • Space complexity: O(E x V)

Where E is the number of tickets (edges) and V is the number of airports (vertices).

Code:

2. Hierholzer’s Algorithm (Recursion) Method

  • This approach is used to find Eulerian paths or circuits. It recursively traverses the graph to ensure that all edges are covered exactly once, building the path as it backtracks from dead ends to construct the flight itinerary.
  • Time complexity: O(E log E)
  • Space complexity: O(E)

Where E is the number of tickets (edges) and V is the number of airports (vertices).

Code:

3. Hierholzer’s Algorithm (Iteration) Method

  • Build a table to solve the problem iteratively, starting from smaller subproblems and combining results. It systematically tracks valid states for substrings, ensuring efficient validation.
  • Time complexity: O(E log E)
  • Space complexity: O(E)

Where E is the number of tickets (edges) and V is the number of airports (vertices).

Code:

More Articles