本文整理汇总了Python中networkx.topological_sort函数的典型用法代码示例。如果您正苦于以下问题:Python topological_sort函数的具体用法?Python topological_sort怎么用?Python topological_sort使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了topological_sort函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_edges_from
def add_edges_from(self, ebunch, attr_dict=None, **attr):
"""Add all the edges in ebunch.
Parameters
----------
ebunch : container of edges
Each edge given in the container will be added to the
graph. The edges must be given as as 2-tuples (u,v) or
3-tuples (u,v,d) where d is a dictionary containing edge
data.
attr_dict : dictionary, optional (default= no attributes)
Dictionary of edge attributes. Key/value pairs will
update existing data associated with each edge.
attr : keyword arguments, optional
Edge data (or labels or objects) can be assigned using
keyword arguments.
Enusres No cycles are in this directed graph (DAG)
"""
# set up attribute dict
if attr_dict is None:
attr_dict = attr
else:
try:
attr_dict.update(attr)
except AttributeError:
raise NetworkXError("The attr_dict argument must be a dict.")
# process ebunch
for e in ebunch:
ne = len(e)
if ne == 3:
u, v, dd = e
assert hasattr(dd, "update")
elif ne == 2:
u, v = e
dd = {}
else:
raise NetworkXError("Edge tuple %s must be a 2-tuple or 3-tuple." % (e,))
if u not in self.succ:
self.succ[u] = {}
self.pred[u] = {}
self.node[u] = {}
if v not in self.succ:
self.succ[v] = {}
self.pred[v] = {}
self.node[v] = {}
datadict = self.adj[u].get(v, {})
datadict.update(attr_dict)
datadict.update(dd)
self.succ[u][v] = datadict
self.pred[v][u] = datadict
# make sure it's a DAG
try:
networkx.topological_sort(self)
except networkx.NetworkXUnfeasible:
self.remove_edge(u, v)
raise networkx.NetworkXUnfeasible(
"Graph contains a cycle! DAG is acyclic! Edge " + u + "-" + v + " cannot be added."
)
示例2: evaluate
def evaluate(self, x):
# XXX: this is a massive hack
x = x[::-1]
assert self.circuit
g = self.circuit.copy()
for node in nx.topological_sort(g):
if 'gate' not in g.node[node]:
if g.node[node]['label'][0].startswith('x'):
g.add_node(node, value=int(x[node]))
else:
g.add_node(node, value=int(g.node[node]['value']))
elif g.node[node]['gate'] in ('ADD', 'MUL', 'SUB'):
keys = g.pred[node].keys()
if len(keys) == 1:
idx1 = idx2 = keys[0]
else:
assert(len(keys) == 2)
idx1 = g.pred[node].keys()[0]
idx2 = g.pred[node].keys()[1]
if g.node[node]['gate'] == 'ADD':
value = g.node[idx1]['value'] + g.node[idx2]['value']
elif g.node[node]['gate'] == 'SUB':
value = g.node[idx1]['value'] - g.node[idx2]['value']
elif g.node[node]['gate'] == 'MUL':
value = g.node[idx1]['value'] * g.node[idx2]['value']
g.add_node(node, value=value)
else:
raise Exception('Unable to evaluate')
idx = nx.topological_sort(g)[-1]
return g.node[idx]['value'] != 0
示例3: evaluate
def evaluate(self, x):
# XXX: this is a massive hack
x = x[::-1]
assert self.circuit
g = self.circuit.copy()
for node in nx.topological_sort(g):
if "gate" not in g.node[node]:
if g.node[node]["label"][0].startswith("x"):
g.add_node(node, value=int(x[node]))
else:
g.add_node(node, value=int(g.node[node]["value"]))
elif g.node[node]["gate"] in ("ADD", "MUL"):
keys = g.pred[node].keys()
if len(keys) == 1:
idx1 = idx2 = keys[0]
else:
assert len(keys) == 2
idx1 = g.pred[node].keys()[0]
idx2 = g.pred[node].keys()[1]
if g.node[node]["gate"] == "ADD":
value = g.node[idx1]["value"] + g.node[idx2]["value"]
elif g.node[node]["gate"] == "MUL":
value = g.node[idx1]["value"] * g.node[idx2]["value"]
g.add_node(node, value=value)
else:
raise Exception("Unable to evaluate")
idx = nx.topological_sort(g)[-1]
return g.node[idx]["value"] != 0
示例4: ensure_dependencies
def ensure_dependencies(request):
r"""
CommandLine:
python -m dtool.base --exec-BaseRequest.ensure_dependencies
Example:
>>> # ENABLE_DOCTEST
>>> from dtool.base import * # NOQA
>>> from dtool.example_depcache import testdata_depc
>>> depc = testdata_depc()
>>> request = depc.new_request('vsmany', [1, 2], [2, 3, 4])
>>> request.ensure_dependencies()
"""
import networkx as nx
depc = request.depc
if False:
dependencies = nx.ancestors(depc.graph, request.tablename)
subgraph = depc.graph.subgraph(set.union(dependencies, {request.tablename}))
dependency_order = nx.topological_sort(subgraph)
root = dependency_order[0]
[nx.algorithms.dijkstra_path(subgraph, root, start)[:-1] +
nx.algorithms.dijkstra_path(subgraph, start, request.tablename)
for start in dependency_order]
graph = depc.graph
root = list(nx.topological_sort(graph))[0]
edges = graph.edges()
#parent_to_children = ut.edges_to_adjacency_list(edges)
child_to_parents = ut.edges_to_adjacency_list([t[::-1] for t in edges])
to_root = {request.tablename:
ut.paths_to_root(request.tablename, root, child_to_parents)}
from_root = ut.reverse_path(to_root, root, child_to_parents)
dependency_levels_ = ut.get_levels(from_root)
dependency_levels = ut.longest_levels(dependency_levels_)
true_order = ut.flatten(dependency_levels)[1:-1]
#print('[req] Ensuring %s request dependencies: %r' % (request, true_order,))
ut.colorprint(
'[req] Ensuring request %s dependencies: %r' % (request, true_order,), 'yellow')
for tablename in true_order:
table = depc[tablename]
if table.ismulti:
pass
else:
# HACK FOR IBEIS
all_aids = ut.flat_unique(request.qaids, request.daids)
depc.get_rowids(tablename, all_aids)
pass
pass
#zip(depc.get_implicit_edges())
#zip(depc.get_implicit_edges())
#raise NotImplementedError('todo')
#depc = request.depc
#parent_rowids = request.parent_rowids
#config = request.config
#rowid_dict = depc.get_all_descendant_rowids(
# request.tablename, root_rowids, config=config)
pass
示例5: test_nbunch_argument
def test_nbunch_argument(self):
G=nx.DiGraph()
G.add_edges_from([(1,2), (2,3), (1,4), (1,5), (2,6)])
assert_equal(nx.topological_sort(G), [1, 2, 3, 6, 4, 5])
assert_equal(nx.topological_sort_recursive(G), [1, 5, 4, 2, 6, 3])
assert_equal(nx.topological_sort(G,[1]), [1, 2, 3, 6, 4, 5])
assert_equal(nx.topological_sort_recursive(G,[1]), [1, 5, 4, 2, 6, 3])
assert_equal(nx.topological_sort(G,[5]), [5])
assert_equal(nx.topological_sort_recursive(G,[5]), [5])
示例6: _compute_lattice_monotonicity
def _compute_lattice_monotonicity(self, reds, pis):
"""
Infer the redundancy and partial information of lattice elements via lattice monotonicity.
Parameters
----------
reds : dict
Currently known redundancy values.
pis : dict
Currently known partial information values.
Returns
-------
reds : dict
Updated redundancy values.
pis : dict
Updated partial information values.
"""
# everything below a redundancy of 0 is a redundancy of 0
nodes = list(nx.topological_sort(self._lattice))
while nodes:
node = nodes.pop(0)
if node in reds and np.isclose(0, reds[node]):
for n in descendants(self._lattice, node):
if n not in reds:
reds[n] = 0
nodes.remove(n)
# everything above a redundancy of I(inputs, output) is I(inputs, output)
nodes = list(reversed(list(nx.topological_sort(self._lattice))))
while nodes:
node = nodes.pop(0)
if node in reds and np.isclose(reds[node], self._total):
for n in ascendants(self._lattice, node):
if n not in reds:
reds[n] = self._total
nodes.remove(n)
# if redundancy of A == redundancy of B, then for all A -> C -> B, redundancy of C = redundancy of A, B
tops = [node for node in self._lattice if node in reds and any((n not in reds) for n in self._lattice[node])]
bottoms = [node for node in self._lattice if
node in reds and any((n not in reds) for n in self._lattice.reverse()[node])]
for top, bottom in product(tops, bottoms):
if np.isclose(reds[top], reds[bottom], atol=1e-5, rtol=1e-5):
for path in nx.all_simple_paths(self._lattice, top, bottom):
for node in path[1:-1]:
if node not in reds:
reds[node] = reds[top]
# if redundancy of A is equal to the redundancy any of A's children, then pi(A) = 0
for node in self._lattice:
if node not in pis:
if node in reds and all(n in reds for n in self._lattice[node]) and self._lattice[node]:
if any(np.isclose(reds[n], reds[node], atol=1e-5, rtol=1e-5) for n in self._lattice[node]):
pis[node] = 0
return reds, pis
示例7: attempt_edge
def attempt_edge(graph, a, b):
a_gate, a_port = a
b_gate, b_port = b
graph.add_edge(a_gate, b_gate)
try:
nx.topological_sort(graph)
except nx.NetworkXUnfeasible:
graph.remove_edge(a_gate, b_gate)
broken_up_links.append((a, b))
示例8: main
def main():
log = logging.getLogger(__name__)
logging.basicConfig(level = logging.INFO)
backend = foradage.RECAST_Backend(2)
g = adage.mk_dag()
global_context = {
'workdir':'/Users/lukas/Code/code-snippets/cap-schema-drafts/steer',
'dataset':'user15.lheinric.p123/'
}
steps_graph = nx.DiGraph()
workflow = json.load(open('capdata/workflow.json'))
for step in workflow:
steps_graph.add_node(step['name'],step)
for x in step['dependencies']:
steps_graph.add_edge(x,step['name'])
rules = {}
for stepname in nx.topological_sort(steps_graph):
stepinfo = steps_graph.node[stepname]
rule = foradage.RECAST_Rule(stepinfo,workflow,rules,global_context)
rules[stepname] = rule
adage.rundag(g,rules.values(), track = True, backend = backend)
provgraph = nx.DiGraph()
for x in nx.topological_sort(g):
attr = g.node[x].copy()
attr.update(color = 'red',label = g.getNode(x).name)
provgraph.add_node(x,attr)
nodeinfo = g.getNode(x).task.node
if 'used_inputs' in nodeinfo:
for k,inputs_from_node in nodeinfo['used_inputs'].iteritems():
for one in inputs_from_node:
depname = 'output_{}_{}_{}'.format(k,one[0],one[1])
provgraph.add_edge(depname,x)
else:
for pre in g.predecessors(x):
provgraph.add_edge(pre,x)
for k,v in g.getNode(x).result_of()['RECAST_metadata']['outputs'].iteritems():
for i,y in enumerate(v):
name = 'output_{}_{}_{}'.format(g.getNode(x).task.node['name'],k,i)
provgraph.add_node(name,{'shape':'box','label':'{}_{}'.format(k,i),'color':'blue'})
provgraph.add_edge(x,name)
nx.write_dot(provgraph,'workflow_instance.dot')
subprocess.call(['dot','-Tpdf','workflow_instance.dot'], stdout = open('workflow_instance.pdf','w'))
nx.write_dot(steps_graph,'steps.dot')
subprocess.call(['dot','-Tpdf','steps.dot'], stdout = open('steps.pdf','w'))
示例9: test_topological_sort2
def test_topological_sort2(self):
DG = nx.DiGraph({1: [2], 2: [3], 3: [4],
4: [5], 5: [1], 11: [12],
12: [13], 13: [14], 14: [15]})
assert_raises(nx.NetworkXUnfeasible, consume, nx.topological_sort(DG))
assert_false(nx.is_directed_acyclic_graph(DG))
DG.remove_edge(1, 2)
consume(nx.topological_sort(DG))
assert_true(nx.is_directed_acyclic_graph(DG))
示例10: __init__
def __init__(self, equations):
# Everythings must be an equation
self.equations = equations
self.equations_by_name = {}
for eq in equations:
if not isinstance(eq, Equation):
raise RuntimeError("Non Equation found.")
if eq.name in equations:
raise RuntimeError("Equations names must be unique within a graph")
self.equations_by_name[eq.name] = eq
# Make a network from this. The first is the full network of both
# equations and variables (a bipartite graph). The second is just the
# network of causal variables (the project of the bipartite graph).
full_network = nx.DiGraph()
causal_network = nx.DiGraph()
for p in equations:
for i in p.inputs:
full_network.add_edge(i, p)
for o in p.outputs:
full_network.add_edge(p, o)
for i in p.inputs:
for o in p.outputs:
causal_network.add_edge(i, o)
self.full_network = full_network
self.causal_network = causal_network
# Nodes are either inputs, outputs, or inner
self.inputs = set()
self.outputs = set()
self.inner = set()
for n in self.causal_network.nodes():
preds = self.full_network.predecessors(n)
sucs = self.full_network.successors(n)
if not preds:
self.inputs.add(n)
if not sucs:
self.outputs.add(n)
if preds and sucs:
self.inner.add(n)
# Sort all nodes into topological order. This allows us to calculate
# the probabilities across the nodes in the right order, so that the
# inputs for each player are always calculated in time (by previous
# equations).
self.ordered_variables = nx.topological_sort(self.causal_network)
self.ordered_nodes = nx.topological_sort(self.full_network)
self.graphviz_prettify(self.full_network)
self.graphviz_prettify(self.causal_network)
示例11: add_edge
def add_edge(self, u, v, attr_dict=None, **attr):
"""Add an edge between u and v.
The nodes u and v will be automatically added if they are
not already in the graph.
Edge attributes can be specified with keywords or by providing
a dictionary with key/value pairs. See examples below.
Ensures no cycle are introduced in the graph.
Parameters
----------
u,v : nodes
Nodes can be, for example, strings or numbers.
Nodes must be hashable (and not None) Python objects.
attr_dict : dictionary, optional (default= no attributes)
Dictionary of edge attributes. Key/value pairs will
update existing data associated with the edge.
attr : keyword arguments, optional
Edge data (or labels or objects) can be assigned using
keyword arguments.
"""
# set up attribute dict
if attr_dict is None:
attr_dict = attr
else:
try:
attr_dict.update(attr)
except AttributeError:
raise NetworkXError("The attr_dict argument must be a dictionary.")
# add nodes
if u not in self.succ:
self.succ[u] = {}
self.pred[u] = {}
self.node[u] = {}
if v not in self.succ:
self.succ[v] = {}
self.pred[v] = {}
self.node[v] = {}
# add the edge
datadict = self.adj[u].get(v, {})
datadict.update(attr_dict)
self.succ[u][v] = datadict
self.pred[v][u] = datadict
# make sure it's a DAG
try:
networkx.topological_sort(self)
except networkx.NetworkXUnfeasible:
self.remove_edge(u, v)
raise networkx.NetworkXUnfeasible("Graph contains a cycle! DAG is acyclic!")
示例12: test_topological_sort1
def test_topological_sort1(self):
DG=nx.DiGraph()
DG.add_edges_from([(1,2),(1,3),(2,3)])
assert_equal(nx.topological_sort(DG),[1, 2, 3])
assert_equal(nx.topological_sort_recursive(DG),[1, 2, 3])
DG.add_edge(3,2)
assert_raises(nx.NetworkXUnfeasible, nx.topological_sort, DG)
assert_raises(nx.NetworkXUnfeasible, nx.topological_sort_recursive, DG)
DG.remove_edge(2,3)
assert_equal(nx.topological_sort(DG),[1, 3, 2])
assert_equal(nx.topological_sort_recursive(DG),[1, 3, 2])
示例13: __delitem__
def __delitem__(self, key):
#print "__delitem__(" + str(key) + ")__"
#a special case: self
if key in ["self", "key", "value", "col", "row"]:
raise KeyError
# if we do not have the key, then we leave
if key not in self.state:
raise KeyError
# we remove the key
#self.G.remove_node(key)
del self.formulas[key]
del self.values[key]
#del self.mode[key]
#del self.state[key]
# we call the callbacks
for i in self.callbacks:
try:
i("delete", key)
except Exception as e:
print "callback delete " + key
print "callback :=" + str(i)
print "error: " + str(e)
pass
for i in self.named_callbacks:
try:
self.__getitem__(i)(self, "delete", key)
except Exception as e:
print "callback delete " + key
print "callback :=" + str(i)
print "error: " + str(e)
pass
# we mark all successor as dirty
for i in nx.topological_sort(self.G, [key]):
if i <> key:
#print str(key) + " -> " + str(i)
self.state[i] = 0
self.update(i)
# if there is not successor, we remove the node, and mode and state
if nx.topological_sort(self.G, [key]) == [key]:
self.G.remove_node(key)
del self.mode[key]
del self.state[key]
示例14: _nodes
def _nodes(self, order=None):
"""
returns all nodes in the graph
"""
if order is None:
node_names = self.name_to_node.keys()
elif order == "architecture":
node_names = nx.topological_sort(self.architectural_tree)
elif order == "computation":
node_names = nx.topological_sort(self.computation_graph)
else:
raise ValueError("Unknown order: %s" % order)
# make sure that all of the original nodes are returned
assert set(self.name_to_node.keys()) == set(node_names)
return [self.name_to_node[name] for name in node_names]
示例15: binarize_dag
def binarize_dag(g,
vertex_weight_key,
edge_weight_key,
dummy_node_name_prefix='d_'):
g = g.copy() # be functional
dummy_node_counter = 1
for u in nx.topological_sort(g):
nbs = g.neighbors(u)
while len(nbs) > 2:
for p_1, p_2 in chunks(nbs, 2):
v = "{}{}".format(
dummy_node_name_prefix,
dummy_node_counter
)
g.add_node(v)
g.node[v]['dummy'] = True
g.add_edge(u, v)
g.add_edges_from([(v, p_1),
(v, p_2)])
g.node[v][vertex_weight_key] = 0
g[v][p_1][edge_weight_key] = g[u][p_1][edge_weight_key]
g[v][p_2][edge_weight_key] = g[u][p_2][edge_weight_key]
g[u][v][edge_weight_key] = 0
g.remove_edges_from([(u, p_1), (u, p_2)])
dummy_node_counter += 1
nbs = g.neighbors(u)
return g