Browse Source

fehler in den rechten layouts behoben

Kolja Strohm 6 years ago
parent
commit
b54c16c290
5 changed files with 188 additions and 35 deletions
  1. 151 0
      papergraph.json
  2. 2 0
      save_err1.json
  3. 19 10
      src/Algorithms/Animated/BK/BlockCalc.java
  4. 9 18
      src/Algorithms/Animated/BK/Compaction.java
  5. 7 7
      src/Main.java

+ 151 - 0
papergraph.json

@@ -0,0 +1,151 @@
+{
+    "layers": [
+        [
+            {"name":"1"},
+            {"name":"2"}
+        ],
+        [
+            {"name":"d1"},
+            {"name":"d2"},
+            {"name":"d3"},
+            {"name":"3"},
+            {"name":"d4"}
+        ],
+        [
+            {"name":"d5"},
+            {"name":"d6"},
+            {"name":"4"},
+            {"name":"5"},
+            {"name":"d7"},
+            {"name":"d8"}
+        ],
+        [
+            {"name":"d9"},
+            {"name":"d10"},
+            {"name":"6"},
+            {"name":"7"},
+            {"name":"d11"},
+            {"name":"d12"}
+        ],
+        [
+            {"name":"d13"},
+            {"name":"d14"},
+            {"name":"8"},
+            {"name":"d15"},
+            {"name":"d16"},
+            {"name":"9"},
+            {"name":"d17"},
+            {"name":"d18"}
+        ],
+        [
+            {"name":"d19"},
+            {"name":"d20"},
+            {"name":"10"},
+            {"name":"11"},
+            {"name":"d21"},
+            {"name":"d22"},
+            {"name":"12"},
+            {"name":"d23"},
+            {"name":"d24"}
+        ],
+        [
+            {"name":"13"},
+            {"name":"d25"},
+            {"name":"14"},
+            {"name":"15"},
+            {"name":"16"},
+            {"name":"d26"},
+            {"name":"d27"},
+            {"name":"d28"},
+            {"name":"d29"}
+        ],
+        [
+            {"name":"17"},
+            {"name":"d30"},
+            {"name":"18"},
+            {"name":"19"},
+            {"name":"d31"},
+            {"name":"20"},
+            {"name":"d32"}
+        ],
+        [
+            {"name":"21"},
+            {"name":"22"},
+            {"name":"d33"},
+            {"name":"d34"}
+        ],
+        [
+            {"name":"23"}
+        ]
+    ],
+    "edges": [
+        {"source":"1","target":"3"},
+        {"source":"1","target":"d1"},
+        {"source":"1","target":"d2"},
+        {"source":"1","target":"d3"},
+        {"source":"2","target":"3"},
+        {"source":"2","target":"d4"},
+        {"source":"d1","target":"d5"},
+        {"source":"d2","target":"d6"},
+        {"source":"d3","target":"4"},
+        {"source":"3","target":"4"},
+        {"source":"3","target":"5"},
+        {"source":"3","target":"d7"},
+        {"source":"d4","target":"d8"},
+        {"source":"d5","target":"d9"},
+        {"source":"d6","target":"d10"},
+        {"source":"4","target":"6"},
+        {"source":"5","target":"7"},
+        {"source":"d7","target":"d11"},
+        {"source":"d8","target":"d12"},
+        {"source":"d9","target":"d13"},
+        {"source":"d10","target":"d14"},
+        {"source":"6","target":"8"},
+        {"source":"6","target":"d15"},
+        {"source":"6","target":"d16"},
+        {"source":"7","target":"9"},
+        {"source":"d11","target":"d17"},
+        {"source":"d12","target":"d18"},
+        {"source":"d13","target":"d19"},
+        {"source":"d14","target":"d20"},
+        {"source":"8","target":"10"},
+        {"source":"8","target":"11"},
+        {"source":"d15","target":"d21"},
+        {"source":"d16","target":"d22"},
+        {"source":"9","target":"12"},
+        {"source":"d17","target":"d23"},
+        {"source":"d18","target":"d24"},
+        {"source":"d19","target":"13"},
+        {"source":"d20","target":"d25"},
+        {"source":"10","target":"13"},
+        {"source":"10","target":"14"},
+        {"source":"10","target":"15"},
+        {"source":"11","target":"15"},
+        {"source":"11","target":"16"},
+        {"source":"d21","target":"16"},
+        {"source":"d22","target":"d26"},
+        {"source":"12","target":"d27"},
+        {"source":"d23","target":"d28"},
+        {"source":"d24","target":"d29"},
+        {"source":"13","target":"17"},
+        {"source":"14","target":"17"},
+        {"source":"14","target":"18"},
+        {"source":"16","target":"18"},
+        {"source":"16","target":"19"},
+        {"source":"16","target":"20"},
+        {"source":"d25","target":"d30"},
+        {"source":"d26","target":"d31"},
+        {"source":"d27","target":"20"},
+        {"source":"d28","target":"d32"},
+        {"source":"d29","target":"20"},
+        {"source":"18","target":"21"},
+        {"source":"19","target":"22"},
+        {"source":"d30","target":"21"},
+        {"source":"d31","target":"d33"},
+        {"source":"21","target":"23"},
+        {"source":"22","target":"23"},
+        {"source":"d32","target":"d34"},
+        {"source":"d33","target":"23"},
+        {"source":"d34","target":"23"}
+    ]
+}

+ 2 - 0
save_err1.json

@@ -1,6 +1,7 @@
 {
 	"layers":[
 		[
+            {"name":"0"},
 			{"name":"1"},
 			{"name":"2"}
 		],
@@ -15,6 +16,7 @@
 		]
 	],
 	"edges":[
+        {"source":"0","target":"5"},
 		{"source":"1","target":"5"},
 		{"source":"2","target":"5"},
 		{"source":"3","target":"7"},

+ 19 - 10
src/Algorithms/Animated/BK/BlockCalc.java

@@ -2,6 +2,7 @@ package Algorithms.Animated.BK;
 
 import java.awt.Color;
 import java.util.ArrayList;
+import java.util.Collections;
 
 import Algorithms.Animated.AlgorithmStage;
 import Algorithms.Animated.BackwardAction;
@@ -64,12 +65,21 @@ public class BlockCalc implements AlgorithmStage {
 	
 	private int calcNodeIndex( int index )
 	{
-        if( layout == LayoutType.TOP_BOTTOM_LEFT || layout == LayoutType.TOP_BOTTOM_RIGHT )
+        if( layout == LayoutType.TOP_BOTTOM_LEFT || layout == LayoutType.BOTTOM_TOP_LEFT )
             return index;
-        //if( layout == LayoutType.BOTTOM_TOP_LEFT || layout == LayoutType.BOTTOM_TOP_RIGHT )
-        //    return graph.getContainedLayers().get( calcLayerIndex() ).size() - index - 1;
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT || layout == LayoutType.BOTTOM_TOP_RIGHT )
+            return graph.getContainedLayers().get( calcLayerIndex() ).size() - index - 1;
         return index;
 	}
+    
+    private int calcBeforeLayerNodeIndex( int index )
+    {
+        if( layout == LayoutType.TOP_BOTTOM_LEFT || layout == LayoutType.BOTTOM_TOP_LEFT )
+            return index;
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT || layout == LayoutType.BOTTOM_TOP_RIGHT )
+            return graph.getContainedLayers().get( calcBeforeLayerIndex() ).size() - index - 1;
+        return index;
+    }
 	
 	@Override
 	public StageStatus forwardStep() {
@@ -87,6 +97,8 @@ public class BlockCalc implements AlgorithmStage {
             incommingEdges = current.getSortedIncomingEdges();
         if( layout == LayoutType.BOTTOM_TOP_LEFT || layout == LayoutType.BOTTOM_TOP_RIGHT )
             incommingEdges = current.getSortedOutgoingEdges();
+        if( layout == LayoutType.TOP_BOTTOM_RIGHT || layout == LayoutType.BOTTOM_TOP_RIGHT )
+        Collections.reverse( incommingEdges );
 		if( incommingEdges.size() == 0 )
 		{
 			backwards.add( 0, () -> {
@@ -94,11 +106,7 @@ public class BlockCalc implements AlgorithmStage {
 			});
 			return calcNextState();
 		}
-		int[] ms = null;
-		if( layout == LayoutType.TOP_BOTTOM_LEFT || layout == LayoutType.BOTTOM_TOP_LEFT )
-		    ms = new int[]{(incommingEdges.size() + 1) / 2, (int)( (incommingEdges.size() + 1) / 2.0 + 0.5 )};
-        if( layout == LayoutType.TOP_BOTTOM_RIGHT || layout == LayoutType.BOTTOM_TOP_RIGHT )
-            ms = new int[]{(int)( (incommingEdges.size() + 1) / 2.0 + 0.5 ), (incommingEdges.size() + 1) / 2};
+		int[] ms = {(incommingEdges.size() + 1) / 2, (int)( (incommingEdges.size() + 1) / 2.0 + 0.5 )};
 		boolean backwardsAdded = false;
 		for( int m : ms )
 		{
@@ -110,7 +118,8 @@ public class BlockCalc implements AlgorithmStage {
                 if( layout == LayoutType.BOTTOM_TOP_LEFT || layout == LayoutType.BOTTOM_TOP_RIGHT )
                     u = incommingEdges.get( m - 1 ).getTargets().get( 0 );
                 ArrayList<LayeredGraphEdge> conflicts = incommingEdges.get( m - 1 ).calcConflictedEdges();
-		        if( !incommingEdges.get( m - 1 ).isConflicted( layout ) && calcNodeIndex( r ) < calcNodeIndex( graph.getContainedLayers().get( calcBeforeLayerIndex() ).indexOf( u ) + 1 ) )
+                
+		        if( !incommingEdges.get( m - 1 ).isConflicted( layout ) && r < calcBeforeLayerNodeIndex( graph.getContainedLayers().get( calcBeforeLayerIndex() ).indexOf( u ) ) + 1 )
 		        {
 		        	System.out.println( "" );
 		        	ArrayList< Boolean > oldConflicts = new ArrayList<>();
@@ -128,7 +137,7 @@ public class BlockCalc implements AlgorithmStage {
 		            current.setColor( u.getRoot( layout ).getColor( layout ), layout );
 		            current.setRoot( u.getRoot( layout ), layout );
 		            current.setAlignTo( current.getRoot( layout ), layout );
-		            r = graph.getContainedLayers().get( calcBeforeLayerIndex() ).indexOf( u ) + 1;
+		            r = calcBeforeLayerNodeIndex( graph.getContainedLayers().get( calcBeforeLayerIndex() ).indexOf( u ) ) + 1;
 		            int oldStep = step++;
 		            final LayeredGraphNode uf = u;
 		            backwards.add( 0, () -> {

+ 9 - 18
src/Algorithms/Animated/BK/Compaction.java

@@ -1,7 +1,6 @@
 package Algorithms.Animated.BK;
 
 import java.util.ArrayList;
-import java.util.Collections;
 
 import Algorithms.Animated.AlgorithmStage;
 import Algorithms.Animated.BackwardAction;
@@ -55,13 +54,9 @@ public class Compaction implements AlgorithmStage{
 		return max;
 	}
 	
-	@SuppressWarnings("unchecked")
 	private LayeredGraphNode getNodeFromIndex( int index )
 	{
-		ArrayList< ArrayList< LayeredGraphNode > > layers = (ArrayList<ArrayList<LayeredGraphNode>>) graph.getContainedLayers().clone();
-        if( layout == LayoutType.BOTTOM_TOP_LEFT || layout == LayoutType.BOTTOM_TOP_RIGHT )
-        	Collections.reverse( layers );
-	    for( ArrayList< LayeredGraphNode > l : layers )
+	    for( ArrayList< LayeredGraphNode > l : graph.getContainedLayers() )
 	    {
 	        if( index >= l.size() )
 	            index -= l.size();
@@ -87,7 +82,7 @@ public class Compaction implements AlgorithmStage{
 				{
 					if( getNodeFromIndex( vIndex ).isXUndefined( layout ) && getNodeFromIndex( vIndex ) == getNodeFromIndex( vIndex ).getRoot( layout ) )
 					{
-						found = true;
+	                    found = true;
 						break;
 					}
 				}
@@ -111,7 +106,7 @@ public class Compaction implements AlgorithmStage{
 					f.v.setX( 0, true, layout );
 					f.v.setSelected( layout ); // zeige knoten als aktiven knoten an
 					f.w = f.v;
-                    System.out.println( "call place_block( " + f.v.getName() + " )" );
+                    System.out.println( "call place_block( " + f.v + " )" );
 					stack.add( 0, f );
 					
 					// die "undo"-action
@@ -143,7 +138,7 @@ public class Compaction implements AlgorithmStage{
 							nsf.v.setX( 0, true, layout );
 							nsf.v.setSelected( layout ); // zeige knoten als aktiven knoten an
 							nsf.w = nsf.v;
-		                    System.out.println( "call place_block( " + nsf.v.getName() + " )" );
+		                    System.out.println( "call place_block( " + nsf.v + " )" );
 							stack.add( 0, nsf );
 		                    
 		                    // die "undo"-action
@@ -170,7 +165,7 @@ public class Compaction implements AlgorithmStage{
 						sf.w = sf.w.getAlignedTo( layout );
 						if( sf.w == sf.v ) // schleifenabbruchbedingung
 						{ //abbrechen, placeblock beendet
-                            System.out.println( "return place_block( " + sf.v.getName() + " )" );
+                            System.out.println( "return place_block( " + sf.v + " )" );
 							stack.remove( 0 );
 							actions.add( 0, ()-> {
 								stack.add( 0, sf );
@@ -203,13 +198,9 @@ public class Compaction implements AlgorithmStage{
                         sf.v.setSink( sf.u.getSink( layout ), layout ); // sink[v] := sink[u]
 
 					if( sf.v.getSink( layout ) != sf.u.getSink( layout ) ) // sink[v] != sink [u]?
-					{
 						sf.u.getSink( layout ).setShift( // shift[sink[u]] =
 						  Math.min( sf.u.getSink( layout ).getShift( layout ),  // min(shift[sink[u]]
 						          sf.v.getX( layout ) - sf.u.getX( layout ) - calcSpacing() ), layout ); // y_v - y_u - s
-						System.out.println( "Set Shift von " + sf.u.getSink( layout ).getName() + " to " + Math.min( sf.u.getSink( layout ).getShift( layout ),  // min(shift[sink[u]]
-						          sf.v.getX( layout ) - sf.u.getX( layout ) - calcSpacing() ) );
-					}
 					else
 					    // y_v = max {y_v, y_u + s}
 						sf.v.setX( Math.max( sf.v.getX( layout ), sf.u.getX( layout ) + calcSpacing() ), true, layout );
@@ -224,7 +215,7 @@ public class Compaction implements AlgorithmStage{
 					
 					if( sf.w == sf.v ) // schleifenabbruchbedingung
 					{ //abbrechen, placeblock beendet  
-					    System.out.println( "return place_block( " + sf.v.getName() + " )" );
+					    System.out.println( "return place_block( " + sf.v + " )" );
 						stack.remove( 0 );
 						actions.add( 0, ()-> {
 							stack.add( 0, sf );
@@ -252,13 +243,13 @@ public class Compaction implements AlgorithmStage{
 		}
 		else if( state == CompactionState.APPLY_SHIFT )
 		{
-			LayeredGraphNode v = getNodeFromIndex( vIndex );
+			LayeredGraphNode v = graph.getContainedNodes().get( vIndex );
 			double oldX = v.getX( layout );
 			boolean oldDef = !v.isXUndefined( layout );
 			v.setSelected( layout );
 			v.setX( v.getRoot( layout ).getX( layout ), true, layout );
-			if( v == v.getRoot( layout ) && v.getSink( layout ).getShift( layout ) < Double.POSITIVE_INFINITY )
-				v.setX( v.getX( layout ) + v.getSink( layout ).getShift( layout ), true, layout );
+			if( v == v.getRoot( layout ) && v.getRoot( layout ).getSink( layout ).getShift( layout ) < Double.POSITIVE_INFINITY )
+				v.setX( v.getX( layout ) + v.getRoot( layout ).getSink( layout ).getShift( layout ), true, layout );
 			actions.add( 0, ()-> {
 				v.setX( oldX, oldDef, layout );
 				v.setSelected( layout );

+ 7 - 7
src/Main.java

@@ -13,13 +13,13 @@ import View.MainView;
 public class Main {
 
 	public static void main(String[] args) {
-		//Reader r = new Reader( "save.graph" );
-		//LayeredGraphNode graph = r.readInputGraph();
-	    RandomGraphGenerator r = new RandomGraphGenerator( 1, 0.5, 2, 2, 2, 2, 3 );
-	    LayeredGraphNode graph = r.createRandomNode( null, 0 );
-	    SweepCrossingMinimizer cminzer = new SweepCrossingMinimizer();
-	    for( int i = 0; i < 10; i++ )
-	    	cminzer.minimizeCrossings( graph );
+		Reader r = new Reader( "save_err1.json" );
+		LayeredGraphNode graph = r.readInputGraph();
+	    //RandomGraphGenerator r = new RandomGraphGenerator( 1, 1, 2, 2, 4, 4, 2 );
+	    //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 );
 	}