本文整理汇总了Python中networkx.disjoint_union_all方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.disjoint_union_all方法的具体用法?Python networkx.disjoint_union_all怎么用?Python networkx.disjoint_union_all使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.disjoint_union_all方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: n_community
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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
示例2: disjoint_cliques_test_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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)
示例3: gen_2hier
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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
示例4: test_input_output
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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)
示例5: test_mixed_type_disjoint_union
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
def test_mixed_type_disjoint_union():
G = nx.Graph()
H = nx.MultiGraph()
I = nx.Graph()
U = nx.disjoint_union_all([G,H,I])
示例6: test_input_output
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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)
示例7: test_mixed_type_disjoint_union
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
def test_mixed_type_disjoint_union():
G = nx.Graph()
H = nx.MultiGraph()
I = nx.Graph()
U = nx.disjoint_union_all([G, H, I])
示例8: test_empty_disjoint_union
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
def test_empty_disjoint_union():
nx.disjoint_union_all([])
示例9: enron_task
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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")
示例10: test_union_all_and_compose_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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'])
示例11: test_union_all_and_compose_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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'])
示例12: windmill_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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
示例13: test_union_all_and_compose_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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'])
示例14: windmill_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import disjoint_union_all [as 别名]
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