public PathResult dijkstra() { PathResult p = new PathResult(); Map dMap = map.newInstance(); HashSet<Node> evaluated = new HashSet<Node>(); HashSet<Node> unevaluated = new HashSet<Node>(); HashMap<String, Double> dist = new HashMap<String, Double>(); HashMap<String, String> previous = new HashMap<String, String>(); for (int i = 0; i < dMap.getSize(); i++) { Node toAdd = dMap.getNode(i); unevaluated.add(toAdd); dist.put(toAdd.getName(), Double.MAX_VALUE); previous.put(toAdd.getName(), null); } dist.put("START", 0D); Node curNode = dMap.getStartNode(), temp = null; while (!unevaluated.isEmpty()) { Iterator<Node> it = unevaluated.iterator(); curNode = it.next(); while (it.hasNext()) { temp = it.next(); if (dist.get(temp.getName()) < dist.get(curNode.getName())) curNode = temp; } evaluated.add(curNode); unevaluated.remove(curNode); if (dist.get(curNode.getName()) == Double.MAX_VALUE || dMap.getEndNode().equals(curNode)) break; for (int i = 0; i < curNode.getNumDepend(); i++) { Node depend = curNode.getDepend(i); if (evaluated.contains(depend)) continue; double tempWeight = dist.get(curNode.getName()) + dMap.getPath(curNode.getName(), depend.getName()).getWeight(); if (tempWeight < dist.get(depend.getName())) { dist.put(depend.getName(), tempWeight); previous.put(depend.getName(), curNode.getName()); } } } p.setWeight(dist.get(curNode.getName())); String name = curNode.getName(); while (previous.get(curNode.getName()) != null) { curNode = dMap.getFromName(previous.get(curNode.getName())); name = curNode.getName() + "," + name; } p.setPath(name); return p; }
public int sizeMap() { return map.getSize(); }