dijkstra - python - build adjacency list from list of nodes and edges -


i have list of node , edge objects national highway planning network database. lot of data hidden me given me:

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 2 nhpn nodes."""      def __init__ (self, begin, end, description):         """create link given beginning , end (which must nodes)         , possibly description string."""         self.begin = begin         self.end = end         self.description = description 

i know there isn't lot of information given trying build adjacency list data. use dictionary. tried:

for node in nodes:     adj[node] = none edge in edges:     adj[node] = (edge.begin, edge.end) #edge.begin , edge.end being node's neighbors 

followed print statement, see if worked. never printed, worries input enormous , code incredibly slow. how can revise implementation? use dictionary open suggestions.

here program computes adjacency list small set of highways. have used code question as could.

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 2 nhpn nodes."""      def __init__ (self, begin, end, description):         """create link given beginning , end (which must nodes)         , possibly 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 = {} edge in edges:     adj.setdefault(edge.begin, set()).add(edge.end)     adj.setdefault(edge.end, set()).add(edge.begin) pprint(adj) 

here if didn't have use link , node provided:

from pprint import pprint 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 = {} edge in edges:     adj.setdefault(edge.begin, set()).add(edge.end)     adj.setdefault(edge.end, set()).add(edge.begin) pprint(adj) 

and here 1 without using class definitions @ all:

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 = {} edge in edges:     adj.setdefault(edge[0], set()).add(edge[1])     adj.setdefault(edge[1], set()).add(edge[0]) pprint(adj) 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -