本文整理汇总了Python中graph_tool.Graph.edges方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.edges方法的具体用法?Python Graph.edges怎么用?Python Graph.edges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph_tool.Graph
的用法示例。
在下文中一共展示了Graph.edges方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ring
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edges [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
示例2: _filter_short_branch
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edges [as 别名]
def _filter_short_branch(self, filter=False, short=30):
"""
filter out very short branches: do this maybe not right for some models, for models with flat part, it is right
I will test how this effect the final matching results
need to delete nodes, switch with the last one then delete last
"""
if filter == False:
self.verts = self.verts_init
self.edges = self.edges_init
else:
init_graph = Graph(directed=False)
init_graph.add_vertex(len(self.verts_init))
for edge in self.edges_init:
init_graph.add_edge(init_graph.vertex(edge[0]), init_graph.vertex(edge[1]))
terminal_node = []
for v in init_graph.vertices():
if v.out_degree() == 1:
terminal_node.append(v)
visitor = DepthVisitor()
short_nodes = []
for tn in terminal_node:
search.dfs_search(init_graph, tn, visitor)
tmp_node = visitor.get_short_branch(min_length=short)
visitor.reset()
for n in tmp_node:
short_nodes.append(n)
## get edges on the short paths
short_nodes = list(set(short_nodes))
short_edges = []
temp_verts = self.verts_init[:]
v_num = len(self.verts_init)
if len(short_nodes):
for v in reversed(sorted(short_nodes)):
for ve in init_graph.vertex(v).out_edges():
short_edges.append(ve)
## delete edges first, then vertex
short_edges = list(set(short_edges))
for e in short_edges:
init_graph.remove_edge(e)
print 'deleting vertex',
for v in reversed(sorted(short_nodes)):
print v,
temp_verts[int(v)] = temp_verts[v_num-1]
init_graph.remove_vertex(v, fast=True)
v_num -= 1
print '\ndeleting related edges' # already done above, just info user
else:
print 'no short branches'
######## new vertices and edges ########
self.verts = temp_verts[:v_num]
self.edges = []
for e in init_graph.edges():
self.edges.append([int(e.source()), int(e.target())])
示例3: str
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edges [as 别名]
alternate_path = child_graph.new_edge_property("int")
flag_path = child_graph.new_edge_property("int")
## Property Assignment
child_graph.gp.layer_name = graph_name
child_graph.ep.edge_capacity = layer_capacities
child_graph.ep.residual_capacity = layer_res_capacity
child_graph.ep.edge_flow = layer_flow
child_graph.ep.shared_path = alternate_path
child_graph.ep.path_flag = flag_path
## Setting the name of the graph
child_graph.gp.layer_name = "Layer_" + str(i)
## For finding the total number of shared edges in use ##
for e in child_graph.edges():
child_graph.ep.shared_path[e] = 1
## Adding layered graphs to a list ##
all_child_graphs.append(child_graph)
######## Variables and instances to be used for finding the primary and the alternate paths ########
paths = []
alternative_paths =[]
s = Stack()
routes_in_use = {}
all_requests_n_paths = {}
substitute_paths = {}
示例4: not
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edges [as 别名]
break
if not(request in all_requests):
all_requests.append(request)
#print("Number of requests are " + str(len(all_requests)))
## Defining the graph properties ##
graph_weight = g.new_edge_property("float")
g.ep.weight = graph_weight
graph_pred_tree = g.new_vertex_property("int")
pred_tree = graph_pred_tree
edges_logger = {}
for e in g.edges():
flags_of_edges = []
# Temporary flag to ensure that alternative path is not on the primary path itself
flags_of_edges.append(1)
# Flags to see which channels are currently in use
for i in range(number_frequency_bands):
flags_of_edges.append(1)
# Flags to keep record of the extent of the usage of a particular channel in a link
for i in range(number_frequency_bands):
flags_of_edges.append(0)
# Flags to fulfil the single point failure protection between those who share their primary paths
for i in range(number_frequency_bands):
flags_of_edges.append(1)
edges_logger[str(e.source()) + " --> " + str(e.target())] = flags_of_edges
edges_logger[str(e.target()) + " --> " + str(e.source())] = flags_of_edges
g.ep.weight[e] = 0
示例5: BoardGraphGraphtool
# 需要导入模块: from graph_tool import Graph [as 别名]
# 或者: from graph_tool.Graph import edges [as 别名]
class BoardGraphGraphtool(BoardGraphBase):
def __init__(self, number_of_vertices, graph_type):
super().__init__(number_of_vertices, graph_type)
# Graph tool creates directed multigraph by default.
self._graph = Graph()
self._graph.add_vertex(number_of_vertices)
self._graph.vertex_properties["cell"] = self._graph.new_vertex_property(
"object", number_of_vertices * [BoardCell()]
)
self._graph.edge_properties["direction"
] = self._graph.new_edge_property("object")
self._graph.edge_properties["weight"
] = self._graph.new_edge_property("int")
def __getitem__(self, position):
return self._graph.vp.cell[self._graph.vertex(position)]
def __setitem__(self, position, board_cell):
self._graph.vp.cell[self._graph.vertex(position)] = board_cell
def __contains__(self, position):
return position in range(0, self.vertices_count())
def vertices_count(self):
return self._graph.num_vertices()
def edges_count(self):
return self._graph.num_edges()
def has_edge(self, source_vertice, target_vertice, direction):
for e in self._graph.vertex(source_vertice).out_edges():
if (
int(e.target()) == target_vertice and
self._graph.ep.direction[e] == direction
):
return True
return False
def out_edges_count(self, source_vertice, target_vertice):
return len([
1 for e in self._graph.vertex(source_vertice).out_edges()
if int(e.target()) == target_vertice
])
def reconfigure_edges(self, width, height, tessellation):
"""
Uses tessellation object to create all edges in graph.
"""
self._graph.clear_edges()
for source_vertice in self._graph.vertices():
for direction in tessellation.legal_directions:
neighbor_vertice = tessellation.neighbor_position(
int(source_vertice),
direction,
board_width=width,
board_height=height
)
if neighbor_vertice is not None:
e = self._graph.add_edge(
source_vertice, neighbor_vertice, add_missing=False
)
self._graph.ep.direction[e] = direction
# TODO: Faster version?
# def reconfigure_edges(self, width, height, tessellation):
# """
# Uses tessellation object to create all edges in graph.
# """
# self._graph.clear_edges()
# edges_to_add = []
# directions_to_add = dict()
# for source_vertice in self._graph.vertices():
# for direction in tessellation.legal_directions:
# neighbor_vertice = tessellation.neighbor_position(
# int(source_vertice), direction,
# board_width=width, board_height=height
# )
# if neighbor_vertice is not None:
# edge = (int(source_vertice), neighbor_vertice,)
# edges_to_add.append(edge)
# if edge not in directions_to_add:
# directions_to_add[edge] = deque()
# directions_to_add[edge].append(direction)
# self._graph.add_edge_list(edges_to_add) if edges_to_add else None
# for e in edges_to_add:
# e_descriptors = self._graph.edge(
# s = self._graph.vertex(e[0]),
# t = self._graph.vertex(e[1]),
# all_edges = True
# )
# for e_descriptor in e_descriptors:
# if len(directions_to_add[e]) > 0:
# self._graph.ep.direction[e_descriptor] = directions_to_add[e][0]
#.........这里部分代码省略.........