m3k_1
m3k_1

Reputation: 417

Recursive function with growing number of calls

So the idea is I'm using steam API to get list of friends of the given user, to gather some ID's for the data analysis. Each time I get friendlist of a user I want to get the 5 friends of his 5 friends. So first I get 5 friends of first user. And then I get the 5 friends of 5 friends so it's 5 -> 25 -> 125 and so on up until some points for example 6 times to get 15 625 ID's. And the question is how to do it because I don't really know how to make this really work. I'm not so good at recursion

Upvotes: 0

Views: 42

Answers (1)

Mushroomator
Mushroomator

Reputation: 9168

Basicly you can imagine a person as a node who has n neighboring nodes (= friends) and you start at one (= yourself) and move on to your neighbor nodes (=friends) then you move on to their neighboring nodes and so on while always keeping track of which nodes you have already visited. This way you are gradually moving away from your start node, until the whole network is explored (you don't want that in your case) or until a certain distance (= nodes between you and your friends) is reached, so for example up to the 6th level as you've described in your post.

The network of friends builds a graph data structure and what you want to do is a well known graph algorithm called breadth-first search. In the wikipedia article you will find some pseudo code and if you google for breadth-first search you will find many, many resources and implementations in any language you need.

By the way, no need for recursion here, so don't use it.

Upvotes: 1

Related Questions