Generate Binary Strings of length N using Branch and Bound Last Updated : 01 Mar, 2023 Comments Improve Suggest changes Like Article Like Report The task is to generate a binary string of length N using branch and bound technique Examples: Input: N = 3 Output: 000 001 010 011 100 101 110 111 Explanation: Numbers with 3 binary digits are 0, 1, 2, 3, 4, 5, 6, 7 Input: N = 2 Output: 00 01 10 11 Approach: Generate Combinations using Branch and Bound : It starts with an empty solution vector.While Queue is not empty remove partial vector from queue.If it is a final vector print the combination else,For the next component of partial vector create k child vectors by fixing all possible states for the next component insert vectors into the queue. Below is the implementation of the above approach C++ // CPP Program to generate // Binary Strings using Branch and Bound #include <bits/stdc++.h> using namespace std; // Creating a Node class class Node { public: int *soln; int level; vector<Node *> child; Node *parent; Node(Node *parent, int level, int N) { this->parent = parent; this->level = level; this->soln = new int[N]; } }; // Utility function to generate binary strings of length n void generate(Node *n, int &N, queue<Node *> &Q) { // If list is full print combination if (n->level == N) { for (int i = 0; i < N; i++) cout << n->soln[i]; cout << endl; } else { int l = n->level; // iterate while length is not equal to n for (int i = 0; i <= 1; i++) { Node *x = new Node(n, l + 1, N); for (int k = 0; k < l; k++) x->soln[k] = n->soln[k]; x->soln[l] = i; n->child.push_back(x); Q.push(x); } } } // Driver Code int main() { // Initiate Generation // Create a root Node int N = 3; Node *root; root = new Node(NULL, 0, N); // Queue that maintains the list of live Nodes queue<Node *> Q; // Instantiate the Queue Q.push(root); while (!Q.empty()) { Node *E = Q.front(); Q.pop(); generate(E, N, Q); } return 0; } // This code is contributed by // sanjeev2552 Java // Java Program to generate // Binary Strings using Branch and Bound import java.io.*; import java.util.*; // Creating a Node class class Node { int soln[]; int level; ArrayList<Node> child; Node parent; Node(Node parent, int level, int N) { this.parent = parent; this.level = level; this.soln = new int[N]; } } class GFG { static int N; // Queue that maintains the list of live Nodes public static Queue<Node> Q; // Utility function to generate binary strings of length n public static void generate(Node n) { // If list is full print combination if (n.level == N) { for (int i = 0; i <= N - 1; i++) { System.out.print(n.soln[i]); } System.out.println(); } else { // Create a new vector for new combination n.child = new ArrayList<Node>(); int l = n.level; // iterate while length is not equal to n for (int i = 0; i <= 1; i++) { Node x = new Node(n, l + 1, N); for (int k = 0; k < l; k++) { x.soln[k] = n.soln[k]; } x.soln[l] = i; n.child.add(x); Q.add(x); } } } // Driver code public static void main(String args[]) { // Initiate Generation // Create a root Node N = 3; Node root = new Node(null, 0, N); // Instantiate the Queue Q = new LinkedList<Node>(); Q.add(root); while (Q.size() != 0) { Node E = Q.poll(); generate(E); } } } Python3 from queue import Queue # Creating a Node class class Node: def __init__(self, parent, level, N): self.parent = parent self.level = level self.soln = [0]*N self.child = [] # Queue that maintains the list of live Nodes Q = Queue() # Utility function to generate binary strings of length n def generate(n): # If list is full print combination if n.level == N: print(''.join(str(x) for x in n.soln)) else: # Create a new list for new combination n.child = [] l = n.level # iterate while length is not equal to n for i in range(2): x = Node(n, l + 1, N) x.soln[:l] = n.soln[:l] x.soln[l] = i n.child.append(x) Q.put(x) # Driver code if __name__ == '__main__': # Initiate Generation # Create a root Node N = 3 root = Node(None, 0, N) # Instantiate the Queue Q.put(root) while not Q.empty(): E = Q.get() generate(E) C# // C# Program to generate // Binary Strings using Branch and Bound using System; using System.Collections.Generic; // Creating a Node class public class Node { public int []soln; public int level; public List<Node> child; public Node parent; public Node(Node parent, int level, int N) { this.parent = parent; this.level = level; this.soln = new int[N]; } } class GFG { static int N; // Queue that maintains the list of live Nodes public static Queue<Node> Q; // Utility function to generate // binary strings of length n public static void generate(Node n) { // If list is full print combination if (n.level == N) { for (int i = 0; i <= N - 1; i++) { Console.Write(n.soln[i]); } Console.WriteLine(); } else { // Create a new vector for new combination n.child = new List<Node>(); int l = n.level; // iterate while length is not equal to n for (int i = 0; i <= 1; i++) { Node x = new Node(n, l + 1, N); for (int k = 0; k < l; k++) { x.soln[k] = n.soln[k]; } x.soln[l] = i; n.child.Add(x); Q.Enqueue(x); } } } // Driver code public static void Main(String []args) { // Initiate Generation // Create a root Node N = 3; Node root = new Node(null, 0, N); // Instantiate the Queue Q = new Queue<Node>(); Q.Enqueue(root); while (Q.Count != 0) { Node E = Q.Dequeue(); generate(E); } } } // This code is contributed by Rajput-Ji JavaScript // Javascript code for the above approach // Creating a Node class class Node { constructor(parent, level, N) { this.parent = parent; this.level = level; this.soln = new Array(N).fill(0); this.child = []; } } // Queue that maintains the list of live Nodes const Q = []; // Utility function to generate binary strings of length n function generate(n, N) { // If list is full print combination if (n.level === N) { console.log(n.soln.join("")); } else { // Create a new list for new combination n.child = []; const l = n.level; // iterate while length is not equal to n for (let i = 0; i < 2; i++) { const x = new Node(n, l + 1, N); x.soln = n.soln.slice(); x.soln[l] = i; n.child.push(x); Q.push(x); } } } // Driver code (() => { // Initiate Generation // Create a root Node const N = 3; const root = new Node(null, 0, N); // Instantiate the Queue Q.push(root); while (Q.length > 0) { const E = Q.shift(); generate(E, N); } })(); // This code is contributed by sdeadityasharma Output:000 001 010 011 100 101 110 111 Time Complexity: O(2^n) Comment More infoAdvertise with us Next Article Generate Binary Strings of length N using Branch and Bound R Ravitha Rajalakshmi Follow Improve Article Tags : Strings Algorithms Analysis of Algorithms Backtracking Technical Scripter Branch and Bound DSA Technical Scripter 2019 binary-string +5 More Practice Tags : AlgorithmsBacktrackingStrings Similar Reads Generate all Binary Strings of length N with equal count of 0s and 1s Given an integer N, the task is to generate all the binary strings with equal 0s and 1s. If no strings are possible, print -1 Examples: Input: N = 2 Output: â01â, â10âExplanation: All possible binary strings of length 2 are: 01, 10, 11, 00. Out of these, only 2 have equal number of 0s and 1s Input: 6 min read Generate all the binary strings of N bits Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.Examples: Input: 2Output:0 00 11 01 1Input: 3Output:0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1Approach: The idea is to try every permutation. For every positio 8 min read Generate the String of length N according to given conditions Given two integers N and M. Then your task is to output the string let's say S of length N by following given conditions: S must be formed by only the first M smaller case letters of the alphabetical series.The length of LPS (Longest palindromic Substring) in S should be the minimum possible.Example 10 min read Count number of strings (made of R, G and B) using given combination We need to make a string of size n. Each character of the string is either âRâ, âBâ or âGâ. In the final string there needs to be at least r number of âRâ, at least b number of âBâ and at least g number of âGâ (such that r + g + b <= n). We need to find number of such strings possible. Examples: 7 min read Count of binary strings of given length consisting of at least one 1 Given an integer N, the task is to print the number of binary strings of length N which at least one '1'. Examples: Input: 2 Output: 3 Explanation: "01", "10" and "11" are the possible strings Input: 3 Output: 7 Explanation: "001", "011", "010", "100", "101", "110" and "111" are the possible strings 3 min read Like