0

Can someone please tell how to generate n-bit strings(all possible combinations) i.e. counting bits form 0 to 2^n-1 using Divide and Conquer Approach.

I was able to do this with the following algorithm, but the space complexity as well as time complexity are of O(2^n). Can someone give me a better algorithm (using Divide and conquer) which requires less space than this.

ArrayList generate(int n)
 {
      if(n==1) 
         {  
            Create an arrayList and store strings "0" and "1";
         }
     else
    {
         generate(n-1)

         now recompose the solution to get 2^n strings and return this arraylist
         "PROBLEM here is that the length of the array list is also getting
         exponential"

    }
 }

1 Answers1

0

I believe you are misunderstood. Generating a bit string is not a problem on its own, so you can not propose a solution for it. Perhaps you left out a part of the problem. For instance you might define a representation of a solution using a bit string and then try to find the optimal bit string for a given problem.

One more thing, in general the time complexity of a problem represented as an n-bit string is always O(2^n) unless the problem is defined. Then you can use problem's criteria for reducing the either complexity. But before the problem is defined, generating and traversing an n-bit string always requires you to tend to each and every single possible 2^n combination.

EDIT:

Here's a pseudocode for divide & conquer, if you must:

solution breakdown(problem p)
{
    if (smallenough(p))
    {
        return solve(p);
    }
    problem[] subproblems = divide(p);
    solution[] subsolutions;
    for (i=0; i<count(subproblems); i++)
    {
        subsolutions[i] = breakdown(subproblems[i]);
    }
    return reconstruct(subsolutions);
}
Mehran
  • 15,593
  • 27
  • 122
  • 221
  • Yes you are right in saying that I need to find one bit string from the set of 2^n possible bit strings, but prior to finding that bit string i must be able to generate those and then see which is the most optimal of these all ( A Brute Force Approach). Thus I am faced with a problem of generating these n-bit strings using divide and conquer, Please help if u can give a divide and conquer algo for the same. I hope I have made myself clear!! –  Sep 17 '12 at 05:50
  • Here again your solution is using space proportional to 2^n. I was even able to solve this problem with this constraint. But I want to solve this problem with space complexity O(n) and use Divide and Conquer approach –  Sep 17 '12 at 08:23
  • Wrong! Its space complexity is not O(2^n), it's O(n)! Each (sub)problem is divided only once. This means you can have n subproblems at most. Hence the space complexity is proportional to n. – Mehran Sep 17 '12 at 09:23
  • Actually I dont think this will lead to O(n). You have just given a Pseudo code, It will be great if u can try to build a working code using the pseudo code (may be in Java--its easy there), and then we can verify –  Sep 17 '12 at 14:14