本文整理汇总了Python中networkx.node_connected_component方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.node_connected_component方法的具体用法?Python networkx.node_connected_component怎么用?Python networkx.node_connected_component使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.node_connected_component方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: remove_cc_with_cycles
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def remove_cc_with_cycles(DG):
# remove pairend links and unitig links (unoriented)
edges_to_remove = []
for edge in DG.edges.data():
if edge[2]['type'] == '-1M':
edges_to_remove.append(edge)
for edge in edges_to_remove:
DG.remove_edge(edge[0],edge[1])
cycles = list(nx.simple_cycles(DG))
# sys.stderr.write(f" removed {len(cycles)} cycles\n") #DEB
# tmpnb=0 #DEB
G=nx.Graph(DG)
for nodes in cycles:
first_node_in_cycle = nodes[0] # get the first node, the other ones in the cycle are in the same CC
# remove the whole CC:
CC_with_cycle = nx.node_connected_component(G, first_node_in_cycle)
for node_id in CC_with_cycle:
if node_id in DG.nodes():
# tmpnb+=1 #DEB
DG.remove_node(node_id)
# sys.stderr.write(f" removed {tmpnb} nodes\n") #DEB
示例2: add_ccs
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def add_ccs(self):
curr = set(self.viewer.state.layers['seg'].segments)
for sid in self.viewer.state.layers['seg'].segments:
if sid in self.graph:
curr |= set(nx.node_connected_component(self.graph, sid))
self.update_segments(curr)
示例3: pick_connected_component
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def pick_connected_component(G):
node_list = nx.node_connected_component(G,0)
return G.subgraph(node_list)
示例4: get_connected_nodes
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def get_connected_nodes(self, node):
"""Get the connected nodes of the given node"""
with self.__lock:
return nx.node_connected_component(self.networkx_graph, node)
示例5: remove_cc
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def remove_cc(graph, u):
graph.remove_nodes_from(nx.node_connected_component(graph, u))
示例6: test_node_connected_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def test_node_connected_components(self):
ncc = nx.node_connected_component
G = self.grid
C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
assert_equal(ncc(G, 1), C)
示例7: test_connected_raise
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def test_connected_raise(self):
assert_raises(NetworkXNotImplemented, nx.connected_components, self.DG)
assert_raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
assert_raises(NetworkXNotImplemented, nx.connected_component_subgraphs, self.DG)
assert_raises(NetworkXNotImplemented, nx.node_connected_component, self.DG,1)
assert_raises(NetworkXNotImplemented, nx.is_connected, self.DG)
assert_raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
示例8: test_node_connected_components
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def test_node_connected_components(self):
ncc = nx.node_connected_component
G = self.grid
C = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
assert_equal(ncc(G, 1), C)
# deprecated
示例9: test_connected_raise
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def test_connected_raise(self):
assert_raises(NetworkXNotImplemented, nx.connected_components, self.DG)
assert_raises(NetworkXNotImplemented, nx.number_connected_components, self.DG)
assert_raises(NetworkXNotImplemented, nx.node_connected_component, self.DG, 1)
assert_raises(NetworkXNotImplemented, nx.is_connected, self.DG)
assert_raises(nx.NetworkXPointlessConcept, nx.is_connected, nx.Graph())
# deprecated
assert_raises(NetworkXNotImplemented, nx.connected_component_subgraphs, self.DG)
示例10: next
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import node_connected_component [as 别名]
def next(self, states):
"""ICM between two first samples in the first two input states."""
if len(states) > 2:
raise ValueError("exactly two input states required")
inp1, inp2 = states
bqm = inp1.problem
ss1 = inp1.samples.change_vartype(dimod.BINARY, inplace=False)
ss2 = inp2.samples.change_vartype(dimod.BINARY, inplace=False)
# sanity check: we operate on the same set of variables
if ss1.variables ^ ss2.variables:
raise ValueError("input samples not over the same set of variables")
# reorder variables, if necessary
# (use sequence comparison, not set)
variables = list(ss1.variables)
if ss2.variables != variables:
reorder = [ss2.variables.index(v) for v in variables]
record = ss2.record[:, reorder]
ss2 = dimod.SampleSet(record, variables, ss2.info, ss2.vartype)
# samples' symmetric difference (XOR)
# (form clusters of input variables with opposite values)
sample1 = ss1.record.sample[0]
sample2 = ss2.record.sample[0]
symdiff = sample1 ^ sample2
# for cluster detection we'll use a reduced problem graph
graph = bqm.to_networkx_graph()
# note: instead of numpy mask indexing of `notcluster`, we enumerate
# non-cluster variables manually to avoid conversion of potentially
# unhashable variable names to numpy types
notcluster = [v for v, d in zip(variables, symdiff) if d == 0]
graph.remove_nodes_from(notcluster)
# pick a random variable that belongs to a cluster, then select the cluster
node = self.random.choice(list(graph.nodes))
cluster = nx.node_connected_component(graph, node)
# flip variables from `cluster` in both input samples
flipper = np.array([1 if v in cluster else 0 for v in variables])
ss1.record.sample[0] ^= flipper
ss2.record.sample[0] ^= flipper
# change vartype back to input's type
ss1.change_vartype(inp1.samples.vartype)
ss2.change_vartype(inp2.samples.vartype)
# update sampleset's energies
ss1.record.energy = bqm.energies(ss1)
ss2.record.energy = bqm.energies(ss2)
return States(inp1.updated(samples=ss1), inp2.updated(samples=ss2))