logo CodeStepByStep logo

kthLevelFriends

Language/Type: C++ graphs collections
Related Links:

Imagine a graph of friends on social media, where users are vertexes and friendships are edges. Write a function named kthLevelFriends that accepts three parameters: a reference to a BasicGraph, the string name of a vertex to start from, and an integer K. Your function should return a set of strings representing the set of people who are exactly K hops away from the given vertex (and not fewer). For example, if K = 1, those are the person's direct friends; if K = 2, they are the person's friends-of-friends. In the graph below, A's direct friends are {B, D}, and A's friends-of-friends are {C, E, G}. If K = 0, return a set containing only the given vertex. You may assume that the parameter values passed are valid.

A --> B <-- C
|     |     ^
|     |     |
V     V     |
D <-- E --> F
|     ^     ^
|     |     |
V     |     |
G <-- H <-- I
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.