这是一个基于图中连接组件的解决方案(由@Blckknght建议):
def make_friends_graph(people, friends):
# graph of friends (adjacency lists representation)
G = {person: [] for person in people} # person -> direct friends list
for a, b in friends:
G[a].append(b) # a is friends with b
G[b].append(a) # b is friends with a
return G
def networks(num_people, friends):
direct_friends = make_friends_graph(range(num_people), friends)
seen = set() # already seen people
# person's friendship circle is a person themselves
# plus friendship circles of all their direct friends
# minus already seen people
def friendship_circle(person): # connected component
seen.add(person)
yield person
for friend in direct_friends[person]:
if friend not in seen:
yield from friendship_circle(friend)
# on Python <3.3
# for indirect_friend in friendship_circle(friend):
# yield indirect_friend
# group people into friendship circles
circles = (friendship_circle(person) for person in range(num_people)
if person not in seen)
# print friendship circles
for i, circle in enumerate(circles):
print("Social network %d is {%s}" % (i, ",".join(map(str, circle))))
例子:
networks(5, [(0,1),(1,2),(3,4)])
# -> Social network 0 is {0,1,2}
# -> Social network 1 is {3,4}