Reputation: 190
I have a dictionary of devices where each device has a list of other devices that it can collaborate with. Think of these as the devices that it is compatible to.
devices = {0: [], 1: [9, 10, 14], 2: [9, 11, 14], 3: [], 4: [], 5: [9, 14, 15], 6: [], 7: [], 8: [], 9: [1, 2, 5, 11, 14, 15], 10: [1], 11: [2, 9, 14], 12: [], 13: [], 14: [1, 2, 5, 9, 11, 16], 15: [5, 9], 16: [14], 17: [], 18: [], 19: []}
I would like to be able to cluster these into a list of lists that contains all possible clusters that can collaborate together. A cluster can only contain devices that are all compatible with each other. In other words, if a cluster [1,9,2] exists then 1 must collaborate with both 9 and 2 and they must collaborate with each other as well. In this scenario the final result should look something like this:
[ [1, 9, 14], [2, 9, 14], [5,9,14], [5,15,9] [9,11,14,2], [10,1], [14,16] ]
I may have made an error manually computing this, but I believe this to be all possible clusters of the items while satisfying their compatibility requirements.
However, I am having some difficulties translating this into code. Any help would be tremendously appreciated.
Upvotes: 1
Views: 60
Reputation: 1284
Though it took me few hours I am happy with the neat and clean solution. Understanding the requirement while coding took me 90% of the time. Lesson learnt - use pencil and paper first.
Code:
devices = {0: [], 1: [9, 10, 14], 2: [9, 11, 14], 3: [], 4: [], 5: [9, 14, 15], 6: [], 7: [], 8: [], 9: [1, 2, 5, 11, 14, 15], 10: [1], 11: [2, 9, 14], 12: [], 13: [], 14: [1, 2, 5, 9, 11, 16], 15: [5, 9], 16: [14], 17: [], 18: [], 19: []}
# manually calculated expected result [ [1, 9, 14], [2, 9, 14], [5,9,14], [5,15,9] [9,11,14,2], [10,1], [14,16] ]
# true result [[1, 9, 14], [2, 9, 11, 14], [5, 9, 14], [10, 1], [15, 5, 9], [16, 14]]
def is_compatible(k,v):
if v in devices[k]:
return True
return False
clusters = []
for k,v_list in devices.items():
if v_list:
cluster = []
cluster.append(k)
cluster.append(v_list[0])
for v_ele in v_list[1:]:
v_ele_further_check = True
for cluster_member in cluster[1:]: # check v_ele compatible with existing cluster members
if not is_compatible(v_ele, cluster_member):
v_ele_further_check = False
break
if v_ele_further_check:
cluster.append(v_ele)
clusters.append(cluster)
print(clusters)
unique_clusters = []
for cluster in clusters: # eliminate_duplicates
if not sorted(cluster) in unique_clusters:
unique_clusters.append(cluster)
print(unique_clusters)
Output:
[[1, 9, 14], [2, 9, 11, 14], [5, 9, 14], [9, 1, 14], [10, 1], [11, 2, 9, 14], [14, 1, 9], [15, 5, 9], [16, 14]]
[[1, 9, 14], [2, 9, 11, 14], [5, 9, 14], [10, 1], [15, 5, 9], [16, 14]]
Comments:
You can see I can easily get rid of function is_compatible but it adds to the readability. The order of cluster in clusters is maintained correctly if that matters.
Upvotes: 0
Reputation: 79228
One way you could do this:
from itertools import combinations
colaborate2 = lambda args, dic : all(x in dic[y] and y in dic[x] for x, y in combinations(args,2))
def group (x, dic, n=None):
if n is None:
n = len(x)
for i in combinations(x, n):
if colaborate2(i, dic):
return tuple(sorted(i))
if n > 1:
return group(x, dic, n - 1)
def groupings(dic):
m = set([group(val + [key], dic) for key, val in dic.items()])
return [i for i in m if len(i)>1]
groupings(items)
[(5, 9, 15), (14, 16), (5, 9, 14), (1, 9, 14), (1, 10), (2, 9, 11, 14)]
Upvotes: 0
Reputation: 11342
There may be a cleaner way to get the result, but this code works.
Note your result includes [2, 9, 14] which is a subset of [9,11,14,2]. The subset is removed in this code.
items = {0: [], 1: [9, 10, 14], 2: [9, 11, 14], 3: [], 4: [], 5: [9, 14, 15], 6: [], 7: [], 8: [],
9: [1, 2, 5, 11, 14, 15], 10: [1], 11: [2, 9, 14], 12: [], 13: [], 14: [1, 2, 5, 9, 11, 16],
15: [5, 9], 16: [14], 17: [], 18: [], 19: []}
grps = []
# create 1-1 groups
for g in items:
for c in items[g]:
if g in items[c]:
grps.append([g,c])
# add other elements to each group
chg = True
while chg: # until no more elements added
chg = False
for g in items: # each single element
for g2 in grps: # check each existing group
if g in g2: continue # if not already in group
ok = True
for c in g2: # check each group member
if not (g in items[c] and c in items[g]): # can we collaborate?
ok = False # no
if ok:
g2.append(g) # add element to group
chg = True
# check for subsets
for i,x in enumerate(grps):
for j,y in enumerate(grps):
if i==j: continue # same group
if set(x) & set(y) == set(x): # if subset
x.clear() # remove elements
grps = [g for g in grps if len(g)] # remove empty groups
print(grps)
Output
[[10, 1], [14, 5, 9], [14, 9, 1], [14, 11, 2, 9], [15, 9, 5], [16, 14]]
Upvotes: 1