def set_path(self, bx, by, ex, ey, level, typ, key=[], ax=0, ay=0): if typ == 0: return bfs(bx, by, ex, ey, level) elif typ == 1: return rand() elif typ == 2 or typ == 3: direct='n' movement=0 if key[pygame.K_LEFT]: direct = 'x' movement = -1 if key[pygame.K_RIGHT]: direct = 'x' movement = 1 if key[pygame.K_UP]: direct = 'y' movement = -1 if key[pygame.K_DOWN]: direct = 'y' movement = 1 ahead_count = 0 x=ex y=ey while ahead_count<=5: ahead_count+=1 if direct=='x': if x+movement>=0 and x+movement<21 and level[y][x+movement]!='W': x+=movement else: break if direct=='y': if y+movement>=0 and y+movement<26 and level[y+movement][x]!='W': y+=movement else: break if typ == 2: return bfs(bx, by, x, y, level) if typ == 3: pos_x = 2*ax - x pos_y = 2*ay - y if pos_x < 0: pos_x = 0 elif pos_x > 21: pos_x = 20 if pos_y < 0: pos_y = 0 elif pos_y > 26: pos_y = 25 while level[pos_y][pos_x]!='W': if pos_x-1 < 0 and level[pos_y][pos_x-1]!='W': pos_x -= 1 elif pos_x+1 > 21 and level[pos_y][pos_x+1]!='W': pos_x += 1 elif pos_y-1 < 0 and level[pos_y-1][pos_x]!='W': pos_y -= 1 elif pos_y+1 > 25 and level[pos_y+1][pos_x]!='W': pos_y += 1 if level[pos_y][pos_x]!='W': break return bfs(bx, by, pos_x, pos_y, level)
def test_bfs(): # setting up the graph adj_mtx = [[0, 1, 1, 0], [0, 0, 1, 0], [1, 0, 0, 1], [0, 0, 0, 1]] # test bfs from different starting nodes assert bfs(adj_mtx, 0) == '0 1 2 3 ' assert bfs(adj_mtx, 1) == '1 2 0 3 ' assert bfs(adj_mtx, 2) == '2 0 3 1 ' assert bfs(adj_mtx, 3) == '3 '
def test_bfs(self): g = Graph() g.add_vertex(0) g.add_vertex(1) g.add_vertex(2) g.add_vertex(3) g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(1, 2) g.add_edge(1, 3) start = g.get_vertex(0) bfs(start) print(g)
def bfs_connected_components(graph): """ Finds all the connected subgraphs in a given undirected graph. It does so by iterating over all non-visited elements in a graph and running bfs over it. Complexity: O(n+m) Args: graph: instance of src.graph.Graph class which encapsulates all edges, vertices as well as values for edges and vertices. Returns: list, of lists of vertexes which are connected. Format [[vertex,..]] """ subgraphs = [] explored_vertices = [] for vertex in graph.get_vertices(): if vertex in explored_vertices: continue visited_vertices = bfs(graph, vertex) explored_vertices.extend(visited_vertices) subgraphs.append(visited_vertices) return subgraphs
def test_not_found_integer_value(self): """ Tests if a False boolean vaslue has been returned for not finding a value in the BST using a Breadth-First Search """ BST = create_bst() result = bfs(BST, 14) self.assertFalse(result)
def test_find_integer_value(self): """ Tests if a True boolean value has been returned for finding a value in the BST successfully using a Breadth-First Search """ BST = create_bst() result = bfs(BST, 3) self.assertTrue(result)
def test_result(self): print('=======================') print('BFS algorithm') print('=======================') result = bfs(State(red=13, blue=16, green=17)) final_state = result['final_state'] self.assertEqual(final_state.red_count, 0) self.assertEqual(final_state.green_count, 46) self.assertEqual(final_state.blue_count, 0) full_path = result['full_path'] print('Full path length: {}'.format(len(full_path))) self.assertEqual(len(full_path), 180169) path = result['path'] print('Path length is {}'.format(len(path))) self.assertEqual(len(path), 17) i = 1 for node in path: print('{}:{}'.format(i, node)) i += 1