Skip to content

Instantly share code, notes, and snippets.

@josiahcoad
Created October 28, 2022 16:27
Show Gist options
  • Save josiahcoad/df4be5171b9de155ded15c25df0e95be to your computer and use it in GitHub Desktop.
Save josiahcoad/df4be5171b9de155ded15c25df0e95be to your computer and use it in GitHub Desktop.
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