disallow self-loop on simple graph
parent
4d724c9f1b
commit
92bab9db75
|
@ -110,6 +110,8 @@ namespace MoonTools.Core.Graph
|
||||||
{
|
{
|
||||||
CheckNodes(v, u);
|
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);
|
neighbors[v].Add(u);
|
||||||
edges.Add((v, u));
|
edges.Add((v, u));
|
||||||
edgesToEdgeData.Add((v, u), edgeData);
|
edgesToEdgeData.Add((v, u), edgeData);
|
||||||
|
|
|
@ -61,6 +61,15 @@ namespace Tests
|
||||||
Assert.That(myGraph.Neighbors(1), Does.Not.Contain(4));
|
Assert.That(myGraph.Neighbors(1), Does.Not.Contain(4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AddSelfEdge()
|
||||||
|
{
|
||||||
|
var myGraph = new DirectedGraph<int, EdgeData>();
|
||||||
|
myGraph.AddNodes(1, 2);
|
||||||
|
|
||||||
|
myGraph.Invoking(x => x.AddEdge(1, 1, dummyEdgeData)).Should().Throw<ArgumentException>();
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Order()
|
public void Order()
|
||||||
{
|
{
|
||||||
|
@ -316,7 +325,6 @@ namespace Tests
|
||||||
var myGraph = new DirectedGraph<int, EdgeData>();
|
var myGraph = new DirectedGraph<int, EdgeData>();
|
||||||
myGraph.AddNodes(1, 2, 3, 4);
|
myGraph.AddNodes(1, 2, 3, 4);
|
||||||
myGraph.AddEdges(
|
myGraph.AddEdges(
|
||||||
(1, 1, dummyEdgeData),
|
|
||||||
(1, 2, dummyEdgeData),
|
(1, 2, dummyEdgeData),
|
||||||
(2, 3, dummyEdgeData),
|
(2, 3, dummyEdgeData),
|
||||||
(2, 1, dummyEdgeData),
|
(2, 1, dummyEdgeData),
|
||||||
|
@ -326,7 +334,7 @@ namespace Tests
|
||||||
var clone = myGraph.Clone();
|
var clone = myGraph.Clone();
|
||||||
Assert.That(clone, Is.Not.EqualTo(myGraph));
|
Assert.That(clone, Is.Not.EqualTo(myGraph));
|
||||||
clone.Nodes.Should().BeEquivalentTo(1, 2, 3, 4);
|
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(2).Should().BeEquivalentTo(3, 1);
|
||||||
clone.Neighbors(3).Should().BeEquivalentTo(4);
|
clone.Neighbors(3).Should().BeEquivalentTo(4);
|
||||||
}
|
}
|
||||||
|
@ -337,7 +345,6 @@ namespace Tests
|
||||||
var myGraph = new DirectedGraph<int, EdgeData>();
|
var myGraph = new DirectedGraph<int, EdgeData>();
|
||||||
myGraph.AddNodes(1, 2, 3, 4);
|
myGraph.AddNodes(1, 2, 3, 4);
|
||||||
myGraph.AddEdges(
|
myGraph.AddEdges(
|
||||||
(1, 1, dummyEdgeData),
|
|
||||||
(1, 2, dummyEdgeData),
|
(1, 2, dummyEdgeData),
|
||||||
(2, 3, dummyEdgeData),
|
(2, 3, dummyEdgeData),
|
||||||
(2, 1, dummyEdgeData),
|
(2, 1, dummyEdgeData),
|
||||||
|
@ -346,7 +353,7 @@ namespace Tests
|
||||||
|
|
||||||
var subGraph = myGraph.SubGraph(1, 2, 3);
|
var subGraph = myGraph.SubGraph(1, 2, 3);
|
||||||
subGraph.Nodes.Should().BeEquivalentTo(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(2).Should().BeEquivalentTo(1, 3);
|
||||||
subGraph.Neighbors(3).Should().NotContain(4);
|
subGraph.Neighbors(3).Should().NotContain(4);
|
||||||
}
|
}
|
||||||
|
@ -357,29 +364,23 @@ namespace Tests
|
||||||
var myGraph = new DirectedGraph<int, EdgeData>();
|
var myGraph = new DirectedGraph<int, EdgeData>();
|
||||||
myGraph.AddNodes(0, 1, 2);
|
myGraph.AddNodes(0, 1, 2);
|
||||||
myGraph.AddEdges(
|
myGraph.AddEdges(
|
||||||
(0, 0, dummyEdgeData),
|
|
||||||
(0, 1, dummyEdgeData),
|
(0, 1, dummyEdgeData),
|
||||||
(0, 2, dummyEdgeData),
|
(0, 2, dummyEdgeData),
|
||||||
(1, 2, dummyEdgeData),
|
(1, 2, dummyEdgeData),
|
||||||
(2, 0, dummyEdgeData),
|
(2, 0, dummyEdgeData),
|
||||||
(2, 1, dummyEdgeData),
|
(2, 1, dummyEdgeData)
|
||||||
(2, 2, dummyEdgeData)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
var result = myGraph.SimpleCycles();
|
var result = myGraph.SimpleCycles();
|
||||||
|
|
||||||
var cycleA = new int[] { 0 };
|
var cycleA = new int[] { 0, 1, 2 };
|
||||||
var cycleB = new int[] { 0, 1, 2 };
|
var cycleB = new int[] { 0, 2 };
|
||||||
var cycleC = new int[] { 0, 2 };
|
var cycleC = new int[] { 1, 2 };
|
||||||
var cycleD = new int[] { 1, 2 };
|
|
||||||
var cycleE = new int[] { 2 };
|
|
||||||
|
|
||||||
result.Should().ContainEquivalentOf(cycleA);
|
result.Should().ContainEquivalentOf(cycleA);
|
||||||
result.Should().ContainEquivalentOf(cycleB);
|
result.Should().ContainEquivalentOf(cycleB);
|
||||||
result.Should().ContainEquivalentOf(cycleC);
|
result.Should().ContainEquivalentOf(cycleC);
|
||||||
result.Should().ContainEquivalentOf(cycleD);
|
result.Should().HaveCount(3);
|
||||||
result.Should().ContainEquivalentOf(cycleE);
|
|
||||||
result.Should().HaveCount(5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|
|
@ -235,7 +235,8 @@ namespace Tests
|
||||||
('f', 'd', 2, run),
|
('f', 'd', 2, run),
|
||||||
('f', 'h', 6, wallJump),
|
('f', 'h', 6, wallJump),
|
||||||
('g', 'h', 7, run),
|
('g', 'h', 7, run),
|
||||||
('h', 'f', 1, jump)
|
('h', 'f', 1, jump),
|
||||||
|
('a', 'a', 3, jump) // cheeky lil self-edge
|
||||||
);
|
);
|
||||||
|
|
||||||
myGraph
|
myGraph
|
||||||
|
|
Loading…
Reference in New Issue