there is an algorithmic problem. I have several subsets of one super set (there can be n-th number of subsets, depending on the length of the super set). Here is an example:
['/img0.jpg ']
['/img1.jpg']
['/img10.jpg']
['/img11.jpg' '/img9.jpg']
['/img12.jpg' '/img15.jpg']
['/img11.jpg' '/img13.jpg' '/img8.jpg' '/img9.jpg']
['/img14.jpg']
['/img15.jpg']
['/img16.jpg']
['/img14.jpg' '/img17.jpg']
['/img18.jpg']
['/img19.jpg' '/img22.jpg']
['/img2.jpg']
['/img19.jpg' '/img20.jpg' '/img21.jpg' '/img22.jpg']
['/img19.jpg' '/img21.jpg' '/img22.jpg']
['/img22.jpg']
['/img23.jpg']
['/img3.jpg']
['/img3.jpg' '/img4.jpg']
['/img0.jpg' '/img5.jpg']
['/img6.jpg']
['/img2.jpg' '/img7.jpg']
['/img11.jpg' '/img8.jpg' '/img9.jpg']
['/img9.jpg']
As a result , you should get:
['/img1.jpg']
['/img10.jpg']
['/img11.jpg' '/img13.jpg' '/img8.jpg' '/img9.jpg']
['/img12.jpg' '/img15.jpg']
['/img14.jpg' '/img17.jpg']
['/img16.jpg']
['/img18.jpg']
['/img19.jpg' '/img20.jpg' '/img21.jpg' '/img22.jpg']
['/img2.jpg' '/img7.jpg']
['/img23.jpg']
['/img3.jpg' '/img4.jpg']
['/img0.jpg' '/img5.jpg']
['/img6.jpg']
Simply put, the task boils down to finding a super set, bypassing all subsets
Please tell me what are the ways and ideas for writing an algorithm?Can be solved without sets optimally?
I have the following code:
dict_dir = {}
compare_keys = compare.keys() #dictionary with keys as input (n-th number of sets with super sets)
for idx, key1 in enumerate(compare_keys):
array = []
for key2 in compare_keys:
if set(compare[key1]).issubset(set(compare[key2])):
array.extend(list(compare[key2]))
arr = list(set(array))
dict_dir[f'dir{idx}'] = arr
The following result is obtained:
[['/img0.jpg', '/img5.jpg'],
['/img1.jpg'],
['/img10.jpg'],
['/img11.jpg', '/img13.jpg', '/img8.jpg', '/img9.jpg'],
['/img12.jpg', '/img15.jpg'],
['/img11.jpg', '/img13.jpg', '/img8.jpg', '/img9.jpg'],
['/img14.jpg', '/img17.jpg'],
['/img12.jpg', '/img15.jpg'],
['/img16.jpg'],
['/img14.jpg', '/img17.jpg'],
['/img18.jpg'],
['/img19.jpg', '/img20.jpg', '/img21.jpg', '/img22.jpg'],
['/img2.jpg', '/img7.jpg'],
['/img19.jpg', '/img20.jpg', '/img21.jpg', '/img22.jpg'],
['/img19.jpg', '/img20.jpg', '/img21.jpg', '/img22.jpg'],
['/img19.jpg', '/img20.jpg', '/img21.jpg', '/img22.jpg'],
['/img23.jpg'],
['/img3.jpg', '/img4.jpg'],
['/img3.jpg', '/img4.jpg'],
['/img0.jpg', '/img5.jpg'],
['/img6.jpg'],
['/img2.jpg', '/img7.jpg'],
['/img11.jpg', '/img13.jpg', '/img8.jpg', '/img9.jpg'],
['/img11.jpg', '/img13.jpg', '/img8.jpg', '/img9.jpg']]
It works a little incorrectly and not optimally, I would like the work of such an algorithm to be minimally spent on memory and time.