From 3a7c90f683c1c29790c2033de02c501f76735aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 5 Nov 2020 16:42:57 +0100 Subject: is --- algorithms.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'algorithms.py') diff --git a/algorithms.py b/algorithms.py index cc8d35f..7140a17 100644 --- a/algorithms.py +++ b/algorithms.py @@ -23,7 +23,7 @@ def get_closest_node_id(nodes, source_node): for node_id, node in nodes.items(): length = length_haversine(source_node, node) - if min_node == None or length < min_value: + if min_node is None or length < min_value: min_node = node_id min_value = length -- cgit v1.2.1 From 7f66e5c211ced9bc0286b893b9a86534fe9ec43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 5 Nov 2020 16:43:16 +0100 Subject: initial dijkstra --- algorithms.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'algorithms.py') diff --git a/algorithms.py b/algorithms.py index 7140a17..06d768c 100644 --- a/algorithms.py +++ b/algorithms.py @@ -1,3 +1,4 @@ +import heapq import math @@ -32,4 +33,24 @@ def get_closest_node_id(nodes, source_node): def find_shortest_path(nodes, source_id, target_id): """ Return the shortest path using Dijkstra's algortihm. """ - return [] + queue = [] + visited = set() + + print("neighbours", nodes[source_id].neighbours) + for neighbour in nodes[source_id].neighbours: + heapq.heappush(queue, (length_haversine(nodes[source_id], neighbour), (source_id, neighbour.id))) + + while queue: + cand_dist, cand_path = heapq.heappop(queue) + walk_end = cand_path[-1] + if walk_end == target_id: + return cand_path + if walk_end in visited: + continue + visited.add(walk_end) + for neighbour in nodes[walk_end].neighbours: + print(neighbour) + if neighbour not in visited: + heapq.heappush(queue, (cand_dist + length_haversine(nodes[walk_end], neighbour), cand_path + (neighbour.id, ))) + # no path found + return None -- cgit v1.2.1 From 7200d4c39f0a1c59bc73d49fcde7e041d28c7dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 6 Nov 2020 10:34:09 +0100 Subject: remove unnecessary print --- algorithms.py | 1 - 1 file changed, 1 deletion(-) (limited to 'algorithms.py') diff --git a/algorithms.py b/algorithms.py index 06d768c..3c84cf2 100644 --- a/algorithms.py +++ b/algorithms.py @@ -49,7 +49,6 @@ def find_shortest_path(nodes, source_id, target_id): continue visited.add(walk_end) for neighbour in nodes[walk_end].neighbours: - print(neighbour) if neighbour not in visited: heapq.heappush(queue, (cand_dist + length_haversine(nodes[walk_end], neighbour), cand_path + (neighbour.id, ))) # no path found -- cgit v1.2.1 From d2791410cc886ef9f1fbed7b9525477963b8b994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 6 Nov 2020 10:46:13 +0100 Subject: minor refactor --- algorithms.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) (limited to 'algorithms.py') diff --git a/algorithms.py b/algorithms.py index 3c84cf2..9d83b26 100644 --- a/algorithms.py +++ b/algorithms.py @@ -33,23 +33,32 @@ def get_closest_node_id(nodes, source_node): def find_shortest_path(nodes, source_id, target_id): """ Return the shortest path using Dijkstra's algortihm. """ - queue = [] + # queue contains multiple (path_length, (node_0, node_1, ... node_n))-tuples + # where (node_0, node_1, ... node_n) is a walk to node_n + queue = [(0, (source_id,))] visited = set() - print("neighbours", nodes[source_id].neighbours) - for neighbour in nodes[source_id].neighbours: - heapq.heappush(queue, (length_haversine(nodes[source_id], neighbour), (source_id, neighbour.id))) - while queue: - cand_dist, cand_path = heapq.heappop(queue) - walk_end = cand_path[-1] + # consider an unchecked walk + walk_dist, walk = heapq.heappop(queue) + walk_end = walk[-1] if walk_end == target_id: - return cand_path + # you have reached your destination + return walk if walk_end in visited: + # there exists a shorter walk to walk_end continue + # otherwise this is the shortest walk to walk_end visited.add(walk_end) + # consider all our neighbours for neighbour in nodes[walk_end].neighbours: - if neighbour not in visited: - heapq.heappush(queue, (cand_dist + length_haversine(nodes[walk_end], neighbour), cand_path + (neighbour.id, ))) + if neighbour in visited: + # there exists a shorter walk to neighbour + continue + # otherwise this MIGHT be the shortest walk to neighbour + # so put it in the queue + new_dist = walk_dist + length_haversine(nodes[walk_end], neighbour) + new_walk = walk + (neighbour.id,) + heapq.heappush(queue, (new_dist, new_walk)) # no path found return None -- cgit v1.2.1 From 606c3eebe19114bcd2d07546a7c7d800ca84973f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Fri, 6 Nov 2020 10:54:18 +0100 Subject: more comments --- algorithms.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'algorithms.py') diff --git a/algorithms.py b/algorithms.py index 9d83b26..6e01287 100644 --- a/algorithms.py +++ b/algorithms.py @@ -33,8 +33,9 @@ def get_closest_node_id(nodes, source_node): def find_shortest_path(nodes, source_id, target_id): """ Return the shortest path using Dijkstra's algortihm. """ - # queue contains multiple (path_length, (node_0, node_1, ... node_n))-tuples + # queue contains multiple (walk_dist, (node_0, node_1, ... node_n))-tuples # where (node_0, node_1, ... node_n) is a walk to node_n + # and walk_dist is the total length of the walk in meters queue = [(0, (source_id,))] visited = set() -- cgit v1.2.1