helpers: powerset and trivial_partition_set

- the powerset is the set of all combinations of a given set;
- the trivial partition set is the simplest partition of a set. For e.g., the
  trivial partition set of {a, b, c}, is simply {{a}, {b}, {c}}.
This commit is contained in:
Simon Désaulniers 2018-02-14 17:00:36 -05:00
parent b94e701ff2
commit 2bb4741bb4
1 changed files with 23 additions and 0 deletions

View File

@ -170,6 +170,29 @@ function helpers.spairs(t)
end end
end end
-- create trivial partition of a set. The trivial partition set is the simplest
-- partition of a set. For e.g., the trivial partition set of {a, b, c}, is
-- simply {{a}, {b}, {c}}.
function helpers.trivial_partition_set(set)
local ss = {}
for _,e in pairs(set) do
ss[#ss+1] = {e}
end
return ss
end
-- creates the powerset of a given set
function helpers.powerset(s)
if not s then return {} end
local t = {{}}
for i = 1, #s do
for j = 1, #t do
t[#t+1] = {s[i],unpack(t[j])}
end
end
return t
end
-- }}} -- }}}
return helpers return helpers