Notes

Search

Search IconIcon to open search

Tree vs Graph Traversals

Last updated Dec 31, 2022

Just as there are many tree traversals:

DFS Preorder

What we did in DepthFirstPaths is called “DFS Preorder”: Action is before DFS.

In a graph, we choose a particular source. We also have to decide how to deal with “tie-breaking”.

DFS Postorder

We could also do a “DFS Postorder” traversal, where our action is after the DFS.

example:

dfs(s):

This would print the nodes in order of dfs returns.

These are analogs to the tree traversals. There is also an analog to level order traversal.

BFS order

BFS stands for Breadth First Search.

BFS Order: Act in order of distance from source.