本文整理汇总了Python中graph_tool.Graph.edge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.edge方法的具体用法?Python Graph.edge怎么用?Python Graph.edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph_tool.Graph
的用法示例。
在下文中一共展示了Graph.edge方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RoadMap
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edge [as 别名]
class RoadMap(object):
def __init__(self, mapfile):
self._mapfile = mapfile
self.DIRECTION_index = 6
self.PATHCLASS_index = 20
self.g = Graph()
self.g.edge_properties["length"] = self.g.new_edge_property("double")
self.g.edge_properties["level"] = self.g.new_edge_property("int")
self.g.vertex_properties["pos"] = self.g.new_vertex_property("vector<double>")
self.cross_pos_index = {}
def load(self):
if self._mapfile[-3:] != 'shp':
self.g = load_graph(self._mapfile)
return
try:
sf = shapefile.Reader(self._mapfile)
except Exception as e:
print(str(e))
return False
roads_records = sf.shapeRecords() # 获取路段信息'
for road_record in roads_records:
cross_s_index = self.add_cross(road_record.shape.points[0])
cross_e_index = self.add_cross(road_record.shape.points[-1])
self.add_road_edge(cross_s_index, cross_e_index, road_record)
if int(road_record.record[self.DIRECTION_index]) == 0: # 若路段是双向车道
self.add_road_edge(cross_e_index, cross_s_index, road_record)
return True
def has_edge(self, s_vertex, e_vertex):
if self.g.num_vertices() >= max(s_vertex, e_vertex):
return self.g.edge(s_vertex, e_vertex)
else:
return None
def add_cross(self, cross_pos):
if cross_pos in self.cross_pos_index:
return self.cross_pos_index.get(cross_pos)
else:
cross_index = self.g.add_vertex()
self.g.vp.pos[cross_index] = cross_pos
self.cross_pos_index[cross_pos] = cross_index
return cross_index
def add_road_edge(self, s_vertex, e_vertex, road):
if self.has_edge(s_vertex, e_vertex):
return self.g.edge(s_vertex, e_vertex)
else:
edge = self.g.add_edge(s_vertex, e_vertex)
self.g.ep.level[edge] = int(road.record[self.PATHCLASS_index])
self.g.ep.length[edge] = self.road_length(road)
return edge
@staticmethod
def road_length(road):
length = 0
for sub_road in zip(road.shape.points[:-1], road.shape.points[1:]):
length += distance.euclidean(sub_road[0], sub_road[1])
return length
示例2: ring
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edge [as 别名]
def ring(num_vtx=100, k=2, p=0.0):
g = Graph(directed=False)
vtx = list(g.add_vertex(num_vtx))
# connect neighbors
for i in vtx:
for j in xrange(1, k + 1):
dest = g.vertex((g.vertex_index[i] - j) % num_vtx)
if g.edge(i, dest) is None:
g.add_edge(i, dest)
# redirect edges
# old_edges = list(g.edges())
old_edges = [(x.source(), x.target()) for x in g.edges()]
for i in old_edges:
n = random.random()
if n < p: # redirect edge; choose random vertex as new destination
vtx_tmp = vtx[:]
vtx_tmp.remove(i[1])
if i[0] in vtx_tmp:
vtx_tmp.remove(i[0])
dest = random.choice(vtx_tmp)
while g.edge(i[0], dest) is not None:
vtx_tmp.remove(dest)
dest = random.choice(vtx_tmp)
g.remove_edge(g.edge(i[0], i[1]))
g.add_edge(i[0], dest)
return g
示例3: parse_graph_from_string
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edge [as 别名]
def parse_graph_from_string(self, graphML_string):
dom = minidom.parseString(graphML_string)
root = dom.getElementsByTagName("graphml")[0]
graph = root.getElementsByTagName("graph")[0]
name = graph.getAttribute('id')
g = Graph(directed=False)
vpos=g.new_vertex_property("vector<double>")
for node in graph.getElementsByTagName("node"):
id=node.getAttribute('id')
n = g.add_vertex()
g.vertex_index[id]
#right now only the positions are available
for attr in node.getElementsByTagName("data"):
if attr.firstChild:
key=attr.getAttribute("key")
#n[key] = attr.firstChild.data
if(key=="x"):
x=attr.firstChild.data
elif(key=="y"):
y=attr.firstChild.data
vpos[id]=(x,y)
g.vertex_properties["pos"]=vpos
#have to workaround the directed graph written by the server
for edge in graph.getElementsByTagName("edge"):
source = edge.getAttribute('source')
dest = edge.getAttribute('target')
edge=g.edge(dest,source)
if(edge==None):
e = g.add_edge(source, dest)
return g
示例4: range
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edge [as 别名]
counter = 0
for seed_edge in src.out_edges():
if g.ep.edge_flow[seed_edge] == 1:
counter += 1
for i in range(counter):
path = []
current = src
path.append(src)
s.push(src)
while (not(current == tgt)) and (not(s.isEmpty())):
for edge in current.out_edges():
if g.ep.edge_flow[edge] == 1 and g.ep.residual_capacity[edge] == 1:
s.push(edge.target())
current = s.pop()
g.ep.residual_capacity[g.edge(path[-1], current)] = 0
g.ep.residual_capacity[g.edge(current, path[-1])] = 0
path.append(current)
paths.append(path)
s.allClear()
temp_list = []
for each_path in paths:
temp_list.append([int(x) for x in each_path])
frequency_n_paths[g.gp.layer_name] = temp_list
paths = []
max_flow = sum(g.ep.edge_flow[e] for e in tgt.in_edges())