Created
November 30, 2013 06:49
-
-
Save cocodrips/7716135 to your computer and use it in GitHub Desktop.
fast ver きたない
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def calculate_distance_fast(self, src_name, dst_name): | |
self.reset() | |
src = self.get_node_from_name(src_name) | |
dst = self.get_node_from_name(dst_name) | |
src.visited = True | |
queue1 = Queue.Queue() | |
queue1.put(src) | |
queue2 = Queue.Queue() | |
queue2.put(dst) | |
q1 = queue1.get() | |
q2 = queue2.get() | |
while True: | |
while q1.distance <= q2.distance: | |
for adjacent in q1.adjacents: | |
if not adjacent.visited: | |
adjacent.previous = q1 | |
adjacent.distance = q1.distance + 1 | |
adjacent.visited = True | |
if adjacent.reverse_visited: | |
return adjacent.distance + adjacent.reverse_distance | |
queue1.put(adjacent) | |
q1 = queue1.get() | |
while q2.distance < q1.distance: | |
for adjacent in q2.adjacents: | |
if not adjacent.reverse_visited: | |
adjacent.reverse_previous = q2 | |
adjacent.reverse_distance = q2.reverse_distance + 1 | |
adjacent.reverse_visited = True | |
if adjacent.visited: | |
return adjacent.distance + adjacent.reverse_distance | |
queue2.put(adjacent) | |
q2 = queue2.get() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment