public void Directed() { var graph = new Graph <int>(true); var vertexList = new List <Vertex <int> >(); for (var i = 1; i < 15; i++) { vertexList.Add(graph.AddVertex(i)); } AddEdge(graph, vertexList, 1, 2, 5); AddEdge(graph, vertexList, 1, 5, 4); AddEdge(graph, vertexList, 2, 4, 10); AddEdge(graph, vertexList, 2, 3, 6); AddEdge(graph, vertexList, 3, 4, 2); AddEdge(graph, vertexList, 3, 5, 6); AddEdge(graph, vertexList, 3, 7, 4); AddEdge(graph, vertexList, 5, 6, 1); AddEdge(graph, vertexList, 6, 7, 1); AddEdge(graph, vertexList, 6, 8, 9); AddEdge(graph, vertexList, 6, 9, 5); AddEdge(graph, vertexList, 7, 9, 3); AddEdge(graph, vertexList, 7, 10, 4); AddEdge(graph, vertexList, 9, 10, 6); AddEdge(graph, vertexList, 9, 12, 2); AddEdge(graph, vertexList, 11, 12, 9); AddEdge(graph, vertexList, 11, 13, 8); AddEdge(graph, vertexList, 13, 14, 6); var resultGraph = GraphAlgorithms.PrimsAlgorithm(graph, vertexList[0]); Assert.IsTrue(resultGraph.ContainsEdge(1, 2)); Assert.IsTrue(resultGraph.ContainsEdge(1, 5)); Assert.IsTrue(resultGraph.ContainsEdge(5, 6)); Assert.IsTrue(resultGraph.ContainsEdge(6, 8)); Assert.IsTrue(resultGraph.ContainsEdge(6, 7)); Assert.IsTrue(resultGraph.ContainsEdge(7, 3)); Assert.IsTrue(resultGraph.ContainsEdge(3, 4)); Assert.IsTrue(resultGraph.ContainsEdge(7, 9)); Assert.IsTrue(resultGraph.ContainsEdge(7, 10)); Assert.IsTrue(resultGraph.ContainsEdge(9, 12)); Assert.IsTrue(resultGraph.ContainsEdge(12, 11)); Assert.IsTrue(resultGraph.ContainsEdge(11, 13)); Assert.IsTrue(resultGraph.ContainsEdge(13, 14)); Assert.AreEqual(resultGraph.Edges.Count, 13); double totalCost = 0; foreach (var edge in resultGraph.Edges) { totalCost += edge.Weight; } Assert.AreEqual(totalCost, 58); }
public void Simple() { var graph = new Graph <int>(false); var vertexList = new List <Vertex <int> >(); for (var i = 1; i < 15; i++) { vertexList.Add(graph.AddVertex(i)); } AddEdge(graph, vertexList, 1, 2, 5); AddEdge(graph, vertexList, 1, 5, 4); AddEdge(graph, vertexList, 2, 4, 10); AddEdge(graph, vertexList, 2, 3, 6); AddEdge(graph, vertexList, 3, 4, 2); AddEdge(graph, vertexList, 3, 5, 6); AddEdge(graph, vertexList, 3, 7, 4); AddEdge(graph, vertexList, 5, 6, 1); AddEdge(graph, vertexList, 6, 7, 1); AddEdge(graph, vertexList, 6, 8, 9); AddEdge(graph, vertexList, 6, 9, 5); AddEdge(graph, vertexList, 7, 9, 3); AddEdge(graph, vertexList, 7, 10, 4); AddEdge(graph, vertexList, 9, 10, 6); AddEdge(graph, vertexList, 9, 12, 2); AddEdge(graph, vertexList, 11, 12, 9); AddEdge(graph, vertexList, 11, 13, 8); AddEdge(graph, vertexList, 13, 14, 6); var resultGraph = GraphAlgorithms.PrimsAlgorithm(graph, vertexList[0]); Assert.IsTrue(resultGraph.ContainsEdge(1, 2)); Assert.IsTrue(resultGraph.ContainsEdge(1, 5)); Assert.IsTrue(resultGraph.ContainsEdge(5, 6)); Assert.IsTrue(resultGraph.ContainsEdge(6, 8)); Assert.IsTrue(resultGraph.ContainsEdge(6, 7)); Assert.IsTrue(resultGraph.ContainsEdge(7, 3)); Assert.IsTrue(resultGraph.ContainsEdge(3, 4)); Assert.IsTrue(resultGraph.ContainsEdge(7, 9)); Assert.IsTrue(resultGraph.ContainsEdge(7, 10)); Assert.IsTrue(resultGraph.ContainsEdge(9, 12)); Assert.IsTrue(resultGraph.ContainsEdge(12, 11)); Assert.IsTrue(resultGraph.ContainsEdge(11, 13)); Assert.IsTrue(resultGraph.ContainsEdge(13, 14)); Assert.AreEqual(13, resultGraph.Edges.Count); double totalCost = resultGraph.Edges.Sum(edge => edge.Weight); Assert.AreEqual(58, totalCost); }
public void ExceptionInvalidVertex() { GraphAlgorithms.PrimsAlgorithm(new Graph <int>(true), new Vertex <int>(5)); }
public void ExceptionNullVertex() { GraphAlgorithms.PrimsAlgorithm(new Graph <int>(true), null); }
public void ExceptionNullGraph() { GraphAlgorithms.PrimsAlgorithm(null, new Vertex <int>(5)); }
private static void Main() { var graph = new Graph <int>(false); var vertexList = new List <Vertex <int> >(); for (var i = 1; i < 10; i++) { vertexList.Add(graph.AddVertex(i)); } /* * * a = 1 * b = 2 * c = 3 * d = 4 * e = 5 * f = 6 * g = 7 * h = 8 * i = 9 * */ // a AddEdge(graph, vertexList, 1, 2, 2); AddEdge(graph, vertexList, 1, 4, 1); // b AddEdge(graph, vertexList, 2, 3, 1); AddEdge(graph, vertexList, 2, 4, 5); AddEdge(graph, vertexList, 2, 5, 3); AddEdge(graph, vertexList, 2, 6, 2); // d AddEdge(graph, vertexList, 4, 5, 1); AddEdge(graph, vertexList, 4, 7, 2); AddEdge(graph, vertexList, 4, 8, 4); // e AddEdge(graph, vertexList, 5, 6, 4); AddEdge(graph, vertexList, 5, 8, 3); AddEdge(graph, vertexList, 5, 9, 4); // f AddEdge(graph, vertexList, 6, 9, 3); // h AddEdge(graph, vertexList, 8, 9, 5); //var resultGraph = GraphAlgorithms.KruskalsAlgorithm(graph); var resultGraph = GraphAlgorithms.PrimsAlgorithm(graph, vertexList[0]); Console.WriteLine("Result tree edges: " + resultGraph.Edges.Count); double totalCost = 0; foreach (var edge in resultGraph.Edges) { Console.WriteLine( "(From: {0}, To: {1}, Weight: {2})", edge.FromVertex.Data, edge.ToVertex.Data, edge.Weight); totalCost += edge.Weight; } Console.WriteLine("Total cost: " + totalCost); }
public void ExceptionInvalidVertex() { Assert.Throws <ArgumentException>(() => GraphAlgorithms.PrimsAlgorithm(new Graph <int>(true), new Vertex <int>(5))); }
public void ExceptionNullVertex() { Assert.Throws <ArgumentNullException>(() => GraphAlgorithms.PrimsAlgorithm(new Graph <int>(true), null)); }
public void ExceptionNullGraph() { Assert.Throws <ArgumentNullException>(() => GraphAlgorithms.PrimsAlgorithm(null, new Vertex <int>(5))); }