Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Python Dijkstra Shortest Path

Reply
Thread Tools

Re: Python Dijkstra Shortest Path

 
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-16-2007
En Wed, 16 May 2007 00:39:20 -0300, Hugo Ferreira <>
escribió:

> While trying to optimize this:
>
> http://aspn.activestate.com/ASPN/Coo.../Recipe/119466
>
> ... and still have a fast edge lookup, I've done the following tweaks:


I've replaced that strange deeply nested list with an old-fashioned tuple
and got about a 10% improvement (over a small graph, 60 nodes). But you
really should try the effect with larger objects.

def findPath(self, start, end):
q = [(0, start, ())] # Heap of (cost, path_head, path_rest).
visited = set() # Visited vertices.

# Eliminate the dots pattern
push, pop, add = heapq.heappush, heapq.heappop, visited.add
G, E = self.G, self.E

while True:
(cost, v1, path) = pop(q)
if v1 not in visited:
add(v1)
path += (v1,)

if v1 == end: return path

for (v2, e) in G[v1].iteritems():
if v2 not in visited:
push(q, (cost + E[e], v2, path))

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Dijkstra Shortest Path Hugo Ferreira Python 0 05-16-2007 03:39 AM
libboost, python, and dijkstra shortest path Bytter Python 2 11-29-2006 08:14 PM
shortest mean path length diffuser78@gmail.com Python 0 10-06-2006 08:51 PM
Recursive algoritme for finding the shortest path Webdad C++ 20 12-09-2004 06:51 PM
Shortest path algorithm (other than Dijkstra) ThanhVu Nguyen C++ 6 08-24-2004 12:50 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57