public Path finishCircuit(int dispair, Vertex home, Graphics g) { visited = true; Path bestPath = new Path(); bestPath.cost = 100000000; if (dispair == 0) // Last being visited { Iterator<Edge> it = edges.iterator(); Boolean match = false; // Looks through all this vertex's edges and find the one that goes home while (!match && it.hasNext()) { Edge f = it.next(); if (f.fromVertex.ident == home.ident) { Path p = new Path(); bestPath = p; bestPath.add(f); match = true; } } } else { Iterator<Edge> it = edges.iterator(); // Finds all possible paths through nodes not visited while (it.hasNext()) { Edge e = it.next(); Vertex c = e.fromVertex; if (!c.visited) { e.drawMe(g, Color.black); // Draws edge Path p = c.finishCircuit(dispair - 1, home, g); p.add(e); e.drawMe(g, Color.green); // Un-draws edge if (p.cost < bestPath.cost) // Sets bestPath { bestPath = p; } } } } visited = false; return bestPath; }
private void adding(Road road, Path path) { roadHelper.setPathId(road.getID(), path.getId()); if (!path.contains(road)) { path.add(road); if (entrances.contains(road)) { path.addEntrance(getEntrance(road)); } } }
private void createAPath(Road neighbourRoad, Path path) { if (!path.contains(neighbourRoad)) { path.add(neighbourRoad); added.add(neighbourRoad); } addEntrances(path, neighbourRoad); for (Road road : roadHelper.getConnectedRoads(neighbourRoad.getID())) { if (entrances.contains(road) || added.contains(road)) { continue; } if (end || roadHelper.getConnectedRoads(neighbourRoad.getID()).size() == 1) { return; } if (headRoads.contains(road)) { if (!end) { end = true; if (!path.contains(road)) { path.add(road); } path.setEndOfPath(road); } } else if (!end) { createAPath(road, path); } } if (!end) { for (Road neighbourR : roadHelper.getConnectedRoads(neighbourRoad.getID())) { if (headRoads.contains(neighbourR)) { if (!path.contains(neighbourR)) { path.add(neighbourR); } path.setEndOfPath(neighbourR); end = true; } } } }
private void addEntrances(Path path, Road road) { Entrance entrance; for (Road n : roadHelper.getConnectedRoads(road.getID())) { if (entrances.contains(n)) { roadHelper.setPathId(n.getID(), path.getId()); if (!path.contains(n) && !added.contains(n)) { path.add(n); added.add(n); } entrance = getEntrance(n); if (!path.getEntrances().contains(entrance)) { path.addEntrance(entrance); } } } }
private List<Path> createThisHeadRoadPaths(Road head) { List<Path> paths = new ArrayList<Path>(); List<Road> headRoadEntrances = new ArrayList<Road>(); for (Road neighbourRoad : roadHelper.getConnectedRoads(head.getID())) { if (added.contains(neighbourRoad)) { continue; } else if (entrances.contains(neighbourRoad)) { added.add(neighbourRoad); headRoadEntrances.add(neighbourRoad); continue; } end = false; Path path = new Path(world); path.add(head); added.add(head); path.setHeadOfPath(head); createAPath(neighbourRoad, path); paths.add(path); added.remove(head); } if (!headRoadEntrances.isEmpty() && paths.size() > 0) { List<Road> temp = new ArrayList<Road>(); temp.addAll(paths.get(0)); paths.get(0).clear(); for (Road road : headRoadEntrances) { paths.get(0).add(road); paths.get(0).addEntrance(getEntrance(road)); } paths.get(0).addAll(temp); } return paths; }