You are given a stack of travel tickets for various transportations that will take you from a point A to point B via several stops on the way. All of the tickets are out of order and you don't know where your journey starts, nor where it ends. Sort the tickets in the right order to complete your journey.
tickets = [ {from: "Barcelona", to: "New York"} {from: "Barcelona", to: "Gerona"}, {from: "Madrid", to: "Barcelona"}, {from: "Gerona", to: "Barcelona"} ]
I suppose, the right order is that one:
tickets = [ {from: "Madrid", to: "Barcelona"}, {from: "Barcelona", to: "Gerona"}, {from: "Gerona", to: "Barcelona"}, {from: "Barcelona", to: "New York"} ]
Because there is no ticket to Madrid, and no ticket from New York.
What would be the best algorithm for that task?
The language is JavaScript, but language-agnostic solution would be good enough.
Update: I changed sample data to be not confused with One-way flight trip problem.
Are Plane Tickets Refundable? The short answer is yes, though each airline can implement a different policy for refunding airfare. Most airlines offer a choice between refundable and non-refundable airline tickets—something a lot of us tend to skip over or overlook altogether.
Yes you can block tickets, but for that you have to contact a travel agent (having GDS access). And blocking tickets comes with a deadline from the airline. So make sure your Visa thing is done, by the time airline is releasing your blocked seats.
As of June 1, IATA will no longer supply paper ticket stock to 60,000 travel agencies in 125 countries around the world as it has done for decades. Many American travelers may think that paper airline tickets are already extinct, as most U.S. airlines stopped issuing paper tickets for domestic flights some years ago.
Each airline handles external tickets differently. If you end up needing to make a change or cancellation, it pays to do so directly with your travel agent. That should definitely be your first point of contact. All existing airline-issued waivers and exceptions will be still be applied to your ticket.
If you can visit a node (a city) more than once, this is the eulerian path problem.
Here are two simple algorithms for solving it, depending on what type of graph you have. You have a recursive implementation on page 3 here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With