An efficient code to determine if a set is a subset of another set
Posted
by
Edward
on Stack Overflow
See other posts from Stack Overflow
or by Edward
Published on 2010-10-12T21:00:27Z
Indexed on
2011/01/04
5:53 UTC
Read the original article
Hit count: 321
I am looking for an efficient way to determine if a set is a subset of another set in Matlab or Mathematica.
Example: Set A = [1 2 3 4] Set B = [4 3] Set C = [3 4 1] Set D = [4 3 2 1]
The output should be: Set A
Sets B and C belong to set A because A contains all of their elements, therefore, they can be deleted (the order of elements in a set doesn't matter). Set D has the same elements as set A and since set A precedes set D, I would like to simply keep set A and delete set D.
So there are two essential rules: 1. Delete a set if it is a subset of another set 2. Delete a set if its elements are the same as those of a preceding set
My Matlab code is not very efficient at doing this - it mostly consists of nested loops.
Suggestions are very welcome!
Additional explanation: the issue is that with a large number of sets there will be a very large number of pairwise comparisons.
© Stack Overflow or respective owner