mirror of
https://github.com/onyx-and-iris/grokking-algorithms.git
synced 2026-04-16 11:33:34 +00:00
adds chapter9 exercises with heap and without
This commit is contained in:
71
chapter9/with_list_function/ex171a.py
Normal file
71
chapter9/with_list_function/ex171a.py
Normal file
@@ -0,0 +1,71 @@
|
||||
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": {},
|
||||
}
|
||||
|
||||
costs = {
|
||||
"a": 5,
|
||||
"b": 2,
|
||||
"c": math.inf,
|
||||
"d": math.inf,
|
||||
"fin": math.inf,
|
||||
}
|
||||
|
||||
parents = {
|
||||
"a": "start",
|
||||
"b": "start",
|
||||
"c": None,
|
||||
"d": None,
|
||||
"fin": None,
|
||||
}
|
||||
|
||||
processed = set()
|
||||
|
||||
|
||||
def find_lowest_cost_node(costs):
|
||||
lowest_cost = math.inf
|
||||
lowest_cost_node = None
|
||||
for node in costs:
|
||||
cost = costs[node]
|
||||
if cost < lowest_cost and node not in processed:
|
||||
lowest_cost = cost
|
||||
lowest_cost_node = node
|
||||
return lowest_cost_node
|
||||
|
||||
|
||||
node = find_lowest_cost_node(costs)
|
||||
while node is not None:
|
||||
cost = costs[node]
|
||||
neighbors = graph[node]
|
||||
for n in neighbors.keys():
|
||||
new_cost = cost + neighbors[n]
|
||||
if costs[n] > new_cost:
|
||||
costs[n] = new_cost
|
||||
parents[n] = node
|
||||
processed.add(node)
|
||||
node = find_lowest_cost_node(costs)
|
||||
|
||||
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()}")
|
||||
55
chapter9/with_list_function/ex171b.py
Normal file
55
chapter9/with_list_function/ex171b.py
Normal file
@@ -0,0 +1,55 @@
|
||||
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": {},
|
||||
}
|
||||
|
||||
costs = {
|
||||
"a": 10,
|
||||
"b": math.inf,
|
||||
"c": math.inf,
|
||||
"fin": math.inf,
|
||||
}
|
||||
|
||||
parents = {
|
||||
"a": "start",
|
||||
"b": None,
|
||||
"c": None,
|
||||
"fin": None,
|
||||
}
|
||||
|
||||
processed = set()
|
||||
|
||||
|
||||
def find_lowest_cost_node(costs):
|
||||
lowest_cost = math.inf
|
||||
lowest_cost_node = None
|
||||
for node in costs:
|
||||
cost = costs[node]
|
||||
if cost < lowest_cost and node not in processed:
|
||||
lowest_cost = cost
|
||||
lowest_cost_node = node
|
||||
return lowest_cost_node
|
||||
|
||||
|
||||
node = find_lowest_cost_node(costs)
|
||||
while node is not None:
|
||||
cost = costs[node]
|
||||
neighbors = graph[node]
|
||||
for n in neighbors.keys():
|
||||
new_cost = cost + neighbors[n]
|
||||
if costs[n] > new_cost:
|
||||
costs[n] = new_cost
|
||||
parents[n] = node
|
||||
processed.add(node)
|
||||
node = find_lowest_cost_node(costs)
|
||||
|
||||
print(f"lowest cost route: {costs['fin']}")
|
||||
56
chapter9/with_list_function/ex171c.py
Normal file
56
chapter9/with_list_function/ex171c.py
Normal file
@@ -0,0 +1,56 @@
|
||||
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": {},
|
||||
}
|
||||
|
||||
|
||||
costs = {
|
||||
"a": 2,
|
||||
"b": 2,
|
||||
"c": math.inf,
|
||||
"fin": math.inf,
|
||||
}
|
||||
|
||||
parents = {
|
||||
"a": "start",
|
||||
"b": "start",
|
||||
"c": None,
|
||||
"fin": None,
|
||||
}
|
||||
|
||||
processed = set()
|
||||
|
||||
|
||||
def find_lowest_cost_node(costs):
|
||||
lowest_cost = math.inf
|
||||
lowest_cost_node = None
|
||||
for node in costs:
|
||||
cost = costs[node]
|
||||
if cost < lowest_cost and node not in processed:
|
||||
lowest_cost = cost
|
||||
lowest_cost_node = node
|
||||
return lowest_cost_node
|
||||
|
||||
|
||||
node = find_lowest_cost_node(costs)
|
||||
while node is not None:
|
||||
cost = costs[node]
|
||||
neighbors = graph[node]
|
||||
for n in neighbors.keys():
|
||||
new_cost = cost + neighbors[n]
|
||||
if costs[n] > new_cost:
|
||||
costs[n] = new_cost
|
||||
parents[n] = node
|
||||
processed.add(node)
|
||||
node = find_lowest_cost_node(costs)
|
||||
|
||||
print(f"lowest cost route: {costs['fin']}")
|
||||
Reference in New Issue
Block a user