def create_graph(edgelist): G = nx.DiGraph() G.add_weighted_edges_from(edgelist) # Graph layout pos = nx.spring_layout(G) # Remove nodes with out-degree = 1------------------------------------------ outdeg = nx.degree(G) while 1 in outdeg.values(): to_remove = [n for n in outdeg if outdeg[n] == 1] G.remove_nodes_from(to_remove) # Remove edges also checking the two values of the tuple edgelist = [x for x in edgelist if x[0] not in to_remove] edgelist = [x for x in edgelist if x[1] not in to_remove] outdeg = nx.degree(G) # --------------------------------------------------------------------------- # Node size ================================================================ d = nx.degree(G) nx.draw_networkx_nodes(G, pos, nodelist=d.keys(), node_size=[v * 100 for v in d.values()]) # =========================================================================== nx.draw_networkx_nodes(G, pos, cmap=plt.get_cmap("jet"), node_size=1) edge_labels = dict([((u, v), d["weight"]) for u, v, d in G.edges(data=True)]) nx.draw_networkx_edges(G, pos, width=0.5, alpha=0.2) nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) labels = {} for node in G.nodes(): labels[node] = node nx.draw_networkx_labels(G, pos, labels, font_size=8) if not len(G) == 1: from networkx.algorithms import approximation as apxm print apxm.min_edge_dominating_set(G) print apxm.max_clique(G) print apxm.maximum_independent_set(G) print apxm.min_maximal_matching(G) print nx.degree_centrality(G) plt.savefig("directed.png") # save as png # clean old graph plt.clf()
(1, 7), (1, 8), (2, 11), (2, 16), (2, 17), (3, 14), (3, 16), (3, 17), (4, 7), (4, 13), (4, 17), (5, 6), (5, 11), (6 ,18), (9 ,12), (10, 13), (11, 17), (13, 15), (15 ,17), (16 ,19)] graph=nx.Graph() graph.add_edges_from(graphEdges) print(graph.nodes()) print(graph.edges()) print(min_maximal_matching(graph)) print(graph) g1=nx.barabasi_albert_graph(1000,400) print(nx.maximal_independent_set((g1))) solution=[-1]*g1
def test_min_maximal_matching(): # smoke test G = nx.Graph() assert_equal(len(a.min_maximal_matching(G)),0)