コード例 #1
0
        public void Preserves_MultipleEdges_ForDirectedGraphs()
        {
            var graph      = new LiteralGraph("A>1>A,A>2>B,A>1>B", true);
            var edgesFromA = string.Join(",", graph.GetEdges('A').Select(s => s.FromVertex + ">" + s.ToVertex));

            Assert.AreEqual("A>A,A>B,A>B", edgesFromA);
        }
コード例 #2
0
        public void VerifyIsBipartite_ReturnsExpectedResult([NotNull] string relationships, bool expected)
        {
            var graph  = new LiteralGraph(relationships, false);
            var actual = graph.IsBipartite;

            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void Ctor_AcceptsUnconnectedVertices()
        {
            var graph = new LiteralGraph("A,B,C-6-D", true);

            TestHelper.AssertSequence(graph.GetEdges('A'));
            TestHelper.AssertSequence(graph.GetEdges('B'));
        }
コード例 #4
0
        public void FindAllArticulationVertices_FindsArticulationsForUndirectedGraphs([NotNull] string relationships,
                                                                                      string expected)
        {
            var graph  = new LiteralGraph(relationships, false);
            var actual = string.Join(",", graph.FindAllArticulationVertices());

            Assert.AreEqual(expected, actual);
        }
コード例 #5
0
        public void FindCheapestPath_FindsTheProperPath_ForDirectedGraphs([NotNull] string relationships, char from,
                                                                          char to, string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var actual = string.Join(",", graph.FindCheapestPath(from, to));

            Assert.AreEqual(expected, actual);
        }
コード例 #6
0
        public void TopologicalSort_AlignsTheVerticesAsExpected([NotNull] string relationships, string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = graph.TopologicalSort().ToList();
            var actual = string.Join(",", result);

            Assert.AreEqual(expected, actual);
        }
コード例 #7
0
        public void Graph_DescribeVertices_ReturnsExpectedDescriptions_ForDirectedGraphs([NotNull] string relationships,
                                                                                         string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = string.Join("; ", graph.DescribeVertices());

            Assert.AreEqual(expected, result);
        }
コード例 #8
0
        public void FindShortestPath_FillsExpectedVertices(
            [NotNull] string relationships, char startVertex, char endVertex, string expected)
        {
            var graph = new LiteralGraph(relationships, true);
            var seq   = graph.FindShortestPath(startVertex, endVertex);

            Assert.AreEqual(expected, string.Join(",", seq));
        }
コード例 #9
0
        public void VerifyIsBipartite_ThrowsException_ForDirectedGraphs()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <InvalidOperationException>(() =>
            {
                var dummy = graph.IsBipartite;
            });
        }
コード例 #10
0
ファイル: GetComponentsTests.cs プロジェクト: pavkam/abacaxi
        public void GetComponents_ReturnsProperComponents_ForDirectedGraphs([NotNull] string relationships,
                                                                            string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = string.Join(";",
                                     graph.GetComponents().Select(component => string.Join(",", component)));

            Assert.AreEqual(expected, result);
        }
コード例 #11
0
        public void GetEdges_ReturnsAllEdges([NotNull] string relationships, char vertex, string expected)
        {
            var graph = new LiteralGraph(relationships, true);

            var v      = graph.GetEdges(vertex).Select(s => $"{s.FromVertex}{s.ToVertex}").ToArray();
            var result = string.Join(",", v);

            Assert.AreEqual(expected, result);
        }
コード例 #12
0
        public void FillWithOneColor_FillsExpectedVertices([NotNull] string relationships, char startVertex,
                                                           string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <char>();

            graph.FillWithOneColor(startVertex, vertex => result.Add(vertex));

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #13
0
        private static string Colorize([NotNull] string relationships)
        {
            var graph = new LiteralGraph(relationships, false);

            var result = new List <string>();

            graph.Colorize((v, c) => result.Add($"{v}({c})"));

            return(string.Join(", ", result));
        }
コード例 #14
0
        public void Enumeration_ReturnsAllVertices()
        {
            var graph = new LiteralGraph("A>1>B,B-1-Z,K<1<T", true);

            var v = graph.ToArray();

            Sorting.QuickSort(v, 0, v.Length, Comparer <char> .Default);

            TestHelper.AssertSequence(v,
                                      'A', 'B', 'K', 'T', 'Z');
        }
コード例 #15
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_ReportsTheCycles_InDirectedGraphs([NotNull] string relationships, string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <string>();

            graph.TraverseDfs('A', True, True, (from, to) =>
            {
                result.Add($"{from.Vertex}~{to.Vertex}");
                return(true);
            });

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #16
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_MarksNodesWithCorrectExitTimes([NotNull] string relationships, string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <string>();

            graph.TraverseDfs('A', True, node =>
            {
                result.Add($"{node.Vertex}{node.ExitTime}");
                return(true);
            },
                              (from, to) => true);

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #17
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_ReturnsProperSequence_ForDirectedGraph([NotNull] string relationships, string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <string>();

            graph.TraverseDfs('A', True, node =>
            {
                Assert.IsNotNull(node);
                result.Add($"{node.Parent?.Vertex}>{node.Vertex}");
                return(true);
            }, (from, to) => true);

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #18
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_InterruptsOnCycle([NotNull] string relationships, char killVertex, string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <string>();

            graph.TraverseDfs('A', True, node =>
            {
                Assert.IsNotNull(node);
                result.Add($"{node.Parent?.Vertex}>{node.Vertex}");
                return(true);
            },
                              (from, to) => to.Vertex != killVertex);

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #19
0
        public void TraverseBfs_ReturnsProperSequence_IfInterrupted([NotNull] string relationships, char killVertex,
                                                                    string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <string>();

            graph.TraverseBfs('A', node =>
            {
                Assert.IsNotNull(node);
                result.Add($"{node.Parent?.Vertex}>{node.Vertex}");
                return(node.Vertex != killVertex);
            });

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #20
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_SelectsTheExpectedEdges_ForDirectedGraph([NotNull] string relationships,
                                                                         string expected)
        {
            var graph  = new LiteralGraph(relationships, true);
            var result = new List <string>();

            graph.TraverseDfs('A', node =>
            {
                Assert.IsNotNull(node);
                result.Add(node.EntryEdge != null ? $"{node.EntryEdge}" : "()");
                return(true);
            }, True, (from, to) => true);

            var actual = string.Join(",", result);

            Assert.AreEqual(expected, actual);
        }
コード例 #21
0
        public void TraverseBfs_SelectsTheExpectedEdges_ForUndirectedGraph([NotNull] string relationships,
                                                                           string expected)
        {
            var graph  = new LiteralGraph(relationships, false);
            var result = new List <string>();

            graph.TraverseBfs('A', node =>
            {
                Assert.IsNotNull(node);
                if (node.EntryEdge != null)
                {
                    result.Add($"{node.EntryEdge}");
                }

                return(true);
            });

            Assert.AreEqual(expected, string.Join(",", result));
        }
コード例 #22
0
        public void Colorize_ThrowsException_ForNullApplyColor()
        {
            var graph = new LiteralGraph("A", false);

            Assert.Throws <ArgumentNullException>(() => graph.Colorize(null));
        }
コード例 #23
0
        public void Colorize_ThrowsException_ForDirectedGraph()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <InvalidOperationException>(() => graph.Colorize((c, i) => { }));
        }
コード例 #24
0
        public void FillWithOneColor_ThrowsException_ForNullApplyColor()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentNullException>(() => graph.FillWithOneColor('A', null));
        }
コード例 #25
0
        public void FillWithOneColor_ThrowsException_ForInvalidVertex()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentException>(() => graph.FillWithOneColor('Z', v => { }));
        }
コード例 #26
0
        public void FindCheapestPath_ThrowsException_IfFromVertexIsInvalid()
        {
            var graph = new LiteralGraph("A-1-B", false);

            Assert.Throws <ArgumentException>(() => graph.FindCheapestPath('Z', 'A'));
        }
コード例 #27
0
        public void FindAllArticulationVertices_ThrowsException_ForDirectedGraphs()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <InvalidOperationException>(() => graph.FindAllArticulationVertices());
        }
コード例 #28
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_ThrowsException_ForInvalidVertex()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentException>(() => graph.TraverseDfs('Z', True, True, (node, dfsNode) => true));
        }
コード例 #29
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_ThrowsException_ForNullCycleHandler()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentNullException>(() => graph.TraverseDfs('A', True, True, null));
        }
コード例 #30
0
ファイル: TraverseDfsTests.cs プロジェクト: pavkam/abacaxi
        public void TraverseDfs_ThrowsException_ForNullVisitationHandler()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentNullException>(() => graph.TraverseDfs('A', null, True, (node, dfsNode) => true));
        }