Skip to main content

Section 12.2 Graph Traversals

Graph traversal, similar to tree traversal, involves visiting vertices in a specific order based on the graph’s topology. Just as tree traversals have various orders like preorder, inorder, and postorder, graph traversals also have different orders, each suited for specific applications. For example, in artificial intelligence programming, graph traversal is crucial to solving problems involving a collection of states connected by edges.
Graph traversal algorithms start from a designated start vertex and aim to visit all remaining vertices. These algorithms face challenges, such as dealing with disconnected graphs where not all vertices are reachable from the start vertex, or handling cycles without getting stuck in infinite loops.
To address these issues, graph traversal algorithms utilize a "VISITED" flag to keep track of visited vertices. At the beginning of the traversal, no vertices have the "VISITED" flag set. As the algorithm visits a vertex, it marks it as "VISITED" to avoid revisiting it. This mechanism prevents infinite loops when encountering cycles.
Once the traversal completes, we can check if all vertices have the "VISITED" flag set to ensure that all vertices have been processed. If some vertices remain unflagged, we can continue the traversal from another unvisited vertex. Remarkably, this process applies to both directed and undirected graphs.
Here is a link to the OpenDSA textbook that has great visualizations of graph traversals: Read Me 1 
You have attempted of activities on this page.
opendsa-server.cs.vt.edu/OpenDSA/Books/Everything/html/GraphTraversal.html