Ejemplo n.º 1
0
  public static boolean isTree(Digraph digraph) {
    Object root = null;
    for (Iterator i = digraph.vertexIterator(); i.hasNext(); ) {
      Object vertex = i.next();
      int inSize = digraph.incomingSize(vertex);
      if (inSize == 0) {
        root = vertex;
        break;
      }
    }

    // not a tree - no vertex with 0 in-degree
    if (root == null) return false;

    // try to reach all vertices from the root candidate
    BreadthFirstSearch traversal = new BreadthFirstSearch(digraph, root);
    while (traversal.isValidTree() && traversal.hasNext()) traversal.next();

    // not a tree - one of vertices has been seen more than once by the BFS
    if (!traversal.isValidTree()) return false;

    // has every vertex been reached?
    Set seenVertices = traversal.getSeenVertices();
    for (Iterator i = digraph.vertexIterator(); i.hasNext(); )
      if (!seenVertices.contains(i.next())) return false;

    // all tests are passed - good!
    return true;
  }
Ejemplo n.º 2
0
  public static Map shiftLevelsDown(Map vertexLevelMap, Digraph digraph) {
    for (Iterator i = digraph.vertexIterator(); i.hasNext(); ) {
      Object rootCandidate = i.next();
      if (digraph.incomingSize(rootCandidate) == 0)
        shiftLevelsDown(vertexLevelMap, digraph, rootCandidate);
    }

    return vertexLevelMap;
  }
Ejemplo n.º 3
0
  public static Map computeLevels(Map vertexLevelMap, Digraph digraph, boolean longest) {
    if (vertexLevelMap == null) vertexLevelMap = new HashMap(digraph.order());

    for (Iterator i = digraph.vertexIterator(); i.hasNext(); ) {
      Object rootCandidate = i.next();
      if (digraph.incomingSize(rootCandidate) == 0)
        computeLevels(vertexLevelMap, digraph, rootCandidate, longest);
    }

    return vertexLevelMap;
  }