Beispiel #1
0
  private boolean existsUnblockedSemiDirectedPath(Node from, Node to, List<Node> cond, Graph G) {
    Queue<Node> Q = new LinkedList<Node>();
    Set<Node> V = new HashSet<Node>();
    Q.offer(from);
    V.add(from);

    while (!Q.isEmpty()) {
      Node t = Q.remove();
      if (t == to) return true;

      for (Node u : G.getAdjacentNodes(t)) {
        Edge edge = G.getEdge(t, u);
        Node c = Edges.traverseSemiDirected(t, edge);
        if (c == null) continue;
        if (cond.contains(c)) continue;
        if (c == to) return true;

        if (!V.contains(c)) {
          V.add(c);
          Q.offer(c);
        }
      }
    }

    return false;
  }
Beispiel #2
0
  private static int shortestPath(Node n1, Node n2, Graph g) {
    Queue<Node> Q = new ArrayDeque<Node>();
    Map<Node, Node> V = new HashMap<Node, Node>();

    Q.offer(n1);
    V.put(n1, null);

    while (!Q.isEmpty()) {
      Node m = Q.poll();

      if (V.containsKey(n2)) break;

      for (Node p : g.getAdjacentNodes(m)) {
        if (V.containsKey(p)) continue;

        Q.offer(p);
        V.put(p, m);
      }
    }

    int s = 0;

    do {
      s++;
      n2 = V.get(n2);
    } while (n2 != null);

    return s;
  }