From 54e59680b73caba774018b6b079dcf26c691b457 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Wed, 20 Dec 2023 20:17:49 +0000 Subject: [PATCH] adds chapter9 exercises with heap and without --- chapter9/with_heap/ex171a.py | 51 +++++++++++++++ chapter9/with_heap/ex171b.py | 36 +++++++++++ chapter9/with_heap/ex171c.py | 37 +++++++++++ chapter9/{ => with_list_function}/ex171a.py | 70 ++++++++++----------- chapter9/{ => with_list_function}/ex171b.py | 41 ++++++------ chapter9/{ => with_list_function}/ex171c.py | 41 ++++++------ 6 files changed, 196 insertions(+), 80 deletions(-) create mode 100644 chapter9/with_heap/ex171a.py create mode 100644 chapter9/with_heap/ex171b.py create mode 100644 chapter9/with_heap/ex171c.py rename chapter9/{ => with_list_function}/ex171a.py (53%) rename chapter9/{ => with_list_function}/ex171b.py (65%) rename chapter9/{ => with_list_function}/ex171c.py (65%) diff --git a/chapter9/with_heap/ex171a.py b/chapter9/with_heap/ex171a.py new file mode 100644 index 0000000..73711ea --- /dev/null +++ b/chapter9/with_heap/ex171a.py @@ -0,0 +1,51 @@ +import heapq +import logging +import math + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +graph = { + "start": {"a": 5, "b": 2}, + "a": {"b": 8, "c": 4, "d": 2}, + "b": {"a": 8, "d": 7}, + "c": {"d": 6, "fin": 3}, + "d": {"fin": 1}, + "fin": {}, +} + + +def dijkstra(graph, node): + costs = {node: math.inf for node in graph} + costs[node] = 0 + parents = {node: None for node in graph} + queue = [(0, node)] + + while queue: + current_cost, current_node = heapq.heappop(queue) + + for next_node, weight in graph[current_node].items(): + new_cost = current_cost + weight + if new_cost < costs[next_node]: + costs[next_node] = new_cost + parents[next_node] = current_node + heapq.heappush(queue, (new_cost, next_node)) + return costs, parents + + +costs, parents = dijkstra(graph, "start") + +print(f"lowest cost route: {costs['fin']}") + + +def get_full_route(): + route = [] + next = "fin" + while next != "start": + route.append(next) + next = parents[next] + route.append("start") + return list(reversed(route)) + + +print(f"route: {get_full_route()}") diff --git a/chapter9/with_heap/ex171b.py b/chapter9/with_heap/ex171b.py new file mode 100644 index 0000000..191b0ba --- /dev/null +++ b/chapter9/with_heap/ex171b.py @@ -0,0 +1,36 @@ +import heapq +import logging +import math + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +graph = { + "start": {"a": 10}, + "a": {"c": 20}, + "b": {"a": 1, "c": 1}, + "c": {"b": 1, "fin": 30}, + "fin": {}, +} + + +def dijkstra(graph, node): + costs = {node: math.inf for node in graph} + costs[node] = 0 + parents = {node: None for node in graph} + queue = [(0, node)] + + while queue: + current_cost, current_node = heapq.heappop(queue) + + for next_node, weight in graph[current_node].items(): + new_cost = current_cost + weight + if new_cost < costs[next_node]: + costs[next_node] = new_cost + parents[next_node] = current_node + heapq.heappush(queue, (new_cost, next_node)) + return costs, parents + + +costs, parents = dijkstra(graph, "start") +print(f"lowest cost route: {costs['fin']}") diff --git a/chapter9/with_heap/ex171c.py b/chapter9/with_heap/ex171c.py new file mode 100644 index 0000000..fb70265 --- /dev/null +++ b/chapter9/with_heap/ex171c.py @@ -0,0 +1,37 @@ +import heapq +import logging +import math + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +graph = { + "start": {"a": 2, "b": 2}, + "a": {"b": 2}, + "b": {"c": 2, "fin": 2}, + "c": {"fin": 2}, + "fin": {}, +} + + +def dijkstra(graph, node): + costs = {node: math.inf for node in graph} + costs[node] = 0 + parents = {node: None for node in graph} + queue = [(0, node)] + + while queue: + current_cost, current_node = heapq.heappop(queue) + + for next_node, weight in graph[current_node].items(): + new_cost = current_cost + weight + if new_cost < costs[next_node]: + costs[next_node] = new_cost + parents[next_node] = current_node + heapq.heappush(queue, (new_cost, next_node)) + return costs, parents + + +costs, parents = dijkstra(graph, "start") + +print(f"lowest cost route: {costs['fin']}") diff --git a/chapter9/ex171a.py b/chapter9/with_list_function/ex171a.py similarity index 53% rename from chapter9/ex171a.py rename to chapter9/with_list_function/ex171a.py index 444b25a..8d36ba0 100644 --- a/chapter9/ex171a.py +++ b/chapter9/with_list_function/ex171a.py @@ -4,37 +4,30 @@ import math logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) -graph = {} -graph["start"] = {} -graph["start"]["a"] = 5 -graph["start"]["b"] = 2 -graph["a"] = {} -graph["a"]["b"] = 8 -graph["a"]["c"] = 4 -graph["a"]["d"] = 2 -graph["b"] = {} -graph["b"]["a"] = 8 -graph["b"]["d"] = 7 -graph["c"] = {} -graph["c"]["d"] = 6 -graph["c"]["fin"] = 3 -graph["d"] = {} -graph["d"]["fin"] = 1 -graph["fin"] = {} +graph = { + "start": {"a": 5, "b": 2}, + "a": {"b": 8, "c": 4, "d": 2}, + "b": {"a": 8, "d": 7}, + "c": {"d": 6, "fin": 3}, + "d": {"fin": 1}, + "fin": {}, +} -costs = {} -costs["a"] = 5 -costs["b"] = 2 -costs["c"] = math.inf -costs["d"] = math.inf -costs["fin"] = math.inf +costs = { + "a": 5, + "b": 2, + "c": math.inf, + "d": math.inf, + "fin": math.inf, +} -parents = {} -parents["a"] = "start" -parents["b"] = "start" -parents["c"] = None -parents["d"] = None -parents["fin"] = None +parents = { + "a": "start", + "b": "start", + "c": None, + "d": None, + "fin": None, +} processed = set() @@ -63,11 +56,16 @@ while node is not None: node = find_lowest_cost_node(costs) print(f"lowest cost route: {costs['fin']}") -route = [] -next = "fin" -while next != "start": - route.append(next) - next = parents[next] -route.append("start") -print(f"route: {list(reversed(route))}") + +def get_full_route(): + route = [] + next = "fin" + while next != "start": + route.append(next) + next = parents[next] + route.append("start") + return list(reversed(route)) + + +print(f"route: {get_full_route()}") diff --git a/chapter9/ex171b.py b/chapter9/with_list_function/ex171b.py similarity index 65% rename from chapter9/ex171b.py rename to chapter9/with_list_function/ex171b.py index 6c7bcb2..c7cb802 100644 --- a/chapter9/ex171b.py +++ b/chapter9/with_list_function/ex171b.py @@ -4,30 +4,27 @@ import math logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) -graph = {} -graph["start"] = {} -graph["start"]["a"] = 10 -graph["a"] = {} -graph["a"]["c"] = 20 -graph["b"] = {} -graph["b"]["a"] = 1 -graph["b"]["c"] = 1 -graph["c"] = {} -graph["c"]["b"] = 1 -graph["c"]["fin"] = 30 -graph["fin"] = {} +graph = { + "start": {"a": 10}, + "a": {"c": 20}, + "b": {"a": 1, "c": 1}, + "c": {"b": 1, "fin": 30}, + "fin": {}, +} -costs = {} -costs["a"] = 10 -costs["b"] = math.inf -costs["c"] = math.inf -costs["fin"] = math.inf +costs = { + "a": 10, + "b": math.inf, + "c": math.inf, + "fin": math.inf, +} -parents = {} -parents["a"] = "start" -parents["b"] = None -parents["c"] = None -parents["fin"] = None +parents = { + "a": "start", + "b": None, + "c": None, + "fin": None, +} processed = set() diff --git a/chapter9/ex171c.py b/chapter9/with_list_function/ex171c.py similarity index 65% rename from chapter9/ex171c.py rename to chapter9/with_list_function/ex171c.py index 74e3cba..3e6fd4b 100644 --- a/chapter9/ex171c.py +++ b/chapter9/with_list_function/ex171c.py @@ -4,31 +4,28 @@ import math logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) -graph = {} -graph["start"] = {} -graph["start"]["a"] = 2 -graph["start"]["b"] = 2 -graph["a"] = {} -graph["a"]["b"] = 2 -graph["b"] = {} -graph["b"]["c"] = 2 -graph["b"]["fin"] = 2 -graph["c"] = {} -graph["c"]["fin"] = 2 -graph["fin"] = {} +graph = { + "start": {"a": 2, "b": 2}, + "a": {"b": 2}, + "b": {"c": 2, "fin": 2}, + "c": {"fin": 2}, + "fin": {}, +} -costs = {} -costs["a"] = 2 -costs["b"] = 2 -costs["c"] = math.inf -costs["fin"] = math.inf +costs = { + "a": 2, + "b": 2, + "c": math.inf, + "fin": math.inf, +} -parents = {} -parents["a"] = "start" -parents["b"] = "start" -parents["c"] = None -parents["fin"] = None +parents = { + "a": "start", + "b": "start", + "c": None, + "fin": None, +} processed = set()