def bfs(problem): """ Breadth first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest or None if there is no solution """ closed = set() fringe = data_structures.Queue() state = problem.start_state() root = data_structures.Node(state) fringe.push(root) while True: if fringe.is_empty(): return None node = fringe.pop() if problem.is_goal(node.state): return node.solution() if node.state not in closed: closed.add(node.state) for child_state, action, action_cost in problem.successors( node.state): child_node = data_structures.Node(child_state, node, action) fringe.push(child_node)
def bfs(problem): """ Breadth first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest or None if there is no solution """ # Enter your code here and remove the pass statement below closed = set() # keep track of our explored states fringe = data_structures.Queue() # for BFS, the fringe is a Queue state = problem.start_state() root = data_structures.Node(state) fringe.push(root) while True: if fringe.is_empty(): return None # Failure - fringe is empty and no solution was found node = fringe.pop() if problem.is_goal(node.state): return node.solution() if node.state not in closed: # we are implementing graph search closed.add(node.state) for child_state, action, action_cost in problem.successors( node.state): child_node = data_structures.Node(child_state, node, action) fringe.push(child_node)
def bfs(problem): """ Breadth first graph search algorithm returns a solution for the given search problem :param problem (a Problem object) representing the quest see Problem class definition in spartanquest.py) :return: list of actions representing the solution to the quest or None if there is no solution """ closed = set() # keep track of our explored states fringe = data_structures.Queue() # for bfs, the fringe is a queue state = problem.start_state() root = data_structures.Node(state, None, None) fringe.push(root) while not fringe.is_empty(): node = fringe.pop() if problem.is_goal(node.state): return node.solution() # we found a solution if node.state not in closed: # we are implementing graph search closed.add(node.state) for child_state, action, action_cost in problem.expand(node.state): child_node = data_structures.Node(child_state, node, action) fringe.push(child_node) return None
def test_dequeue_empty(self): test_queue = data_structures.Queue() try: dequeued = test_queue.dequeue() except: pass else: assert False, "Expected an exception; instead got a Node with value '{}'.".format( dequeued.data)
def test_queue(self): x = data_structures.Queue() x.enqueue('lorem') x.enqueue('dim') x.enqueue('sum') x.enqueue('Cha') x.enqueue('siu') x.enqueue('bao') return x
assert stackwithwrapper.pop() == 3 assert stackwithwrapper.peek() == 2 assert stackwithwrapper.size() == 3 # test object reference is returned stackwithobj = ds.Stack([[0, 1], [2, 3], [4, 5]]) # stack of lists (stackwithobj.peek())[0] = 6 assert (stackwithobj.peek())[0] == 6 assert (stackwithobj.pop())[0] == 6 #================================== # QUEUE TESTS #================================== # test initialisation conditions queue = ds.Queue() assert queue.size() == 0 assert queue.isEmpty() # test push() conditions queue.offer('abc') assert queue.size() == 1 assert not queue.isEmpty() # test pop() conditions assert queue.poll() == 'abc' assert queue.size() == 0 assert queue.isEmpty() # test null cases assert queue.peek() == None