Kaynağa Gözat

no incoherent graphs can be generated annymore

Kolja Strohm 6 yıl önce
ebeveyn
işleme
0d5dad8ae0
2 değiştirilmiş dosya ile 64 ekleme ve 9 silme
  1. 51 2
      src/graph/RandomGraphGenerator.java
  2. 13 7
      src/view/MainView.java

+ 51 - 2
src/graph/RandomGraphGenerator.java

@@ -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;
+    }
 }

+ 13 - 7
src/view/MainView.java

@@ -23,6 +23,7 @@ import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JLayeredPane;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
@@ -573,13 +574,18 @@ public class MainView {
                         if( ok )
                         {                            
                             RandomGraphGenerator r = new RandomGraphGenerator( pSubGraphD, pEdgeD, minLayerI, maxLayerI, minNodeI, maxNodeI, maxDepthI );
-                            LayeredGraphNode graph = r.createRandomNode( null, 0 );
-                            SweepCrossingMinimizer cminzer = new SweepCrossingMinimizer();
-                            for( int i = 0; i < 10; i++ )
-                              cminzer.minimizeCrossings( graph );
-                            InitializeNodePositions.placeNodes( graph );
-                            new MainView( graph );
-                            diag.setVisible( false );
+                            try {
+                                LayeredGraphNode graph = r.createRandomNode( null, 0, true );
+                                SweepCrossingMinimizer cminzer = new SweepCrossingMinimizer();
+                                for( int i = 0; i < 10; i++ )
+                                  cminzer.minimizeCrossings( graph );
+                                InitializeNodePositions.placeNodes( graph );
+                                new MainView( graph );
+                                diag.setVisible( false );
+                            } catch( Exception e1 )
+                            {
+                                JOptionPane.showMessageDialog(frame, e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
+                            }
                         }
                     }