Пример #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
        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
        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
        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
        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
        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
        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
        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
        public void TraverseDfs_ThrowsException_ForNullCycleHandler()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentNullException>(() => graph.TraverseDfs('A', True, True, null));
        }
Пример #30
0
        public void TraverseDfs_ThrowsException_ForNullVisitationHandler()
        {
            var graph = new LiteralGraph("A>1>B", true);

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