Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Queue import *
- graph = {
- '1': ['2', '3'],
- '2': ['4', '5'],
- '5': ['6', '7']
- }
- #from enum import Enum
- #ColorCode = Enum('ColorCode', 'white grey black')
- def bfs (source):
- queue = []
- #visited = []
- queue.append(source)
- while queue:
- path = queue.pop(0)
- node = path[-1]
- print (node + " -> ")
- for adjacent in graph.get(node, []):
- new_path = list(path)
- new_path.append(adjacent)
- node = new_path[-1]
- print (adjacent)
- queue.append(new_path)
- bfs('1')
- color = []
- for i in range(30):
- color.append(0)
- def dfs (source):
- for adjacent in graph.get(source, []):
- new_path = []
- new_path.append(adjacent)
- node = new_path[-1]
- dfs (node)
- print (node)
- dfs('1')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement