本文整理汇总了Python中networkx.is_directed_acyclic_graph方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.is_directed_acyclic_graph方法的具体用法?Python networkx.is_directed_acyclic_graph怎么用?Python networkx.is_directed_acyclic_graph使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx
的用法示例。
在下文中一共展示了networkx.is_directed_acyclic_graph方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_topological_ordering
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_topological_ordering(N=1):
np.random.seed(12345)
i = 0
while i < N:
p = np.random.uniform(0.25, 1)
n_v = np.random.randint(5, 10)
G = random_DAG(n_v, p)
G_nx = to_networkx(G)
if nx.is_directed_acyclic_graph(G_nx):
topo_order = G.topological_ordering()
# test topological order
seen_it = set()
for n_i in topo_order:
seen_it.add(n_i)
assert any([c_i in seen_it for c_i in G.get_neighbors(n_i)]) == False
print("PASSED")
i += 1
示例2: load_feed
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def load_feed(
path: str, view: Optional[View] = None, config: Optional[nx.DiGraph] = None
) -> Feed:
config = default_config() if config is None else config
view = {} if view is None else view
if not nx.is_directed_acyclic_graph(config):
raise ValueError("Config must be a DAG")
if os.path.isdir(path):
feed = _load_feed(path, view, config)
elif os.path.isfile(path):
feed = _unpack_feed(path, view, config)
else:
raise ValueError("File or path not found: {}".format(path))
return feed
示例3: remove_cycle_edges_by_mfas
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def remove_cycle_edges_by_mfas(graph_file):
g = nx.read_edgelist(graph_file,create_using = nx.DiGraph(),nodetype = int)
from remove_self_loops import remove_self_loops_from_graph
self_loops = remove_self_loops_from_graph(g)
scc_nodes,_,_,_ = scc_nodes_edges(g)
degree_dict = get_nodes_degree_dict(g,scc_nodes)
sccs = get_big_sccs(g)
if len(sccs) == 0:
print("After removal of self loop edgs: %s" % nx.is_directed_acyclic_graph(g))
return self_loops
edges_to_be_removed = []
import timeit
t1 = timeit.default_timer()
greedy_local_heuristic(sccs,degree_dict,edges_to_be_removed)
t2 = timeit.default_timer()
print("mfas time usage: %0.4f s" % (t2 - t1))
edges_to_be_removed = list(set(edges_to_be_removed))
g.remove_edges_from(edges_to_be_removed)
edges_to_be_removed += self_loops
edges_to_be_removed_file = graph_file[:len(graph_file)-6] + "_removed_by_mfas.edges"
write_pairs_to_file(edges_to_be_removed,edges_to_be_removed_file)
return edges_to_be_removed
开发者ID:zhenv5,项目名称:breaking_cycles_in_noisy_hierarchies,代码行数:25,代码来源:remove_cycle_edges_by_minimum_feedback_arc_set_greedy.py
示例4: _validate
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def _validate(G):
'''
Validates dependency graph to ensure it has no missing or cyclic dependencies
'''
for name in G.nodes():
if 'value' not in G.node[name] and 'template' not in G.node[name]:
msg = 'Dependency unsatisfied in variable "%s"' % name
raise ParamException(msg)
if not nx.is_directed_acyclic_graph(G):
graph_cycles = nx.simple_cycles(G)
variable_names = []
for cycle in graph_cycles:
try:
variable_name = cycle[0]
except IndexError:
continue
variable_names.append(variable_name)
variable_names = ', '.join(sorted(variable_names))
msg = ('Cyclic dependency found in the following variables: %s. Likely the variable is '
'referencing itself' % (variable_names))
raise ParamException(msg)
示例5: dagify_min_edge
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def dagify_min_edge(g):
"""Input a graph and output a DAG.
The heuristic is to reverse the edge with the lowest score of the cycle
if possible, else remove it.
Args:
g (networkx.DiGraph): Graph to modify to output a DAG
Returns:
networkx.DiGraph: DAG made out of the input graph.
Example:
>>> from cdt.utils.graph import dagify_min_edge
>>> import networkx as nx
>>> import numpy as np
>>> # Generate sample data
>>> graph = nx.DiGraph((np.ones(4) - np.eye(4)) *
np.random.uniform(size=(4,4)))
>>> output = dagify_min_edge(graph)
"""
ncycles = len(list(nx.simple_cycles(g)))
while not nx.is_directed_acyclic_graph(g):
cycle = next(nx.simple_cycles(g))
edges = [(cycle[-1], cycle[0])]
scores = [(g[cycle[-1]][cycle[0]]['weight'])]
for i, j in zip(cycle[:-1], cycle[1:]):
edges.append((i, j))
scores.append(g[i][j]['weight'])
i, j = edges[scores.index(min(scores))]
gc = deepcopy(g)
gc.remove_edge(i, j)
gc.add_edge(j, i)
ngc = len(list(nx.simple_cycles(gc)))
if ngc < ncycles:
g.add_edge(j, i, weight=min(scores))
g.remove_edge(i, j)
ncycles = ngc
return g
示例6: test_contract_scc1
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_contract_scc1(self):
G = nx.DiGraph()
G.add_edges_from([
(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3), (4, 5), (5, 6),
(6, 5), (6, 7), (7, 8), (7, 9), (7, 10), (8, 9), (9, 7), (10, 6),
(11, 2), (11, 4), (11, 6), (12, 6), (12, 11),
])
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
# DAG
assert_true(nx.is_directed_acyclic_graph(cG))
# nodes
assert_equal(sorted(cG.nodes()), [0, 1, 2, 3])
# edges
mapping={}
for i, component in enumerate(scc):
for n in component:
mapping[n] = i
edge = (mapping[2], mapping[3])
assert_true(cG.has_edge(*edge))
edge = (mapping[2], mapping[5])
assert_true(cG.has_edge(*edge))
edge = (mapping[3], mapping[5])
assert_true(cG.has_edge(*edge))
示例7: test_contract_scc1
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_contract_scc1(self):
G = nx.DiGraph()
G.add_edges_from([
(1, 2), (2, 3), (2, 11), (2, 12), (3, 4), (4, 3), (4, 5), (5, 6),
(6, 5), (6, 7), (7, 8), (7, 9), (7, 10), (8, 9), (9, 7), (10, 6),
(11, 2), (11, 4), (11, 6), (12, 6), (12, 11),
])
scc = list(nx.strongly_connected_components(G))
cG = nx.condensation(G, scc)
# DAG
assert_true(nx.is_directed_acyclic_graph(cG))
# nodes
assert_equal(sorted(cG.nodes()), [0, 1, 2, 3])
# edges
mapping = {}
for i, component in enumerate(scc):
for n in component:
mapping[n] = i
edge = (mapping[2], mapping[3])
assert_true(cG.has_edge(*edge))
edge = (mapping[2], mapping[5])
assert_true(cG.has_edge(*edge))
edge = (mapping[3], mapping[5])
assert_true(cG.has_edge(*edge))
示例8: create_is_dag_condition
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def create_is_dag_condition(node_list):
def is_dag_condition(G):
return nx.is_directed_acyclic_graph(G)
return is_dag_condition
示例9: _has_cycle
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def _has_cycle(self):
"""
Test if the region contains a cycle.
:return: True if the region contains a cycle, False otherwise.
:rtype: bool
"""
return not networkx.is_directed_acyclic_graph(self._region.graph)
示例10: test_random_DAG
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_random_DAG(N=1):
np.random.seed(12345)
i = 0
while i < N:
p = np.random.uniform(0.25, 1)
n_v = np.random.randint(5, 50)
G = random_DAG(n_v, p)
G_nx = to_networkx(G)
assert nx.is_directed_acyclic_graph(G_nx)
print("PASSED")
i += 1
示例11: test_is_acyclic
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_is_acyclic(N=1):
np.random.seed(12345)
i = 0
while i < N:
p = np.random.rand()
directed = np.random.rand() < 0.5
G = random_unweighted_graph(n_vertices=10, edge_prob=p, directed=True)
G_nx = to_networkx(G)
assert G.is_acyclic() == nx.is_directed_acyclic_graph(G_nx)
print("PASSED")
i += 1
示例12: __transitive_reduction
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def __transitive_reduction(self):
"""Transitive reduction for acyclic graphs."""
assert nx.is_directed_acyclic_graph(self.digraph)
for u in self.digraph:
transitive_vertex = []
for v in self.digraph[u]:
transitive_vertex.extend(
x for _, x in nx.dfs_edges(self.digraph, v))
self.digraph.remove_edges_from((u, x) for x in transitive_vertex)
示例13: test_apply_operation_back_conditional
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_apply_operation_back_conditional(self):
"""Test consistency of apply_operation_back with condition set."""
# Single qubit gate conditional: qc.h(qr[2]).c_if(cr, 3)
h_gate = HGate()
h_gate.condition = self.condition
h_node = self.dag.apply_operation_back(
h_gate, [self.qubit2], [])
self.assertEqual(h_node.qargs, [self.qubit2])
self.assertEqual(h_node.cargs, [])
self.assertEqual(h_node.condition, h_gate.condition)
self.assertEqual(
sorted(self.dag._get_multi_graph_in_edges(h_node._node_id)),
sorted([
(self.dag.input_map[self.qubit2]._node_id, h_node._node_id,
{'wire': self.qubit2, 'name': 'qr[2]'}),
(self.dag.input_map[self.clbit0]._node_id, h_node._node_id,
{'wire': self.clbit0, 'name': 'cr[0]'}),
(self.dag.input_map[self.clbit1]._node_id, h_node._node_id,
{'wire': self.clbit1, 'name': 'cr[1]'}),
]))
self.assertEqual(
sorted(self.dag._get_multi_graph_out_edges(h_node._node_id)),
sorted([
(h_node._node_id, self.dag.output_map[self.qubit2]._node_id,
{'wire': self.qubit2, 'name': 'qr[2]'}),
(h_node._node_id, self.dag.output_map[self.clbit0]._node_id,
{'wire': self.clbit0, 'name': 'cr[0]'}),
(h_node._node_id, self.dag.output_map[self.clbit1]._node_id,
{'wire': self.clbit1, 'name': 'cr[1]'}),
]))
if self.dag._USE_RX:
self.assertTrue(rx.is_directed_acyclic_graph(self.dag._multi_graph))
else:
self.assertTrue(nx.is_directed_acyclic_graph(self.dag._multi_graph))
示例14: test_apply_operation_back_conditional_measure_to_self
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def test_apply_operation_back_conditional_measure_to_self(self):
"""Test consistency of apply_operation_back for measure onto conditioning bit."""
# Measure targeting a clbit which _is_ a member of the conditional
# register. qc.measure(qr[0], cr[0]).c_if(cr, 3)
meas_gate = Measure()
meas_gate.condition = self.condition
meas_node = self.dag.apply_operation_back(
meas_gate, [self.qubit1], [self.clbit1])
self.assertEqual(meas_node.qargs, [self.qubit1])
self.assertEqual(meas_node.cargs, [self.clbit1])
self.assertEqual(meas_node.condition, meas_gate.condition)
self.assertEqual(
sorted(self.dag._get_multi_graph_in_edges(meas_node._node_id)),
sorted([
(self.dag.input_map[self.qubit1]._node_id, meas_node._node_id,
{'wire': self.qubit1, 'name': 'qr[1]'}),
(self.dag.input_map[self.clbit0]._node_id, meas_node._node_id,
{'wire': self.clbit0, 'name': 'cr[0]'}),
(self.dag.input_map[self.clbit1]._node_id, meas_node._node_id,
{'wire': self.clbit1, 'name': 'cr[1]'}),
]))
self.assertEqual(
sorted(self.dag._get_multi_graph_out_edges(meas_node._node_id)),
sorted([
(meas_node._node_id, self.dag.output_map[self.qubit1]._node_id,
{'wire': self.qubit1, 'name': 'qr[1]'}),
(meas_node._node_id, self.dag.output_map[self.clbit0]._node_id,
{'wire': self.clbit0, 'name': 'cr[0]'}),
(meas_node._node_id, self.dag.output_map[self.clbit1]._node_id,
{'wire': self.clbit1, 'name': 'cr[1]'}),
]))
if self.dag._USE_RX:
self.assertTrue(rx.is_directed_acyclic_graph(self.dag._multi_graph))
else:
self.assertTrue(nx.is_directed_acyclic_graph(self.dag._multi_graph))
示例15: scc_based_to_remove_cycle_edges_iterately
# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import is_directed_acyclic_graph [as 别名]
def scc_based_to_remove_cycle_edges_iterately(g,nodes_score):
from remove_self_loops import remove_self_loops_from_graph
self_loops = remove_self_loops_from_graph(g)
big_sccs = get_big_sccs(g)
scc_nodes_score_dict = scores_of_nodes_in_scc(big_sccs,nodes_score)
edges_to_be_removed = []
if len(big_sccs) == 0:
print("After removal of self loop edgs: %s" % nx.is_directed_acyclic_graph(g))
return self_loops
remove_cycle_edges_by_agony_iterately(big_sccs,scc_nodes_score_dict,edges_to_be_removed)
#print(" # edges to be removed: %d" % len(edges_to_be_removed))
return edges_to_be_removed+self_loops
开发者ID:zhenv5,项目名称:breaking_cycles_in_noisy_hierarchies,代码行数:15,代码来源:remove_cycle_edges_by_hierarchy_greedy.py