def test_small_graph_prim(): g = create_graph(directed=False) generators.gnp_random_graph(g, n=500, p=0.1, seed=17) mst_w, mst_edges = spanning.prim(g) assert mst_w == 499.0
def test_prim(): g = build_graph() mst_w, mst_edges = spanning.prim(g) assert mst_w == 9.0 expected = set([0, 1, 2, 3, 4, 5, 6, 7, 8]) solution = set(mst_edges) assert expected == solution
def test_result_with_difference_symmetric(): g = build_graph() mst_weight, mst_tree = spanning.prim(g) non_mst_edges = g.edges - set(mst_tree) # test that our intermediate set results, property implement # method _from_iterable assert non_mst_edges == {9, 10, 11, 12, 13, 14, 15, 16, 17}
def test_anyhashableg_prim(): g = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, any_hashable=True, ) g.add_vertex("0") g.add_vertex("1") g.add_vertex("2") e1 = g.add_edge("0", "1") g.set_edge_weight(e1, 1.0) e2 = g.add_edge("1", "2") g.set_edge_weight(e2, 2.0) e3 = g.add_edge("2", "0") g.set_edge_weight(e3, 3.0) mst_w, mst_edges = spanning.prim(g) assert mst_w == 3.0 assert set(mst_edges) == {e1, e2}
# %% # We also assign some random weights from [0, 1) to the edges. rng = random.Random(17) for e in g.edges: g.set_edge_weight(e, rng.random()) # %% # Let us print the graph print(g) # %% # Then, we execute Prim's algorithm. mst_weight, mst_edges = spanning.prim(g) # %% # The result is a tuple which contains the weight and the minimum spanning tree. # print('mst weight: {}'.format(mst_weight)) print('mst tree: {}'.format(mst_edges)) # %% # Ploting the graph with highlighted the MST edges # positions = drawing.layout(g, name="fruchterman_reingold", seed=17) drawing.draw_jgrapht_vertices(g, positions=positions)