Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

KeyNotFoundException with MDS layout

Open
#328 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Reproduce the exception in GraphLayout/Samples/WpfApplicationSample/WpfApplicationSample.cs using MdsLayoutSettings and the shown subgraph configuration. Start at the SimpleIntEdge construction where nodeIndex[e.Source] and nodeIndex[e.Target] are accessed, then trace how the MDS layout builds nodeIndex and handles subgraph2. Done means the sample no longer throws KeyNotFoundException for this layout.

Written by the indexing model from the issue text.

Description

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

Dominant language
C#
Stars
1.5k
Forks
317
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/automatic-graph-layout

All issues in microsoft/automatic-graph-layout

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.