Skip to content

Instantly share code, notes, and snippets.

@kartikkukreja
Last active August 29, 2015 14:22
Show Gist options
  • Save kartikkukreja/7c15d0d84f656e625fc0 to your computer and use it in GitHub Desktop.
Save kartikkukreja/7c15d0d84f656e625fc0 to your computer and use it in GitHub Desktop.
Uniform Cost Search Algorithm
import heapq
class PriorityQueue:
def __init__(self, priorityFunction):
self.priorityFunction = priorityFunction
self.heap = []
def push(self, item):
heapq.heappush(self.heap, (self.priorityFunction(item), item))
def pop(self):
(_, item) = heapq.heappop(self.heap)
return item
def empty(self):
return len(self.heap) == 0
def uniformCostSearch(problem):
return graphSearch(problem, PriorityQueue(lambda state: len(state[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment