Learn

Great work! You can now use a variety of tools to traverse and search your graphs. Let’s recap the types of graph search functions we’ve implemented.

  • Depth-First Search (DFS): DFS explores nodes by starting at a root node and exploring each branch as far as it can. Once a branch is complete, DFS backtracks and searches the next branch.
  • Breadth-First Search (BFS): BFS explores nodes by starting at a root node and exploring each neighboring node before continuing to each neighboring nodes’ child nodes. Once a layer is complete, BFS continues to the next layer.

Let’s call our new algorithms on some data and see how they perform!

Instructions

1.

Create a constant dfs and assign it to a call of dfs(startingAt:) on graph, passing in the node atlanta. Print out dfs to the terminal.

2.

Create a constant bfs and assign it to a call of bfs(startingAt:) on graph, passing in the node atlanta. Print out bfs to the terminal.

Notice how this returns the nodes in a different order than the depth-first search!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?