Solution: Calculate the Number of Nodes in a Graph Level
Review the solution to calculate the number of nodes at a given level in a graph in detail.
We'll cover the following...
We'll cover the following...
Solution
Explanation
The solution above modifies the visited list to store the level of each node. Later, we count the nodes with the same level.
In this code, while visiting each node, the level of that node is set with an increment in the level of its parent node, i.e.:
visited[child] = visited[parent] + 1
This is how the level of each node is determined.
Time complexity
Its time complexity is the same as the breadth-first traversal algorithm. We have added no new loops, just a simple list to do our job.
The time complexity of BFS can be computed as the total number of iterations performed by the loop.
Let ...