Пример #1
0
  /**
   * The nitty gritty implementation of finding the paths for each node. Pretty much a recursive DFS
   * that tracks it currentPath at every step. This implementation will not continue to hop through
   * cycles.
   *
   * @param source
   * @param sharedPathsList
   * @param currentPath
   * @param count
   * @return A List of all the "Paths" taken from this node.
   */
  public List<ArrayList<Node>> findPaths(
      Node source, List<ArrayList<Node>> sharedPathsList, Stack<Node> currentPath, int count) {

    currentPath.push(source);
    source.markAsVisiting();

    // Copy current path and add it to list of all paths
    ArrayList<Node> path = new ArrayList<Node>(currentPath);
    sharedPathsList.add(sharedPathsList.size(), path);

    if (++count >= 10) {
      return sharedPathsList;
    }

    for (Node neighbor : source.getNeighbors()) {
      if (neighbor.isBeingVisited()) {
        ArrayList<Node> temp = new ArrayList<Node>(currentPath);
        temp.add(temp.size(), neighbor);
        sharedPathsList.add(sharedPathsList.size(), temp);
      } else {
        findPaths(neighbor, sharedPathsList, currentPath, count);
      }
    }

    currentPath.pop();
    source.markAsVisited();
    return sharedPathsList;
  }
Пример #2
0
  /**
   * Simple A*. Computes shortest paths to all reachable nodes from the source node given. This
   * method will cache the result so that if it is called later on the same node and nothing has
   * been changed (no nodes added or removed), it will not compute the result again, but will return
   * the cached value.
   *
   * @param source
   * @return
   */
  public Map<Node, Route> computeShortestRoutes(Node source) {

    // Don't perform calculation if it's been done before and nothings changed
    if (!dirty && (shortestRoutesCache.get(source) != null)) {
      return shortestRoutesCache.get(source);
    }

    List<Node> nodes = new LinkedList<Node>();
    Map<Node, Integer> weights = new HashMap<Node, Integer>();
    Map<Node, Node> prevNodes = new HashMap<Node, Node>();

    weights.put(source, 0);

    // Initialize all nodes to have effectively infinite distance
    for (Node node : allNodes) {
      if (!node.equals(source)) {
        weights.put(node, Integer.MAX_VALUE);
        prevNodes.put(node, null);
      }
      nodes.add(node);
    }

    // A*
    while (!nodes.isEmpty()) {
      Node node = findMinimum(nodes, weights);
      if (weights.get(node).equals(Integer.MAX_VALUE)) {
        continue;
      }
      for (Node neighbor : node.getNeighbors()) {
        Integer alternateWeight = weights.get(node) + node.getEdgeForNeighbor(neighbor).getWeight();
        if (alternateWeight < weights.get(neighbor)) {
          weights.put(neighbor, alternateWeight);
          prevNodes.put(neighbor, node);
        }
      }
    }

    List<Node> unreachableNodes = new ArrayList<Node>();

    for (Map.Entry<Node, Integer> entry : weights.entrySet()) {
      if (entry.getValue().equals(Integer.MAX_VALUE)) {
        unreachableNodes.add(entry.getKey());
      }
    }

    for (Node unreachableNode : unreachableNodes) {
      weights.remove(unreachableNode);
    }
    Map<Node, Route> routeMap = makeRouteMap(source, weights, prevNodes);

    // Cache result
    shortestRoutesCache.put(source, routeMap);

    dirty = Boolean.FALSE;

    return routeMap;
  }
Пример #3
0
 public void writeDOT(File f) throws FileNotFoundException {
   PrintStream ps = new PrintStream(f);
   ps.println("graph {");
   for (Node n : nodes) {
     for (Node n2 : n.getNeighbors()) {
       ps.printf("\"%s\" -- \"%s\";\n", n.getShortName(), n2.getShortName());
     }
   }
   ps.println("}");
 }
Пример #4
0
  private static Set<Functionality.Node> getNodesInDistance(
      Functionality.Node focusNode, int maxDistance, EdgeTypeEnum coreEdgeType) {
    Set<Functionality.Node> visitedNodes = new HashSet<Functionality.Node>();
    visitedNodes.add(focusNode);

    Set<Functionality.Node> retNodes = new HashSet<Functionality.Node>();
    retNodes.add(focusNode);

    List<Functionality.Node> proceedNodes = new ArrayList<Functionality.Node>();
    List<Integer> proceedDist = new ArrayList<Integer>();

    int nodePointer = 0;
    Node currNode;
    proceedNodes.add(focusNode);
    proceedDist.add(0);

    while (nodePointer < proceedNodes.size() && proceedDist.get(nodePointer) <= maxDistance - 1) {
      currNode = proceedNodes.get(nodePointer);
      for (Iterator<Node> it = currNode.getNeighbors().iterator(); it.hasNext(); ) {
        Node neib = it.next();

        if (coreEdgeType == EdgeTypeEnum.normal
                && DataModule.displayedGraph.edgeIsDotted(currNode, neib)
            || coreEdgeType == EdgeTypeEnum.thick
                && (DataModule.displayedGraph.edgeIsDotted(currNode, neib)
                    || DataModule.displayedGraph.edgeIsNormal(currNode, neib))) {
          continue;
        }

        if (visitedNodes.contains(neib) == false) {
          visitedNodes.add(neib);
          proceedDist.add(proceedDist.get(nodePointer) + 1);
          proceedNodes.add(neib);
        }
      }
      nodePointer++;
    }

    return visitedNodes;
  }
Пример #5
0
 @Override
 // Process every node that is gray and explore its neighbors
 public void map(LongWritable key, Text value, Context context)
     throws IOException, InterruptedException {
   Node child;
   String nodeProperties = value.toString();
   // Create a Node
   Node node = new Node(nodeProperties);
   if (node.getColor() == Node.Color.GRAY) { // 'G'
     int distance = node.getDistance();
     int idParent = node.getId();
     // Explore their neighbors
     ArrayList<Integer> neigh = node.getNeighbors();
     for (int i : neigh) {
       child = new Node(i, distance + 1, idParent); // Node(IDP)
       // Writing the explored neighbors
       context.write(new IntWritable(child.getId()), new Text(child.toString()));
     }
     // Change the explored node to Black color
     node.setColor(Node.Color.BLACK);
   }
   // Black, White and Gray => Black nodes Written
   context.write(new IntWritable(node.getId()), new Text(node.toString()));
 } // End of map()
Пример #6
0
  private static int getLongestPathLength(Node seed) {
    Queue<Node> queue = new LinkedList<Node>();
    Queue<Node> next = new LinkedList<Node>();
    Set<Node> seen = new TreeSet<Node>();

    queue.add(seed);
    seen.add(seed);

    int dist;
    for (dist = 0; !queue.isEmpty(); dist++) {
      while (!queue.isEmpty()) {
        Node dequeue = queue.poll();
        for (Node neighbor : dequeue.getNeighbors()) {
          if (seen.contains(neighbor)) continue;
          next.add(neighbor);
          seen.add(neighbor);
        }
      }

      queue.addAll(next);
      next.clear();
    }
    return dist;
  }