Ejemplo n.º 1
0
  public static Map computeLevels(
      Map vertexLevelMap, DigraphIteration digraph, Object root, boolean longest) {
    if (vertexLevelMap == null) vertexLevelMap = new HashMap();

    MutableInteger rootLevel = (MutableInteger) vertexLevelMap.get(root);
    if (rootLevel == null) {
      rootLevel = new MutableInteger(0);
      vertexLevelMap.put(root, rootLevel);
    }

    for (ArcIterator i = digraph.outgoingIterator(root); i.hasNext(); ) {
      i.next();
      Object child = i.getDestination();
      int childLevelCandidate = rootLevel.intValue() + 1;
      MutableInteger childLevel = (MutableInteger) vertexLevelMap.get(child);
      if (childLevel == null) {
        childLevel = new MutableInteger(childLevelCandidate);
        vertexLevelMap.put(child, childLevel);
        computeLevels(vertexLevelMap, digraph, child, longest);
      } else if ((longest && childLevel.intValue() < childLevelCandidate)
          || (!longest && childLevel.intValue() > childLevelCandidate)) {
        childLevel.setValue(childLevelCandidate);
        computeLevels(vertexLevelMap, digraph, child, longest);
      }
    }

    return vertexLevelMap;
  }
Ejemplo n.º 2
0
  public static Map shiftLevelsDown(Map vertexLevelMap, DigraphIteration digraph, Object root) {
    int minChildLevel = Integer.MAX_VALUE;
    for (ArcIterator i = digraph.outgoingIterator(root); i.hasNext(); ) {
      i.next();
      Object child = i.getDestination();
      shiftLevelsDown(vertexLevelMap, digraph, child);
      MutableInteger childLevel = (MutableInteger) vertexLevelMap.get(child);
      minChildLevel =
          (minChildLevel <= childLevel.intValue() ? minChildLevel : childLevel.intValue());
    }

    if (minChildLevel != Integer.MAX_VALUE) {
      MutableInteger rootLevel = (MutableInteger) vertexLevelMap.get(root);
      rootLevel.setValue(minChildLevel - 1);
    }

    return vertexLevelMap;
  }