def test_mixed_nodetypes(): # Smoke test to make sure no TypeError is raised for mixed node types. G = nx.Graph() edgelist = [(0, 3, [('weight', 5)]), (0, '1', [('weight', 5)])] G.add_edges_from(edgelist) G = G.to_directed() x = branchings.minimum_spanning_arborescence(G)
def test_mst(): # Make sure we get the same results for undirected graphs. # Example from: http://en.wikipedia.org/wiki/Kruskal's_algorithm G = nx.Graph() edgelist = [(0, 3, [('weight', 5)]), (0, 1, [('weight', 7)]), (1, 3, [('weight', 9)]), (1, 2, [('weight', 8)]), (1, 4, [('weight', 7)]), (3, 4, [('weight', 15)]), (3, 5, [('weight', 6)]), (2, 4, [('weight', 5)]), (4, 5, [('weight', 8)]), (4, 6, [('weight', 9)]), (5, 6, [('weight', 11)])] G.add_edges_from(edgelist) G = G.to_directed() x = branchings.minimum_spanning_arborescence(G) edges = [(set([0, 1]), 7), (set([0, 3]), 5), (set([3, 5]), 6), (set([1, 4]), 7), (set([4, 2]), 5), (set([4, 6]), 9)] assert_equal(x.number_of_edges(), len(edges)) for u, v, d in x.edges(data=True): assert_true( (set([u,v]), d['weight']) in edges )
def test_partition_spanning_arborescence(): """ Test that we can generate minimum spanning arborescences which respect the given partition. """ G = nx.from_numpy_array(G_array, create_using=nx.DiGraph) G[3][0]["partition"] = nx.EdgePartition.EXCLUDED G[2][3]["partition"] = nx.EdgePartition.INCLUDED G[7][3]["partition"] = nx.EdgePartition.EXCLUDED G[0][2]["partition"] = nx.EdgePartition.EXCLUDED G[6][2]["partition"] = nx.EdgePartition.INCLUDED actual_edges = [ (0, 4, 12), (1, 0, 4), (1, 5, 13), (2, 3, 21), (4, 7, 12), (5, 6, 14), (5, 8, 12), (6, 2, 21), ] B = branchings.minimum_spanning_arborescence(G, partition="partition") assert_equal_branchings(build_branching(actual_edges), B)
def test_mst(): # Make sure we get the same results for undirected graphs. # Example from: https://en.wikipedia.org/wiki/Kruskal's_algorithm G = nx.Graph() edgelist = [ (0, 3, [("weight", 5)]), (0, 1, [("weight", 7)]), (1, 3, [("weight", 9)]), (1, 2, [("weight", 8)]), (1, 4, [("weight", 7)]), (3, 4, [("weight", 15)]), (3, 5, [("weight", 6)]), (2, 4, [("weight", 5)]), (4, 5, [("weight", 8)]), (4, 6, [("weight", 9)]), (5, 6, [("weight", 11)]), ] G.add_edges_from(edgelist) G = G.to_directed() x = branchings.minimum_spanning_arborescence(G) edges = [ ({0, 1}, 7), ({0, 3}, 5), ({3, 5}, 6), ({1, 4}, 7), ({4, 2}, 5), ({4, 6}, 9), ] assert x.number_of_edges() == len(edges) for u, v, d in x.edges(data=True): assert ({u, v}, d["weight"]) in edges
def test_mst(): # Make sure we get the same results for undirected graphs. # Example from: https://en.wikipedia.org/wiki/Kruskal's_algorithm G = nx.Graph() edgelist = [(0, 3, [('weight', 5)]), (0, 1, [('weight', 7)]), (1, 3, [('weight', 9)]), (1, 2, [('weight', 8)]), (1, 4, [('weight', 7)]), (3, 4, [('weight', 15)]), (3, 5, [('weight', 6)]), (2, 4, [('weight', 5)]), (4, 5, [('weight', 8)]), (4, 6, [('weight', 9)]), (5, 6, [('weight', 11)])] G.add_edges_from(edgelist) G = G.to_directed() x = branchings.minimum_spanning_arborescence(G) edges = [(set([0, 1]), 7), (set([0, 3]), 5), (set([3, 5]), 6), (set([1, 4]), 7), (set([4, 2]), 5), (set([4, 6]), 9)] assert_equal(x.number_of_edges(), len(edges)) for u, v, d in x.edges(data=True): assert_true((set([u, v]), d['weight']) in edges)
def test_edmonds2_minarbor(): G = G1() x = branchings.minimum_spanning_arborescence(G) # This was obtained from algorithm. Need to verify it independently. # Branch weight is: 96 edges = [(3, 0, 5), (0, 2, 12), (0, 4, 12), (2, 5, 12), (4, 7, 12), (5, 8, 12), (5, 6, 14), (2, 1, 17)] x_ = build_branching(edges) assert_equal_branchings(x, x_)
def test_edmonds2_minarbor(): G = G1() x = branchings.minimum_spanning_arborescence(G) # This was obtained from algorithm. Need to verify it independently. # Branch weight is: 96 edges = [ (3, 0, 5), (0, 2, 12), (0, 4, 12), (2, 5, 12), (4, 7, 12), (5, 8, 12), (5, 6, 14), (2, 1, 17) ] x_ = build_branching(edges) assert_equal_branchings(x, x_)