Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何访问边的邻居_Python 3.x_Neural Network_Networkx_Breadth First Search_Path Finding - Fatal编程技术网

Python 3.x 如何访问边的邻居

Python 3.x 如何访问边的邻居,python-3.x,neural-network,networkx,breadth-first-search,path-finding,Python 3.x,Neural Network,Networkx,Breadth First Search,Path Finding,我正在尝试用Python实现BFS。我需要使用城市之间的权重来计算最短/最好的路线。我不明白如何为图形添加权重,因此决定将Networkx用于python。请看下面的代码 import networkx as nx nodes = ["Arad","Zerind","Oradea","Timisoara","Lugoj","Mehadia" "Drobeta", "Sibiu","Rimnicu","Fagaras","Pitesti","Craiova" "N

我正在尝试用Python实现BFS。我需要使用城市之间的权重来计算最短/最好的路线。我不明白如何为图形添加权重,因此决定将Networkx用于python。请看下面的代码

import networkx as nx

nodes = ["Arad","Zerind","Oradea","Timisoara","Lugoj","Mehadia"
        "Drobeta", "Sibiu","Rimnicu","Fagaras","Pitesti","Craiova"
        "Neamt","Bucharest","Giurgiu","Urziceni","Vaslui",
        "Iasi","Hirsova","Eforie"]

g ={
    ("Arad", "Sibiu", 140),("Arad", "Zerind", 75),("Arad", "Timisoara", 118),
    ("Zerind", "Oradea", 71),("Oradea", "Sibiu", 151),("Sibiu","Fagaras", 99),
    ("Sibiu", "Rimnicu", 90),("Timisoara","Lugoj", 111),("Lugoj", "Mehadia", 70),
    ("Mehadia","Drobeta",75),("Drobeta", "Craiova", 120), ("Sibiu","Fagaras", 99),
    ("Sibiu", "Rimnicu", 80),("Rimnicu","Pitesti",97),("Rimnicu","Craiova",146),
    ("Fagaras","Bucharest",211),("Pitesti","Bucharest",101),("Bucharest","Giurgiu",90),
    ("Bucharest","Urziceni",85),("Urziceni","Vaslui",142),("Vaslui","Iasi",92),
    ("Iasi","Neamt",87),("Urziceni","Hirsova",98),("Hirsova","Eforie",86)

}

graph = nx.Graph()
print ("Graph created")

for node in nodes:
  graph.add_node(node)

graph.add_weighted_edges_from(g)

for (n1,n2,dat) in graph.edges.data('weight'):
  neibors = graph[n1]
  print (neibors)


这是输出,我认为它是好的

Graph created
{'Zerind': {'weight': 75}, 'Timisoara': {'weight': 118}, 'Sibiu': {'weight': 140}}
{'Zerind': {'weight': 75}, 'Timisoara': {'weight': 118}, 'Sibiu': {'weight': 140}}
{'Zerind': {'weight': 75}, 'Timisoara': {'weight': 118}, 'Sibiu': {'weight': 140}}
{'Arad': {'weight': 75}, 'Oradea': {'weight': 71}}
{'Sibiu': {'weight': 151}, 'Zerind': {'weight': 71}}
{'Lugoj': {'weight': 111}, 'Arad': {'weight': 118}}
{'Mehadia': {'weight': 70}, 'Timisoara': {'weight': 111}}
{'Rimnicu': {'weight': 90}, 'Oradea': {'weight': 151}, 'Fagaras': {'weight': 99}, 'Arad': {'weight': 140}}
{'Rimnicu': {'weight': 90}, 'Oradea': {'weight': 151}, 'Fagaras': {'weight': 99}, 'Arad': {'weight': 140}}
{'Craiova': {'weight': 146}, 'Sibiu': {'weight': 90}, 'Pitesti': {'weight': 97}}
{'Craiova': {'weight': 146}, 'Sibiu': {'weight': 90}, 'Pitesti': {'weight': 97}}
{'Bucharest': {'weight': 211}, 'Sibiu': {'weight': 99}}
{'Rimnicu': {'weight': 97}, 'Bucharest': {'weight': 101}}
{'Fagaras': {'weight': 211}, 'Pitesti': {'weight': 101}, 'Giurgiu': {'weight': 90}, 'Urziceni': {'weight': 85}}
{'Fagaras': {'weight': 211}, 'Pitesti': {'weight': 101}, 'Giurgiu': {'weight': 90}, 'Urziceni': {'weight': 85}}
{'Hirsova': {'weight': 98}, 'Vaslui': {'weight': 142}, 'Bucharest': {'weight': 85}}
{'Hirsova': {'weight': 98}, 'Vaslui': {'weight': 142}, 'Bucharest': {'weight': 85}}
{'Urziceni': {'weight': 142}, 'Iasi': {'weight': 92}}
{'Neamt': {'weight': 87}, 'Vaslui': {'weight': 92}}
{'Eforie': {'weight': 86}, 'Urziceni': {'weight': 98}}
{'Rimnicu': {'weight': 146}, 'Drobeta': {'weight': 120}}
{'Lugoj': {'weight': 70}, 'Drobeta': {'weight': 75}}
现在,我想知道如何访问节点及其连接的节点,并使用这种方式进行循环

  • 读取节点并检查它是否已被探测
  • 如果不是,则获取它的子对象并将它们添加到队列中
  • 将它们添加到集合并展开
  • 重复此过程,直到找到目标状态
  • 我是python新手,因此我需要从图中访问数据项的帮助