Kotlinlearncs.online LogoJava
Return to List

Homework: Undirected Graph Size

Created By: Geoffrey Challen
/ Version: 2021.10.0

Create a method name size that receives a reference to an unweighted graph containing Int values using cs1.graphs.UnweightedGraph, so a UnweightedGraph<Int>, and returns the size of the graph as an Int.

To complete this problem you'll need to implement graph traversal. From a given node, you want to visit all of its neighbors except any nodes that you've already visited. If you use a Set to track the nodes that you've visited, then you can simply return the size of that Set when you are finished. We've provided some starter code to get you off on the right track.

For reference, cs1.graphs.UnweightedGraph is declared somewhat like this:

And cs1.graphs.GraphNode is declared somewhat like this:

Related Lessons

Stuck? You may find these lessons helpful: