public void KruskalsMinimumSpanningTreeTest() { // Assign var graph = CreateTestWeightedGraph(); var expectedEdges = new[] { graph["F", "E"], graph["F", "A"], graph["A", "B"], graph["D", "I"], graph["E", "H"], graph["E", "G"], graph["D", "C"], graph["I", "J"], graph["E", "I"] }; var expected = new MinimumSpanTree <string>(expectedEdges, 48); // Act var actual = graph.KruskalsMinimumSpanningTree(); // Assert Assert.True(MinimumSpanningTreeEquals(expected, actual)); }
private bool MinimumSpanningTreeEquals(MinimumSpanTree <string> x, MinimumSpanTree <string> y) { if (x == null || y == null || x.Edges.Count != y.Edges.Count || x.Distance != y.Distance) { return(false); } var thisEdgesKeys = new HashSet <Tuple <string, string> >(x.Edges.Select(key => new Tuple <string, string>(key.StartVertex.Value, key.EndVertex.Value))); foreach (var edge in y.Edges) { var normalKey = new Tuple <string, string>(edge.StartVertex.Value, edge.EndVertex.Value); var invertedKey = new Tuple <string, string>(edge.EndVertex.Value, edge.StartVertex.Value); if (!thisEdgesKeys.Contains(normalKey) && !thisEdgesKeys.Contains(invertedKey)) { return(false); } } return(true); }
private bool MinimumSpanningTreeEquals(MinimumSpanTree x, MinimumSpanTree y) { if (x == null || y == null || x.Edges.Count != y.Edges.Count || x.Distance != y.Distance) return false; var thisEdgesKeys = new HashSet<Tuple<string, string>>(x.Edges.Select(key => new Tuple<string, string>(key.StartVertex.Label, key.EndVertex.Label))); foreach (var edge in y.Edges) { var normalKey = new Tuple<string, string>(edge.StartVertex.Label, edge.EndVertex.Label); var invertedKey = new Tuple<string, string>(edge.EndVertex.Label, edge.StartVertex.Label); if (!thisEdgesKeys.Contains(normalKey) && !thisEdgesKeys.Contains(invertedKey)) { return false; } } return true; }
public void PrimsMinimumSpanningTreeTest() { // Assign var graph = CreateTestWeightedGraph(); var expectedEdges = new[] { graph["F", "E"], graph["F", "A"], graph["A", "B"], graph["D", "I"], graph["E", "H"], graph["E", "G"], graph["D", "C"], graph["I", "J"], graph["E", "I"] }; var expected = new MinimumSpanTree(expectedEdges, 48); // Act var actual = graph.PrimsMinimumSpanningTree(); // Assert Assert.IsTrue(MinimumSpanningTreeEquals(expected, actual)); }