Question: Need help in optimizing code

Hello everybody,

I need help to increase the speed of two procedures.

My first (recursive) procedure returns a list of all k - element subsets of a given set {1, ... , n}:


nkSubSetList := proc(n :: nonnegint, k :: nonnegint)
        option remember;          # boosts with factor 5
        local X, prev, nextOne;

        # Local function that augments a nonnegint to a given set, e.g.
        local augment := (SetA, intm) -> {op(SetA), intm};

        # Case k > n allowed & needed for recursive call
        if n < 0 or k < 0 or k > n then
            X := {};
            return X
        elif n = 0 or k = 0 then
            X :=  [{}];
            return X
        else
            prev := nkSubSetList(n - 1, k);
            nextOne := map(augment, nkSubSetList(n - 1, k - 1), n);
            return [op(prev), op(nextOne)]
        end if;
end proc:

# Starting the Profiler on my system the call nkSubSetList(20, 10) needs 0,5 sec.
The other algorithm returns a list of all k-element subsets of a given set T:

TkSubSetList := proc(T :: set(posint), k :: nonnegint) :: list(set(posint));
    local n := nops(T);                                 # Cardinality of index set T
    local callSet := A -> map(i -> T[i], A);     # Call the i-th entry of the given set T

    # Input ASSERTS
    ASSERT(k <= n, "k is bigger than the cardinality of index set T");

    return map(callSet, nkSubSetList(n, k));
end proc:

# Starting the Profiler on my system the call TkSubSetList({1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}, 10) needs 1,8 seconds.

Who have an idea how to increase the speed of these algorithms?

Please Wait...