Skip to content

Instantly share code, notes, and snippets.

@qpwo
qpwo / dijkstra.py
Last active September 22, 2022 21:01 — forked from potpath/dijkstra.py
Dijkstra's Algorithm in python using PriorityQueue. Quits early upon goal discovery. More like uniform cost search actually. Fast.
from queue import PriorityQueue # essentially a binary heap
def dijkstra(G, start, goal):
""" Uniform-cost search / dijkstra """
visited = set()
cost = {start: 0}
parent = {start: None}
todo = PriorityQueue()
todo.put((0, start))