本文整理汇总了Python中networkx.classes.digraph.DiGraph.edges方法的典型用法代码示例。如果您正苦于以下问题:Python DiGraph.edges方法的具体用法?Python DiGraph.edges怎么用?Python DiGraph.edges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.classes.digraph.DiGraph
的用法示例。
在下文中一共展示了DiGraph.edges方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_example_3
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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)
示例2: get_example_5
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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)
示例3: get_example_4
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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)
示例4: get_example_6
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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)
示例5: get_variance_example_1
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [as 别名]
def get_variance_example_1():
g = DiGraph()
g.add_edges_from([
(0, 1), (0, 2),
(2, 3), (3, 4),
(2, 'dummy'),
('dummy', 5)
])
g.node['dummy']['dummy'] = True
for n in (0, 1, 2, 5): # topic 1
g.node[n]['repr'] = np.array([0, 0])
for n in (3, 4): # topic 2
g.node[n]['repr'] = np.array([1, 1])
for n in g.nodes_iter():
g.node[n]['r'] = 1
# correct is (0, 1, 2, 5) for cost 0
U = [0, 42]
expected_edge_set = [
set(g.edges()) - {(2, 3), (3, 4)},
set(g.edges())
]
return (g, U, expected_edge_set)
示例6: __init__
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [as 别名]
#.........这里部分代码省略.........
new_node = NodeS(len(self.nodes), nType, node_key, label, real)
self.nodes[node_key] = new_node
self.nodes_id[new_node.id] = new_node
return self.nodes[node_key]
def _get_existed_node(self, key):
node_key = None
if isinstance(key, basestring):
node_key = key
elif isinstance(key, tuple):
node_key = "%s %s %s" % key
else:
logger.error("Unknown instance type of key!!!")
try:
return self.nodes[node_key]
except KeyError:
logger.error("Could not find existed node [%s]!" % node_key)
return None
def export_to_gexf(self) :
buff = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
buff += "<gexf xmlns=\"http://www.gephi.org/gexf\" xmlns:viz=\"http://www.gephi.org/gexf/viz\">\n"
buff += "<graph type=\"static\">\n"
buff += "<attributes class=\"node\" type=\"static\">\n"
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_TYPE, ID_ATTRIBUTES[ ATTR_TYPE ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_CLASS_NAME, ID_ATTRIBUTES[ ATTR_CLASS_NAME ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_METHOD_NAME, ID_ATTRIBUTES[ ATTR_METHOD_NAME ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_DESCRIPTOR, ID_ATTRIBUTES[ ATTR_DESCRIPTOR ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_REAL, ID_ATTRIBUTES[ ATTR_REAL ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_PERM_NAME, ID_ATTRIBUTES[ ATTR_PERM_NAME ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_PERM_LEVEL, ID_ATTRIBUTES[ ATTR_PERM_LEVEL ])
buff += "<attribute title=\"%s\" id=\"%d\" type=\"string\"/>\n" % (ATTR_DEXLOAD_FILENAME, ID_ATTRIBUTES[ ATTR_DEXLOAD_FILENAME ])
buff += "</attributes>\n"
# buff += "<attributes class=\"node\" type=\"static\">\n"
# buff += "<attribute id=\"%d\" title=\"type\" type=\"string\" default=\"normal\"/>\n" % ID_ATTRIBUTES[ "type"]
# buff += "<attribute id=\"%d\" title=\"class_name\" type=\"string\"/>\n" % ID_ATTRIBUTES[ "class_name"]
# buff += "<attribute id=\"%d\" title=\"method_name\" type=\"string\"/>\n" % ID_ATTRIBUTES[ "method_name"]
# buff += "<attribute id=\"%d\" title=\"descriptor\" type=\"string\"/>\n" % ID_ATTRIBUTES[ "descriptor"]
#
#
# buff += "<attribute id=\"%d\" title=\"permissions\" type=\"integer\" default=\"0\"/>\n" % ID_ATTRIBUTES[ "permissions"]
# buff += "<attribute id=\"%d\" title=\"permissions_level\" type=\"string\" default=\"normal\"/>\n" % ID_ATTRIBUTES[ "permissions_level"]
#
# buff += "<attribute id=\"%d\" title=\"dynamic_code\" type=\"boolean\" default=\"false\"/>\n" % ID_ATTRIBUTES[ "dynamic_code"]
#
# buff += "</attributes>\n"
buff += "<nodes>\n"
for node in self.G.nodes() :
buff += "<node id=\"%d\" label=\"%s\">\n" % (node, escape(self.nodes_id[ node ].label))
buff += self.nodes_id[ node ].get_attributes_gexf()
buff += "</node>\n"
buff += "</nodes>\n"
buff += "<edges>\n"
nb = 0
for edge in self.G.edges() :
buff += "<edge id=\"%d\" source=\"%d\" target=\"%d\"/>\n" % (nb, edge[0], edge[1])
nb += 1
buff += "</edges>\n"
buff += "</graph>\n"
buff += "</gexf>\n"
return buff
def get_current_real_node_count(self):
count = 0
for node in self.nodes_id.keys():
if self.nodes_id[node].get_attribute(ATTR_REAL) == "True":
count += 1
return count
def get_current_node_count(self):
return len(self.G.nodes())
def get_current_edge_count(self):
return len(self.G.edges())
def get_current_permission_level_node_count(self, permission_level):
count = 0
for node in self.nodes_id.keys():
if self.nodes_id[node].get_attribute(ATTR_PERM_LEVEL) == permission_level:
count += 1
return count
def get_current_protected_node_count(self):
count = 0
for node in self.nodes_id.keys():
if self.nodes_id[node].get_attribute(ATTR_PERM_LEVEL) != None:
count += 1
return count
示例7: load
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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)
#.........这里部分代码省略.........
示例8: find_MECs
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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
示例9: find_SCCs
# 需要导入模块: from networkx.classes.digraph import DiGraph [as 别名]
# 或者: from networkx.classes.digraph.DiGraph import edges [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