def test_that_sorting_arrays_succeeds(self): unsorted_array = [2, 1, 6, 3, 8, 4] sorted_array = quicksort(unsorted_array) assert sorted_array[0] == 1 assert sorted_array[len(unsorted_array) - 1] == 8
def get_edges(self): edges = [] node_idx = {x: i for i, x in enumerate(self.get_nodes())} for v, value in self.g_dict.iteritems(): for w, cost in value.iteritems(): if (cost, w, v) not in edges: edges.append((cost, node_idx[v], node_idx[w])) return quicksort(edges)
def depth_limited(problem, limit): """Depth Limited Search""" frontier = Stack() start_node = Node(problem['start']) frontier.push(start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count if node.depth == limit: pass else: # Expansion adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_node = Node(child_value, node) frontier.push(child_node) return None
def test_quicksort(self, _, n): expected = list(range(n)) if n else [] shuffled = copy(expected) shuffle(shuffled) assert quicksort.quicksort(shuffled) == expected
def general_search(problem): """Problem: {doamin, method, graph, coords, start, goal} method: B, D, I, U, A route: graph search (maintain a closed set) except Astar tsp: tree search """ method = problem['method'] if method not in ['B', 'D', 'I', 'U', 'A']: print("Enter Correct method!") return None if problem['start'] not in problem['graph'].get_nodes() or ( problem['domain'] == 'route' and problem['goal'] not in problem['graph'].get_nodes()): print("Enter Correct Start/Goal!") return None # BFS, DFS if method in ['B', 'D']: if method == 'B': frontier = Queue() else: frontier = Stack() start_node = Node(problem['start']) frontier.push(start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count # Expansion closed_set.add(node.value) adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_node = Node(child_value, node) frontier.push(child_node) return None """Uniform Cost Search / Astar Search""" if method in ['U', 'A']: frontier = PriorityQueue() start_node = Node(problem['start']) priority = 0 if method == 'U' else astar_heuristic(problem, start_node) frontier.push(priority, start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count # Expansion adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_cost = node.cost_so_far + \ problem['graph'].get_cost(node.value, child_value) # g_n child_node = Node(child_value, node, child_cost) priority = child_cost if method == 'U' else child_cost + \ astar_heuristic(problem, child_node) frontier.push(priority, child_node) return None """Iterative Deepening Search""" if method == 'I': def depth_limited(problem, limit): """Depth Limited Search""" frontier = Stack() start_node = Node(problem['start']) frontier.push(start_node) closed_set = set() while not frontier.isEmpty(): node = frontier.pop() closed_set.add(node.value) if goal_test(problem, node): path = node.get_path() cost = cal_path_cost(problem['graph'], path) return path, cost, frontier.count if node.depth == limit: pass else: # Expansion adj = problem['graph'].adj(node.value) for child_value in quicksort(adj): if push_or_not(problem, node, child_value, closed_set): child_node = Node(child_value, node) frontier.push(child_node) return None max_depth = 20 for i in range(max_depth): result = depth_limited(problem, i) if result: return result
def test_arr1_qs(): actual = quicksort(arr1, 0, right) expected = [4, 8, 15, 16, 23, 42] assert actual == expected
def test_arr3_qs(): actual = quicksort(arr3, 0, right) expected = [5, 5, 5, 7, 7, 12] assert actual == expected
def test_arr2_qs(): actual = quicksort(arr2, 0, right) expected = [-2, 5, 8, 12, 18, 20] assert actual == expected