|
@@ -1,5 +1,7 @@
|
|
|
package graph;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
+
|
|
|
/**
|
|
|
* creates random {@link LayeredGraphNode}s for testing purposes
|
|
|
* @author kolja
|
|
@@ -7,6 +9,7 @@ package graph;
|
|
|
*/
|
|
|
public class RandomGraphGenerator {
|
|
|
|
|
|
+ private static final int MAX_TRIES = 100;
|
|
|
private double pSubgraph;
|
|
|
private double pEdge;
|
|
|
private int minLayer;
|
|
@@ -42,7 +45,7 @@ public class RandomGraphGenerator {
|
|
|
* @param depth the current depth of the graph (used to check if the maximum depth is reached)
|
|
|
* @return the generated {@link LayeredGraphNode}
|
|
|
*/
|
|
|
- public LayeredGraphNode createRandomNode( LayeredGraphNode parent, int depth )
|
|
|
+ public LayeredGraphNode createRandomNode( LayeredGraphNode parent, int depth, boolean prove )
|
|
|
{
|
|
|
LayeredGraphNode node = new LayeredNode( null, null );
|
|
|
if( parent != null )
|
|
@@ -56,7 +59,7 @@ public class RandomGraphGenerator {
|
|
|
int knoten = (int)( Math.random() * ( maxNodePerLayer - minNodePerLayer ) ) + minNodePerLayer;
|
|
|
for( int j = 0; j < knoten; j++ )
|
|
|
{
|
|
|
- LayeredGraphNode n = createRandomNode( node, depth + 1 );
|
|
|
+ LayeredGraphNode n = createRandomNode( node, depth + 1, true );
|
|
|
n.setLayer( i );
|
|
|
if( i > 0 )
|
|
|
{
|
|
@@ -73,6 +76,52 @@ public class RandomGraphGenerator {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if( prove )
|
|
|
+ {
|
|
|
+ for( int i = 1; i < MAX_TRIES; i++ )
|
|
|
+ {
|
|
|
+ if( validate( node ) )
|
|
|
+ break;
|
|
|
+ node = createRandomNode( parent, depth, false );
|
|
|
+ }
|
|
|
+ if( !validate( node ) )
|
|
|
+ throw new IllegalArgumentException( "No coherent graph found with this parameters.");
|
|
|
+ }
|
|
|
return node;
|
|
|
}
|
|
|
+
|
|
|
+ public static boolean validate( LayeredGraphNode graph )
|
|
|
+ {
|
|
|
+ if( graph.getContainedNodes().size() == 0 )
|
|
|
+ return true;
|
|
|
+ boolean[] visited = new boolean[ graph.getContainedNodes().size() ];
|
|
|
+ ArrayList< LayeredGraphNode > queue = new ArrayList<>();
|
|
|
+ queue.add( graph.getContainedNodes().get( 0 ) );
|
|
|
+ outerloop:
|
|
|
+ while( !queue.isEmpty() )
|
|
|
+ {
|
|
|
+ LayeredGraphNode curr = queue.get( 0 );
|
|
|
+ queue.remove( 0 );
|
|
|
+ for( int i = 0; i < visited.length; i++ )
|
|
|
+ {
|
|
|
+ if( graph.getContainedNodes().get( i ) == curr )
|
|
|
+ {
|
|
|
+ if( visited[ i ] )
|
|
|
+ continue outerloop;
|
|
|
+ else
|
|
|
+ visited[ i ] = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for( LayeredGraphEdge e : curr.getOutgoingEdges() )
|
|
|
+ queue.addAll( e.getTargets() );
|
|
|
+ for( LayeredGraphEdge e : curr.getIncomingEdges() )
|
|
|
+ queue.addAll( e.getSources() );
|
|
|
+ }
|
|
|
+ for( boolean v : visited )
|
|
|
+ {
|
|
|
+ if( !v )
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|