mirror of
				https://github.com/onyx-and-iris/grokking-algorithms.git
				synced 2025-10-31 20:41:46 +00:00 
			
		
		
		
	adds chapter9 exercises with heap and without
This commit is contained in:
		
							parent
							
								
									0cb2160e9e
								
							
						
					
					
						commit
						54e59680b7
					
				
							
								
								
									
										51
									
								
								chapter9/with_heap/ex171a.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								chapter9/with_heap/ex171a.py
									
									
									
									
									
										Normal file
									
								
							| @ -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()}") | ||||||
							
								
								
									
										36
									
								
								chapter9/with_heap/ex171b.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								chapter9/with_heap/ex171b.py
									
									
									
									
									
										Normal file
									
								
							| @ -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']}") | ||||||
							
								
								
									
										37
									
								
								chapter9/with_heap/ex171c.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								chapter9/with_heap/ex171c.py
									
									
									
									
									
										Normal file
									
								
							| @ -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']}") | ||||||
| @ -4,37 +4,30 @@ import math | |||||||
| logging.basicConfig(level=logging.DEBUG) | logging.basicConfig(level=logging.DEBUG) | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| graph = {} | graph = { | ||||||
| graph["start"] = {} |     "start": {"a": 5, "b": 2}, | ||||||
| graph["start"]["a"] = 5 |     "a": {"b": 8, "c": 4, "d": 2}, | ||||||
| graph["start"]["b"] = 2 |     "b": {"a": 8, "d": 7}, | ||||||
| graph["a"] = {} |     "c": {"d": 6, "fin": 3}, | ||||||
| graph["a"]["b"] = 8 |     "d": {"fin": 1}, | ||||||
| graph["a"]["c"] = 4 |     "fin": {}, | ||||||
| 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"] = {} |  | ||||||
| 
 | 
 | ||||||
| costs = {} | costs = { | ||||||
| costs["a"] = 5 |     "a": 5, | ||||||
| costs["b"] = 2 |     "b": 2, | ||||||
| costs["c"] = math.inf |     "c": math.inf, | ||||||
| costs["d"] = math.inf |     "d": math.inf, | ||||||
| costs["fin"] = math.inf |     "fin": math.inf, | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| parents = {} | parents = { | ||||||
| parents["a"] = "start" |     "a": "start", | ||||||
| parents["b"] = "start" |     "b": "start", | ||||||
| parents["c"] = None |     "c": None, | ||||||
| parents["d"] = None |     "d": None, | ||||||
| parents["fin"] = None |     "fin": None, | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| processed = set() | processed = set() | ||||||
| 
 | 
 | ||||||
| @ -63,11 +56,16 @@ while node is not None: | |||||||
|     node = find_lowest_cost_node(costs) |     node = find_lowest_cost_node(costs) | ||||||
| 
 | 
 | ||||||
| print(f"lowest cost route: {costs['fin']}") | print(f"lowest cost route: {costs['fin']}") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def get_full_route(): | ||||||
|     route = [] |     route = [] | ||||||
|     next = "fin" |     next = "fin" | ||||||
|     while next != "start": |     while next != "start": | ||||||
|         route.append(next) |         route.append(next) | ||||||
|         next = parents[next] |         next = parents[next] | ||||||
|     route.append("start") |     route.append("start") | ||||||
|  |     return list(reversed(route)) | ||||||
| 
 | 
 | ||||||
| print(f"route: {list(reversed(route))}") | 
 | ||||||
|  | print(f"route: {get_full_route()}") | ||||||
| @ -4,30 +4,27 @@ import math | |||||||
| logging.basicConfig(level=logging.DEBUG) | logging.basicConfig(level=logging.DEBUG) | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| graph = {} | graph = { | ||||||
| graph["start"] = {} |     "start": {"a": 10}, | ||||||
| graph["start"]["a"] = 10 |     "a": {"c": 20}, | ||||||
| graph["a"] = {} |     "b": {"a": 1, "c": 1}, | ||||||
| graph["a"]["c"] = 20 |     "c": {"b": 1, "fin": 30}, | ||||||
| graph["b"] = {} |     "fin": {}, | ||||||
| graph["b"]["a"] = 1 | } | ||||||
| graph["b"]["c"] = 1 |  | ||||||
| graph["c"] = {} |  | ||||||
| graph["c"]["b"] = 1 |  | ||||||
| graph["c"]["fin"] = 30 |  | ||||||
| graph["fin"] = {} |  | ||||||
| 
 | 
 | ||||||
| costs = {} | costs = { | ||||||
| costs["a"] = 10 |     "a": 10, | ||||||
| costs["b"] = math.inf |     "b": math.inf, | ||||||
| costs["c"] = math.inf |     "c": math.inf, | ||||||
| costs["fin"] = math.inf |     "fin": math.inf, | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| parents = {} | parents = { | ||||||
| parents["a"] = "start" |     "a": "start", | ||||||
| parents["b"] = None |     "b": None, | ||||||
| parents["c"] = None |     "c": None, | ||||||
| parents["fin"] = None |     "fin": None, | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| processed = set() | processed = set() | ||||||
| 
 | 
 | ||||||
| @ -4,31 +4,28 @@ import math | |||||||
| logging.basicConfig(level=logging.DEBUG) | logging.basicConfig(level=logging.DEBUG) | ||||||
| logger = logging.getLogger(__name__) | logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
| graph = {} | graph = { | ||||||
| graph["start"] = {} |     "start": {"a": 2, "b": 2}, | ||||||
| graph["start"]["a"] = 2 |     "a": {"b": 2}, | ||||||
| graph["start"]["b"] = 2 |     "b": {"c": 2, "fin": 2}, | ||||||
| graph["a"] = {} |     "c": {"fin": 2}, | ||||||
| graph["a"]["b"] = 2 |     "fin": {}, | ||||||
| graph["b"] = {} | } | ||||||
| graph["b"]["c"] = 2 |  | ||||||
| graph["b"]["fin"] = 2 |  | ||||||
| graph["c"] = {} |  | ||||||
| graph["c"]["fin"] = 2 |  | ||||||
| graph["fin"] = {} |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| costs = {} | costs = { | ||||||
| costs["a"] = 2 |     "a": 2, | ||||||
| costs["b"] = 2 |     "b": 2, | ||||||
| costs["c"] = math.inf |     "c": math.inf, | ||||||
| costs["fin"] = math.inf |     "fin": math.inf, | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| parents = {} | parents = { | ||||||
| parents["a"] = "start" |     "a": "start", | ||||||
| parents["b"] = "start" |     "b": "start", | ||||||
| parents["c"] = None |     "c": None, | ||||||
| parents["fin"] = None |     "fin": None, | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| processed = set() | processed = set() | ||||||
| 
 | 
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user