private static LinkedList<Coords> polarSort(LinkedList<Coords> list) { // find point with lowest x-coord int i = 0; double lowest = 9999999999.0; int lowIndex = 0; Coords tmp; while (i < list.size()) { tmp = list.get(i); if (tmp.getX() < lowest) { lowest = tmp.getX(); lowIndex = i; } else if (tmp.getX() == lowest) { if (tmp.getY() < list.get(lowIndex).getY()) { lowIndex = i; } } i++; } Coords lowestCoord = list.remove(lowIndex); LinkedList<Coords> sorted = new LinkedList<Coords>(); sorted.add(list.poll()); while (list.size() > 0) { tmp = list.poll(); i = sorted.size(); while (i > 0 && compVect(lowestCoord, sorted.get(i - 1), tmp) < 0.0) { i--; } sorted.add(i, tmp); } sorted.add(0, lowestCoord); return sorted; }
public static int[] bfs(int s, int e, int[][] f) { int[] prev = new int[f.length]; Arrays.fill(prev, -1); LinkedList<Integer> q = new LinkedList<Integer>(); q.add(s); while (q.size() > 0) { int at = q.poll(); for (int i = 0; i < f.length; i++) { if (prev[i] != -1 || i == at) continue; if (f[at][i] > 0) { prev[i] = at; if (i == e) return prev; q.addLast(i); } } } return null; }
/* * Finds the number of routes from a specified start node to a specified end node. * Restrictions include max, distance, and lessthan. */ private static void findNumberofRoutes(String inputString) { int routes = 0; int counter1 = 0; int counter2 = 0; int depth = 0; String restriction = inputString.substring(input.indexOf(":") + 1, input.indexOf("=")); String restrictionValueString = inputString.substring(input.indexOf("=") + 2); int restrictionValue = Integer.parseInt(restrictionValueString); LinkedList<Edge> bfs = new LinkedList<Edge>(); Stack<Edge> dfs = new Stack<Edge>(); Stack<Integer> dfslength = new Stack<Integer>(); Edge current; int currentLength; first = inputString.charAt(0); second = inputString.charAt(2); if (restriction.equals("max")) { edgeList = nodeMap.get(first).getEdges(); for (i = 0; i < edgeList.size(); i++) { bfs.add(edgeList.get(i)); counter2++; } while (depth < restrictionValue) { counter1 = counter2; counter2 = 0; for (i = 0; i < counter1; i++) { current = bfs.poll(); if (current != null && current.getEdgeDestination() == second) { routes++; } edgeList = nodeMap.get(current.getEdgeDestination()).getEdges(); for (j = 0; j < edgeList.size(); j++) { bfs.add(edgeList.get(j)); counter2++; } } depth++; } } else if (restriction.equals("distance")) { edgeList = nodeMap.get(first).getEdges(); for (i = 0; i < edgeList.size(); i++) { bfs.add(edgeList.get(i)); counter2++; } while (depth <= restrictionValue) { counter1 = counter2; counter2 = 0; for (i = 0; i < counter1; i++) { current = bfs.poll(); if (current != null && current.getEdgeDestination() == second && depth == restrictionValue) { routes++; } edgeList = nodeMap.get(current.getEdgeDestination()).getEdges(); for (j = 0; j < edgeList.size(); j++) { bfs.add(edgeList.get(j)); counter2++; } } depth++; } } else if (restriction.equals("lessthan")) { edgeList = nodeMap.get(first).getEdges(); for (i = 0; i < edgeList.size(); i++) { if (edgeList.get(i).getEdgeLength() < restrictionValue) { dfs.push(edgeList.get(i)); dfslength.push(edgeList.get(i).getEdgeLength()); } } while (!dfs.empty()) { current = dfs.pop(); currentLength = dfslength.pop(); edgeList = nodeMap.get(current.getEdgeDestination()).getEdges(); for (i = 0; i < edgeList.size(); i++) { if (currentLength + edgeList.get(i).getEdgeLength() < restrictionValue) { if (edgeList.get(i).getEdgeDestination() == second) { routes++; } dfs.push(edgeList.get(i)); dfslength.push(edgeList.get(i).getEdgeLength() + currentLength); } } } } try { bw.write(Integer.toString(routes)); bw.newLine(); } catch (IOException e) { System.out.println(e); } }