PayPal | Onsite | Find Itinerary & Shortest Path
Anonymous User
509

I'm having trouble understanding how to solve the following problem: (Appreciate any help with the logic)

Problem: You are given a list[list[str]] which represents boarding passes for an itinerary. 
You optimized for the lowest cost for the trip, and hence you chose multiple layovers (including cyclic flight paths).
Each item in the list is a [source_airport, destination_airport]
Eg:
boarding_passes = [
    ["LAS", "SLC"],
    ["DFW", "JFK"],
    ["SLC", "LAX"],
    ["LAX", "LAS"],
    ["LAX", "DFW"],
    ["SFO", "LAX"],
]

Your task:
1. Find the source and destination from the above itinerary
2. Find the actual path travelled
3. Find the shortest path that you could have taken if you did not optimize for cost of the trip

My solution:

1. You can find source and target quite easily: 
	method1: source = set(sources) - set(targets); target = set(targets) - set(sources)
	method2: construct a graph from the edges, find a node with indegree=0 (which is the source) and outdegree=0 (which would be target)
2. Finding the actual path travelled:
   (Here is where I had problem formulating the logic with graphs
     - the actual path will be the dfs path that has all nodes of the graph
     - but because the graph can have cycles, how to account this in DFS? without running into infinite loops
     
	 Any help with an approach/code is appreciated)
	 Is this by chance a hamiltonian path finding problem?
3. Find shortest path had you not optimized for cost: easy again - since this can be done with BFS

Can someone help me figure out the answer for part 2?

image

Comments (3)