Graph Traversal
Last updated
Last updated
Two typical and common graph traversal algorithms are DFS and BFS.
DFS has the idea to reach all the way down on the depth of a graph before moving on to the next one.
DFS is usually written recursively (which DOES means to be careful of StackOverflow), with an adjacency list and a boolean visited arrays.
Notes: If the graph is a tree, then the visited array is not necessary. The dfs function header can take in an extra parameter on the parent node, to make sure the next search does not go back up the tree.
BFS, on the other hand, traverses all nodes on each degree (or row, level, depth, whatever you wanna call it), before moving one depth further.
A queue is typically used for BFS and an iterative approach is usually taken.
https://leetcode.com/problems/even-odd-tree/description/ (BFS level order traversal)