在节点之间的旅行期间打印所有轨道

发布于 2025-02-06 12:50:47 字数 1560 浏览 2 评论 0原文

这是一个代码,可以进行广度的第一次搜索和打印我们访问的所有道路 我得到了一些不完整的结果,但是结果有10或11个节点,这是大多数节点没有出现的,结果我的节点为6个节点。 不幸的是,清单只有这条路: [0,2,3,1,3] [0,2,4,2] [0,2,3,5,3] [0,2,6,6,7,6] [0、2、3、5、9、5] [0、2、4、5、10、5] [0、2、3、5、9、10] 因此,它如何看到列表包含所有节点,这意味着它将包含11个节点,请提供任何帮助

import copy
graph = {
  0 : [2],
  1 : [3],
  2 : [0,3,4,6],
  3 : [1,2,5],
  4 : [2,5,7],
  5 : [3,4,9,10],
  6 : [2,7,8],
  7 : [4,6,9],
  8 : [6,9],
  9 : [5,7,8,10],
  10 : [5,9]
}
visited = [] # List for visited nodes.
queue = []     #Initialize a queue
list = [[0]]
def bfs(visited, graph, node): #function for BFS
  visited.append(node)
  queue.append(node)

  while queue:          # Creating loop to visit each node
    m = queue.pop(0) 
    print (m, end = "-->")
    for neighbour in graph[m]:
        temp = []
        for raw in list:
            if raw[-1]==m :#condition to add node to the right road 
                    temp = copy.deepcopy(raw) #i.e list =[2,1,3,4],[2,5,1,3,5],... 
                    raw.append(neighbour)# if m= 4 => add neighbour  
                    break                # so become [2,1,3,4,neibour]
        
        if neighbour not in visited:
            visited.append(neighbour)
            queue.append(neighbour)
        if temp != '' :  # here i save the old roads to edit it for a new road
            list.append(temp) 

# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, 0)    # function calling
for i in list:       # here i want only the roads more than 6 nodes 
  if len(i) >= 6:
    print(i)



       

here is a code to do breadth first search and print all roads we visit
I got some incomplete results , but the results have 10 or 11 nodes which is most of node doesn't appear , I get result to 6 nodes .
unfortunately the list have only this roads :
[0, 2, 3, 1, 3] [0, 2, 4, 2][0, 2, 3, 5, 3][0, 2, 6, 7, 6]
[0, 2, 3, 5, 9, 5][0, 2, 4, 5, 10, 5] [0, 2, 3, 5, 9, 10]
so how It can see the list contain all nodes that mean it will contain 11 nodes, any help please

import copy
graph = {
  0 : [2],
  1 : [3],
  2 : [0,3,4,6],
  3 : [1,2,5],
  4 : [2,5,7],
  5 : [3,4,9,10],
  6 : [2,7,8],
  7 : [4,6,9],
  8 : [6,9],
  9 : [5,7,8,10],
  10 : [5,9]
}
visited = [] # List for visited nodes.
queue = []     #Initialize a queue
list = [[0]]
def bfs(visited, graph, node): #function for BFS
  visited.append(node)
  queue.append(node)

  while queue:          # Creating loop to visit each node
    m = queue.pop(0) 
    print (m, end = "-->")
    for neighbour in graph[m]:
        temp = []
        for raw in list:
            if raw[-1]==m :#condition to add node to the right road 
                    temp = copy.deepcopy(raw) #i.e list =[2,1,3,4],[2,5,1,3,5],... 
                    raw.append(neighbour)# if m= 4 => add neighbour  
                    break                # so become [2,1,3,4,neibour]
        
        if neighbour not in visited:
            visited.append(neighbour)
            queue.append(neighbour)
        if temp != '' :  # here i save the old roads to edit it for a new road
            list.append(temp) 

# Driver Code
print("Following is the Breadth-First Search")
bfs(visited, graph, 0)    # function calling
for i in list:       # here i want only the roads more than 6 nodes 
  if len(i) >= 6:
    print(i)



       

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文