Created
October 21, 2012 00:18
-
-
Save versae/3925318 to your computer and use it in GitHub Desktop.
Neighbors function : problematic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def find_neighbors(n, v, visited=set()): | |
""" | |
Finds a node's neighbors to n degrees of seperation. | |
Param n: degrees of seperation. n > 1 | |
v: node id | |
Return: a list of neighbors to the nth degree with no duplicates or 'NoneType' | |
""" | |
neighbors = set(v.neighbors) | |
visited.update([v]) | |
if n > 1: | |
for neighbor in neighbors - visited: | |
n_neighbors = find_neighbors(n - 1, neighbor, visited) | |
neighbors.update(n_neighbors) | |
return neighbors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment