2025-11-17
traverseFromVertex(graph, startVertex, process):
mark all vertices in the graph as unvisited
add the startVertex to an empty collection
while the collection is not empty:
pop a vertex from the collection
if the vertex has not been visited:
mark the vertex as visited
process(vertex)
add all adjacent unvisited vertices to the collectionstartVertex are processed exactly onceDepth-first traversal – uses a stack as the collection in the generic algorithmBreadth-first traversal – uses a queue as the collection in the generic algorithmDepth-First Traversal (DFS):

This diagram shows the order in which the vertices are visited (which differ from the ID numbers used to identify vertices).
Breadth-First Traversal (BFS):

Here, you first visit A. Then you take all the vertices adjacent to A and insert each one into the queue as you visit and mark it.
Psedocode: Iterative vs Recursive functions
traverseAll(graph, process):
mark all vertices in the graph as unvisited
instantiate an empty collection
for each vertex in the graph:
if the vertex has not been visited:
add the vertex to the collection
while the collection is not empty:
pop a vertex from the collection
if the vertex has not been visited:
mark the vertex as visited
process(vertex)
add all adjacent unvisited vertices to the collectionPsedocode: Iterative vs Recursive functions
components = [{A, B, C}, {D, E}, {F}]traverseFromVertex shown in the Psedocode implicitly yields a tree rooted at the vertex from which the traversal starts and includes all the vertices reached during the traversaldfs has just been called using vertex v:
w now occurs, you can consider w to be a child of v The edge ( v , w ) corresponds to the parent-child relationship, or edge, between v and wdepth-first search treealgorithm for finding a minimum spanning treeMath 101 → Math 201 (Math 101 is a prerequisite for Math 201)CS 101 → CS 201 (CS 101 is a prerequisite for CS 201)CS 201 → CS 301Math 201 → CS 301The first topological ordering of the graph

The first topological ordering of the graph
