Challenge: Calculate the Number of Nodes in a Given Graph Level
Given a graph, calculate the number of nodes in an undirected graph level.
We'll cover the following
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.
Input
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.
Output
The output is the number of nodes returned as a simple integer.
Sample input
Graph:
graph {0 - 1
0 - 2
1 - 3
2 - 3
3 - 5
2 - 4
}
Level: 1
Starting vertex: 0
Sample output
1
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.