本文整理汇总了Python中networkx.Graph.edges_iter方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.edges_iter方法的具体用法?Python Graph.edges_iter怎么用?Python Graph.edges_iter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类networkx.Graph
的用法示例。
在下文中一共展示了Graph.edges_iter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: edmondskarp
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import edges_iter [as 别名]
def edmondskarp(G: nx.Graph, s, t):
RG = G.copy()
for u,v in G.edges_iter():
G.edge[u][v]['flow'] = 0
path = isthereapath(RG,s,t)
while len(path) > 0:
path_cp = min([RG.edge[u][v]['capacity'] for u,v in path])
for u,v in path:
if G.has_edge(u,v):
G.edge[u][v]['flow'] += path_cp
RG.edge[u][v]['capacity'] -= path_cp
if RG.edge[u][v]['capacity'] == 0:
RG.remove_edge(u,v)
if RG.has_edge(v,u):
RG.edge[v][u]['capacity'] += path_cp
else:
RG.add_edge(v,u,capacity=path_cp)
else:
# then this edge is a "cancelling" flow
# residue should go up and cancelling "capacity" should go down
G.edge[v][u]['flow'] -= path_cp
RG.edge[v][u]['capacity'] += path_cp
RG.edge[u][v]['capacity'] -= path_cp
if RG.edge[u][v]['capacity'] == 0:
RG.remove_edge(u,v)
path = isthereapath(RG,s,t)
return RG
示例2: edges_iter
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import edges_iter [as 别名]
def edges_iter(self, nbunch=None):
for edge in Graph.edges_iter(self, nbunch, data=True):
yield edge
示例3: make_steiner_tree
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import edges_iter [as 别名]
def make_steiner_tree(G, voi, generator=None):
mst = Graph()
for v in voi:
if not v in G:
raise ValueError, "make_steiner_tree(): Some vertice not in original graph"
if len(voi) == 0:
return mst
if len(voi) == 1:
mst.add_node(voi[0])
return mst
# Initially, use (a version of) Kruskal's algorithm to extract a minimal spanning tree
# from a weighted graph. This algorithm differs in that only a subset of vertices are
# going to be present in the final subgraph (which is not truely a MST - must use Prim's
# algorithm later.
# extract all shortest paths among the voi
heapq = []
paths = {}
# load all the paths bwteen the Steiner vertices. Store them in a heap queue
# and reconstruct the MST of the complete graph using Kruskal's algorithm
for i in range(len(voi) - 1):
v1 = voi[i]
for v2 in voi[i+1:]:
result = bidirectional_dijkstra(G, v1, v2)
if result == False:
raise RuntimeError, "The two vertices given (%s, %s) don't exist on the same connected graph" % (v1, v2)
#print "The two vertices given (%s, %s) don't exist on the same connected graph" % (v1, v2)
distance, vertList = result
keys = [v1, v2]
keys.sort()
key = "%s:%s" % tuple(keys)
paths[key] = (vertList)
heappush(heapq, (distance, v1, v2))
# construct the minimum spanning tree of the complete graph
while heapq:
w, v1, v2 = heappop(heapq)
# if no path exists yet between v1 and v2, add this one
if v1 not in mst or v2 not in mst or not has_path(mst, v1, v2):
mst.add_edge(v1, v2,weight=w)
# check if the graph is tree and correct
sTree = set(mst.nodes())
sSteiner = set(voi)
if sTree ^ sSteiner:
raise RuntimeError, 'Failed to construct MST spanning tree'
# reconstruct subgraph of origGraph using the paths
if generator is None:
subgraph = Graph()
else:
subgraph = generator()
for edge in mst.edges_iter(data=True):
keys = [edge[0],edge[1]]
keys.sort()
key = "%s:%s" % tuple(keys)
vList = paths[key]
for i in range(len(vList) - 1):
v1 = vList[i]
v2 = vList[i+1]
w = G[v1][v2]
subgraph.add_edge(v1, v2, w)
# get rid of possible loops - result will be a true MST
subgraph = make_prim_mst(subgraph, generator)
# remove intermediate nodes in paths that are not in list of voi
return _trimTree(subgraph, voi)
示例4: Network
# 需要导入模块: from networkx import Graph [as 别名]
# 或者: from networkx.Graph import edges_iter [as 别名]
#.........这里部分代码省略.........
self.directed = int(directed)
self.hierarchical = int(hierarchical)
self.hypergraph = int(hypergraph)
if src is None and not pickled_graph is None:
self.load_pickled_graphml(pickled_graph)
else:
if not src is None:
# generates NetworkX Graph
self.graph = self.parse_network_graphml(src)
elif not graph is None:
self.graph = graph
else:
if self.directed:
from networkx import DiGraph
self.graph = DiGraph()
logger.info("Initialize with empty directed Graph")
else:
from networkx import Graph
self.graph = Graph()
logger.info("Initialize with empty undirected Graph")
# initializes the weight key of the graph
# with the first edgekey
if len(self.edgekeys) > 0:
edgk = self.edgekeys.keys()
if not 'weight' in edgk:
self.set_weight_key(edgk[0])
else:
# try grabbing first edge from the graph
if self.graph.number_of_edges() > 0:
it = self.graph.edges_iter(data=True)
edg = it.next()
if len(edg[2]) > 0:
# if it has a weigth key, just leave it
edgk = edg[2].keys()
if not 'weight' in edgk:
self.set_weight_key(edgk[0])
else:
pass
# logger.error('Cannot set weight key for network : ' + self.networkname)
def _name_default(self):
return self.networkname
def _active_default(self):
return False
def _active_changed(self , value):
if value:
n = self.name
if ' [Active]' not in n:
self.name = "%s [Active]" % n
# XXX: do refactor with threaded loading of surfaces
# and default spring force layout for graph rendering!
# see also TraitsUI Demos: Multi thread demo
# load the surface containers data
# make a deep copy of the already loaded surface containers
import copy
self.surfaces = copy.deepcopy(self.surfaces_loaded)
for surfcont in self.surfaces:
surfcont.load_surface_container()