Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithms] BFS and DFS through a graph #33

Open
lpatmo opened this issue Mar 15, 2019 · 1 comment
Open

[Algorithms] BFS and DFS through a graph #33

lpatmo opened this issue Mar 15, 2019 · 1 comment

Comments

@lpatmo
Copy link
Member

lpatmo commented Mar 15, 2019

Prompt: Traverse through a graph using BFS and DFS. You can either do this iteratively or recursively.

@cs-cordero
Copy link

from collections import deque

def bfs(root: Node):
    queue = deque([root])
    while queue:
        node = queue.popleft()
        # do stuff with node
        queue.extend(node.children)


def dfs(node: Node):
    for child in node.children:
        dfs(child)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants