我想将一个不完整的图分成单独的,未连接的体.图表的边缘位于列表中edges.
代码在改组边的顺序时给出不同的结果.这是为什么?
from random import shuffle
edges = [('7', '9'), ('2', '8'), ('4', '10'), ('5', '9'), ('1', '2'), ('1', '6'), ('6', '10')]
bodylist = []
shuffle(edges)
for edge in edges:
#If at least one node of the edge is anywhere in bodylist, append the new nodes to that list.
try:
index = [i for i, body in enumerate(bodylist) if edge[0] in body or edge[1] in body][0]
bodylist[index].append(edge[0])
bodylist[index].append(edge[1])
#If not, make a new list containing the new nodes. …Run Code Online (Sandbox Code Playgroud)