Program to find airports in correct order in Python?


Suppose we have a list of flights as [origin, destination] pairs. The list is shuffled; we have to find all the airports that were visited in the correct order. If there are more than one valid itinerary, return lexicographically smallest ones first.

So, if the input is like flights = [["Mumbai", "Kolkata"],["Delhi", "Mumbai"],["Kolkata", "Delhi"] ], then the output will be ['Delhi', 'Mumbai', 'Kolkata', 'Delhi']

To solve this, we will follow these steps

  • ins := an empty map

  • outs := an empty map

  • adj_list := an empty map

  • Define a function dfs() . This will take airport

  • while outs[airport] is not null, do

    • nxt := size of adj_list[airport] - outs[airport]

    • outs[airport] := outs[airport] - 1

    • insert airport at the end of ans

  • Define a method called solve(), this will take flights

  • for each start end pair s, e in flights, do

    • insert e at the end of adj_list[s]

    • outs[s] := outs[s] + 1

    • ins[e] := ins[e] + 1

  • for each l in list of all values of adj_list, do

    • sort the list l

  • start := null, end := null

  • for each airport in list of all keys of adj_list, do

    • if outs[airport] - ins[airport] is same as 1, then

      • if start not null, then

        • return

      • start := airport

    • otherwise when outs[airport] - ins[airport] is same as -1, then

      • if end is not null, then

        • return

      • end := airport

    • otherwise when outs[airport] - ins[airport] is not same as 0, then

      • return

  • start := start if start is not null otherwise minimum of all keys of adj_list

  • ans := a new list

  • dfs(start)

  • return reverse of ans

  • From the main method call solve(flights)


Example

 Live Demo

from collections import defaultdict


class Solution:
   def solve(self, flights):
      ins = defaultdict(int)
      outs = defaultdict(int)
      adj_list = defaultdict(list)
      for s, e in flights:
         adj_list[s].append(e)
         outs[s] += 1
         ins[e] += 1
      for l in adj_list.values():
         l.sort()
      start = None
      end = None
      for airport in adj_list.keys():
         if outs[airport] - ins[airport] == 1:
            if start:
               return
            start = airport
         elif outs[airport] - ins[airport] == -1:
            if end:
               return
            end = airport
         elif outs[airport] - ins[airport] != 0:
            return
      start = start if start else min(adj_list.keys())
      ans = []

      def dfs(airport):
         while outs[airport]:
            nxt = len(adj_list[airport]) - outs[airport]
               outs[airport] -= 1
               dfs(adj_list[airport][nxt])
            ans.append(airport)

      dfs(start)
      return ans[::-1]

ob = Solution()
flights = [
   ["Mumbai", "Kolkata"],
   ["Delhi", "Mumbai"],
   ["Kolkata", "Delhi"]
]
print(ob.solve(flights))

Input

[["Mumbai", "Kolkata"],
["Delhi", "Mumbai"],
["Kolkata", "Delhi"] ]

Output

['Delhi', 'Mumbai', 'Kolkata', 'Delhi']

Updated on: 10-Nov-2020

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements