Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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'));
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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));
        }
Esempio n. 9
0
        public void VerifyIsBipartite_ThrowsException_ForDirectedGraphs()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <InvalidOperationException>(() =>
            {
                var dummy = graph.IsBipartite;
            });
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 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');
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 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));
        }
Esempio n. 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);
        }
Esempio n. 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));
        }
Esempio n. 22
0
        public void Colorize_ThrowsException_ForNullApplyColor()
        {
            var graph = new LiteralGraph("A", false);

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

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

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

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

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

            Assert.Throws <InvalidOperationException>(() => graph.FindAllArticulationVertices());
        }
Esempio n. 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));
        }
Esempio n. 29
0
        public void TraverseDfs_ThrowsException_ForNullCycleHandler()
        {
            var graph = new LiteralGraph("A>1>B", true);

            Assert.Throws <ArgumentNullException>(() => graph.TraverseDfs('A', True, True, null));
        }
Esempio n. 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));
        }