python-从节点和边列表构建邻接列表
我有一个国家公路规划网数据库中的节点和边对象列表。许多数据对我隐藏,但这是给我的:python-从节点和边列表构建邻接列表,python,dijkstra,adjacency-list,Python,Dijkstra,Adjacency List,我有一个国家公路规划网数据库中的节点和边对象列表。许多数据对我隐藏,但这是给我的: class Node: def __init__(self, longitude, latitude, state, description): self.longitude = longitude self.latitude = latitude self.state = state self.description = descripti
class Node:
def __init__(self, longitude, latitude, state, description):
self.longitude = longitude
self.latitude = latitude
self.state = state
self.description = description
class Link:
"""A bi-directional edge linking two NHPN nodes."""
def __init__ (self, begin, end, description):
"""create a link given its beginning and end (which must be nodes)
and possibly a description string."""
self.begin = begin
self.end = end
self.description = description
我知道没有给出太多的信息,但我正试图根据这些数据建立一个邻接列表。我很想用字典。这就是我所尝试的:
for node in nodes:
adj[node] = None
for edge in edges:
adj[node] = (edge.begin, edge.end) #edge.begin and edge.end being node's neighbors
然后是打印语句,只是为了看看它是否有效。但它从未打印,这让我担心输入量太大,我的代码速度会非常慢。如何修改我的实现?我很想使用字典,但我愿意接受所有建议。这里有一个程序,可以计算一小部分高速公路的邻接列表。我已经尽可能多地使用了你问题中的代码
from pprint import pprint
class Node:
def __init__(self, longitude, latitude, state, description):
self._longitude = longitude
self._latitude = latitude
self.state = state
self.description = description
@property
def longitude(self):
return self._longitude
@property
def latitude(self):
return self._latitude
def __hash__(self):
return hash((self.longitude, self.latitude))
def __repr__(self):
return 'Node({_longitude!r}, {_latitude!r}, {state!r}, {description!r})'.format(**vars(self))
class Link:
"""A bi-directional edge linking two NHPN nodes."""
def __init__ (self, begin, end, description):
"""create a link given its beginning and end (which must be nodes)
and possibly a description string."""
self.begin = begin
self.end = end
self.description = description
chicago = Node(-87, 41, 'IL', 'Windy City')
bloomington = Node(-89, 40, 'IL', 'Twin City')
indy = Node(-86, 40, 'IN', 'Naptown')
nodes = [ chicago, bloomington, indy ]
edges = [
Link(chicago, bloomington, 'I-55'),
Link(chicago, indy, 'I-65'),
Link(indy, bloomington, 'I-74'),
]
adj = {}
for edge in edges:
adj.setdefault(edge.begin, set()).add(edge.end)
adj.setdefault(edge.end, set()).add(edge.begin)
pprint(adj)
下面是如果我们不必使用所提供的链接
和节点
时的情况:
from pprint import pprint
from collections import namedtuple
Node = namedtuple('Node', 'longitude latitude state description')
Link = namedtuple('Link', 'begin end description')
chicago = Node(-87, 41, 'IL', 'Windy City')
bloomington = Node(-89, 40, 'IL', 'Twin City')
indy = Node(-86, 40, 'IN', 'Naptown')
nodes = [ chicago, bloomington, indy ]
edges = [
Link(chicago, bloomington, 'I-55'),
Link(chicago, indy, 'I-65'),
Link(indy, bloomington, 'I-74'),
]
adj = {}
for edge in edges:
adj.setdefault(edge.begin, set()).add(edge.end)
adj.setdefault(edge.end, set()).add(edge.begin)
pprint(adj)
这里有一个根本不使用任何类定义的:
from pprint import pprint
chicago = (-87, 41, 'IL', 'Windy City')
bloomington = (-89, 40, 'IL', 'Twin City')
indy = (-86, 40, 'IN', 'Naptown')
nodes = [ chicago, bloomington, indy ]
edges = [
(chicago, bloomington, 'I-55'),
(chicago, indy, 'I-65'),
(indy, bloomington, 'I-74'),
]
adj = {}
for edge in edges:
adj.setdefault(edge[0], set()).add(edge[1])
adj.setdefault(edge[1], set()).add(edge[0])
pprint(adj)
下面是一个程序,用于计算一组非常小的高速公路的邻接列表。我已经尽可能多地使用了你问题中的代码
from pprint import pprint
class Node:
def __init__(self, longitude, latitude, state, description):
self._longitude = longitude
self._latitude = latitude
self.state = state
self.description = description
@property
def longitude(self):
return self._longitude
@property
def latitude(self):
return self._latitude
def __hash__(self):
return hash((self.longitude, self.latitude))
def __repr__(self):
return 'Node({_longitude!r}, {_latitude!r}, {state!r}, {description!r})'.format(**vars(self))
class Link:
"""A bi-directional edge linking two NHPN nodes."""
def __init__ (self, begin, end, description):
"""create a link given its beginning and end (which must be nodes)
and possibly a description string."""
self.begin = begin
self.end = end
self.description = description
chicago = Node(-87, 41, 'IL', 'Windy City')
bloomington = Node(-89, 40, 'IL', 'Twin City')
indy = Node(-86, 40, 'IN', 'Naptown')
nodes = [ chicago, bloomington, indy ]
edges = [
Link(chicago, bloomington, 'I-55'),
Link(chicago, indy, 'I-65'),
Link(indy, bloomington, 'I-74'),
]
adj = {}
for edge in edges:
adj.setdefault(edge.begin, set()).add(edge.end)
adj.setdefault(edge.end, set()).add(edge.begin)
pprint(adj)
下面是如果我们不必使用所提供的链接
和节点
时的情况:
from pprint import pprint
from collections import namedtuple
Node = namedtuple('Node', 'longitude latitude state description')
Link = namedtuple('Link', 'begin end description')
chicago = Node(-87, 41, 'IL', 'Windy City')
bloomington = Node(-89, 40, 'IL', 'Twin City')
indy = Node(-86, 40, 'IN', 'Naptown')
nodes = [ chicago, bloomington, indy ]
edges = [
Link(chicago, bloomington, 'I-55'),
Link(chicago, indy, 'I-65'),
Link(indy, bloomington, 'I-74'),
]
adj = {}
for edge in edges:
adj.setdefault(edge.begin, set()).add(edge.end)
adj.setdefault(edge.end, set()).add(edge.begin)
pprint(adj)
这里有一个根本不使用任何类定义的:
from pprint import pprint
chicago = (-87, 41, 'IL', 'Windy City')
bloomington = (-89, 40, 'IL', 'Twin City')
indy = (-86, 40, 'IN', 'Naptown')
nodes = [ chicago, bloomington, indy ]
edges = [
(chicago, bloomington, 'I-55'),
(chicago, indy, 'I-65'),
(indy, bloomington, 'I-74'),
]
adj = {}
for edge in edges:
adj.setdefault(edge[0], set()).add(edge[1])
adj.setdefault(edge[1], set()).add(edge[0])
pprint(adj)
您必须使用这些类定义吗?字典需要可散列的键,而这两个类都不是可散列的。@Robᵩ 如果你指的是节点和链接,很有可能是的,因为我无法想象有任何其他方法可以迭代节点和边。如果你指的是经度/纬度/etc和开始/结束,可能不是,但我想不出没有开始/结束的方法。NHPN数据库似乎相当大。我认为这个问题来自于@Robᵩ 旨在发现数据存储格式是否可以进行任何更改。你的内存中已经有这些记录了吗?您是否正在使用其他人的XML文件解析器?另外,邻接列表的预期用例是什么?插入数据库?绘制地图?解决旅行推销员问题?@AustinHastings dijskstra的算法,找到从我周围某个区域到远离我的某个区域(例如圣地亚哥)的最短路径,你必须使用这些类定义吗?字典需要可散列的键,而这两个类都不是可散列的。@Robᵩ 如果你指的是节点和链接,很有可能是的,因为我无法想象有任何其他方法可以迭代节点和边。如果你指的是经度/纬度/etc和开始/结束,可能不是,但我想不出没有开始/结束的方法。NHPN数据库似乎相当大。我认为这个问题来自于@Robᵩ 旨在发现数据存储格式是否可以进行任何更改。你的内存中已经有这些记录了吗?您是否正在使用其他人的XML文件解析器?另外,邻接列表的预期用例是什么?插入数据库?绘制地图?解决旅行推销员问题?@Austinastings dijskstra的算法,从我周围的某个区域到离我很远的某个区域(例如圣地亚哥)寻找最短路径