def test_getEdgesColors(self): n = Node(0, 1) n.addEdge(2) n.addEdge(3) graph = dict([(0, n), (1, Node(1, 3, 2)), (2, Node(2, 3, 1)), (3, Node(3, 3, 5)), (4, Node(4, 3, 7))]) self.assertEqual(n.getEdgesColors(graph), set([2, 1, 5]))
def test_connected_graph(self): a, b, c = [Node('a'), Node('b'), Node('c')] a.add_edge(b) b.add_edge(c) self.assertFalse(has_cycle([a, b, c])) c.add_edge(a) self.assertTrue(has_cycle([a, b, c]))
def test_deepest(self): a, b, c, d = Node('a'), Node('b'), Node('c'), Node('d') root = a root.left = b b.left = d root.right = c self.assertEqual(deepest(root), (d, 3))
def test_solveWithBFS(self): n0 = Node(0, 1) n1 = Node(1, 2) n1.addEdge(0) n1.addEdge(3) n2 = Node(2, 1) n3 = Node(3, 1) graph = dict([(0, n0), (1, n1), (2, n2), (3, n3)]) self.assertEqual(solver.solveWithBFS(graph), (2, [0, 1, 0, 0]))
def test_disconnected_graph(self): a, b, c, d, e = [Node('a'), Node('b'), Node('c'), Node('d'), Node('e')] # First cluster (no cycle) a.add_edge(b) # Second cluster (with cycle) c.add_edge(d) d.add_edge(e) e.add_edge(c) self.assertTrue(has_cycle([a, b, c, d, e]))
def test_connected_graph(self): a, b, c, d, e = Node('a'), Node('b'), Node('c'), Node('d'), Node('e') source, target = a, e # First path (distance: 30) a.add_edge(b, 10); b.add_edge(a, 10); b.add_edge(c, 10); c.add_edge(b, 10); c.add_edge(e, 10); e.add_edge(c, 10); # Second path (distance: 35) a.add_edge(d, 25); d.add_edge(a, 25); d.add_edge(e, 10); e.add_edge(d, 10); g = Graph([a,b,c,d,e]) self.assertEqual(g.shortest_path(a, e), [a, b, c, e])
def test_getEdgesColorsByDegree(self): n = Node(0, 1) n.addEdge(2) n.addEdge(3) n.addEdge(5) n.addEdge(6) n.addEdge(7) graph = dict([(0, n), (1, Node(1, 3, 5)), (2, Node(2, 3, 1)), (3, Node(3, 3, 2)), (4, Node(4, 3, 7)), (5, Node(5, 3, 2)), (6, Node(6, 3, 2)), (7, Node(7, 3, 5))]) self.assertEqual(n.getEdgesColorsByDegree(graph, [2, 5]), [(5, [1, 7]), (2, [3, 5, 6])])
def test_evaluate(self): # Single sum: (15 + 5) root = Node(solver.PLUS, Node(15), Node(5)) self.assertEqual(evaluate(root), 20) # Single subtraction: (15 - 5) root = Node(solver.MINUS, Node(15), Node(5)) self.assertEqual(evaluate(root), 10) # Single multiplication: (15 * 5) root = Node(solver.TIMES, Node(15), Node(5)) self.assertEqual(evaluate(root), 75) # Single division: (15 / 5) root = Node(solver.DIVIDE, Node(15), Node(5)) self.assertEqual(evaluate(root), 3) # Chained operations: (3 + 2) * (4 + 5) root = Node(solver.TIMES, Node(solver.PLUS, Node(3), Node(2)), Node(solver.PLUS, Node(4), Node(5))) self.assertEqual(evaluate(root), 45)
def test_invert_recursively(self): root = Node('a') root.left = Node('b') root.right = Node('c') root.left.left = Node('d') root.left.right = Node('e') root.right.left = Node('f') self.assertEqual(root.string(), "abdecf") Solution().invert_recursively(root) self.assertEqual(root.string(), "acfbed")
#return states[0] n = 4 m = 4 x = 0 y = 0 x = 1 matrix = [[" " for j in range(m)] for i in range(n)] for i in range(n): for j in range(m): matrix[i][j] = x x = x + 1 matrix[n - 1][m - 1] = " " start_state = Node(matrix, 0, None) num = randint(0, 100) print("The maximum number of moves required = " + str(num)) for i in range(num): states = start_state.generate_successors() shuffle(states) start_state = Node(states[0].matrix, 0, None) #matrix = [[1,2,3,4],[5,6,12,7],[9,10," ",11],[13,14,15,8]] #start_state = Node(matrix, 0,None) start_state.print_matrix() curr_state = start_state frontier = []
def test_full_binary_tree(self): # 1 1 # / \ / \ # 2 3 ----> 0 3 # / / \ / \ # 0 9 4 9 4 root = Node(1, Node(2, Node(0)), Node(3, Node(9), Node(4))) expected_str_output = "1\n03\n94" self.assertEqual(str(full_binary_tree(root)), expected_str_output) # 1 1 # / \ / \ # 3 2 ----> 3 4 # / \ \ / \ # 0 9 4 0 9 root = Node(1, Node(3, Node(0), Node(9)), Node(2, right=Node(4))) expected_str_output = "1\n34\n09" self.assertEqual(str(full_binary_tree(root)), expected_str_output) # 1 3 # / ----> # 3 root = Node(1, Node(3)) expected_str_output = "3" self.assertEqual(str(full_binary_tree(root)), expected_str_output) # 1 3 # \ ----> # 3 root = Node(1, right=Node(3)) expected_str_output = "3" self.assertEqual(str(full_binary_tree(root)), expected_str_output)
def test_values_at_height(self): root = Node(1) self.assertEqual(values_at_height(root, 1), [1]) root = Node(1, Node(2), Node(3)) self.assertEqual(values_at_height(root, 2), [2, 3]) root = Node(1, Node(3)) self.assertEqual(values_at_height(root, 2), [3]) root = Node(1, Node(2), Node(3)) self.assertEqual(values_at_height(root, 5), []) root = Node(1, Node(2, Node(4), Node(5)), Node(3, right=Node(7))) self.assertEqual(values_at_height(root, 3), [4, 5, 7])
def test_level_order(self): root = Node(1, Node(2), Node(3, Node(4), Node(5))) self.assertEqual(level_order(root), [1, 2, 3, 4, 5]) root = Node(1, right=Node(3, right=Node(5))) self.assertEqual(level_order(root), [1, 3, 5]) root = Node(1, left=Node(2, left=Node(4))) self.assertEqual(level_order(root), [1, 2, 4]) root = Node(1) self.assertEqual(level_order(root), [1]) root = None self.assertEqual(level_order(root), [])
def dynamicMLA(maze, agent, reserved, start_wait = 0, resting = []): """Returns a list of tuples as a path from the given start to the given end in the given maze""" # copy waypoints waypoints = agent.waypoints.copy() # Initialize both open and closed list open_list = [] closed_list = set() # Last label belongs to end node and is equal to number of waypoints + 1. Eg. 2 for an agent with 1 waypoint. start_node = Node(None, agent.start) start_node.g = start_node.h = start_node.f = 0 if start_wait > 0: wait = construct_wait(start_node, 0, start_wait) if len(agent.waypoints) == 0: wait.l = 2 open_list.append(wait) else: # Add the start node if len(agent.waypoints) == 0: start_node.l = 2 open_list.append(start_node) # If we have waypoints, create waypoint node and set it to be the first waypoint. if len(agent.waypoints) > 0: waypoint = Node(None, select_waypoint(agent.start, waypoints)) waypoint.g = waypoint.h = waypoint.f = 0 else: waypoint = Node(None, (-1, -1)) # mid_node = Node(None, agent.waypoints[0]) # mid_node.g = mid_node.h = mid_node.f = 0 end_node = Node(None, agent.end) end_node.g = end_node.h = end_node.f = 0 # Loop until you find the end while len(open_list) > 0: # Get the current node current_node = heappop(open_list) closed_list.add(current_node) if current_node.position != agent.end and current_node.position in resting: if len(open_list) > 0: continue # TODO Do more efficiently. Checks if this space is reserved to avoid swap conflicts. if current_node.position in reserved and current_node.g + 1 in reserved[current_node.position]: continue # Found the waypoint if current_node.l == 1 and current_node == waypoint: # construct new search node. n_prime = Node(current_node.parent, current_node.position) n_prime.g = current_node.g # Waypoint is now the next waypoint. if len(waypoints) > 0: waypoint = Node(None, select_waypoint(current_node.position, waypoints)) n_prime.h = abs(current_node.position[0] - waypoint.position[0]) + abs( current_node.position[1] - waypoint.position[1]) else: n_prime.l = 2 waypoint = Node(None, (-1, -1)) n_prime.h = abs(current_node.position[0] - end_node.position[0]) + abs( current_node.position[1] - end_node.position[1]) # N_prime is the new search node, which has the same position as the current node, but with new label and new h. n_prime.f = n_prime.g + n_prime.h closed_list = set() open_list = [] heappush(open_list, n_prime) continue if current_node.l == 2 and current_node == end_node: path = [] current = current_node while current is not None: path.append(current) current = current.parent return path[::-1] # Return reversed path # Loop through children # for child in map(lambda x: Node(current_node, x), # maze.get_neighbours(current_node.position[0], current_node.position[1])): # if child.position in reserved and current_node.g + 1 in reserved[child.position]: # collision = True # # Check if we can wait here. If not we dont consider this step. # if current_node.position in reserved and current_node.g + 1 in reserved[child.position]: # break # # We can wait, so add a node to wait and then move into that position. # elif child.position in reserved and current_node.g + 2 in reserved[child.position]: # break # else: # wait = Node(current_node, current_node.position) # wait.g = current_node.g + 1 # wait_move = Node(wait, child.position) # wait_move.g = current_node.g + 2 # if child.l == 1: # child.h = abs(current_node.position[0] - waypoint.position[0]) + abs( # current_node.position[1] - waypoint.position[1]) # else: # child.h = abs(child.position[0] - end_node.position[0]) + abs( # child.position[1] - end_node.position[1]) # wait_move.f = wait_move.g + wait_move.f # heappush(open_list, wait_move) for child in map(lambda x: Node(current_node, x), maze.get_neighbours(current_node.position[0], current_node.position[1])): # TODO USE HASHSET if child.position in reserved and current_node.g + 1 in reserved[child.position]: # Check if we can wait here. If not we dont consider this step. if current_node.position in reserved and current_node.g + 1 in reserved[child.position]: break # We can wait, so add a node to wait and then move into that position. elif child.position in reserved and current_node.g + 2 in reserved[child.position]: break else: wait = Node(current_node, current_node.position) wait.g = current_node.g + 1 wait_move = Node(wait, child.position) wait_move.g = current_node.g + 2 if child.l == 1: child.h = abs(current_node.position[0] - waypoint.position[0]) + abs( current_node.position[1] - waypoint.position[1]) else: child.h = abs(child.position[0] - end_node.position[0]) + abs( child.position[1] - end_node.position[1]) wait_move.f = wait_move.g + wait_move.f heappush(open_list, wait_move) continue # In case of collision # Child is on the closed list if child in closed_list: continue if child in open_list: continue # Create the f, g, and h values child.g = current_node.g + 1 child.l = current_node.l if child.l == 1: child.h = abs(current_node.position[0] - waypoint.position[0]) + abs(current_node.position[1] - waypoint.position[1]) else: child.h = abs(child.position[0] - end_node.position[0]) + abs(child.position[1] - end_node.position[1]) child.f = child.g + child.h # Add the child to the open list heappush(open_list, child) if start_wait == 0: return dynamicMLA(maze, agent, reserved, 1, resting) else: return dynamicMLA(maze, agent, reserved, start_wait + 1, resting)
def test_remove_middle(self): head = Node(1, Node(2, Node(3, Node(4, Node(5))))) self.assertEqual(str(head), "[1, 2, 3, 4, 5]") head = remove_kth_last(head, 3) self.assertEqual(str(head), "[1, 2, 4, 5]")
def test_largest_bst(self): # BST on root + left branch root = Node(5, Node(4, Node(3), Node(7)), Node(6, Node(8), Node(9))) self.assertEqual(largest_bst(root), 3) # BST on root + right branch root = Node(5, Node(4, Node(6), Node(7)), Node(6, Node(2), Node(8))) self.assertEqual(largest_bst(root), 3) # Full tree is a BST root = Node(5, Node(4, Node(2, Node(0), Node(3)), Node(6)), Node(6)) self.assertEqual(largest_bst(root), 4) # BST on a subtree in the left node root = Node(5, Node(8, Node(4, Node(1)), Node(9)), Node(9, Node(10), Node(11))) self.assertEqual(largest_bst(root), 3) # BST on a subtree in the right node root = Node(5, Node(8, Node(10), Node(11)), Node(4, Node(2, Node(1)), Node(6))) self.assertEqual(largest_bst(root), 3) # No subtree is a BST root = Node(5, Node(6, Node(9), Node(5)), Node(4, Node(6), Node(2))) self.assertEqual(largest_bst(root), 1) # Any leaf is a BST by definition # Root with no children root = Node(1) self.assertEqual(largest_bst(root), 1) # Root is 'None' root = None self.assertEqual(largest_bst(root), 0)
def test_is_bst(self): root = Node(5, Node(1), Node(4, Node(3), Node(6))) self.assertFalse(is_bst(root)) root = Node(5, Node(3, Node(2), Node(4)), Node(7, Node(6), Node(8))) self.assertTrue(is_bst(root)) root = Node(0) self.assertTrue(is_bst(root)) root = Node(2, Node(1)) self.assertTrue(is_bst(root)) root = Node(2, right=Node(3)) self.assertTrue(is_bst(root))