Skip to content

Instantly share code, notes, and snippets.

@seanhuggins1
Created November 23, 2020 20:42
Show Gist options
  • Save seanhuggins1/285080539e2b649e9696b92d9dbc74c5 to your computer and use it in GitHub Desktop.
Save seanhuggins1/285080539e2b649e9696b92d9dbc74c5 to your computer and use it in GitHub Desktop.
visited = set();
def dfs(visited, graph, node):
if node not in visited:
yield node
visited.add(node)
for neighbor in graph[node]:
yield from dfs(visited, graph, neighbor)
def printFriendGroups(graph):
while (len(graph) > 0):
friendGroup = []
graphCopy = graph.copy()
for node in dfs(visited, graphCopy, list(graph.keys())[0] ):
friendGroup.append(node)
graph.pop(node)
print(friendGroup)
graph = {
'0':['1','2'],
'1':['0','5'],
'2':['0'],
'3':['6'],
'4':[],
'5':['1'],
'6':['3'],
}
printFriendGroups(graph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment