SHARE
TWEET

Untitled

a guest Oct 21st, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.text.DecimalFormat;
  4. import java.util.*;
  5.  
  6. public class Dijkstra {
  7.  
  8.     public static Graph calculateShortestPathFromSource(Graph graph, Node source) {
  9.  
  10.         source.setDistance(0);
  11.  
  12.         Set<Node> settledNodes = new HashSet<>();
  13.         Set<Node> unsettledNodes = new HashSet<>();
  14.         unsettledNodes.add(source);
  15.  
  16.         while (unsettledNodes.size() != 0) {
  17.             Node currentNode = getLowestDistanceNode(unsettledNodes);
  18.             unsettledNodes.remove(currentNode);
  19.             for (Map.Entry<Node, Integer> adjacencyPair : currentNode.getAdjacentNodes().entrySet()) {
  20.                 Node adjacentNode = adjacencyPair.getKey();
  21.                 Integer edgeWeigh = adjacencyPair.getValue();
  22.  
  23.                 if (!settledNodes.contains(adjacentNode)) {
  24.                     CalculateMinimumDistance(adjacentNode, edgeWeigh, currentNode);
  25.                     unsettledNodes.add(adjacentNode);
  26.                 }
  27.             }
  28.             settledNodes.add(currentNode);
  29.         }
  30.         return graph;
  31.     }
  32.  
  33.     public class Graph {
  34.  
  35.         private Set<Node> nodes = new HashSet<>();
  36.  
  37.         public void addNode(Node nodeA) {
  38.             nodes.add(nodeA);
  39.         }
  40.  
  41.         public Set<Node> getNodes() {
  42.             return nodes;
  43.         }
  44.  
  45.         public void setNodes(Set<Node> nodes) {
  46.             this.nodes = nodes;
  47.         }
  48.     }
  49.  
  50.     public class Node {
  51.  
  52.         private String name;
  53.  
  54.         private LinkedList<Node> shortestPath = new LinkedList<>();
  55.  
  56.         private Integer distance = Integer.MAX_VALUE;
  57.  
  58.         private Map<Node, Integer> adjacentNodes = new HashMap<>();
  59.  
  60.         public Node(String name) {
  61.             this.name = name;
  62.         }
  63.  
  64.         public void addDestination(Node destination, int distance) {
  65.             adjacentNodes.put(destination, distance);
  66.         }
  67.  
  68.         public String getName() {
  69.             return name;
  70.         }
  71.  
  72.         public void setName(String name) {
  73.             this.name = name;
  74.         }
  75.  
  76.         public Map<Node, Integer> getAdjacentNodes() {
  77.             return adjacentNodes;
  78.         }
  79.  
  80.         public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
  81.             this.adjacentNodes = adjacentNodes;
  82.         }
  83.  
  84.         public Integer getDistance() {
  85.             return distance;
  86.         }
  87.  
  88.         public void setDistance(Integer distance) {
  89.             this.distance = distance;
  90.         }
  91.  
  92.         public List<Node> getShortestPath() {
  93.             return shortestPath;
  94.         }
  95.  
  96.         public void setShortestPath(LinkedList<Node> shortestPath) {
  97.             this.shortestPath = shortestPath;
  98.         }
  99.  
  100.     }
  101.  
  102.     private static void CalculateMinimumDistance(Node evaluationNode, Integer edgeWeigh, Node sourceNode) {
  103.         Integer sourceDistance = sourceNode.getDistance();
  104.         if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) {
  105.             evaluationNode.setDistance(sourceDistance + edgeWeigh);
  106.             LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
  107.             shortestPath.add(sourceNode);
  108.             evaluationNode.setShortestPath(shortestPath);
  109.         }
  110.     }
  111.  
  112.     private static Node getLowestDistanceNode(Set<Node> unsettledNodes) {
  113.         Node lowestDistanceNode = null;
  114.         int lowestDistance = Integer.MAX_VALUE;
  115.         for (Node node : unsettledNodes) {
  116.             int nodeDistance = node.getDistance();
  117.             if (nodeDistance < lowestDistance) {
  118.                 lowestDistance = nodeDistance;
  119.                 lowestDistanceNode = node;
  120.             }
  121.         }
  122.         return lowestDistanceNode;
  123.     }
  124. }
  125.  
  126. class Main {
  127.  
  128.     static HashMap<String, Double> aircraftEm = new HashMap<String, Double>();
  129.     public static double solution(String[] aircraftEmissions, String[] flightItineraries, String origin, String destination) {
  130.  
  131.         for(String aircraft : aircraftEmissions) {
  132.  
  133.             String[] parts = aircraft.split("-");
  134.             aircraftEm.put(parts[0], Double.valueOf(parts[1]));
  135.         }
  136.         return calculateEmissions("boeing_737", 534.0 );
  137.     }
  138.  
  139.     public static double calculateEmissions(String aircraft, Double km){
  140.         return Math.floor(km*aircraftEm.getOrDefault(aircraft, 0.0) * 100) / 100;
  141.  
  142.     }
  143.  
  144.     public static void main(String[] args) {
  145.         String[] aE = {"boeing_737-0.101","boeing_747-0.12"};
  146.         String[] aB = {"LON-EDI-534-boeing_737","LON-EDI-534-boeing_787","-LON-464-embraer_190"};
  147.  
  148.         System.out.println(Main.solution(aE, aB , "LON", "EDI"));
  149.     }
  150.  
  151.  
  152.  
  153. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top