Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [ (1,2,3), (5,2,6), (7,8,9), (6,12,13), (21,8,34), (19,20) ]
- [ [ (1,2,3), (5,2,6), (6,12,13) ], [ (7,8,9), (21,8,34) ], [ (19,20) ] ]
- element | set_id
- ----------------
- 1 | 1
- 2 | 1
- 3 | 1
- 5 | 2
- 2 | 2
- 6 | 2
- nodes = [ [1,2,3], [2,4,5], [6,7,8], [10,11,12], [7,10,13], [12], [] ]
- links = [ set() for x in nodes ]
- #first find the links
- for n in range(len(nodes)):
- for item in nodes[n]:
- for m in range(n+1, len(nodes)):
- if (item in nodes[m]):
- links[n].add(m)
- links[m].add(n)
- sets = []
- nodes_not_in_a_set = range(len(nodes))
- while len(nodes_not_in_a_set) > 0:
- nodes_to_explore = [nodes_not_in_a_set.pop()]
- current_set = set()
- while len(nodes_to_explore) > 0:
- current_node = nodes_to_explore.pop()
- current_set.add(current_node)
- if current_node in nodes_not_in_a_set:
- nodes_not_in_a_set.remove(current_node)
- for l in links[current_node]:
- if l not in current_set and l not in nodes_to_explore:
- nodes_to_explore.append(l)
- if len(current_set) > 0:
- sets.append(current_set)
- for s in sets:
- print [nodes[n] for n in s]
- [[]]
- [[6, 7, 8], [10, 11, 12], [7, 10, 13], [12]]
- [[1, 2, 3], [2, 4, 5]]
Add Comment
Please, Sign In to add comment