One-way flight trip problem « Ajeet Singh
You are going on a one-way indirect flight trip that includes an unknown very large number of transfers. You are not stopping twice in the same airport. You have 1 ticket for each part of your trip. Each ticket contains src and dst airport. All the tickets you have are randomly sorted. You forgot the original departure airport (very first src) and your destination (last dst). We need to design an algorithm to reconstruct your trip with minimum big-O complexity.
http://stackoverflow.com/questions/2991950/one-way-flight-trip-problem
http://www.careercup.com/question?id=5768610725232640
Bad named variable.
You are going on a one-way indirect flight trip that includes an unknown very large number of transfers. You are not stopping twice in the same airport. You have 1 ticket for each part of your trip. Each ticket contains src and dst airport. All the tickets you have are randomly sorted. You forgot the original departure airport (very first src) and your destination (last dst). We need to design an algorithm to reconstruct your trip with minimum big-O complexity.
http://stackoverflow.com/questions/2991950/one-way-flight-trip-problem
Construct a hashtable and add each airport into the hash table.
<key,value> = <airport, count>
Count for the airport increases if the airport is either the source or the destination. So for every airport the count will be 2 ( 1 for src and 1 for dst) except for the source and the destination of your trip which will have the count as 1.
String[] places =
new
[N+
1
];
// N-1 links required to connect N places
Step
1
: Store all tickets in a HashMap with end location as key and ticket as value.
It will take O(N) time and O(N) space. Where N = number of tickets
Step
2
: String endLocation= current_location;
location = N+
1
;
while
(! map.isEmpty()) {
Ticket ticket = map.getTicket(endLocation)
// from HashMap.
places[location--] = endLocation;
endLocation = ticket.getStartPlace();
}
places[location--] = ticket.getStartPlace();
Step
3
:
return
places;
// it is an array of places in visited sequence.
Bad named variable.
public static ArrayList<String> getPath(String[] arr){
HashSet<String> fromSet = new HashSet<String>(arr.length / 2);
HashMap<String, String> toMap = new HashMap<String, String>(arr.length / 2);
from(int i = 0; i < arr.length -1; i += 2){
fromSet.add(arr[i+1]);
toMap.put(arr[i], arr[i+1]);
}
String start = null;
for(String str : toMap.getKeys()){
if(!fromSet.contains(str)){
start = str;
break;
}
}
ArrayList<String> results = new ArrayList<String>(arr.length / 2 + 1);
while(start != null){
results.add(start);
start = toMap.get(start);
}
return results;
}
http://blueocean-penn.blogspot.com/2015/01/topological-sort-of-airports.html
The key to topological sort is to do DFS, then add the visited node at the end, in that way the the nodes are sorted.
public class Airport {
public Airport(String from) {
this.name = from;
this.destinations = new ArrayList<Airport>();
}
String name;
List<Airport> destinations;
}
public class Edge{
String from;
String to;
}
public Stack<Airport> sortAirports(List<Edge> edges){
Map<String, Airport> graph = new HashMap<String, Airport>();
buildGraph(edges, graph);
Set<String> visited = new HashSet<String>();
Stack<Airport> s = new Stack<Airport>();
for(Airport a : graph.values()){
sort(a, s, visited);
}
return s;
}
public void sort(Airport a, Stack<Airport> s, Set<String> visited){
if(visited.contains(a.name))
return;
visited.add(a.name);
for(Airport d : a.destinations){
sort(d, s, visited);
}
s.add(a); <== add the earlier visited airport at last
}
Read full article from One-way flight trip problem « Ajeet Singh