Javatpoint Logo
Javatpoint Logo

Find Itinerary from a given list of tickets


It is fundamental to have an unmistakable itinerary while traveling, especially while heading out to numerous areas, to guarantee a smooth journey. Envision you have a list of tickets with their destinations of departure and arrival. How might you effectively develop the agenda to visit all objections precisely once? In this article, we'll investigate a graph-based way to tackle this issue utilizing the C programming language.

Prior to diving into the arrangement, we should explain the problem statement. We are given a rundown of flight tickets addressed as pairs of departure and arrival locations. Our task is to build the total schedule beginning from a particular departure point, visiting every destination precisely once, and finishing at the last destination. We can model the flight tickets as a directed graph to solve this issue. Every area fills in as a node, and each ticket addresses a directed edge from the departure location to the arrival location.



Find Itinerary from a given list of tickets

Code Explanation

  • It characterizes a Ticket structure addressing a flight ticket with departure and destination airports and a HashMap structure to store the tickets effectively.
  • The code gives capabilities to make a new ticket, make a new hash map, insert a ticket into the hash map, track down the itinerary, and free memory.
  • In the main function, it makes a new hash map and inserts example flight tickets into it. Then, at that point, it calls the findItinerary function to decide the itinerary based on the provided tickets.
  • The findItinerary function begins by recognizing the starting point of the itinerary by checking for a ticket where the "from" airport doesn't show up as the "to" airport in some other ticket.
  • It then iteratively prints the itinerary by following the associations between airports in the tickets until it arrives at the finish of the itinerary.
  • At last, the program liberates the memory assigned for the hash map and tickets utilizing the freeMemory function to prevent memory leaks.
  • Overall, this code effectively decides and prints the itinerary of a journey based on the provided flight tickets, exhibiting essential use of structures, memory allocation, string manipulation, and iteration in C.

Time and Space Complexity

The code shows a time complexity of O(n^2) for tracking down the starting point of the itinerary, where n addresses the number of tickets because of nested iteration. Traversal and printing of the itinerary contribute to O(n) time complexity. Concerning, the code requires O(n) space for the hash map and ticket storage relative to the number of tickets. Notwithstanding the quadratic complexity for recognizing the starting point, the overall time and space complexities empower the effective handling of moderate-sized datasets, with the caveat that presentation might degrade for bigger datasets.


we investigated a graph-based way to deal with finding an itinerary from a given list of tickets utilizing the C programming language. We efficiently construct the itinerary by representing the tickets as edges in a directed graph and employing depth-first search, ensuring that each location is visited precisely once. This arrangement can be adjusted for different travel arranging situations, giving a robust and scalable methodology for itinerary generation in C.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA