2024-01-11 18:28:03 +00:00
|
|
|
import logging
|
|
|
|
from collections import deque
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Node:
|
|
|
|
name: str
|
|
|
|
distance: int
|
|
|
|
|
|
|
|
|
|
|
|
graph = {
|
|
|
|
"cab": [Node("cat", 1), Node("car", 1)],
|
|
|
|
"cat": [Node("mat", 0), Node("bat", 0)],
|
|
|
|
"car": [Node("cat", 0), Node("bar", 0)],
|
|
|
|
"mat": [Node("bat", 0)],
|
|
|
|
"bar": [Node("bat", 0)],
|
|
|
|
"bat": [],
|
|
|
|
}
|
|
|
|
|
2024-01-11 18:54:08 +00:00
|
|
|
visited = set()
|
2024-01-11 18:28:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def bfs():
|
|
|
|
queue = deque()
|
|
|
|
queue += graph["cab"]
|
|
|
|
|
|
|
|
while queue:
|
|
|
|
logger.debug(queue)
|
|
|
|
current_node = queue.popleft()
|
|
|
|
if current_node.name == "bat":
|
|
|
|
return current_node.distance
|
|
|
|
|
|
|
|
if current_node.name in visited:
|
|
|
|
continue
|
2024-01-11 18:54:08 +00:00
|
|
|
visited.add(current_node.name)
|
2024-01-11 18:28:03 +00:00
|
|
|
|
|
|
|
next_nodes = graph[current_node.name]
|
|
|
|
for node in next_nodes:
|
|
|
|
node.distance = current_node.distance + 1
|
|
|
|
queue += next_nodes
|
|
|
|
|
|
|
|
|
|
|
|
distance = bfs()
|
|
|
|
print(f"shortest distance from cab to bat: {distance}")
|