Complexity of an algorithm with two cases [closed]











up vote
0
down vote

favorite












What is the complexity for following algorithm if



1) $f(i, j) = (j - 1) & j$



2) $f(i, j) = (j - 1) & i$



s = 0

for(i = 0; i <= pow(2, n); i++){

for(j = i; j > 0; j = f(i, j)) {
s++;
}

}









share|cite|improve this question















closed as off-topic by user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo Nov 19 at 0:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Your code is almost unreadable. Please use "insert code" and use indentation in your code. I suppose & is for "and" operator. What I would do is find value of $f(i,j)$ by making cases, this will enable you to find the number of times the "s++" line is executed...
    – Nicolas FRANCOIS
    Nov 17 at 15:06






  • 1




    As an example, for case 1), suppose $j=*...*10...0=m.2^{k+1}+1.2^k$. Put it like this : $k$ is the position of the first bit set to $1$ in the binary notation of $j$. Then you find $j-1=*...*01...1$, so $(j-1)& j=*...*00...0=m.2^{k+1}$. Use $k$ (and $m$) to categorize the cases.
    – Nicolas FRANCOIS
    Nov 17 at 15:16












  • Yes, & is for 'and ' operator
    – Daniel
    Nov 17 at 16:12










  • Can you detail, please for second case?
    – Daniel
    Nov 17 at 16:20






  • 1




    BTW, are you sure the end condition in the internal loop is not "j>0" ? Because if j is unsigned, this could go forever... But maybe it's simply that I don't understand C code...
    – Nicolas FRANCOIS
    Nov 17 at 18:11















up vote
0
down vote

favorite












What is the complexity for following algorithm if



1) $f(i, j) = (j - 1) & j$



2) $f(i, j) = (j - 1) & i$



s = 0

for(i = 0; i <= pow(2, n); i++){

for(j = i; j > 0; j = f(i, j)) {
s++;
}

}









share|cite|improve this question















closed as off-topic by user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo Nov 19 at 0:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    Your code is almost unreadable. Please use "insert code" and use indentation in your code. I suppose & is for "and" operator. What I would do is find value of $f(i,j)$ by making cases, this will enable you to find the number of times the "s++" line is executed...
    – Nicolas FRANCOIS
    Nov 17 at 15:06






  • 1




    As an example, for case 1), suppose $j=*...*10...0=m.2^{k+1}+1.2^k$. Put it like this : $k$ is the position of the first bit set to $1$ in the binary notation of $j$. Then you find $j-1=*...*01...1$, so $(j-1)& j=*...*00...0=m.2^{k+1}$. Use $k$ (and $m$) to categorize the cases.
    – Nicolas FRANCOIS
    Nov 17 at 15:16












  • Yes, & is for 'and ' operator
    – Daniel
    Nov 17 at 16:12










  • Can you detail, please for second case?
    – Daniel
    Nov 17 at 16:20






  • 1




    BTW, are you sure the end condition in the internal loop is not "j>0" ? Because if j is unsigned, this could go forever... But maybe it's simply that I don't understand C code...
    – Nicolas FRANCOIS
    Nov 17 at 18:11













up vote
0
down vote

favorite









up vote
0
down vote

favorite











What is the complexity for following algorithm if



1) $f(i, j) = (j - 1) & j$



2) $f(i, j) = (j - 1) & i$



s = 0

for(i = 0; i <= pow(2, n); i++){

for(j = i; j > 0; j = f(i, j)) {
s++;
}

}









share|cite|improve this question















What is the complexity for following algorithm if



1) $f(i, j) = (j - 1) & j$



2) $f(i, j) = (j - 1) & i$



s = 0

for(i = 0; i <= pow(2, n); i++){

for(j = i; j > 0; j = f(i, j)) {
s++;
}

}






algorithms






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Nov 17 at 18:28

























asked Nov 17 at 14:51









Daniel

166




166




closed as off-topic by user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo Nov 19 at 0:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo Nov 19 at 0:44


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "This question is missing context or other details: Please improve the question by providing additional context, which ideally includes your thoughts on the problem and any attempts you have made to solve it. This information helps others identify where you have difficulties and helps them write answers appropriate to your experience level." – user21820, Paul Frost, José Carlos Santos, Shailesh, Cesareo

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1




    Your code is almost unreadable. Please use "insert code" and use indentation in your code. I suppose & is for "and" operator. What I would do is find value of $f(i,j)$ by making cases, this will enable you to find the number of times the "s++" line is executed...
    – Nicolas FRANCOIS
    Nov 17 at 15:06






  • 1




    As an example, for case 1), suppose $j=*...*10...0=m.2^{k+1}+1.2^k$. Put it like this : $k$ is the position of the first bit set to $1$ in the binary notation of $j$. Then you find $j-1=*...*01...1$, so $(j-1)& j=*...*00...0=m.2^{k+1}$. Use $k$ (and $m$) to categorize the cases.
    – Nicolas FRANCOIS
    Nov 17 at 15:16












  • Yes, & is for 'and ' operator
    – Daniel
    Nov 17 at 16:12










  • Can you detail, please for second case?
    – Daniel
    Nov 17 at 16:20






  • 1




    BTW, are you sure the end condition in the internal loop is not "j>0" ? Because if j is unsigned, this could go forever... But maybe it's simply that I don't understand C code...
    – Nicolas FRANCOIS
    Nov 17 at 18:11














  • 1




    Your code is almost unreadable. Please use "insert code" and use indentation in your code. I suppose & is for "and" operator. What I would do is find value of $f(i,j)$ by making cases, this will enable you to find the number of times the "s++" line is executed...
    – Nicolas FRANCOIS
    Nov 17 at 15:06






  • 1




    As an example, for case 1), suppose $j=*...*10...0=m.2^{k+1}+1.2^k$. Put it like this : $k$ is the position of the first bit set to $1$ in the binary notation of $j$. Then you find $j-1=*...*01...1$, so $(j-1)& j=*...*00...0=m.2^{k+1}$. Use $k$ (and $m$) to categorize the cases.
    – Nicolas FRANCOIS
    Nov 17 at 15:16












  • Yes, & is for 'and ' operator
    – Daniel
    Nov 17 at 16:12










  • Can you detail, please for second case?
    – Daniel
    Nov 17 at 16:20






  • 1




    BTW, are you sure the end condition in the internal loop is not "j>0" ? Because if j is unsigned, this could go forever... But maybe it's simply that I don't understand C code...
    – Nicolas FRANCOIS
    Nov 17 at 18:11








1




1




Your code is almost unreadable. Please use "insert code" and use indentation in your code. I suppose & is for "and" operator. What I would do is find value of $f(i,j)$ by making cases, this will enable you to find the number of times the "s++" line is executed...
– Nicolas FRANCOIS
Nov 17 at 15:06




Your code is almost unreadable. Please use "insert code" and use indentation in your code. I suppose & is for "and" operator. What I would do is find value of $f(i,j)$ by making cases, this will enable you to find the number of times the "s++" line is executed...
– Nicolas FRANCOIS
Nov 17 at 15:06




1




1




As an example, for case 1), suppose $j=*...*10...0=m.2^{k+1}+1.2^k$. Put it like this : $k$ is the position of the first bit set to $1$ in the binary notation of $j$. Then you find $j-1=*...*01...1$, so $(j-1)& j=*...*00...0=m.2^{k+1}$. Use $k$ (and $m$) to categorize the cases.
– Nicolas FRANCOIS
Nov 17 at 15:16






As an example, for case 1), suppose $j=*...*10...0=m.2^{k+1}+1.2^k$. Put it like this : $k$ is the position of the first bit set to $1$ in the binary notation of $j$. Then you find $j-1=*...*01...1$, so $(j-1)& j=*...*00...0=m.2^{k+1}$. Use $k$ (and $m$) to categorize the cases.
– Nicolas FRANCOIS
Nov 17 at 15:16














Yes, & is for 'and ' operator
– Daniel
Nov 17 at 16:12




Yes, & is for 'and ' operator
– Daniel
Nov 17 at 16:12












Can you detail, please for second case?
– Daniel
Nov 17 at 16:20




Can you detail, please for second case?
– Daniel
Nov 17 at 16:20




1




1




BTW, are you sure the end condition in the internal loop is not "j>0" ? Because if j is unsigned, this could go forever... But maybe it's simply that I don't understand C code...
– Nicolas FRANCOIS
Nov 17 at 18:11




BTW, are you sure the end condition in the internal loop is not "j>0" ? Because if j is unsigned, this could go forever... But maybe it's simply that I don't understand C code...
– Nicolas FRANCOIS
Nov 17 at 18:11










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










For the second case, let's explore an example : let's choose $i=203=11001011_2$, we find the following sequence for $j$ :



$11001011$



$11001010$



$11001001$



$11001000$



$11000011$



$11000010$



$11000001$



$11000000$



$10001011$



etc



So everything is as if the zeroes where ignored : you count from $x$ to $0$, where $x$ is written whit only the ones in the binary writing of $i$.



If $i$ has $k$ bits set to $1$, then $x=2^k-1$ is the time spent in the inner loop. So the complexity is
$$1+sum_k=0^n (2^k-1)binom nk = 1+sum_k=0^n 2^kbinom nk - sum_k=0^n binom nk = 1 + (1+2)^n - 2^n = 3^n+1-2^n$$
Hope my analysis is correct.






share|cite|improve this answer





















  • Yes, it is correct! Nice solution, thanks very much!
    – Daniel
    Nov 18 at 18:08


















up vote
0
down vote













In both cases, the inner loop runs forever because j remains stuck at 0.






share|cite|improve this answer




























    up vote
    0
    down vote













    Let's see the first case : as was said in the comments, $f(i,j)=(j-1)& j$ consists in changing the lest significant bit set to $1$ in $j$ to $0$. So repeating operation $j=f(i,j)$ until $j=0$ does erase the $1$ bits of $j$ from right to left, you make as many passages in the loop as there are bits set to $1$ in $j$.



    As $j$ is initially $i$ in the inner loop, you have to count the number of bits set to $1$ in $i$. As $i$ goes from $0$ to $2^n-1$, $i$ has $n$ bits. The number of $i$ for which the binary form contains $k$ bits set to $1$ is $binom nk$, for you have to choose the places of the ones. For those $i$, the inner loop is executed $k$ times. So the number of times the instruction $s++$ is executed is
    $$sum_{k=0}^n k.binom nk = sum_{k=1}^n n.binom{n-1}{k-1} = nsum_{k'=0}^{n-1} binom{n-1}{k'} = n.2^{n-1}$$
    to which you have to add $1$ because the upper bound is $2^n$, not $2^n-1$, so there is one more iteration for $i=2^n$.



    The second case is a little bit more difficult, but you can benefit from the fact that the initial value of $j$ is $i$. So the first time, the rightmost bit set to one in $j$ is set to $0$. After that, you have to figure out what will happen. The loop must end at some time, because $(j-1)& ile j-1<j$. Study some example to figure out how many times the inner loop is executed.






    share|cite|improve this answer






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      For the second case, let's explore an example : let's choose $i=203=11001011_2$, we find the following sequence for $j$ :



      $11001011$



      $11001010$



      $11001001$



      $11001000$



      $11000011$



      $11000010$



      $11000001$



      $11000000$



      $10001011$



      etc



      So everything is as if the zeroes where ignored : you count from $x$ to $0$, where $x$ is written whit only the ones in the binary writing of $i$.



      If $i$ has $k$ bits set to $1$, then $x=2^k-1$ is the time spent in the inner loop. So the complexity is
      $$1+sum_k=0^n (2^k-1)binom nk = 1+sum_k=0^n 2^kbinom nk - sum_k=0^n binom nk = 1 + (1+2)^n - 2^n = 3^n+1-2^n$$
      Hope my analysis is correct.






      share|cite|improve this answer





















      • Yes, it is correct! Nice solution, thanks very much!
        – Daniel
        Nov 18 at 18:08















      up vote
      0
      down vote



      accepted










      For the second case, let's explore an example : let's choose $i=203=11001011_2$, we find the following sequence for $j$ :



      $11001011$



      $11001010$



      $11001001$



      $11001000$



      $11000011$



      $11000010$



      $11000001$



      $11000000$



      $10001011$



      etc



      So everything is as if the zeroes where ignored : you count from $x$ to $0$, where $x$ is written whit only the ones in the binary writing of $i$.



      If $i$ has $k$ bits set to $1$, then $x=2^k-1$ is the time spent in the inner loop. So the complexity is
      $$1+sum_k=0^n (2^k-1)binom nk = 1+sum_k=0^n 2^kbinom nk - sum_k=0^n binom nk = 1 + (1+2)^n - 2^n = 3^n+1-2^n$$
      Hope my analysis is correct.






      share|cite|improve this answer





















      • Yes, it is correct! Nice solution, thanks very much!
        – Daniel
        Nov 18 at 18:08













      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      For the second case, let's explore an example : let's choose $i=203=11001011_2$, we find the following sequence for $j$ :



      $11001011$



      $11001010$



      $11001001$



      $11001000$



      $11000011$



      $11000010$



      $11000001$



      $11000000$



      $10001011$



      etc



      So everything is as if the zeroes where ignored : you count from $x$ to $0$, where $x$ is written whit only the ones in the binary writing of $i$.



      If $i$ has $k$ bits set to $1$, then $x=2^k-1$ is the time spent in the inner loop. So the complexity is
      $$1+sum_k=0^n (2^k-1)binom nk = 1+sum_k=0^n 2^kbinom nk - sum_k=0^n binom nk = 1 + (1+2)^n - 2^n = 3^n+1-2^n$$
      Hope my analysis is correct.






      share|cite|improve this answer












      For the second case, let's explore an example : let's choose $i=203=11001011_2$, we find the following sequence for $j$ :



      $11001011$



      $11001010$



      $11001001$



      $11001000$



      $11000011$



      $11000010$



      $11000001$



      $11000000$



      $10001011$



      etc



      So everything is as if the zeroes where ignored : you count from $x$ to $0$, where $x$ is written whit only the ones in the binary writing of $i$.



      If $i$ has $k$ bits set to $1$, then $x=2^k-1$ is the time spent in the inner loop. So the complexity is
      $$1+sum_k=0^n (2^k-1)binom nk = 1+sum_k=0^n 2^kbinom nk - sum_k=0^n binom nk = 1 + (1+2)^n - 2^n = 3^n+1-2^n$$
      Hope my analysis is correct.







      share|cite|improve this answer












      share|cite|improve this answer



      share|cite|improve this answer










      answered Nov 18 at 17:39









      Nicolas FRANCOIS

      3,6221516




      3,6221516












      • Yes, it is correct! Nice solution, thanks very much!
        – Daniel
        Nov 18 at 18:08


















      • Yes, it is correct! Nice solution, thanks very much!
        – Daniel
        Nov 18 at 18:08
















      Yes, it is correct! Nice solution, thanks very much!
      – Daniel
      Nov 18 at 18:08




      Yes, it is correct! Nice solution, thanks very much!
      – Daniel
      Nov 18 at 18:08










      up vote
      0
      down vote













      In both cases, the inner loop runs forever because j remains stuck at 0.






      share|cite|improve this answer

























        up vote
        0
        down vote













        In both cases, the inner loop runs forever because j remains stuck at 0.






        share|cite|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          In both cases, the inner loop runs forever because j remains stuck at 0.






          share|cite|improve this answer












          In both cases, the inner loop runs forever because j remains stuck at 0.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Nov 17 at 18:18









          Yves Daoust

          122k668217




          122k668217






















              up vote
              0
              down vote













              Let's see the first case : as was said in the comments, $f(i,j)=(j-1)& j$ consists in changing the lest significant bit set to $1$ in $j$ to $0$. So repeating operation $j=f(i,j)$ until $j=0$ does erase the $1$ bits of $j$ from right to left, you make as many passages in the loop as there are bits set to $1$ in $j$.



              As $j$ is initially $i$ in the inner loop, you have to count the number of bits set to $1$ in $i$. As $i$ goes from $0$ to $2^n-1$, $i$ has $n$ bits. The number of $i$ for which the binary form contains $k$ bits set to $1$ is $binom nk$, for you have to choose the places of the ones. For those $i$, the inner loop is executed $k$ times. So the number of times the instruction $s++$ is executed is
              $$sum_{k=0}^n k.binom nk = sum_{k=1}^n n.binom{n-1}{k-1} = nsum_{k'=0}^{n-1} binom{n-1}{k'} = n.2^{n-1}$$
              to which you have to add $1$ because the upper bound is $2^n$, not $2^n-1$, so there is one more iteration for $i=2^n$.



              The second case is a little bit more difficult, but you can benefit from the fact that the initial value of $j$ is $i$. So the first time, the rightmost bit set to one in $j$ is set to $0$. After that, you have to figure out what will happen. The loop must end at some time, because $(j-1)& ile j-1<j$. Study some example to figure out how many times the inner loop is executed.






              share|cite|improve this answer



























                up vote
                0
                down vote













                Let's see the first case : as was said in the comments, $f(i,j)=(j-1)& j$ consists in changing the lest significant bit set to $1$ in $j$ to $0$. So repeating operation $j=f(i,j)$ until $j=0$ does erase the $1$ bits of $j$ from right to left, you make as many passages in the loop as there are bits set to $1$ in $j$.



                As $j$ is initially $i$ in the inner loop, you have to count the number of bits set to $1$ in $i$. As $i$ goes from $0$ to $2^n-1$, $i$ has $n$ bits. The number of $i$ for which the binary form contains $k$ bits set to $1$ is $binom nk$, for you have to choose the places of the ones. For those $i$, the inner loop is executed $k$ times. So the number of times the instruction $s++$ is executed is
                $$sum_{k=0}^n k.binom nk = sum_{k=1}^n n.binom{n-1}{k-1} = nsum_{k'=0}^{n-1} binom{n-1}{k'} = n.2^{n-1}$$
                to which you have to add $1$ because the upper bound is $2^n$, not $2^n-1$, so there is one more iteration for $i=2^n$.



                The second case is a little bit more difficult, but you can benefit from the fact that the initial value of $j$ is $i$. So the first time, the rightmost bit set to one in $j$ is set to $0$. After that, you have to figure out what will happen. The loop must end at some time, because $(j-1)& ile j-1<j$. Study some example to figure out how many times the inner loop is executed.






                share|cite|improve this answer

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Let's see the first case : as was said in the comments, $f(i,j)=(j-1)& j$ consists in changing the lest significant bit set to $1$ in $j$ to $0$. So repeating operation $j=f(i,j)$ until $j=0$ does erase the $1$ bits of $j$ from right to left, you make as many passages in the loop as there are bits set to $1$ in $j$.



                  As $j$ is initially $i$ in the inner loop, you have to count the number of bits set to $1$ in $i$. As $i$ goes from $0$ to $2^n-1$, $i$ has $n$ bits. The number of $i$ for which the binary form contains $k$ bits set to $1$ is $binom nk$, for you have to choose the places of the ones. For those $i$, the inner loop is executed $k$ times. So the number of times the instruction $s++$ is executed is
                  $$sum_{k=0}^n k.binom nk = sum_{k=1}^n n.binom{n-1}{k-1} = nsum_{k'=0}^{n-1} binom{n-1}{k'} = n.2^{n-1}$$
                  to which you have to add $1$ because the upper bound is $2^n$, not $2^n-1$, so there is one more iteration for $i=2^n$.



                  The second case is a little bit more difficult, but you can benefit from the fact that the initial value of $j$ is $i$. So the first time, the rightmost bit set to one in $j$ is set to $0$. After that, you have to figure out what will happen. The loop must end at some time, because $(j-1)& ile j-1<j$. Study some example to figure out how many times the inner loop is executed.






                  share|cite|improve this answer














                  Let's see the first case : as was said in the comments, $f(i,j)=(j-1)& j$ consists in changing the lest significant bit set to $1$ in $j$ to $0$. So repeating operation $j=f(i,j)$ until $j=0$ does erase the $1$ bits of $j$ from right to left, you make as many passages in the loop as there are bits set to $1$ in $j$.



                  As $j$ is initially $i$ in the inner loop, you have to count the number of bits set to $1$ in $i$. As $i$ goes from $0$ to $2^n-1$, $i$ has $n$ bits. The number of $i$ for which the binary form contains $k$ bits set to $1$ is $binom nk$, for you have to choose the places of the ones. For those $i$, the inner loop is executed $k$ times. So the number of times the instruction $s++$ is executed is
                  $$sum_{k=0}^n k.binom nk = sum_{k=1}^n n.binom{n-1}{k-1} = nsum_{k'=0}^{n-1} binom{n-1}{k'} = n.2^{n-1}$$
                  to which you have to add $1$ because the upper bound is $2^n$, not $2^n-1$, so there is one more iteration for $i=2^n$.



                  The second case is a little bit more difficult, but you can benefit from the fact that the initial value of $j$ is $i$. So the first time, the rightmost bit set to one in $j$ is set to $0$. After that, you have to figure out what will happen. The loop must end at some time, because $(j-1)& ile j-1<j$. Study some example to figure out how many times the inner loop is executed.







                  share|cite|improve this answer














                  share|cite|improve this answer



                  share|cite|improve this answer








                  edited Nov 18 at 16:51

























                  answered Nov 18 at 16:45









                  Nicolas FRANCOIS

                  3,6221516




                  3,6221516















                      Popular posts from this blog

                      QoS: MAC-Priority for clients behind a repeater

                      Ивакино (Тотемский район)

                      Can't locate Autom4te/ChannelDefs.pm in @INC (when it definitely is there)