Last active
August 29, 2015 14:22
-
-
Save kartikkukreja/7c15d0d84f656e625fc0 to your computer and use it in GitHub Desktop.
Uniform Cost Search Algorithm
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
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