public void TraverseGraphIterativeTest() { var directedGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Directed); directedGraph.AddVertex(1); directedGraph.AddVertex(2); directedGraph.AddVertex(3); directedGraph.AddVertex(4); directedGraph.AddEdge(1, 2); directedGraph.AddEdge(2, 3); directedGraph.AddEdge(3, 4); directedGraph.AddEdge(4, 1); var undirectedGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Undirected); undirectedGraph.AddVertex(1); undirectedGraph.AddVertex(2); undirectedGraph.AddVertex(3); undirectedGraph.AddVertex(4); undirectedGraph.AddEdge(1, 2); undirectedGraph.AddEdge(2, 3); undirectedGraph.AddEdge(3, 4); undirectedGraph.AddEdge(4, 1); Assert.Equal(new [] { 1, 2, 3, 4 }, BFS.TraverseGraphIterative(directedGraph)); Assert.Equal(new [] { 1, 2, 4, 3 }, BFS.TraverseGraphIterative(undirectedGraph)); }
public void TraverseGraphWithEdgesIterativeTest() { var directedGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Directed); directedGraph.AddVertex(1); directedGraph.AddVertex(2); directedGraph.AddVertex(3); directedGraph.AddVertex(4); directedGraph.AddEdge(1, 2); directedGraph.AddEdge(2, 3); directedGraph.AddEdge(3, 4); directedGraph.AddEdge(4, 1); var undirectedGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Undirected); undirectedGraph.AddVertex(1); undirectedGraph.AddVertex(2); undirectedGraph.AddVertex(3); undirectedGraph.AddVertex(4); undirectedGraph.AddEdge(1, 2); undirectedGraph.AddEdge(2, 3); undirectedGraph.AddEdge(3, 4); undirectedGraph.AddEdge(4, 1); Assert.Equal(new [] { 1, 2, 3, 4 }, BFS.TraverseGraphWithEdgesIterative(directedGraph).Item1); Assert.Equal(new [] { "1-2", "2-3", "3-4", "4-1" }, BFS.TraverseGraphWithEdgesIterative(directedGraph).Item2); Assert.Equal(new [] { 1, 2, 4, 3 }, BFS.TraverseGraphWithEdgesIterative(undirectedGraph).Item1); Assert.Equal(new [] { "1-2", "1-4", "2-3", "4-3" }, BFS.TraverseGraphWithEdgesIterative(undirectedGraph).Item2); }
public void Test() { var emptyGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Directed); var nonEmptyGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Directed); nonEmptyGraph.AddVertex(1); nonEmptyGraph.AddVertex(2); nonEmptyGraph.AddVertex(3); nonEmptyGraph.AddVertex(4); nonEmptyGraph.AddEdge(1, 2); nonEmptyGraph.AddEdge(2, 3); nonEmptyGraph.AddEdge(3, 4); nonEmptyGraph.AddEdge(4, 2); Assert.Equal(0, emptyGraph.NumberOfVertices); Assert.Equal(0, emptyGraph.NumberOfEdges); Assert.Equal(4, nonEmptyGraph.NumberOfVertices); Assert.Equal(4, nonEmptyGraph.NumberOfEdges); Assert.Equal(true, nonEmptyGraph.HasEdge(1, 2)); Assert.Equal(false, nonEmptyGraph.HasEdge(2, 1)); Assert.Equal(false, nonEmptyGraph.HasEdge(4, 1)); }
public void TopologicalSortingTest() { var directedGraphWithCycles = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Directed); directedGraphWithCycles.AddVertex(1); // G directedGraphWithCycles.AddVertex(2); // A directedGraphWithCycles.AddVertex(3); // B directedGraphWithCycles.AddVertex(4); // C directedGraphWithCycles.AddVertex(5); // F directedGraphWithCycles.AddVertex(6); // E directedGraphWithCycles.AddVertex(7); // D directedGraphWithCycles.AddEdge(1, 2); directedGraphWithCycles.AddEdge(1, 5); directedGraphWithCycles.AddEdge(2, 3); directedGraphWithCycles.AddEdge(2, 4); directedGraphWithCycles.AddEdge(3, 4); directedGraphWithCycles.AddEdge(3, 7); directedGraphWithCycles.AddEdge(4, 5); directedGraphWithCycles.AddEdge(4, 6); directedGraphWithCycles.AddEdge(5, 6); directedGraphWithCycles.AddEdge(6, 7); Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7 }, DFS.TopologicalSorting(directedGraphWithCycles)); }
public void GetConnectedComponentsCountTest() { var singleComponentGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Undirected); singleComponentGraph.AddVertex(1); singleComponentGraph.AddVertex(2); singleComponentGraph.AddVertex(3); singleComponentGraph.AddVertex(4); singleComponentGraph.AddEdge(1, 2); singleComponentGraph.AddEdge(2, 3); singleComponentGraph.AddEdge(3, 4); singleComponentGraph.AddEdge(4, 1); var twoComponentsGraph = new GraphAdjacentLists <int>(GraphAdjacentLists <int> .DirectedGraphEnum.Undirected); twoComponentsGraph.AddVertex(1); twoComponentsGraph.AddVertex(2); twoComponentsGraph.AddVertex(3); twoComponentsGraph.AddVertex(4); twoComponentsGraph.AddEdge(1, 2); twoComponentsGraph.AddEdge(2, 3); Assert.Equal(2, BFS.GetConnectedComponentsCount(twoComponentsGraph)); }
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)); }