How to define/call this process?

76 Views Asked by At

Sorry for my funny question. I would like to separate this "abcd" into -

a, b, c, d
ab, ac, ad, bc, bd, cd,
abc, abd, acd, bdc
abcd

How to call/define in math? Theory or formula or law?

Let'say, to calculate the area of a right triangle is used pythagorean theorem.

update

I have to write a program. Eg, Input is "abcd". Output will be like as below.

a, b, c, d
ab, ac, ad, bc, bd, cd,
abc, abd, acd, bdc
abcd

For fasted way, which theory or formula will I have to use?

3

There are 3 best solutions below

0
On

If we interpret $abcd$ as a sequence, the following terms make sense:

The result of your process is "all subsequences of the sequence $abcd$".

Hence, the process could be called "building all subsequences of the sequence $abcd$".

In generel, for a sequence of symbols $s_1s_2 \cdots s_n$, the subsequences are $$ \{ s_{i_1}s_{i_2}\cdots s_{i_k} \mid k \leq n \text{ and } 1 \leq i_1 < i_2 < \cdots < i_k \leq n\} $$

0
On

By your example, it seems that you're computing all the combinations of $k$ elements of a set $X$ having $n$ elements. Intuitively, you wrote all possible strings, without considering the order (i.e. ab=ba as string) with the elements of $X$.

Observe also that $\sum\limits_{k=0}^n \binom{n}{k}=2^n$, i.e. all the possible subsets of $X$.

1
On

To write a program you could use the following trick. You could take an integer $i$, then you repeat the following procedure for $i=1,2,3,...,2^n - 1$ where $n$ is the number of elements in your set. In your case $n=4$.

For $k=1,...,n$ look if the $k$-th bit of $i$ is set to $1$ (e.g. via (i >> (k-1)) & 1 in C/C++). If so, then output the $k$-th element of your set.

For example, for $i=6$, the binary is $110$ (read it backwards because the smallest digit is on the right), so you output the second and third element, in your case b and c. Then iterated over all $i=1,...,2^n-1$ this process will output you all non-empty subsets.

Note: This will output the subsets in some strange order. However, you can sort them by length afterwards and you will find your result.

Note: This usually will only work for sets up to a size of $64$ as this is the usual upper limit of bits in an integer on the current systems. But this should not be of any concern in reality (as String pointed out in the comments), as enumerating all subsets might be infeasable long before $2^{64}$ combinations.