logo CodeStepByStep logo


Language/Type: C++ graphs collections
Related Links:
Author: Marty Stepp (on 2016/06/16)

Imagine a graph of Facebook friends, 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
Type your C++ solution code here:

This is a function problem. Write a C++ function as described. Do not write a complete program; just the function(s) above.

You must log in before you can solve this problem.

Log In

Need help?

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.

© Marty Stepp, all rights reserved.