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


Python Graph.dfs方法代码示例

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


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

示例1: repaint

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import dfs [as 别名]
def repaint():
    canvas.delete("point")
    
    if len(circles) == 0: return # Nothing to paint

    # Build the edges
    edges = []
    for i in range(len(circles)):
        for j in range(i + 1, len(circles)):
            if distance(circles[i], circles[j]) <= 2 * radius:
                edges.append([i, j])
                edges.append([j, i])

    graph = Graph(circles, edges)
    tree = graph.dfs(0)
    isAllCirclesConnected = \
        len(circles) == tree.getNumberOfVerticesFound()

    for [x, y] in circles:
        if isAllCirclesConnected: # All circles are connected
            canvas.create_oval(x - radius, y - radius, x + radius, 
                y + radius, fill = "red", tags = "point")
        else:
            canvas.create_oval(x - radius, y - radius, x + radius, 
                y + radius, tags = "point")            
开发者ID:EthanSeaver,项目名称:Python-Projects,代码行数:27,代码来源:ConnectedCircles.py

示例2: dfs

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import dfs [as 别名]
 def dfs(self):
     self.lines = True
     graph = Graph(self.point, self.edge)
     dfs = graph.dfs(eval(self.v1.get()))
     order = dfs.getSearchOrders()
     for i in range(1, len(order)):
         for j in range(len(self.edge)):
             parent = dfs.getParent(i)
             self.canvas.create_line(self.node[parent][0], self.node[parent][1], self.node[order[i]][0], self.node[order[i]][1], arrow = LAST, fill = "red")
     self.generate()
开发者ID:dmaslin,项目名称:Python-work,代码行数:12,代码来源:22.21.py

示例3: Graph

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import dfs [as 别名]
# Create an edge list for graph in Figure 16.1
edges = [
    [0, 1], [0, 3], [0, 5],
    [1, 0], [1, 2], [1, 3],
    [2, 1], [2, 3], [2, 4], [2, 10],
    [3, 0], [3, 1], [3, 2], [3, 4], [3, 5],
    [4, 2], [4, 3], [4, 5], [4, 7], [4, 8], [4, 10],
    [5, 0], [5, 3], [5, 4], [5, 6], [5, 7],
    [6, 5], [6, 7],
    [7, 4], [7, 5], [7, 6], [7, 8],
    [8, 4], [8, 7], [8, 9], [8, 10], [8, 11],
    [9, 8], [9, 11],
    [10, 2], [10, 4], [10, 8], [10, 11],
    [11, 8], [11, 9], [11, 10]
  ]

graph = Graph(vertices, edges)

dfs = graph.dfs(graph.getIndex("Chicago"))

searchOrders = dfs.getSearchOrders()
print(str(dfs.getNumberOfVerticesFound()) +
    " vertices are searched in this DFS order:")
for i in range(len(searchOrders)):
    print(graph.getVertex(searchOrders[i]), end = " ")
print();

for i in range(len(searchOrders)):
    if dfs.getParent(i) != -1:
        print("parent of " + graph.getVertex(i) +
            " is " + graph.getVertex(dfs.getParent(i)))
开发者ID:EthanSeaver,项目名称:Python-Projects,代码行数:33,代码来源:TestDFS.py

示例4: eval

# 需要导入模块: from Graph import Graph [as 别名]
# 或者: from Graph.Graph import dfs [as 别名]
l = eval(l[0])
v = []
for i in range(l):
    v.append(fin.readline().split(" "))

for row in range(len(v)):
    for column in range(len(v[row])):
        v[row][column] = eval(v[row][column])
counter = 0
vertex = []
for i in range(len(v)):
    vertex.append(str(v[i][0]))
edges = []
for i in range(l):
    for j in range(1, len(v[i])):
        edges.append([])
        edges[counter].append(i)
        edges[counter].append(v[i][j])
        counter += 1

g = Graph(vertex, edges)
tree = g.dfs(0)
print("The number of vertricies is: " + str(l))
g.printEdges()
if tree.getNumberOfVerticesFound() == l:
    print("They are connected")
else:
    print("They are not connected")


开发者ID:dmaslin,项目名称:Python-work,代码行数:30,代码来源:22.1.py


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