コード例 #1
0
ファイル: GraphUtils.java プロジェクト: andrus/ashwood
 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;
 }
コード例 #2
0
ファイル: GraphUtils.java プロジェクト: andrus/ashwood
 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;
 }
コード例 #3
0
ファイル: GraphUtils.java プロジェクト: andrus/ashwood
 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;
 }