Last active
July 14, 2017 21:59
-
-
Save isaacmg/49eeab607790610414e98a9eee84e47e to your computer and use it in GitHub Desktop.
Dijkstra's algorithm with PriorityQueue in order to find least expensive combination of flights.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.*; | |
import java.util.Comparator; | |
/** | |
* Created by isaac on 7/12/17. | |
*/ | |
public class AirlineBasic { | |
public static Node getNodeFromMap(String city2, HashMap<String,Node> nodes){ | |
Node theNode2 = new Node(city2); | |
if(nodes.containsKey(city2)){ | |
theNode2 = nodes.get(city2); | |
} | |
return theNode2; | |
} | |
private static Scanner loadFile(String path){ | |
File file = new File(path); | |
try { | |
Scanner sc = new Scanner(file); | |
return sc; | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static void main(String args[]){ | |
HashMap<String, Node> nodes = new HashMap<String,Node>(); | |
Scanner s = loadFile("alg2.txt"); | |
String startString = s.next(); | |
String endCity = s.next(); | |
int numFlights = s.nextInt(); | |
// Create graph structure from input | |
for(int i=0; i<numFlights; i++){ | |
String city1 = s.next(); | |
String city2 = s.next(); | |
int price = s.nextInt(); | |
Node theNode = getNodeFromMap(city1,nodes); | |
Node theNode2 = getNodeFromMap(city2, nodes); | |
//System.out.println(theNode.city); | |
theNode.addNode(theNode2, price); | |
nodes.put(city1,theNode); | |
nodes.put(city2,theNode2); | |
} | |
// Now | |
Node startNode = nodes.get(startString); | |
startNode.distance = 0; | |
nodes.put(startString,startNode); | |
//Init priority queue | |
Comparator<Node> com = new NodeComparator(); | |
PriorityQueue<Node> queue=new PriorityQueue<Node>(20,com); | |
for(Node n : nodes.values()){ | |
queue.add(n); | |
} | |
while(!queue.isEmpty()){ | |
Node u = queue.poll(); | |
for(Map.Entry<Node, Integer> in: u.flights.entrySet()){ | |
int distance = u.distance + in.getValue(); | |
//System.out.println(u.distance + " " + u.city + in.getKey().city + in.getValue()); | |
int value = in.getValue(); | |
Node city = in.getKey(); | |
if(distance<city.distance){ | |
city.distance = distance; | |
city.parent = u; | |
//System.out.println(city.city + " " + city.distance); | |
nodes.put(city.city,city); | |
//Update priority in que | |
queue.remove(city); | |
queue.add(city); | |
} | |
} | |
} | |
for(Node n: nodes.values()){ | |
System.out.println(n.distance + " " + n.city); | |
} | |
// Print result list | |
Node theEndCity = nodes.get(endCity); | |
Stack<Node> n = new Stack<Node>(); | |
while(theEndCity.parent!=null){ | |
n.push(theEndCity); | |
theEndCity = theEndCity.parent; | |
} | |
while(!n.isEmpty()){ | |
System.out.println(n.pop().city); | |
} | |
} | |
public static class NodeComparator implements Comparator<Node> | |
{ | |
public int compare(Node x, Node y) | |
{ | |
// Assume neither string is null. Real code should | |
// probably be more robust | |
// You could also just return x.length() - y.length(), | |
// which would be more efficient. | |
if (x.distance < y.distance) | |
{ | |
return -1; | |
} | |
if (x.distance > y.distance){ | |
return 1; | |
} | |
return 0; | |
}}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment