From 92bab9db75a9c43774dc89f53ce181489b8f7ce2 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 23 Oct 2019 17:42:48 -0700 Subject: [PATCH] disallow self-loop on simple graph --- Graph/DirectedGraph.cs | 2 ++ test/DirectedGraph.cs | 31 +++++++++++++++--------------- test/DirectedWeightedMultiGraph.cs | 3 ++- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/Graph/DirectedGraph.cs b/Graph/DirectedGraph.cs index 9de68c9..6b750c4 100644 --- a/Graph/DirectedGraph.cs +++ b/Graph/DirectedGraph.cs @@ -110,6 +110,8 @@ namespace MoonTools.Core.Graph { CheckNodes(v, u); + if (v.Equals(u)) { throw new ArgumentException("Self-edges are not allowed in a simple graph. Use a multigraph instead"); } + neighbors[v].Add(u); edges.Add((v, u)); edgesToEdgeData.Add((v, u), edgeData); diff --git a/test/DirectedGraph.cs b/test/DirectedGraph.cs index 84daa47..420826a 100644 --- a/test/DirectedGraph.cs +++ b/test/DirectedGraph.cs @@ -61,6 +61,15 @@ namespace Tests Assert.That(myGraph.Neighbors(1), Does.Not.Contain(4)); } + [Test] + public void AddSelfEdge() + { + var myGraph = new DirectedGraph(); + myGraph.AddNodes(1, 2); + + myGraph.Invoking(x => x.AddEdge(1, 1, dummyEdgeData)).Should().Throw(); + } + [Test] public void Order() { @@ -316,7 +325,6 @@ namespace Tests var myGraph = new DirectedGraph(); myGraph.AddNodes(1, 2, 3, 4); myGraph.AddEdges( - (1, 1, dummyEdgeData), (1, 2, dummyEdgeData), (2, 3, dummyEdgeData), (2, 1, dummyEdgeData), @@ -326,7 +334,7 @@ namespace Tests var clone = myGraph.Clone(); Assert.That(clone, Is.Not.EqualTo(myGraph)); clone.Nodes.Should().BeEquivalentTo(1, 2, 3, 4); - clone.Neighbors(1).Should().BeEquivalentTo(1, 2); + clone.Neighbors(1).Should().BeEquivalentTo(2); clone.Neighbors(2).Should().BeEquivalentTo(3, 1); clone.Neighbors(3).Should().BeEquivalentTo(4); } @@ -337,7 +345,6 @@ namespace Tests var myGraph = new DirectedGraph(); myGraph.AddNodes(1, 2, 3, 4); myGraph.AddEdges( - (1, 1, dummyEdgeData), (1, 2, dummyEdgeData), (2, 3, dummyEdgeData), (2, 1, dummyEdgeData), @@ -346,7 +353,7 @@ namespace Tests var subGraph = myGraph.SubGraph(1, 2, 3); subGraph.Nodes.Should().BeEquivalentTo(1, 2, 3); - subGraph.Neighbors(1).Should().BeEquivalentTo(1, 2); + subGraph.Neighbors(1).Should().BeEquivalentTo(2); subGraph.Neighbors(2).Should().BeEquivalentTo(1, 3); subGraph.Neighbors(3).Should().NotContain(4); } @@ -357,29 +364,23 @@ namespace Tests var myGraph = new DirectedGraph(); myGraph.AddNodes(0, 1, 2); myGraph.AddEdges( - (0, 0, dummyEdgeData), (0, 1, dummyEdgeData), (0, 2, dummyEdgeData), (1, 2, dummyEdgeData), (2, 0, dummyEdgeData), - (2, 1, dummyEdgeData), - (2, 2, dummyEdgeData) + (2, 1, dummyEdgeData) ); var result = myGraph.SimpleCycles(); - var cycleA = new int[] { 0 }; - var cycleB = new int[] { 0, 1, 2 }; - var cycleC = new int[] { 0, 2 }; - var cycleD = new int[] { 1, 2 }; - var cycleE = new int[] { 2 }; + var cycleA = new int[] { 0, 1, 2 }; + var cycleB = new int[] { 0, 2 }; + var cycleC = new int[] { 1, 2 }; result.Should().ContainEquivalentOf(cycleA); result.Should().ContainEquivalentOf(cycleB); result.Should().ContainEquivalentOf(cycleC); - result.Should().ContainEquivalentOf(cycleD); - result.Should().ContainEquivalentOf(cycleE); - result.Should().HaveCount(5); + result.Should().HaveCount(3); } [Test] diff --git a/test/DirectedWeightedMultiGraph.cs b/test/DirectedWeightedMultiGraph.cs index 18b67a4..1caed31 100644 --- a/test/DirectedWeightedMultiGraph.cs +++ b/test/DirectedWeightedMultiGraph.cs @@ -235,7 +235,8 @@ namespace Tests ('f', 'd', 2, run), ('f', 'h', 6, wallJump), ('g', 'h', 7, run), - ('h', 'f', 1, jump) + ('h', 'f', 1, jump), + ('a', 'a', 3, jump) // cheeky lil self-edge ); myGraph