from collections import deque
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
def bfs(graph, start):
visited = []
queue = deque([start])
while queue:
node = queue.popleft()
if node not in visited:
visited.append(node)
queue.extend(graph[node])
return visited
def dfs(graph, start):
visited = []
stack = [start]
while stack:
node = stack.pop()
if node not in visited:
visited.append(node)
stack.extend(reversed(graph[node]))
return visited
bfs_result = bfs(graph, 'A')
dfs_result = dfs(graph, 'A')
print("BFS:", bfs_result)
print("DFS:", dfs_result)
ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVxdWUKCmdyYXBoID0gewogICAgJ0EnOiBbJ0InLCAnQyddLAogICAgJ0InOiBbJ0QnLCAnRSddLAogICAgJ0MnOiBbJ0YnXSwKICAgICdEJzogW10sCiAgICAnRSc6IFsnRiddLAogICAgJ0YnOiBbXQp9CgpkZWYgYmZzKGdyYXBoLCBzdGFydCk6CiAgICB2aXNpdGVkID0gW10KICAgIHF1ZXVlID0gZGVxdWUoW3N0YXJ0XSkKICAgIHdoaWxlIHF1ZXVlOgogICAgICAgIG5vZGUgPSBxdWV1ZS5wb3BsZWZ0KCkKICAgICAgICBpZiBub2RlIG5vdCBpbiB2aXNpdGVkOgogICAgICAgICAgICB2aXNpdGVkLmFwcGVuZChub2RlKQogICAgICAgICAgICBxdWV1ZS5leHRlbmQoZ3JhcGhbbm9kZV0pCiAgICByZXR1cm4gdmlzaXRlZAoKZGVmIGRmcyhncmFwaCwgc3RhcnQpOgogICAgdmlzaXRlZCA9IFtdCiAgICBzdGFjayA9IFtzdGFydF0KICAgIHdoaWxlIHN0YWNrOgogICAgICAgIG5vZGUgPSBzdGFjay5wb3AoKQogICAgICAgIGlmIG5vZGUgbm90IGluIHZpc2l0ZWQ6CiAgICAgICAgICAgIHZpc2l0ZWQuYXBwZW5kKG5vZGUpCiAgICAgICAgICAgIHN0YWNrLmV4dGVuZChyZXZlcnNlZChncmFwaFtub2RlXSkpCiAgICByZXR1cm4gdmlzaXRlZAoKYmZzX3Jlc3VsdCA9IGJmcyhncmFwaCwgJ0EnKQpkZnNfcmVzdWx0ID0gZGZzKGdyYXBoLCAnQScpCgpwcmludCgiQkZTOiIsIGJmc19yZXN1bHQpCnByaW50KCJERlM6IiwgZGZzX3Jlc3VsdCkK
('BFS:', ['A', 'B', 'C', 'D', 'E', 'F'])
('DFS:', ['A', 'B', 'D', 'E', 'F', 'C'])