summaryrefslogtreecommitdiffstats
path: root/algorithms.py
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2020-11-12 14:14:27 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2020-11-20 13:39:20 +0100
commita178b3088fa8e90a080926c79d608497e4b56448 (patch)
treeaab6b3bba61c829b2c59c79d4eb83d461e6431dd /algorithms.py
parent0ebe417a8c9edd12d9e87f3843006eba510c97f9 (diff)
downloadtdde25-a178b3088fa8e90a080926c79d608497e4b56448.tar.gz
count and store iterations in search algorithm
Diffstat (limited to 'algorithms.py')
-rw-r--r--algorithms.py4
1 files changed, 3 insertions, 1 deletions
diff --git a/algorithms.py b/algorithms.py
index 8a6e2d3..1e70fa1 100644
--- a/algorithms.py
+++ b/algorithms.py
@@ -71,15 +71,17 @@ def find_shortest_path(nodes, source_id, target_id):
# 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,))]
+ iterations = 0
visited = set()
while queue:
# consider an unchecked walk
walk_dist, walk = heapq.heappop(queue)
walk_end = walk[-1]
+ iterations += 1
if walk_end == target_id:
# you have reached your destination
- return walk
+ return walk, iterations
if walk_end in visited:
# there exists a shorter walk to walk_end
continue