Python networkx.disjoint_union_all() Examples
The following are 18
code examples of networkx.disjoint_union_all().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: utils.py From graph-generation with MIT License | 6 votes |
def n_community(c_sizes, p_inter=0.01): graphs = [nx.gnp_random_graph(c_sizes[i], 0.7, seed=i) for i in range(len(c_sizes))] G = nx.disjoint_union_all(graphs) communities = list(nx.connected_component_subgraphs(G)) for i in range(len(communities)): subG1 = communities[i] nodes1 = list(subG1.nodes()) for j in range(i+1, len(communities)): subG2 = communities[j] nodes2 = list(subG2.nodes()) has_inter_edge = False for n1 in nodes1: for n2 in nodes2: if np.random.rand() < p_inter: G.add_edge(n1, n2) has_inter_edge = True if not has_inter_edge: G.add_edge(nodes1[0], nodes2[0]) #print('connected comp: ', len(list(nx.connected_component_subgraphs(G)))) return G
Example #2
Source File: utils.py From GraphRNN with MIT License | 6 votes |
def n_community(c_sizes, p_inter=0.01): graphs = [nx.gnp_random_graph(c_sizes[i], 0.7, seed=i) for i in range(len(c_sizes))] G = nx.disjoint_union_all(graphs) communities = list(nx.connected_component_subgraphs(G)) for i in range(len(communities)): subG1 = communities[i] nodes1 = list(subG1.nodes()) for j in range(i+1, len(communities)): subG2 = communities[j] nodes2 = list(subG2.nodes()) has_inter_edge = False for n1 in nodes1: for n2 in nodes2: if np.random.rand() < p_inter: G.add_edge(n1, n2) has_inter_edge = True if not has_inter_edge: G.add_edge(nodes1[0], nodes2[0]) #print('connected comp: ', len(list(nx.connected_component_subgraphs(G)))) return G
Example #3
Source File: mmsb.py From graph-generation with MIT License | 5 votes |
def disjoint_cliques_test_graph(num_cliques, clique_size): G = nx.disjoint_union_all([nx.complete_graph(clique_size) for _ in range(num_cliques)]) return nx.to_numpy_matrix(G)
Example #4
Source File: mmsb.py From GraphRNN with MIT License | 5 votes |
def disjoint_cliques_test_graph(num_cliques, clique_size): G = nx.disjoint_union_all([nx.complete_graph(clique_size) for _ in range(num_cliques)]) return nx.to_numpy_matrix(G)
Example #5
Source File: data.py From diffpool with MIT License | 5 votes |
def gen_2hier(num_graphs, num_clusters, n, m_range, inter_prob1, inter_prob2, feat_gen): ''' Each community is a BA graph. Args: inter_prob1: probability of one node connecting to any node in the other community within the large cluster. inter_prob2: probability of one node connecting to any node in the other community between the large cluster. ''' graphs = [] for i in range(num_graphs): clusters2 = [] for j in range(len(num_clusters)): clusters = gen_er(range(n, n+1), 0.5, num_clusters[j], feat_gen[0]) G = nx.disjoint_union_all(clusters) for u1 in range(G.number_of_nodes()): if np.random.rand() < inter_prob1: target = np.random.choice(G.number_of_nodes() - n) # move one cluster after to make sure it's not an intra-cluster edge if target // n >= u1 // n: target += n G.add_edge(u1, target) clusters2.append(G) G = nx.disjoint_union_all(clusters2) cluster_sizes_cum = np.cumsum([cluster2.number_of_nodes() for cluster2 in clusters2]) curr_cluster = 0 for u1 in range(G.number_of_nodes()): if u1 >= cluster_sizes_cum[curr_cluster]: curr_cluster += 1 if np.random.rand() < inter_prob2: target = np.random.choice(G.number_of_nodes() - clusters2[curr_cluster].number_of_nodes()) # move one cluster after to make sure it's not an intra-cluster edge if curr_cluster == 0 or target >= cluster_sizes_cum[curr_cluster - 1]: target += cluster_sizes_cum[curr_cluster] G.add_edge(u1, target) graphs.append(G) return graphs
Example #6
Source File: test_all.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_mixed_type_disjoint_union(): G = nx.Graph() H = nx.MultiGraph() I = nx.Graph() U = nx.disjoint_union_all([G,H,I])
Example #7
Source File: test_all.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_input_output(): l = [nx.Graph([(1,2)]),nx.Graph([(3,4)])] U = nx.disjoint_union_all(l) assert_equal(len(l),2) C = nx.compose_all(l) assert_equal(len(l),2) l = [nx.Graph([(1,2)]),nx.Graph([(1,2)])] R = nx.intersection_all(l) assert_equal(len(l),2)
Example #8
Source File: test_all.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 5 votes |
def test_mixed_type_disjoint_union(): G = nx.Graph() H = nx.MultiGraph() I = nx.Graph() U = nx.disjoint_union_all([G,H,I])
Example #9
Source File: test_all.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_input_output(): l = [nx.Graph([(1,2)]),nx.Graph([(3,4)])] U = nx.disjoint_union_all(l) assert_equal(len(l),2) C = nx.compose_all(l) assert_equal(len(l),2) l = [nx.Graph([(1,2)]),nx.Graph([(1,2)])] R = nx.intersection_all(l) assert_equal(len(l),2)
Example #10
Source File: test_all.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_input_output(): l = [nx.Graph([(1, 2)]), nx.Graph([(3, 4)])] U = nx.disjoint_union_all(l) assert_equal(len(l), 2) C = nx.compose_all(l) assert_equal(len(l), 2) l = [nx.Graph([(1, 2)]), nx.Graph([(1, 2)])] R = nx.intersection_all(l) assert_equal(len(l), 2)
Example #11
Source File: test_all.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_mixed_type_disjoint_union(): G = nx.Graph() H = nx.MultiGraph() I = nx.Graph() U = nx.disjoint_union_all([G, H, I])
Example #12
Source File: test_all.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_empty_disjoint_union(): nx.disjoint_union_all([])
Example #13
Source File: community.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def windmill_graph(n, k): """Generate a windmill graph. A windmill graph is a graph of `n` cliques each of size `k` that are all joined at one node. It can be thought of as taking a disjoint union of `n` cliques of size `k`, selecting one point from each, and contracting all of the selected points. Alternatively, one could generate `n` cliques of size `k-1` and one node that is connected to all other nodes in the graph. Parameters ---------- n : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph windmill graph with n cliques of size k Raises ------ NetworkXError If the number of cliques is less than two If the size of the cliques are less than two Examples -------- >>> G = nx.windmill_graph(4, 5) Notes ----- The node labeled `0` will be the node connected to all other nodes. Note that windmill graphs are usually denoted `Wd(k,n)`, so the parameters are in the opposite order as the parameters of this method. """ if n < 2: msg = 'A windmill graph must have at least two cliques' raise nx.NetworkXError(msg) if k < 2: raise nx.NetworkXError('The cliques must have at least two nodes') G = nx.disjoint_union_all(itertools.chain([nx.complete_graph(k)], (nx.complete_graph(k - 1) for _ in range(n - 1)))) G.add_edges_from((0, i) for i in range(k, G.number_of_nodes())) return G
Example #14
Source File: test_all.py From aws-kube-codesuite with Apache License 2.0 | 4 votes |
def test_union_all_and_compose_all(): K3=nx.complete_graph(3) P3=nx.path_graph(3) G1=nx.DiGraph() G1.add_edge('A','B') G1.add_edge('A','C') G1.add_edge('A','D') G2=nx.DiGraph() G2.add_edge('1','2') G2.add_edge('1','3') G2.add_edge('1','4') G=nx.union_all([G1,G2]) H=nx.compose_all([G1,G2]) assert_edges_equal(G.edges(),H.edges()) assert_false(G.has_edge('A','1')) assert_raises(nx.NetworkXError, nx.union, K3, P3) H1=nx.union_all([H,G1],rename=('H','G1')) assert_equal(sorted(H1.nodes()), ['G1A', 'G1B', 'G1C', 'G1D', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) H2=nx.union_all([H,G2],rename=("H","")) assert_equal(sorted(H2.nodes()), ['1', '2', '3', '4', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) assert_false(H1.has_edge('NB','NA')) G=nx.compose_all([G,G]) assert_edges_equal(G.edges(),H.edges()) G2=nx.union_all([G2,G2],rename=('','copy')) assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) assert_equal(sorted(G2.neighbors('copy4')),[]) assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4']) assert_equal(len(G),8) assert_equal(nx.number_of_edges(G),6) E=nx.disjoint_union_all([G,G]) assert_equal(len(E),16) assert_equal(nx.number_of_edges(E),12) E=nx.disjoint_union_all([G1,G2]) assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) G1=nx.DiGraph() G1.add_edge('A','B') G2=nx.DiGraph() G2.add_edge(1,2) G3=nx.DiGraph() G3.add_edge(11,22) G4=nx.union_all([G1,G2,G3],rename=("G1","G2","G3")) assert_equal(sorted(G4.nodes()), ['G1A', 'G1B', 'G21', 'G22', 'G311', 'G322'])
Example #15
Source File: community.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def windmill_graph(n, k): """Generate a windmill graph. A windmill graph is a graph of `n` cliques each of size `k` that are all joined at one node. It can be thought of as taking a disjoint union of `n` cliques of size `k`, selecting one point from each, and contracting all of the selected points. Alternatively, one could generate `n` cliques of size `k-1` and one node that is connected to all other nodes in the graph. Parameters ---------- n : int Number of cliques k : int Size of cliques Returns ------- G : NetworkX Graph windmill graph with n cliques of size k Raises ------ NetworkXError If the number of cliques is less than two If the size of the cliques are less than two Examples -------- >>> G = nx.windmill_graph(4, 5) Notes ----- The node labeled `0` will be the node connected to all other nodes. Note that windmill graphs are usually denoted `Wd(k,n)`, so the parameters are in the opposite order as the parameters of this method. """ if n < 2: msg = 'A windmill graph must have at least two cliques' raise nx.NetworkXError(msg) if k < 2: raise nx.NetworkXError('The cliques must have at least two nodes') G = nx.disjoint_union_all(itertools.chain([nx.complete_graph(k)], (nx.complete_graph(k - 1) for _ in range(n - 1)))) G.add_edges_from((0, i) for i in range(k, G.number_of_nodes())) return G
Example #16
Source File: test_all.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def test_union_all_and_compose_all(): K3 = nx.complete_graph(3) P3 = nx.path_graph(3) G1 = nx.DiGraph() G1.add_edge('A', 'B') G1.add_edge('A', 'C') G1.add_edge('A', 'D') G2 = nx.DiGraph() G2.add_edge('1', '2') G2.add_edge('1', '3') G2.add_edge('1', '4') G = nx.union_all([G1, G2]) H = nx.compose_all([G1, G2]) assert_edges_equal(G.edges(), H.edges()) assert_false(G.has_edge('A', '1')) assert_raises(nx.NetworkXError, nx.union, K3, P3) H1 = nx.union_all([H, G1], rename=('H', 'G1')) assert_equal(sorted(H1.nodes()), ['G1A', 'G1B', 'G1C', 'G1D', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) H2 = nx.union_all([H, G2], rename=("H", "")) assert_equal(sorted(H2.nodes()), ['1', '2', '3', '4', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) assert_false(H1.has_edge('NB', 'NA')) G = nx.compose_all([G, G]) assert_edges_equal(G.edges(), H.edges()) G2 = nx.union_all([G2, G2], rename=('', 'copy')) assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) assert_equal(sorted(G2.neighbors('copy4')), []) assert_equal(sorted(G2.neighbors('copy1')), ['copy2', 'copy3', 'copy4']) assert_equal(len(G), 8) assert_equal(nx.number_of_edges(G), 6) E = nx.disjoint_union_all([G, G]) assert_equal(len(E), 16) assert_equal(nx.number_of_edges(E), 12) E = nx.disjoint_union_all([G1, G2]) assert_equal(sorted(E.nodes()), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) G1 = nx.DiGraph() G1.add_edge('A', 'B') G2 = nx.DiGraph() G2.add_edge(1, 2) G3 = nx.DiGraph() G3.add_edge(11, 22) G4 = nx.union_all([G1, G2, G3], rename=("G1", "G2", "G3")) assert_equal(sorted(G4.nodes()), ['G1A', 'G1B', 'G21', 'G22', 'G311', 'G322'])
Example #17
Source File: test_all.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 4 votes |
def test_union_all_and_compose_all(): K3=nx.complete_graph(3) P3=nx.path_graph(3) G1=nx.DiGraph() G1.add_edge('A','B') G1.add_edge('A','C') G1.add_edge('A','D') G2=nx.DiGraph() G2.add_edge('1','2') G2.add_edge('1','3') G2.add_edge('1','4') G=nx.union_all([G1,G2]) H=nx.compose_all([G1,G2]) assert_edges_equal(G.edges(),H.edges()) assert_false(G.has_edge('A','1')) assert_raises(nx.NetworkXError, nx.union, K3, P3) H1=nx.union_all([H,G1],rename=('H','G1')) assert_equal(sorted(H1.nodes()), ['G1A', 'G1B', 'G1C', 'G1D', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) H2=nx.union_all([H,G2],rename=("H","")) assert_equal(sorted(H2.nodes()), ['1', '2', '3', '4', 'H1', 'H2', 'H3', 'H4', 'HA', 'HB', 'HC', 'HD']) assert_false(H1.has_edge('NB','NA')) G=nx.compose_all([G,G]) assert_edges_equal(G.edges(),H.edges()) G2=nx.union_all([G2,G2],rename=('','copy')) assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) assert_equal(G2.neighbors('copy4'),[]) assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4']) assert_equal(len(G),8) assert_equal(nx.number_of_edges(G),6) E=nx.disjoint_union_all([G,G]) assert_equal(len(E),16) assert_equal(nx.number_of_edges(E),12) E=nx.disjoint_union_all([G1,G2]) assert_equal(sorted(E.nodes()),[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]) G1=nx.DiGraph() G1.add_edge('A','B') G2=nx.DiGraph() G2.add_edge(1,2) G3=nx.DiGraph() G3.add_edge(11,22) G4=nx.union_all([G1,G2,G3],rename=("G1","G2","G3")) assert_equal(sorted(G4.nodes()), ['G1A', 'G1B', 'G21', 'G22', 'G311', 'G322'])
Example #18
Source File: train.py From gnn-model-explainer with Apache License 2.0 | 4 votes |
def enron_task(args, idx=None, writer=None): labels_dict = { "None": 5, "Employee": 0, "Vice President": 1, "Manager": 2, "Trader": 3, "CEO+Managing Director+Director+President": 4, } max_enron_id = 183 if idx is None: G_list = [] labels_list = [] for i in range(10): net = pickle.load( open("data/gnn-explainer-enron/enron_slice_{}.pkl".format(i), "rb") ) # net.add_nodes_from(range(max_enron_id)) # labels=[n[1].get('role', 'None') for n in net.nodes(data=True)] # labels_num = [labels_dict[l] for l in labels] featgen_const = featgen.ConstFeatureGen( np.ones(args.input_dim, dtype=float) ) featgen_const.gen_node_features(net) G_list.append(net) print(net.number_of_nodes()) # labels_list.append(labels_num) G = nx.disjoint_union_all(G_list) model = models.GcnEncoderNode( args.input_dim, args.hidden_dim, args.output_dim, len(labels_dict), args.num_gc_layers, bn=args.bn, args=args, ) labels = [n[1].get("role", "None") for n in G.nodes(data=True)] labels_num = [labels_dict[l] for l in labels] for i in range(5): print("Label ", i, ": ", labels_num.count(i)) print("Total num nodes: ", len(labels_num)) print(labels_num) if args.gpu: model = model.cuda() train_node_classifier(G, labels_num, model, args, writer=writer) else: print("Running Enron full task")