Challenge: Calculate the Number of Nodes in a Given Graph Level

Given a graph, calculate the number of nodes in an undirected graph level.

Problem statement

Implement a function that returns the number of nodes at a given level of an undirected graph in Java.

Try modifying the breadth-first Traversal algorithm to achieve this goal.


The inputs are a graph that is represented as an adjacency list, a starting vertex, and the level whose number of nodes we need to find.


The output is the number of nodes returned as a simple integer.

Sample input


graph {0 - 1
       0 - 2
       1 - 3
       2 - 3
       3 - 5
       2 - 4

Level: 1
Starting vertex: 0

Sample output


Level up your interview prep. Join Educative to access 70+ hands-on prep courses.