示例#1
0
 public static boolean isAcyclic(Digraph digraph) {
   int order = digraph.order();
   if (order == 0) return true;
   Set spanned = new HashSet(order);
   DepthFirstStampSearch dfs = new DepthFirstStampSearch(digraph, digraph.vertexIterator().next());
   for (Iterator i = digraph.vertexIterator(); i.hasNext(); ) {
     Object dfsRoot = i.next();
     if (spanned.contains(dfsRoot)) continue;
     dfs.reset(dfsRoot);
     Map dfsOrders = dfs.traverse(new HashMap(digraph.order()));
     for (Iterator j = dfsOrders.entrySet().iterator(); j.hasNext(); ) {
       Map.Entry entry = (Map.Entry) j.next();
       Object origin = entry.getKey();
       DepthFirstStampSearch.OrderPair orgOrders =
           (DepthFirstStampSearch.OrderPair) entry.getValue();
       spanned.add(origin);
       for (ArcIterator k = digraph.outgoingIterator(origin); k.hasNext(); ) {
         k.next();
         Object dst = k.getDestination();
         DepthFirstStampSearch.OrderPair dstOrders =
             (DepthFirstStampSearch.OrderPair) dfsOrders.get(dst);
         if (dstOrders.getPostOrder() > orgOrders.getPostOrder()) return false;
       }
     }
     if (dfsOrders.size() == order) break;
   }
   return true;
 }
示例#2
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;
  }
示例#3
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;
  }
示例#4
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;
  }
示例#5
0
 public static Digraph merge(Digraph destination, DigraphIteration graphToMerge) {
   for (Iterator i = graphToMerge.vertexIterator(); i.hasNext(); ) {
     destination.addVertex(i.next());
   }
   for (ArcIterator i = graphToMerge.arcIterator(); i.hasNext(); ) {
     Object arc = i.next();
     Object origin = i.getOrigin();
     Object dst = i.getDestination();
     destination.putArc(origin, dst, arc);
   }
   return destination;
 }
示例#6
0
 public static Digraph randomize(Digraph digraph, int order, int size, Random randomizer) {
   for (int i = 1; i <= order; i++) digraph.addVertex(new Integer(i));
   Random random = randomizer;
   int n_2 = order * order;
   size = Math.min(size, n_2);
   for (int arc = 1; arc <= size; arc++) {
     int arcCode = random.nextInt(n_2);
     int origin = arcCode / order + 1;
     int dst = arcCode % order + 1;
     digraph.putArc(new Integer(origin), new Integer(dst), new Integer(arc));
   }
   return digraph;
 }
示例#7
0
 public static Digraph randomizeAcyclic(
     Digraph digraph, int order, int incomingSize, int outgoingSize, Random randomizer) {
   Random random = randomizer;
   int arc = 1;
   for (int i = 1; i <= order; i++) {
     Integer destination = new Integer(i);
     digraph.addVertex(destination);
     for (int j = 0; j < incomingSize; j++) {
       int org = random.nextInt(i);
       if (org == 0) continue;
       Integer origin = new Integer(org);
       if (digraph.outgoingSize(origin) >= outgoingSize) continue;
       digraph.putArc(origin, destination, new Integer(arc++));
     }
   }
   return digraph;
 }
示例#8
0
 public static Digraph transform(
     Digraph result,
     DigraphIteration source,
     Transformer vertexTransform,
     Transformer arcTransform) {
   for (Iterator i = new TransformIterator(source.vertexIterator(), vertexTransform);
       i.hasNext(); ) {
     result.addVertex(i.next());
   }
   for (ArcIterator i =
           new TransformArcIterator(source.arcIterator(), vertexTransform, arcTransform);
       i.hasNext(); ) {
     Object arc = i.next();
     Object origin = i.getOrigin();
     Object dst = i.getDestination();
     result.putArc(origin, dst, arc);
   }
   return result;
 }
示例#9
0
 public static Digraph randomizeTree(
     Digraph digraph, int maxChildren, int maxLevels, Random randomizer) {
   int vertexIndex = 1;
   Object root = new Integer(vertexIndex);
   List level = Collections.singletonList(root);
   digraph.addVertex(root);
   for (int i = 1; i < maxLevels; i++) {
     List childLevel = new ArrayList(level.size() * maxChildren);
     for (Iterator j = level.iterator(); j.hasNext(); ) {
       Object parent = j.next();
       int childCount = randomizer.nextInt(maxChildren + 1);
       for (int k = 0; k < childCount; k++) {
         Object child = new Integer(++vertexIndex);
         digraph.addVertex(child);
         digraph.putArc(parent, child, Boolean.TRUE);
         childLevel.add(child);
       }
     }
     if (childLevel.isEmpty()) break;
     level = childLevel;
   }
   return digraph;
 }
示例#10
0
 public static boolean isStronglyConnected(Digraph digraph) {
   return isStronglyConnected(digraph, digraph.vertexIterator().next(), digraph.order());
 }