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
| def bfs(graph, start_vertex, target): | |
| path = [start_vertex] | |
| vertex_and_path = [start_vertex, path] | |
| bfs_queue = [vertex_and_path] | |
| visited = set() | |
| paths = {} | |
| counter = 0 | |
| while bfs_queue: | |
| print('-'*24) |
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
| def dfs(graph, current_vertex, target, visited=None): | |
| if visited == None: | |
| visited = [] | |
| visited.append(current_vertex) | |
| if current_vertex == target: | |
| return visited | |
| else: | |
| for neighbour in graph[current_vertex]: | |
| if not neighbour in visited: | |
| path = dfs(graph, neighbour, target, visited) |
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
| from Tree import Tree | |
| from collections import deque | |
| def bfs(root, count=0): | |
| path_queue = deque() | |
| initial_path = [root] | |
| path_queue.appendleft(initial_path) | |
| while path_queue: | |
| if count == 0 or len(path_queue) == 2: |
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
| from tree import TreeNode | |
| def dfs(root_node, goal, path=(), layers_lst=[], root=None): | |
| if root == None: | |
| root = root_node | |
| path = path + (root_node,) | |
| current_level_lst=[] | |
| for i in path: | |
| if not i.value in current_level_lst: |
NewerOlder