What is the computational complexity of `itertools.combinations` in python?











up vote
0
down vote

favorite












itertools.combinations in python is a powerful tool for finding all combination of r terms, however, I want to know about its computational complexity.



Let's say I want to know the complexity in terms of n and r, and certainly it will give me all the r terms combination from a list of n terms.



According to the Official document, this is the rough implementation.



def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)









share|improve this question


















  • 3




    It's gonna be nCr, i.e O(n choose r)
    – juanpa.arrivillaga
    Nov 21 at 19:51








  • 1




    @juanpa.arrivillaga There is going to be at least another factor r for the tuple generation.
    – eukaryota
    Nov 22 at 1:32










  • @eukaryota makes sense
    – juanpa.arrivillaga
    Nov 22 at 3:06















up vote
0
down vote

favorite












itertools.combinations in python is a powerful tool for finding all combination of r terms, however, I want to know about its computational complexity.



Let's say I want to know the complexity in terms of n and r, and certainly it will give me all the r terms combination from a list of n terms.



According to the Official document, this is the rough implementation.



def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)









share|improve this question


















  • 3




    It's gonna be nCr, i.e O(n choose r)
    – juanpa.arrivillaga
    Nov 21 at 19:51








  • 1




    @juanpa.arrivillaga There is going to be at least another factor r for the tuple generation.
    – eukaryota
    Nov 22 at 1:32










  • @eukaryota makes sense
    – juanpa.arrivillaga
    Nov 22 at 3:06













up vote
0
down vote

favorite









up vote
0
down vote

favorite











itertools.combinations in python is a powerful tool for finding all combination of r terms, however, I want to know about its computational complexity.



Let's say I want to know the complexity in terms of n and r, and certainly it will give me all the r terms combination from a list of n terms.



According to the Official document, this is the rough implementation.



def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)









share|improve this question













itertools.combinations in python is a powerful tool for finding all combination of r terms, however, I want to know about its computational complexity.



Let's say I want to know the complexity in terms of n and r, and certainly it will give me all the r terms combination from a list of n terms.



According to the Official document, this is the rough implementation.



def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)






python time-complexity complexity-theory






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 19:49









ArtificiallyIntelligence

326312




326312








  • 3




    It's gonna be nCr, i.e O(n choose r)
    – juanpa.arrivillaga
    Nov 21 at 19:51








  • 1




    @juanpa.arrivillaga There is going to be at least another factor r for the tuple generation.
    – eukaryota
    Nov 22 at 1:32










  • @eukaryota makes sense
    – juanpa.arrivillaga
    Nov 22 at 3:06














  • 3




    It's gonna be nCr, i.e O(n choose r)
    – juanpa.arrivillaga
    Nov 21 at 19:51








  • 1




    @juanpa.arrivillaga There is going to be at least another factor r for the tuple generation.
    – eukaryota
    Nov 22 at 1:32










  • @eukaryota makes sense
    – juanpa.arrivillaga
    Nov 22 at 3:06








3




3




It's gonna be nCr, i.e O(n choose r)
– juanpa.arrivillaga
Nov 21 at 19:51






It's gonna be nCr, i.e O(n choose r)
– juanpa.arrivillaga
Nov 21 at 19:51






1




1




@juanpa.arrivillaga There is going to be at least another factor r for the tuple generation.
– eukaryota
Nov 22 at 1:32




@juanpa.arrivillaga There is going to be at least another factor r for the tuple generation.
– eukaryota
Nov 22 at 1:32












@eukaryota makes sense
– juanpa.arrivillaga
Nov 22 at 3:06




@eukaryota makes sense
– juanpa.arrivillaga
Nov 22 at 3:06












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I would say it is θ[r (n choose r)], the n choose r part is the number of times the generator has to yield and also the number of times the outer while iterates.



In each iteration at least the output tuple of length r needs to be generated, which gives the additional factor r. The other inner loops will be O(r) per outer iteration as well.



This is assuming that the tuple generation is actually O(r) and that the list get/set are indeed O(1) at least on average given the particular access pattern in the algorithm. If this is not the case, then still Ω[r (n choose r)] though.



As usual in this kind of analysis I assumed all integer operations to be O(1) even if their size is not bounded.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419536%2fwhat-is-the-computational-complexity-of-itertools-combinations-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    I would say it is θ[r (n choose r)], the n choose r part is the number of times the generator has to yield and also the number of times the outer while iterates.



    In each iteration at least the output tuple of length r needs to be generated, which gives the additional factor r. The other inner loops will be O(r) per outer iteration as well.



    This is assuming that the tuple generation is actually O(r) and that the list get/set are indeed O(1) at least on average given the particular access pattern in the algorithm. If this is not the case, then still Ω[r (n choose r)] though.



    As usual in this kind of analysis I assumed all integer operations to be O(1) even if their size is not bounded.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      I would say it is θ[r (n choose r)], the n choose r part is the number of times the generator has to yield and also the number of times the outer while iterates.



      In each iteration at least the output tuple of length r needs to be generated, which gives the additional factor r. The other inner loops will be O(r) per outer iteration as well.



      This is assuming that the tuple generation is actually O(r) and that the list get/set are indeed O(1) at least on average given the particular access pattern in the algorithm. If this is not the case, then still Ω[r (n choose r)] though.



      As usual in this kind of analysis I assumed all integer operations to be O(1) even if their size is not bounded.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        I would say it is θ[r (n choose r)], the n choose r part is the number of times the generator has to yield and also the number of times the outer while iterates.



        In each iteration at least the output tuple of length r needs to be generated, which gives the additional factor r. The other inner loops will be O(r) per outer iteration as well.



        This is assuming that the tuple generation is actually O(r) and that the list get/set are indeed O(1) at least on average given the particular access pattern in the algorithm. If this is not the case, then still Ω[r (n choose r)] though.



        As usual in this kind of analysis I assumed all integer operations to be O(1) even if their size is not bounded.






        share|improve this answer














        I would say it is θ[r (n choose r)], the n choose r part is the number of times the generator has to yield and also the number of times the outer while iterates.



        In each iteration at least the output tuple of length r needs to be generated, which gives the additional factor r. The other inner loops will be O(r) per outer iteration as well.



        This is assuming that the tuple generation is actually O(r) and that the list get/set are indeed O(1) at least on average given the particular access pattern in the algorithm. If this is not the case, then still Ω[r (n choose r)] though.



        As usual in this kind of analysis I assumed all integer operations to be O(1) even if their size is not bounded.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 1:33

























        answered Nov 22 at 1:20









        eukaryota

        1,154115




        1,154115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419536%2fwhat-is-the-computational-complexity-of-itertools-combinations-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu