python - 从节点和边列表构建邻接列表

时间:2016-04-20 01:32:30

标签: python dijkstra adjacency-list

我有国家公路规划网络数据库中的节点和边缘对象列表。很多数据对我来说是隐藏的,但这是给我的:

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

后面跟一个print语句,看它是否有效。但它从未打印过,它担心输入是巨大的,我的代码会非常慢。我该如何修改我的实施?我非常想使用字典,但我对所有建议持开放态度。

1 个答案:

答案 0 :(得分:2)

这是一个程序,用于计算一小组高速公路的邻接列表。我尽可能多地使用了你问题中的代码。

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)

如果我们不必按照提供的方式使用LinkNode,那么就会出现以下情况:

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)