如何访问邻居的边缘

时间:2019-03-11 02:59:17

标签: 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"
        "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}}

现在,我想知道如何以一种可以循环到

的方式访问该节点及其连接的节点。
  1. 读取节点并检查是否被探索
  2. 如果不是,请让其为孩子并将其添加到队列中
  3. 将它们添加到浏览集中并进行扩展
  4. 重复该过程,直到找到目标状态

我是python的新手,因此我需要帮助来访问图形中的数据项。

0 个答案:

没有答案