Created
October 28, 2022 16:27
-
-
Save josiahcoad/df4be5171b9de155ded15c25df0e95be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Callable, Tuple | |
from copy import deepcopy | |
from dataclasses import dataclass, field | |
from typing import Dict, List, Tuple | |
from copy import deepcopy | |
import heapq | |
@dataclass | |
class Node: | |
matrix: Matrix | |
parent: 'Node' = None | |
cost: float = float('inf') | |
depth: int = float('inf') | |
def __hash__(self) -> int: | |
return hash(str(self.matrix)) | |
@dataclass(order=True) | |
class PriorityItem: | |
priority: int | |
item: Node = field(compare=False) | |
class PriorityQueue: | |
def __init__(self) -> None: | |
self._list = [] | |
def add(self, node: Node) -> None: | |
heapq.heappush(self._list, PriorityItem(node.cost + node.depth, node)) | |
def pop(self) -> Node: | |
return heapq.heappop(self._list).item | |
def isempty(self) -> bool: | |
return len(self._list) == 0 | |
def __contains__(self, node: Node) -> bool: | |
return node in self._list | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment