package pathfinder; import graph.Graph; import graph.Node; import pathfinder.datastructures.Path; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.PriorityQueue; // HERE TO MAKE TESTING FINDSHORTESTPATH EASY public class WeightedGraph extends Graph { /** * Finds the shortest path, by distance, between the two provided buildings. * * @param startPoint The short name of the building at the beginning of this path. * @param endPoint The short name of the building at the end of this path. * @return A path between {@code startBuilding} and {@code endBuilding}, or {@literal null} * if none exists. * @throws IllegalArgumentException if {@code startBuilding} or {@code endBuilding} are * {@literal null}, or not valid short names of buildings in * this campus map. */ public Path findShortestPath(E startPoint, E endPoint) { PriorityQueue> active = new PriorityQueue>(); Map> finished = new HashMap>(); finished.put(startPoint, new Path(startPoint)); active.add(new Path(startPoint)); while (!active.isEmpty()) { Path minPath = active.remove(); E curPoint = minPath.getEnd(); if (curPoint.equals(endPoint)) { return minPath; } finished.put(curPoint, minPath); Node curNode = getNode(curPoint); Map>> children = curNode.getEdges(); for (double d : children.keySet()) { for (Node child : children.get(d)) { if (!finished.containsKey(child.getName())) { Path newPath = minPath.extend(child.getName(), d); if (!active.contains(newPath)) { active.add(newPath); } } } } } return null; } }