Challenge: Calculate the Number of Nodes in a Graph Level
Given a graph, calculate the number of nodes in a graph at a given level.
We'll cover the following
Problem statement
Implement a function that returns the number of nodes at a given level starting from a root node of a directed graph.
Try modifying the breadth-first traversal algorithm to achieve this goal.
To solve this problem, all the previously-implemented data structures will be available to us.
Input
An undirected graph represented as an adjacency list, and the level whose number of nodes we need to find
Output
The number of nodes returned as a simple integer
Sample input
Graph:
Vertex | Edges |
---|---|
0 | 2, 1 |
1 | 4, 3 |
2 | None |
3 | None |
4 | None |
Level: 1
Sample output
1
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.