Esempio n. 1
0
        public void DFSGraphEdgeRecursiveTest()
        {
            var undirectedGraphWithCycles = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Undirected);

            undirectedGraphWithCycles.AddVertex(1);
            undirectedGraphWithCycles.AddVertex(2);
            undirectedGraphWithCycles.AddVertex(3);
            undirectedGraphWithCycles.AddVertex(4);
            undirectedGraphWithCycles.AddVertex(5);
            undirectedGraphWithCycles.AddVertex(6);
            undirectedGraphWithCycles.AddEdge(1, 6);
            undirectedGraphWithCycles.AddEdge(1, 2);
            undirectedGraphWithCycles.AddEdge(2, 3);
            undirectedGraphWithCycles.AddEdge(3, 4);
            undirectedGraphWithCycles.AddEdge(4, 5);
            undirectedGraphWithCycles.AddEdge(5, 2);
            undirectedGraphWithCycles.AddEdge(5, 1);

            var directedGraphWithCycles = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Directed);

            directedGraphWithCycles.AddVertex(1);
            directedGraphWithCycles.AddVertex(2);
            directedGraphWithCycles.AddVertex(3);
            directedGraphWithCycles.AddVertex(4);
            directedGraphWithCycles.AddVertex(5);
            directedGraphWithCycles.AddVertex(6);
            directedGraphWithCycles.AddEdge(1, 6);
            directedGraphWithCycles.AddEdge(1, 2);
            directedGraphWithCycles.AddEdge(2, 3);
            directedGraphWithCycles.AddEdge(3, 4);
            directedGraphWithCycles.AddEdge(4, 5);
            directedGraphWithCycles.AddEdge(5, 2);
            directedGraphWithCycles.AddEdge(5, 1);

            var undirectedGraphWithoutCycles = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Undirected);

            undirectedGraphWithoutCycles.AddVertex(1);
            undirectedGraphWithoutCycles.AddVertex(2);
            undirectedGraphWithoutCycles.AddVertex(3);
            undirectedGraphWithoutCycles.AddVertex(4);
            undirectedGraphWithoutCycles.AddVertex(5);
            undirectedGraphWithoutCycles.AddVertex(6);
            undirectedGraphWithoutCycles.AddEdge(1, 6);
            undirectedGraphWithoutCycles.AddEdge(1, 2);
            undirectedGraphWithoutCycles.AddEdge(2, 3);
            undirectedGraphWithoutCycles.AddEdge(3, 4);
            undirectedGraphWithoutCycles.AddEdge(4, 5);


            Assert.Equal(new []
            {
                "tree edge: 1-6",
                "tree edge: 1-2",
                "tree edge: 2-3",
                "tree edge: 3-4",
                "tree edge: 4-5",
                "back edge: 5-2",
                "back edge: 5-1"
            }, DFS.DFSGraphEdgesRecursive(undirectedGraphWithCycles, 1));

            Assert.True(DFS.DFSGraphHasCycles(undirectedGraphWithCycles));

            Assert.Equal(new []
            {
                "tree edge: 1-6",
                "tree edge: 1-2",
                "tree edge: 2-3",
                "tree edge: 3-4",
                "tree edge: 4-5",
                "back edge: 5-2",
                "back edge: 5-1"
            }, DFS.DFSGraphEdgesRecursive(directedGraphWithCycles, 1));

            Assert.True(DFS.DFSGraphHasCycles(directedGraphWithCycles));

            Assert.Equal(new []
            {
                "tree edge: 1-6",
                "tree edge: 1-2",
                "tree edge: 2-3",
                "tree edge: 3-4",
                "tree edge: 4-5"
            }, DFS.DFSGraphEdgesRecursive(undirectedGraphWithoutCycles, 1));

            Assert.False(DFS.DFSGraphHasCycles(undirectedGraphWithoutCycles));
        }