microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.36k stars 304 forks source link

KeyNotFoundException with MDS layout #328

Open michaelkruglos opened 2 years ago

michaelkruglos commented 2 years ago

Hi Lev, I tried to modify WpfAplicationSample to show MDS layout, and it throws KeyNotFoundException. Here are my modifications:

diff --git a/GraphLayout/Samples/WpfApplicationSample/WpfApplicationSample.cs b/GraphLayout/Samples/WpfApplicationSample/WpfApplicationSample.cs
index 5a6e6dd..f16ec58 100644
--- a/GraphLayout/Samples/WpfApplicationSample/WpfApplicationSample.cs
+++ b/GraphLayout/Samples/WpfApplicationSample/WpfApplicationSample.cs
@@ -291,6 +291,7 @@ namespace WpfApplicationSample
 //                //graph.LayoutAlgorithmSettings.EdgeRoutingSettings.EdgeRoutingMode = EdgeRoutingMode.SplineBundling;
 //                //layout the graph and draw it
                 Graph graph = new Graph();
+                graph.LayoutAlgorithmSettings=new Microsoft.Msagl.Layout.MDS.MdsLayoutSettings();
                 graph.AddEdge("47", "58");
                 graph.AddEdge("70", "71");

@@ -315,11 +316,11 @@ namespace WpfApplicationSample
                 graph.Attr.LayerDirection = LayerDirection.LR;
                 //graph.LayoutAlgorithmSettings.EdgeRoutingSettings.EdgeRoutingMode = EdgeRoutingMode.Rectilinear;

-                var global = (SugiyamaLayoutSettings) graph.LayoutAlgorithmSettings;
-                var local  = (SugiyamaLayoutSettings) global.Clone();
-                local.Transformation = PlaneTransformation.Rotation(-Math.PI / 2);
+                var global = (Microsoft.Msagl.Layout.MDS.MdsLayoutSettings) graph.LayoutAlgorithmSettings;
+                var local  = (Microsoft.Msagl.Layout.MDS.MdsLayoutSettings) global.Clone();
+                //local.Transformation = PlaneTransformation.Rotation(-Math.PI / 2);
                 subgraph2.LayoutSettings = local;   // for Collapsing\Expanding
-                global.ClusterSettings.Add(subgraph2, local);
+                //global.ClusterSettings.Add(subgraph2, local);

                 graphViewer.Graph = graph;
             }

The line, which throws exception is:

                intEdges.Add(new SimpleIntEdge {Source = nodeIndex[e.Source], Target = nodeIndex[e.Target]});

Checking with debugger, I can see that subgraph2 is missing from nodeIndex dictionary. image I have no idea why it happens specifically with MDS, but doesn't happen with Sugiyama. Am I doing something wrong, or is there a bug in MDS layout algorithm? Here's the screenshot of the exception being thrown: image

levnach commented 2 years ago

Unfortunately, MDS layout does not support clusters, but the Sugiyama one does. With clusters present, to use the MDS layout, one needs to go through InitialLayoutByClusters. The simple option of assigning viewer.Graph = graph; would not work.