当前位置: 首页>>代码示例>>Python>>正文


Python Graph.get_edge方法代码示例

本文整理汇总了Python中Graph.get_edge方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.get_edge方法的具体用法?Python Graph.get_edge怎么用?Python Graph.get_edge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


在下文中一共展示了Graph.get_edge方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Graph

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import get_edge [as 别名]
g = Graph([v,w,p,q], [e1,e2,e3])
g1 = Graph ([v,w,p], [])
#g2 = copy.deepcopy(g)

#g = Graph([v,w], [e1])

#Create vertices and edges not in the graph
r = Vertex('r')
s = Vertex('s')

e4 = Edge(r,s)
e5 = Edge(v,q)

#Test some methods
#get_edge(e)
g.get_edge(v,w)

print "got e1"

g.get_edge(v,q)

print "\n"
print g
print "\n"

#remove_edge(e)
# g1.remove_edge(e1)
# g1.remove_edge(e2)
# g1.remove_edge(e3)

# print "The graph with removed edge\n"
开发者ID:cmnzs,项目名称:think-complexity,代码行数:33,代码来源:tester.py

示例2: BestPath

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import get_edge [as 别名]

#.........这里部分代码省略.........
            self._visitedPoints.remove(city)
        else:
            self._visitedPoints.remove(city)
    
    def all_visited(self):
        """Returns a True bool type if all the interest cities have been visited
        """
        # if the length of interest cities list and visited cities lists are equal
        if len(self.interestCities) == len(self._visitedCities):
            return True
        else:
            return False
    
    def next_cities(self):
        """
        It generates a list of points that have not been visited that we may
        potentially want to visit
        """
        # sets the current city as the top item on currentTour stack
        currentCity = self.currentTour.top() 
        # calls Graph Class's out vertices method to get all the vertices out of currentCity in the map
        possibleMoves =  self._map.out_vertices(currentCity)    
        
        # The following line returns a list of points that we can visit next
        # that has not already been visited yet in the currentTour
        return [move for move in possibleMoves if move not in self._visitedPoints]

                
    def move_branch(self, currentCity):
        """This method generates a bestTour by starting at currentCity and explores
        all the possible vertices until it reaches a dead_end or all the interestCities
        have been visited.
        
        It does that by recursively exploring all possibilities but has a bounding algorithm
        that causes it to prune.
        """
        # base case
        if self.dead_end() or self.all_visited():
            if self.all_visited():
                if self.currentCost < self.bestCost:
                    self.bestCost = self.currentCost
                    self.bestTour = Stack() # reset bestTour stack
                    for i in self.currentTour.items[::-1]:  # add all the items in currentTour to bestTour
                        self.bestTour.push(i)

            return
        
        # recursive call
        else:
            for move in self.next_cities(): # for each available moves
                self.mark_visit(move)   # first mark it visited
                newEdge = self._map.get_edge(currentCity, move) # create an edge from the last city to the new one
                self.currentCost += newEdge.get_distance()  # add the tour cost of this new city
                self.currentTour.push(move) # add the new city to currentTour
                self.move_branch(move)  # call the function again with this new city
                
                # The following part of the code is executed during backtracking
                self.currentTour.pop()  # pop the last item in the list
                self.currentCost -= newEdge.get_distance()  # remove that from the tour cost
                self.mark_unvisit(move) # mark it unvitied
            return
                
    def genrate_actualtour(self, StartCity):
        """
        This method generates the graph of the bestTour. 
        
        It calls the move_branch method with a given StartCity. From the bestTour
        stack it filters out only the interestCities by leaving out all the 
        intersection points. It then creates an instance of graph class in the same
        order as in bestTour.
        """
        tour = Stack()  # create a new stack
        
        self.currentTour.push(StartCity)    # push the startCity in currentTour stack
        self.mark_visit(StartCity)  # mark it visited
        self.move_branch(StartCity) # call move_branch recursive function, with the given city
        
        # The following block of code removes vertices from bestTour and filters out
        # only the interest points and adds it to tour Stack
        while self.bestTour.size() != 0:    
            city = self.bestTour.pop()
            if city in self.interestCities:
                tour.push(city)
                
        # The following block of code generates a Graph object from the tour Stack
        newGraph = Graph(tour.items)    # adds only the vertices in the graph
        for i in range(len(tour.items)-1):
            newEdge = Edge(tour.items[i], tour.items[i+1])  # create an edge within two consecutive vertices
            newGraph.add_edge(newEdge)  # add the edge to the graph
        return newGraph
        
    def dead_end(self):
        """
        If there are no other options left to move it returns True otherwise it
        returns False. 
        """
        if self.next_cities() == []:    # if the returned list from next_citites method is empty
            return True
        else:
            return False    
开发者ID:agarwali,项目名称:CampusTourGuide,代码行数:104,代码来源:best_tour.py

示例3: find_path2

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import get_edge [as 别名]
"""
Given a directed graph, design an algorithm to find out whether there is a route 
between two nodes.
"""

from Graph import *

def find_path2(g, start_node, end_node):
    """
    This requires a simple graph traversal using either depth-first search 
    or breadth-first search. Breadth-first search is likely to be quicker and 
    is likely to provide the shortest path.
    """
    
    
    return False       


if __name__ == '__main__':
	g = Graph(True)
	v_a = g.insert_vertex('a')
	v_b = g.insert_vertex('b')
	v_b = g.insert_vertex('c')

	g.insert_edge(v_a, v_b, 'a->b')
	e_ab = g.get_edge(v_a, v_b)
	print e_ab._element
开发者ID:sylwiam,项目名称:ctci-python,代码行数:29,代码来源:4.1-Graph-Route.py


注:本文中的Graph.get_edge方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。