public void IsBipartite_ForBiggerGraphs() { // This graph is some bipartite mess. Odds and evens go in their own set, edges between them haphazardly. var graph = SimpleGraph.CreateFromOneBasedEdges(22, new[, ] { { 2, 3 }, { 2, 9 }, { 2, 11 }, { 2, 15 }, { 4, 1 }, { 4, 7 }, { 4, 11 }, { 4, 17 }, { 6, 1 }, { 6, 11 }, { 6, 7 }, { 6, 15 }, { 8, 1 }, { 8, 15 }, { 8, 13 }, { 8, 17 }, { 10, 1 }, { 10, 3 }, { 10, 13 }, { 10, 15 }, { 14, 1 }, { 16, 3 }, { 16, 7 }, { 16, 17 }, { 16, 1 }, { 16, 3 }, { 16, 7 }, { 16, 17 }, { 16, 9 }, { 16, 11 }, { 16, 5 }, { 16, 19 }, { 18, 9 }, { 18, 11 }, { 18, 5 }, { 18, 19 }, { 20, 21 } }); Assert.IsTrue(graph.IsBipartite()); // This graph is like above, except I threw in an edge between an even pair of vertices. graph = SimpleGraph.CreateFromOneBasedEdges(22, new[, ] { { 2, 3 }, { 2, 9 }, { 2, 11 }, { 2, 15 }, { 4, 1 }, { 4, 7 }, { 4, 11 }, { 4, 17 }, { 6, 1 }, { 6, 11 }, { 6, 7 }, { 6, 15 }, { 8, 1 }, { 8, 15 }, { 8, 13 }, { 8, 17 }, { 10, 1 }, { 10, 3 }, { 10, 13 }, { 10, 15 }, { 14, 1 }, { 16, 3 }, { 16, 7 }, { 16, 17 }, { 16, 1 }, { 16, 3 }, { 16, 7 }, { 16, 17 }, { 16, 9 }, { 16, 11 }, { 16, 5 }, { 16, 19 }, { 18, 9 }, { 18, 11 }, { 18, 5 }, { 18, 19 }, { 20, 21 }, { 10, 16 } }); Assert.IsFalse(graph.IsBipartite()); }
// Skipping the details, a graph is a tree if it's connected and has (vertexCount - 1) edges. public static bool Solve(int vertexCount, int edgeCount, int[,] edges) { if (edgeCount != vertexCount - 1) { return(false); } return(SimpleGraph .CreateFromOneBasedEdges(vertexCount, edges) .IsConnected()); }
// See image for details: http://i.imgur.com/hWnw1N9.jpg. public static int Solve(int nodeCount, int[,] edges) { if (nodeCount == 1) { return(0); } var graph = SimpleGraph.CreateFromOneBasedEdges(nodeCount, edges); var firstVertex = graph.Vertices.First(v => v.Degree == 1); var secondVertex = graph.FindFurthestVertex(firstVertex).Item1; int longestPathLength = graph.FindFurthestVertex(secondVertex).Item2; return(longestPathLength); }
public void ValidatesAGraph3() { var graph = SimpleGraph.CreateFromOneBasedEdges(3, new[, ] { { 1, 2 }, { 2, 3 } }); Assert.IsTrue(graph.Vertices[0].HasNeighbor(1)); Assert.IsTrue(graph.Vertices[1].HasNeighbor(0)); Assert.IsTrue(graph.Vertices[1].HasNeighbor(2)); Assert.IsTrue(graph.Vertices[2].HasNeighbor(1)); Assert.IsTrue(graph.HasEdge(0, 1)); Assert.IsTrue(graph.HasEdge(1, 2)); Assert.AreEqual(1, graph.Vertices[0].Degree); Assert.AreEqual(2, graph.Vertices[1].Degree); Assert.AreEqual(1, graph.Vertices[2].Degree); }