add some debug logging

This commit is contained in:
onyx-and-iris 2023-12-20 21:40:23 +00:00
parent 54e59680b7
commit 0eb3476e48
3 changed files with 17 additions and 4 deletions

View File

@ -23,6 +23,7 @@ def dijkstra(graph, node):
while queue: while queue:
current_cost, current_node = heapq.heappop(queue) current_cost, current_node = heapq.heappop(queue)
logger.debug(f"node {current_node} with cost {current_cost} popped from pqueue")
for next_node, weight in graph[current_node].items(): for next_node, weight in graph[current_node].items():
new_cost = current_cost + weight new_cost = current_cost + weight
@ -30,12 +31,15 @@ def dijkstra(graph, node):
costs[next_node] = new_cost costs[next_node] = new_cost
parents[next_node] = current_node parents[next_node] = current_node
heapq.heappush(queue, (new_cost, next_node)) heapq.heappush(queue, (new_cost, next_node))
logger.debug(
f"node {next_node} with new cost {new_cost} appended to pqueue"
)
return costs, parents return costs, parents
costs, parents = dijkstra(graph, "start") costs, parents = dijkstra(graph, "start")
print(f"lowest cost route: {costs['fin']}") print(f"lowest cost from start to fin: {costs['fin']}")
def get_full_route(): def get_full_route():
@ -48,4 +52,4 @@ def get_full_route():
return list(reversed(route)) return list(reversed(route))
print(f"route: {get_full_route()}") print(f"full route: {get_full_route()}")

View File

@ -22,6 +22,7 @@ def dijkstra(graph, node):
while queue: while queue:
current_cost, current_node = heapq.heappop(queue) current_cost, current_node = heapq.heappop(queue)
logger.debug(f"node {current_node} with cost {current_cost} popped from pqueue")
for next_node, weight in graph[current_node].items(): for next_node, weight in graph[current_node].items():
new_cost = current_cost + weight new_cost = current_cost + weight
@ -29,8 +30,12 @@ def dijkstra(graph, node):
costs[next_node] = new_cost costs[next_node] = new_cost
parents[next_node] = current_node parents[next_node] = current_node
heapq.heappush(queue, (new_cost, next_node)) heapq.heappush(queue, (new_cost, next_node))
logger.debug(
f"node {next_node} with new cost {new_cost} appended to pqueue"
)
return costs, parents return costs, parents
costs, parents = dijkstra(graph, "start") costs, parents = dijkstra(graph, "start")
print(f"lowest cost route: {costs['fin']}")
print(f"lowest cost from start to fin: {costs['fin']}")

View File

@ -22,6 +22,7 @@ def dijkstra(graph, node):
while queue: while queue:
current_cost, current_node = heapq.heappop(queue) current_cost, current_node = heapq.heappop(queue)
logger.debug(f"node {current_node} with cost {current_cost} popped from pqueue")
for next_node, weight in graph[current_node].items(): for next_node, weight in graph[current_node].items():
new_cost = current_cost + weight new_cost = current_cost + weight
@ -29,9 +30,12 @@ def dijkstra(graph, node):
costs[next_node] = new_cost costs[next_node] = new_cost
parents[next_node] = current_node parents[next_node] = current_node
heapq.heappush(queue, (new_cost, next_node)) heapq.heappush(queue, (new_cost, next_node))
logger.debug(
f"node {next_node} with new cost {new_cost} appended to pqueue"
)
return costs, parents return costs, parents
costs, parents = dijkstra(graph, "start") costs, parents = dijkstra(graph, "start")
print(f"lowest cost route: {costs['fin']}") print(f"lowest cost from start to fin: {costs['fin']}")