# Graph theory: BFS (Breadth First Search) - why is current processed first?

I am referencing some code I found on GeeksForGeeks.com: Why is the current node printed (and processed) first before its children are processed? Wouldn't "breadth first" mean "Process children first, then process parent"? or, is that only for Trees? I can't be the only one to not understand this, so instead of flaming me, somebody please simply post the answer?

void Graph::DFSUtil(int v, bool visited[])
{
visited[v] = true; <-- why is this printed FIRST?
cout << v << " ";

// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
if (!visited[*i])
DFSUtil(*i, visited);
}

// DFS traversal of the vertices reachable from v.
// It uses recursive DFSUtil()
void Graph::DFS(int v)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;

// Call the recursive helper function
// to print DFS traversal
DFSUtil(v, visited);
}

• (side note: I'm relatively new here too but as far as I can tell the people are dramatically friendlier than on stackoverflow) – Matthew C Dec 2 '19 at 20:22
• this site IS stackoverflow. er, right? – eric frazer Dec 2 '19 at 20:33
• The question is about BFS but the code you posted is for DFS...? – Tom van der Zanden Dec 2 '19 at 20:36
• Welcome to cs.stackexchange! I would recommend against jabs such as "so instead of flaming me, somebody please simply post the answer?" as they discourage users from answering your question, no matter its merits. – eru-cs Dec 3 '19 at 1:58
• – greybeard Dec 3 '19 at 8:11

Trees are acyclic. If you have for instance the directed graph $$(1,2),(2,1)$$ and try running the algorithm with marking visited after the recursive call, you will get a stack overflow. Or for an undirected graph you can consider a triangle $$(1,2),(2,3),(1,3)$$ which will result in the same thing. For acyclic graphs this issue won't arise so mark visited in either place.