Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating all possible sub-sequences of a given length (C#)

Tags:

c#

algorithm

If I have a sequence as follows (let's say it's an IEnumerable<T>):

[A, B, C, D, E]

Then what's the cleanest way to compute all possible (continuous and non-continuous) subsequences of a given length? Ordering of the results in the result set isn't important, but it shouldn't include duplicates.

e.g. If I want to compute all possible subsequences of length 3 the result set would be:

[A, B, C]
[A, B, D]
[A, B, E]
[A, C, D]
[A, C, E]
[A, D, E]
[B, C, D]
[B, C, E]
[B, D, E]
[C, D, E]

For the record, the accepted answer below gave me a good starting point, and here's the code I've gone with that is updated to use some of the new .NET 3.5 extension methods:

public static IEnumerable<IEnumerable<T>> Subsequences<T>(
    this IEnumerable<T> source, 
    int count)
{
    if (count == 0)
    {
        yield return Enumerable.Empty<T>();
    }
    else
    {
        var skip = 1;
        foreach (var first in source)
        {
            foreach (var rest in source.Skip(skip).Subsequences(count - 1))
            {
                yield return Enumerable.Repeat(first, 1).Concat(rest);
            }

            skip++;
        }
    }
}
like image 993
Greg Beech Avatar asked May 12 '09 12:05

Greg Beech


People also ask

How many subsequence possible if string length is n?

@RossMillikan So the total number of subsequences in a string is 2n, where n is the length of the string.

How many subsequences are possible?

The total number of subsequences possible is equal to 2^n. Hence, the time complexity is O(2^n) which is exponential. We use an unordered set to store all unique subsequences. In the worst case, the number of unique subsequences can be equal to 2^n.

How do you find the subsequence of a sequence?

A subsequence is derived from the original sequence by removing elements, keeping the order. One can remove finite or infinite many elements. Valid edge cases are: remove all elements of the original sequence.


1 Answers

I've had success with IanG's PermuteUtils class:

char[] items = new char[] { 'A', 'B', 'C', 'D', 'E' };

foreach (IEnumerable<char> permutation in PermuteUtils.Permute(items, 3)) {
    Console.Write("[");
    foreach (char c in permutation) {
        Console.Write(" " + c);
    }
    Console.WriteLine(" ]");
}

Results in:

[ A B C ]
[ A B D ]
[ A B E ]
[ A C B ]
[ A C D ]
[ A C E ]
[ A D B ]
[ A D C ]
[ A D E ]
[ A E B ]
[ A E C ]
[ A E D ]
[ B A C ]
[ B A D ]
[ B A E ]
[ B C A ]
[ B C D ]
[ B C E ]
[ B D A ]
[ B D C ]
...
like image 189
Sean Bright Avatar answered Sep 30 '22 02:09

Sean Bright