本文整理汇总了Python中networkx.union方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.union方法的具体用法?Python networkx.union怎么用?Python networkx.union使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.union方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def __call__(self, code, charge_type):
l,d = code.depth, code.dimension
s = {}
for type in code.types:
s[type] = code.Syndrome(type, charge_type)
uc = nx.union(s['green'], nx.union(s['red'], s['blue']))
for edge in code.Dual['red'].edges():
break
scale = common.euclidean_dist(edge[0], edge[1])+.1
i = 1
while uc.nodes() != []:
clusters = GCC_Partition(uc, i*scale)
for cluster in clusters:
code, uc = GCC_Annihilate(cluster, code, uc, charge_type, i*scale)
i += 1
return code
示例2: test_union_attributes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def test_union_attributes():
g = nx.Graph()
g.add_node(0, x=4)
g.add_node(1, x=5)
g.add_edge(0, 1, size=5)
g.graph['name'] = 'g'
h = g.copy()
h.graph['name'] = 'h'
h.graph['attr'] = 'attr'
h.node[0]['x'] = 7
gh = nx.union(g, h, rename=('g', 'h'))
assert_equal( set(gh.nodes()) , set(['h0', 'h1', 'g0', 'g1']) )
for n in gh:
graph, node = n
assert_equal( gh.node[n], eval(graph).node[int(node)] )
assert_equal(gh.graph['attr'],'attr')
assert_equal(gh.graph['name'],'h') # h graph attributes take precendent
示例3: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def setUp(self):
# G is the example graph in Figure 1 from Batagelj and
# Zaversnik's paper titled An O(m) Algorithm for Cores
# Decomposition of Networks, 2003,
# http://arXiv.org/abs/cs/0310049. With nodes labeled as
# shown, the 3-core is given by nodes 1-8, the 2-core by nodes
# 9-16, the 1-core by nodes 17-20 and node 21 is in the
# 0-core.
t1=nx.convert_node_labels_to_integers(nx.tetrahedral_graph(),1)
t2=nx.convert_node_labels_to_integers(t1,5)
G=nx.union(t1,t2)
G.add_edges_from( [(3,7), (2,11), (11,5), (11,12), (5,12), (12,19),
(12,18), (3,9), (7,9), (7,10), (9,10), (9,20),
(17,13), (13,14), (14,15), (15,16), (16,13)])
G.add_node(21)
self.G=G
# Create the graph H resulting from the degree sequence
# [0,1,2,2,2,2,3] when using the Havel-Hakimi algorithm.
degseq=[0,1,2,2,2,2,3]
H = nx.havel_hakimi_graph(degseq)
mapping = {6:0, 0:1, 4:3, 5:6, 3:4, 1:2, 2:5 }
self.H = nx.relabel_nodes(H, mapping)
示例4: test_union_attributes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def test_union_attributes():
g = nx.Graph()
g.add_node(0, x=4)
g.add_node(1, x=5)
g.add_edge(0, 1, size=5)
g.graph['name'] = 'g'
h = g.copy()
h.graph['name'] = 'h'
h.graph['attr'] = 'attr'
h.nodes[0]['x'] = 7
gh = nx.union(g, h, rename=('g', 'h'))
assert_equal(set(gh.nodes()), set(['h0', 'h1', 'g0', 'g1']))
for n in gh:
graph, node = n
assert_equal(gh.nodes[n], eval(graph).nodes[int(node)])
assert_equal(gh.graph['attr'], 'attr')
assert_equal(gh.graph['name'], 'h') # h graph attributes take precendent
示例5: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def setUp(self):
# G is the example graph in Figure 1 from Batagelj and
# Zaversnik's paper titled An O(m) Algorithm for Cores
# Decomposition of Networks, 2003,
# http://arXiv.org/abs/cs/0310049. With nodes labeled as
# shown, the 3-core is given by nodes 1-8, the 2-core by nodes
# 9-16, the 1-core by nodes 17-20 and node 21 is in the
# 0-core.
t1 = nx.convert_node_labels_to_integers(nx.tetrahedral_graph(), 1)
t2 = nx.convert_node_labels_to_integers(t1, 5)
G = nx.union(t1, t2)
G.add_edges_from([(3, 7), (2, 11), (11, 5), (11, 12), (5, 12),
(12, 19), (12, 18), (3, 9), (7, 9), (7, 10),
(9, 10), (9, 20), (17, 13), (13, 14), (14, 15),
(15, 16), (16, 13)])
G.add_node(21)
self.G = G
# Create the graph H resulting from the degree sequence
# [0, 1, 2, 2, 2, 2, 3] when using the Havel-Hakimi algorithm.
degseq = [0, 1, 2, 2, 2, 2, 3]
H = nx.havel_hakimi_graph(degseq)
mapping = {6: 0, 0: 1, 4: 3, 5: 6, 3: 4, 1: 2, 2: 5}
self.H = nx.relabel_nodes(H, mapping)
示例6: test_disconnected_graph
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def test_disconnected_graph(self):
"""Test for a graph with multiple connected components."""
G = nx.barbell_graph(3, 0)
H = nx.barbell_graph(3, 0)
mapping = dict(zip(range(6), 'abcdef'))
nx.relabel_nodes(H, mapping, copy=False)
G = nx.union(G, H)
chains = list(nx.chain_decomposition(G))
expected = [
[(0, 1), (1, 2), (2, 0)],
[(3, 4), (4, 5), (5, 3)],
[('a', 'b'), ('b', 'c'), ('c', 'a')],
[('d', 'e'), ('e', 'f'), ('f', 'd')],
]
self.assertEqual(len(chains), len(expected))
for chain in chains:
self.assertContainsChain(chain, expected)
示例7: test_union_attributes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def test_union_attributes():
g = nx.Graph()
g.add_node(0, x=4)
g.add_node(1, x=5)
g.add_edge(0, 1, size=5)
g.graph['name'] = 'g'
h = g.copy()
h.graph['name'] = 'h'
h.graph['attr'] = 'attr'
h.nodes[0]['x'] = 7
gh = nx.union(g, h, rename=('g', 'h'))
assert_equal( set(gh.nodes()) , set(['h0', 'h1', 'g0', 'g1']) )
for n in gh:
graph, node = n
assert_equal( gh.nodes[n], eval(graph).nodes[int(node)] )
assert_equal(gh.graph['attr'],'attr')
assert_equal(gh.graph['name'],'h') # h graph attributes take precendent
示例8: __call__
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def __call__(self, code, charge_type):
errors = {}
for type in code.types:
errors[type] = code.Syndrome(type, charge_type)
shrunk_errs, shrunk_exts, matches = {}, {}, {}
loops_graph = nx.Graph()
for t1 in code.types:
[t2, t3] = code.complementaryTypes(t1)
shrunk_errs[t1] = nx.union(errors[t2], errors[t3])
shrunk_exts[t1] = code.External[t2] + code.External[t3]
alt_ext = code.External[t1][0]
matches[t1] = DSP_Matching(shrunk_errs[t1], shrunk_exts[t1], 2, alt_ext)
for start in matches[t1]:
end = matches[t1][start]
chain = DSP_Path(code.Dual[t1], start, end)
links = len(chain) -1
for i in range(links):
node1, node2 = chain[i], chain[i+1]
edge = (node1, node2)
if edge in loops_graph.edges():
loops_graph.remove_edge(*edge)
else:
loops_graph.add_edge(*edge)
Exts = code.External['red']+code.External['blue']+code.External['green']
code, loops_graph = correctLoops(code, loops_graph, charge_type)
while hasConnectedBoundaries(code, loops_graph, Exts):
ext1, ext2 = connectedBoundaries(loops_graph, Exts)
code, loops_graph = makeBoundLoop(code, loops_graph, ext1, ext2)
code, loops_graph = correctLoops(code, loops_graph, charge_type)
return code
示例9: merge_graphs_to
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def merge_graphs_to(graph, graphs):
head_node = graph.graph['head_node']
for i, g in enumerate(graphs):
graph = nx.union(graph, g)
graph.add_edge(head_node, g.graph['head_node'], arg=i)
graph.graph['head_node'] = head_node
return graph
示例10: disjoint_union_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def disjoint_union_all(graphs):
"""Return the disjoint union of all graphs.
This operation forces distinct integer node labels starting with 0
for the first graph in the list and numbering consecutively.
Parameters
----------
graphs : list
List of NetworkX graphs
Returns
-------
U : A graph with the same type as the first graph in list
Notes
-----
It is recommended that the graphs be either all directed or all undirected.
Graph, edge, and node attributes are propagated to the union graph.
If a graph attribute is present in multiple graphs, then the value
from the last graph in the list with that attribute is used.
"""
graphs = iter(graphs)
U = next(graphs)
for H in graphs:
U = nx.disjoint_union(U, H)
return U
示例11: compose_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def compose_all(graphs, name=None):
"""Return the composition of all graphs.
Composition is the simple union of the node sets and edge sets.
The node sets of the supplied graphs need not be disjoint.
Parameters
----------
graphs : list
List of NetworkX graphs
name : string
Specify name for new graph
Returns
-------
C : A graph with the same type as the first graph in list
Notes
-----
It is recommended that the supplied graphs be either all directed or all
undirected.
Graph, edge, and node attributes are propagated to the union graph.
If a graph attribute is present in multiple graphs, then the value
from the last graph in the list with that attribute is used.
"""
graphs = iter(graphs)
C = next(graphs)
for H in graphs:
C = nx.compose(C, H, name=name)
return C
示例12: test_mixed_type_union
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def test_mixed_type_union():
G = nx.Graph()
H = nx.MultiGraph()
U = nx.union(G,H)
示例13: setUp
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def setUp(self):
G1 = cnlti(nx.grid_2d_graph(2, 2), first_label=0, ordering="sorted")
G2 = cnlti(nx.lollipop_graph(3, 3), first_label=4, ordering="sorted")
G3 = cnlti(nx.house_graph(), first_label=10, ordering="sorted")
self.G = nx.union(G1, G2)
self.G = nx.union(self.G, G3)
self.DG = nx.DiGraph([(1, 2), (1, 3), (2, 3)])
self.grid = cnlti(nx.grid_2d_graph(4, 4), first_label=1)
self.gc = []
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (2, 8), (3, 4), (3, 7), (4, 5),
(5, 3), (5, 6), (7, 4), (7, 6), (8, 1), (8, 7)])
C = [[3, 4, 5, 7], [1, 2, 8], [6]]
self.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (1, 3), (1, 4), (4, 2), (3, 4), (2, 3)])
C = [[2, 3, 4],[1]]
self.gc.append((G, C))
G = nx.DiGraph()
G.add_edges_from([(1, 2), (2, 3), (3, 2), (2, 1)])
C = [[1, 2, 3]]
self.gc.append((G,C))
# Eppstein's tests
G = nx.DiGraph({0:[1], 1:[2, 3], 2:[4, 5], 3:[4, 5], 4:[6], 5:[], 6:[]})
C = [[0], [1], [2],[ 3], [4], [5], [6]]
self.gc.append((G,C))
G = nx.DiGraph({0:[1], 1:[2, 3, 4], 2:[0, 3], 3:[4], 4:[3]})
C = [[0, 1, 2], [3, 4]]
self.gc.append((G, C))
示例14: disjoint_union_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def disjoint_union_all(graphs):
"""Returns the disjoint union of all graphs.
This operation forces distinct integer node labels starting with 0
for the first graph in the list and numbering consecutively.
Parameters
----------
graphs : list
List of NetworkX graphs
Returns
-------
U : A graph with the same type as the first graph in list
Raises
------
ValueError
If `graphs` is an empty list.
Notes
-----
It is recommended that the graphs be either all directed or all undirected.
Graph, edge, and node attributes are propagated to the union graph.
If a graph attribute is present in multiple graphs, then the value
from the last graph in the list with that attribute is used.
"""
if not graphs:
raise ValueError('cannot apply disjoint_union_all to an empty list')
graphs = iter(graphs)
U = next(graphs)
for H in graphs:
U = nx.disjoint_union(U, H)
return U
示例15: compose_all
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import union [as 别名]
def compose_all(graphs):
"""Returns the composition of all graphs.
Composition is the simple union of the node sets and edge sets.
The node sets of the supplied graphs need not be disjoint.
Parameters
----------
graphs : list
List of NetworkX graphs
Returns
-------
C : A graph with the same type as the first graph in list
Raises
------
ValueError
If `graphs` is an empty list.
Notes
-----
It is recommended that the supplied graphs be either all directed or all
undirected.
Graph, edge, and node attributes are propagated to the union graph.
If a graph attribute is present in multiple graphs, then the value
from the last graph in the list with that attribute is used.
"""
if not graphs:
raise ValueError('cannot apply compose_all to an empty list')
graphs = iter(graphs)
C = next(graphs)
for H in graphs:
C = nx.compose(C, H)
return C