Created
August 2, 2021 16:03
-
-
Save GEEGABYTE1/135dc59a60625ff7abf3ee87ade78578 to your computer and use it in GitHub Desktop.
Graph Search Algorithms
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) | |
if path: | |
return path | |
def bfs(graph, start_vertex, target): | |
path = [start_vertex] | |
vertex_and_path = [start_vertex, path] | |
bfs_queue = [vertex_and_path] | |
visited = set() | |
while bfs_queue: | |
current_vertex, path = bfs_queue.pop() | |
visited.add(current_vertex) | |
for neighbour in graph[current_vertex]: | |
if not neighbour in visited: | |
if neighbour == target: | |
path.append(neighbour) | |
return path | |
else: | |
path.append(neighbour) | |
bfs_queue.append([neighbour, path]) | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment