Application Center - Maplesoft

App Preview:

Selecting Subsets of Power Sets with Desired Properties

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

powersets.mws

Selecting Subsets of Power Sets with Desired Properties

by Prof. Bill Bauldry,
 Appalachian State University
,
 BauldryWC@appstate.edu

Introduction

Let A = {1, 2, 3,..., 10}. We will use Maple to generate the set B of all subsets of A of size 2 containing at least one odd integer.  More generally, we can do this for A = {1, 2, 3,..., n}for any n, generating the set B of all subsets of A of size k containing at least one integer that has any desired property (e.g. prime, power of two, etc.)

Program

>    restart; with(combinat);

Warning, the protected name Chi has been redefined and unprotected

[Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, fibonacci, firstpart, graycode, inttovec, lastpart, multinomial, nextpart, numbcomb, numbcomp, numbpart...
[Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, fibonacci, firstpart, graycode, inttovec, lastpart, multinomial, nextpart, numbcomb, numbcomp, numbpart...
[Chi, bell, binomial, cartprod, character, choose, composition, conjpart, decodepart, encodepart, fibonacci, firstpart, graycode, inttovec, lastpart, multinomial, nextpart, numbcomb, numbcomp, numbpart...

We first generate all order-2 subsets of the power set {1, 2, 3,..., 10}.  (For large n, using the choose( )  function is more efficient than generating the entire power set of A and then selecting the order-2 subsets.)

>    k_sets := choose( [seq(i, i=1..10 )], 2 );

k_sets := [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [2, 10], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, ...
k_sets := [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [2, 10], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, ...
k_sets := [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [2, 10], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, ...

Next, we write a little function that takes a subset x  and returns true  if any element of x  is odd.

>    HasAnOdd := x -> convert( map(type, x, odd), `or`):

>    HasAnOdd( [ 1, 6, 8 ] );

true

>    HasAnOdd( [ 2, 6, 8, 10 ] );

false

Now we can generate B  by filtering the subsets of size 2 using our function HasAnOdd .  

>    B :=  select( HasAnOdd, k_sets );

B := [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 5], [2, 7], [2, 9], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [4, 5], [4, 7], [4, 9], [5, 6], [...
B := [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 5], [2, 7], [2, 9], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [4, 5], [4, 7], [4, 9], [5, 6], [...

The size of B

>    nops(B);

35

Here is a procedure that does the above for any size power set (n), subset size (k) and integer property (prop)

>    B_size := proc(n, k, prop)
  local k_sets, HasProperty, B;

  k_sets := combinat[choose]( [seq(i, i=1..n )], k );
  HasProperty := x -> convert( map(type, x, prop), `or`):
  B :=  select( HasProperty, k_sets );
  [nops(B), B];
end proc:

Examples

n = 10, k=2, odd numbers

>    B_size( 10, 2, odd );

[35, [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 5], [2, 7], [2, 9], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [4, 5], [4, 7], [4, 9], [5, 6], [...
[35, [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10], [2, 3], [2, 5], [2, 7], [2, 9], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [4, 5], [4, 7], [4, 9], [5, 6], [...

n = 10, k = 3, even numbers

>    B_size( 10, 3, even );

[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...
[110, [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 3, 4], [1, 3, 6], [1, 3, 8], [1, 3, 10], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4...


n = 10, k=2, prime numbers

>    B_size( 10, 2, prime );

[30, [[1, 2], [1, 3], [1, 5], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [2, 10], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [4, 5], [4, 7], [5, 6], [5, 7], [5, 8], [...
[30, [[1, 2], [1, 3], [1, 5], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 9], [2, 10], [3, 4], [3, 5], [3, 6], [3, 7], [3, 8], [3, 9], [3, 10], [4, 5], [4, 7], [5, 6], [5, 7], [5, 8], [...

Disclaimer:  While every effort has been made to validate the solutions in this worksheet, Waterloo Maple Inc. and the contributors are not responsible for any errors contained and are not liable for any damages resulting from the use of this material.