當前位置: 首頁>>代碼示例>>Python>>正文


Python Graph.edge方法代碼示例

本文整理匯總了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
開發者ID:elvis2els,項目名稱:map,代碼行數:62,代碼來源:gt_roadmap.py

示例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
開發者ID:kroq-gar78,項目名稱:BigGraphAnalysis,代碼行數:32,代碼來源:graph_generate.py

示例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
開發者ID:kwsamarasinghe,項目名稱:GraphSimViz,代碼行數:40,代碼來源:graphml_parser.py

示例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())
開發者ID:sanjaythakur,項目名稱:RoutingNWavelengthAssignment,代碼行數:32,代碼來源:Temp.py


注:本文中的graph_tool.Graph.edge方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。