本文整理汇总了Python中networkx.classes.digraph.DiGraph.nodes方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.nodes方法的具体用法?Python DiGraph.nodes怎么用?Python DiGraph.nodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.nodes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DuoBA_from_ltls
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def DuoBA_from_ltls(hard_spec, soft_spec):
hard_buchi = buchi_from_ltl(hard_spec, 'hard_buchi')
soft_buchi = buchi_from_ltl(soft_spec, 'soft_buchi')
hard_symbols = hard_buchi.graph['symbols']
soft_symbols = soft_buchi.graph['symbols']
symbols = set(hard_symbols).union(set(soft_symbols))
DuoBA = DiGraph(type='safe_buchi', hard=hard_buchi, soft=soft_buchi, symols=symbols)
initial = set()
accept = set()
for (h_node, s_node, l) in cartesian_product(hard_buchi.nodes(), soft_buchi.nodes(), [1, 2]):
DuoNode = (h_node, s_node, l)
DuoBA.add_node(DuoNode,hard=h_node, soft=s_node, level=l)
if (h_node in hard_buchi.graph['initial'] and
s_node in soft_buchi.graph['initial'] and l == 1):
initial.add(DuoNode)
if (h_node in hard_buchi.graph['accept'] and l == 1):
accept.add(DuoNode)
DuoBA.graph['accept'] = accept
DuoBA.graph['initial'] = initial
for f_duonode in DuoBA.nodes():
for t_duonode in DuoBA.nodes():
f_h_node, f_s_node, f_level = check_duo_attr(DuoBA, f_duonode)
t_h_node, t_s_node, t_level = check_duo_attr(DuoBA, t_duonode)
if (t_h_node not in DuoBA.graph['hard'].neighbors(f_h_node) or
t_s_node not in DuoBA.graph['soft'].neighbors(f_s_node)):
continue
# relaxed because no common input alphabets are enabled
hardguard = DuoBA.graph['hard'].edges[f_h_node,t_h_node]['guard']
softguard = DuoBA.graph['soft'].edges[f_s_node,t_s_node]['guard']
if ((f_h_node not in DuoBA.graph['hard'].graph['accept'] and
f_level == 1 and t_level == 1) or
(f_h_node in DuoBA.graph['hard'].graph['accept'] and
f_level == 1 and t_level == 2) or
(f_s_node not in DuoBA.graph['soft'].graph['accept'] and
f_level == 2 and t_level == 2) or
(f_s_node in DuoBA.graph['soft'].graph['accept'] and
f_level == 2 and t_level == 1)):
DuoBA.add_edge(f_duonode, t_duonode, hardguard=hardguard, softguard=softguard)
return DuoBA
示例2: BoardGraph
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
class BoardGraph(object):
def walk(self, board, size_limit=maxint):
pending_nodes = []
self.graph = DiGraph()
self.start = board.display(cropped=True)
self.graph.add_node(self.start)
pending_nodes.append(self.start)
self.min_domino_count = len(board.dominoes)
while pending_nodes:
if len(self.graph) >= size_limit:
raise GraphLimitExceeded(size_limit)
state = pending_nodes.pop()
board = Board.create(state, border=1)
dominoes = set(board.dominoes)
self.min_domino_count = min(self.min_domino_count, len(dominoes))
for domino in dominoes:
dx, dy = domino.direction
self.try_move(state, domino, dx, dy, pending_nodes)
self.try_move(state, domino, -dx, -dy, pending_nodes)
self.last = state
return set(self.graph.nodes())
def try_move(self, old_state, domino, dx, dy, pending_states):
try:
new_state = self.move(domino, dx, dy)
move = domino.describe_move(dx, dy)
if not self.graph.has_node(new_state):
# new node
self.graph.add_node(new_state)
pending_states.append(new_state)
self.graph.add_edge(old_state, new_state, move=move)
except BoardError:
pass
def move(self, domino, dx, dy):
""" Move a domino and calculate the new board state.
Afterward, put the board back in its original state.
@return: the new board state
@raise BoardError: if the move is illegal
"""
domino.move(dx, dy)
try:
board = domino.head.board
if not board.isConnected():
raise BoardError('Board is not connected.')
if board.hasLoner():
raise BoardError('Board has a lonely domino.')
return board.display(cropped=True)
finally:
domino.move(-dx, -dy)
示例3: get_example_5
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def get_example_5():
g = DiGraph()
g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4),
(2, 4), (2, 5), (2, 6)])
for s, t in g.edges():
g[s][t]['c'] = 1
g[1][4]['c'] = 0
g[2][4]['c'] = 0
g[2][6]['c'] = 3
for n in g.nodes():
g.node[n]['r'] = 1
g.node[3]['r'] = 10
g.node[4]['r'] = 100
g.node[5]['r'] = 11
U = [10]
# sub-optimal answer actually
expected_edge_set = [[(0, 2), (2, 4), (2, 5), (2, 6)]]
return (g, U, expected_edge_set)
示例4: find_SCCs
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def find_SCCs(mdp, Sneg):
#----simply find strongly connected components----
print 'Remaining states size', len(Sneg)
SCC = set()
simple_digraph = DiGraph()
A = dict()
for s in mdp.nodes():
A[s] = mdp.node[s]['act'].copy()
for s_f in Sneg:
if s_f not in simple_digraph:
simple_digraph.add_node(s_f)
for s_t in mdp.successors_iter(s_f):
if s_t in Sneg:
simple_digraph.add_edge(s_f,s_t)
print "SubGraph of one Sf: %s states and %s edges" %(str(len(simple_digraph.nodes())), str(len(simple_digraph.edges())))
sccs = strongly_connected_component_subgraphs(simple_digraph)
for scc in sccs:
SCC.add(frozenset(scc.nodes()))
return SCC, A
示例5: get_example_4
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def get_example_4():
g = DiGraph()
g.add_edges_from([(0, 1), (1, 2), (2, 3), (2, 14), # tree 1
(2, 15), (3, 16), (3, 17),
(0, 4), (4, 5), (4, 6), # tree 2
(5, 11), (6, 11), (6, 12), (6, 13),
(0, 7), (7, 8), (7, 9), # tree 3
(8, 10), (8, 11), (9, 12), (9, 13)])
for s, t in g.edges():
g[s][t]['c'] = 1
for n in g.nodes():
g.node[n]['r'] = 1
g.node[10]['r'] = 2
U = [7]
expected_edge_set = [
[(0, 7), (7, 8), (7, 9), # tree 3
(8, 10), (8, 11), (9, 12), (9, 13)]
]
return (g, U, expected_edge_set)
示例6: get_example_6
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def get_example_6():
# IN-OPTIMAL CASE
g = DiGraph()
g.add_edges_from([(0, 1), (0, 2), (1, 3),
(1, 4), (2, 4), (2, 5)])
for s, t in g.edges():
g[s][t]['c'] = 0
g[1][3]['c'] = 4
g[1][4]['c'] = 4
g[2][4]['c'] = 2
g[2][5]['c'] = 1
for n in g.nodes():
g.node[n]['r'] = 0
g.node[3]['r'] = 1
g.node[4]['r'] = 100
g.node[5]['r'] = 1
U = [7]
# sub-optimal answer actually
expected_edge_set = [[(0, 2), (2, 4), (2, 5)]]
return (g, U, expected_edge_set)
示例7: get_example_3
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def get_example_3():
"""get a binarized example, whose original graph is
more complicated than the above example
"""
g = DiGraph()
g.add_nodes_from(range(1, 10))
g.add_edges_from([(1, 2), (1, 3), (1, 7),
(2, 4), (2, 5), (2, 6),
(2, 7), (3, 8), (3, 9)])
rewards = range(1, 10)
for r, n in zip(rewards, g.nodes()):
g.node[n]['r'] = r
# all edges have cost 2 except 1 -> 2 and 1 -> 3(cost 1)
for s, t in g.edges():
g[s][t]['c'] = 2
g[1][2]['c'] = 1
g[1][3]['c'] = 1
g = binarize_dag(g,
vertex_weight_key='r',
edge_weight_key='c',
dummy_node_name_prefix='d_')
# parameters and expected output
U = [0, 2, 3, 4, 100]
expected_edges_set = [
[],
[(1, 7)],
[(1, 'd_1'), ('d_1', 3), (3, 9)],
[(1, 'd_1'), ('d_1', 3), (3, 9), ('d_1', 2)],
# (1, 7) removed to make it a tree
list(set(g.edges()) - set([(1, 7)]))
]
return (g, U, expected_edges_set)
示例8: __init__
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
class StadynaMcgAnalysis:
def __init__(self):
self.androGuardObjects = []
self.nodes = {}
self.nodes_id = {}
self.entry_nodes = []
self.G = DiGraph()
# self.internal_methods = []
#self.GI = DiGraph()
def analyseFile(self, vmx, apk):
vm = vmx.get_vm()
self.androGuardObjects.append((apk, vm, vmx))
# self.internal_methods.extend(vm.get_methods())
#creating real internal nodes
internal_called_methods = vmx.get_tainted_packages().stadyna_get_internal_called_methods()
for method in internal_called_methods:
class_name, method_name, descriptor = method
nodeType = None
if method_name == "<clinit>":
nodeType = NODE_STATIC_INIT
elif method_name == "<init>":
nodeType = NODE_CONSTRUCTOR
else:
nodeType = NODE_METHOD
n = self._get_node(nodeType, (class_name, method_name, descriptor))
n.set_attribute(ATTR_CLASS_NAME, class_name)
n.set_attribute(ATTR_METHOD_NAME, method_name)
n.set_attribute(ATTR_DESCRIPTOR, descriptor)
self.G.add_node(n.id)
#creating real edges (nodes are already there)
#currently we are working only with internal packages.
for j in vmx.get_tainted_packages().get_internal_packages():
src_class_name, src_method_name, src_descriptor = j.get_src(vm.get_class_manager())
dst_class_name, dst_method_name, dst_descriptor = j.get_dst(vm.get_class_manager())
n1 = self._get_existed_node((src_class_name, src_method_name, src_descriptor))
# n1.set_attribute(ATTR_CLASS_NAME, src_class_name)
# n1.set_attribute(ATTR_METHOD_NAME, src_method_name)
# n1.set_attribute(ATTR_DESCRIPTOR, src_descriptor)
n2 = self._get_existed_node((dst_class_name, dst_method_name, dst_descriptor))
# n2.set_attribute(ATTR_CLASS_NAME, dst_class_name)
# n2.set_attribute(ATTR_METHOD_NAME, dst_method_name)
# n2.set_attribute(ATTR_DESCRIPTOR, dst_descriptor)
self.G.add_edge(n1.id, n2.id)
#adding fake class nodes
for method in internal_called_methods:
src_class_name, src_method_name, src_descriptor = method
if src_method_name == "<init>" or src_method_name == "<clinit>":
n1 = self._get_existed_node((src_class_name, src_method_name, src_descriptor))
n2 = self._get_node(NODE_FAKE_CLASS, src_class_name, None, False)
n2.set_attribute(ATTR_CLASS_NAME, src_class_name)
if src_method_name == "<clinit>":
self.G.add_edge(n1.id, n2.id)
elif src_method_name == "<init>":
self.G.add_edge(n2.id, n1.id)
#real (external) reflection invoke nodes
reflection_invoke_paths = analysis.seccon_get_invoke_method_paths(vmx)
for j in reflection_invoke_paths:
src_class_name, src_method_name, src_descriptor = j.get_src( vm.get_class_manager() )
dst_class_name, dst_method_name, dst_descriptor = j.get_dst( vm.get_class_manager() )
n1 = self._get_existed_node((src_class_name, src_method_name, src_descriptor))
if n1 == None:
logger.warning("Cannot find the node [%s], where reflection invoke is called!" % (src_class_name, src_method_name, src_descriptor))
continue
key = "%s %s %s %s %s %s %s" % (src_class_name, src_method_name, src_descriptor, dst_class_name, dst_method_name, dst_descriptor, POSTFIX_REFL_INVOKE)
n2 = self._get_node(NODE_REFL_INVOKE, key, LABEL_REFL_INVOKE, True)
n2.set_attribute(ATTR_CLASS_NAME, src_class_name)
n2.set_attribute(ATTR_METHOD_NAME, src_method_name)
n2.set_attribute(ATTR_DESCRIPTOR, src_descriptor)
self.G.add_edge( n1.id, n2.id )
#real (external) reflection new instance nodes
reflection_newInstance_paths = analysis.seccon_get_newInstance_method_paths(vmx)
for j in reflection_newInstance_paths:
src_class_name, src_method_name, src_descriptor = j.get_src( vm.get_class_manager() )
dst_class_name, dst_method_name, dst_descriptor = j.get_dst( vm.get_class_manager() )
n1 = self._get_existed_node((src_class_name, src_method_name, src_descriptor))
if n1 == None:
#.........这里部分代码省略.........
示例9: load
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def load(fname):
def clean_bool(string):
if string == "0":
return None
else:
return string
def to_bool(string):
if string == "1" or string == "True":
return True
elif string == "0" or string == "False":
return False
else:
return string
def to_float(string):
if string == "None":
return None
try:
return float(string)
except:
return string
mode = "node0"
nodes = []
edges = []
volatiles = set()
outputs = None
inputs = None
named_ranges = {}
infile = gzip.GzipFile(fname, 'r')
for line in infile.read().splitlines():
if line == "====":
mode = "node0"
continue
if line == "-----":
cellmap_temp = {n.address(): n for n in nodes}
Range = RangeFactory(cellmap_temp)
mode = "node0"
continue
elif line == "edges":
cellmap = {n.address(): n for n in nodes}
mode = "edges"
continue
elif line == "outputs":
mode = "outputs"
continue
elif line == "inputs":
mode = "inputs"
continue
elif line == "named_ranges":
mode = "named_ranges"
continue
if mode == "node0":
[address, formula, python_expression, is_range, is_named_range, is_volatile, should_eval] = line.split(SEP)
formula = clean_bool(formula)
python_expression = clean_bool(python_expression)
is_range = to_bool(is_range)
is_named_range = to_bool(is_named_range)
is_volatile = to_bool(is_volatile)
should_eval = should_eval
mode = "node1"
elif mode == "node1":
if is_range:
reference = json.loads(line) if is_volatile else line # in order to be able to parse dicts
vv = Range(reference)
if is_volatile:
if not is_named_range:
address = vv.name
volatiles.add(address)
cell = Cell(address, None, vv, formula, is_range, is_named_range, should_eval)
cell.python_expression = python_expression
nodes.append(cell)
else:
value = to_bool(to_float(line))
cell = Cell(address, None, value, formula, is_range, is_named_range, should_eval)
cell.python_expression = python_expression
if formula:
if 'OFFSET' in formula or 'INDEX' in formula:
volatiles.add(address)
cell.compile()
nodes.append(cell)
elif mode == "edges":
source, target = line.split(SEP)
edges.append((cellmap[source], cellmap[target]))
elif mode == "outputs":
outputs = line.split(SEP)
elif mode == "inputs":
inputs = line.split(SEP)
#.........这里部分代码省略.........
示例10: find_MECs
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import nodes [as 别名]
def find_MECs(mdp, Sneg):
#----implementation of Alg.47 P866 of Baier08----
print 'Remaining states size', len(Sneg)
U = mdp.graph['U']
A = dict()
for s in Sneg:
A[s] = mdp.node[s]['act'].copy()
if not A[s]:
print "Isolated state"
MEC = set()
MECnew = set()
MECnew.add(frozenset(Sneg))
#----
k = 0
while MEC != MECnew:
print "<============iteration %s============>" %k
k +=1
MEC = MECnew
MECnew = set()
print "MEC size: %s" %len(MEC)
print "MECnew size: %s" %len(MECnew)
for T in MEC:
R = set()
T_temp = set(T)
simple_digraph = DiGraph()
for s_f in T_temp:
if s_f not in simple_digraph:
simple_digraph.add_node(s_f)
for s_t in mdp.successors_iter(s_f):
if s_t in T_temp:
simple_digraph.add_edge(s_f,s_t)
print "SubGraph of one MEC: %s states and %s edges" %(str(len(simple_digraph.nodes())), str(len(simple_digraph.edges())))
Sccs = strongly_connected_component_subgraphs(simple_digraph)
i = 0
for Scc in Sccs:
i += 1
if (len(Scc.edges())>=1):
for s in Scc.nodes():
U_to_remove = set()
for u in A[s]:
for t in mdp.successors_iter(s):
if ((u in mdp.edge[s][t]['prop'].keys()) and (t not in Scc.nodes())):
U_to_remove.add(u)
A[s].difference_update(U_to_remove)
if not A[s]:
R.add(s)
while R:
s = R.pop()
T_temp.remove(s)
for f in mdp.predecessors(s):
if f in T_temp:
A[f].difference_update(set(mdp.edge[f][s]['prop'].keys()))
if not A[f]:
R.add(f)
New_Sccs = strongly_connected_component_subgraphs(simple_digraph)
j = 0
for Scc in New_Sccs:
j += 1
if (len(Scc.edges()) >= 1):
common = set(Scc.nodes()).intersection(T_temp)
if common:
MECnew.add(frozenset(common))
#---------------
print 'Final MEC and MECnew size:', len(MEC)
return MEC, A