Program to print all subset of length l - c++
#include<iostream>
using namespace std;
void combinationUtil(int arr[], int n, int r, int index, int data[], int i);
void printCombination(int arr[], int n, int r)
{
int data[r];
combinationUtil(arr, n, r, 0, data, 0);
}
void combinationUtil(int arr[], int n, int r, int index, int data[], int i)
{
if (index == r)
{
for (int j = 0; j < r; j++)
cout << data[j] << " ";
cout << endl;
return;
}
if (i >= n)
return;
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1, data, i + 1);
combinationUtil(arr, n, r, index, data, i + 1);
}
int main()
{
int n = 5;
int arr[n] = { 1, 2, 3, 4, 5 };
int k = 3;
printCombination(arr, n, k);
return 0;
}
This is the code to print all possible subsets of length k. I didn’t understand the part how the function returns to the part where it prints the subset 134 after printing the subset 125. Please explain.
I even drew the tree, but how does the code create that "13" series after printing 125 . I'm quite weak at recursion, please correct me if there's a mistake in my code or tree.
Recursion Tree:
Let's inspect the programme execution (you can do this easily using a debugger).
Firstly since arr, n and r don't change in the call tree, let's note them down first:
n := 5, r := 3, arr := {1, 2, 3, 4, 5}
For the variable arguments let's build a table and note the first call to combinationUtil:
Callstack | index | i | data | Cases (printing, returning or recursing)
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 0 | 0 | {., ., .} | recursing (index != r, i != n)
printCombination | | | |
main | | | |
Let's ignore main and printCombination in the future, but note that the top of the stack describes the function we've just entered with the given state of index, i and data (and arr, n, r but those won't change) - specifically we did not yet execute any of the body of the function like assigning data[index] = arr[i] or similar. Following the execution note how we're neither printing nor breaking out early since index != r and i != n meaning we'll recurse twice in this function call. Let's step to the first recursive call:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 1 | 1 | {1, ., .} | recursing
combinationUtil | 0 | 0 | |
When we enter our first recursive call, 1 will have been written to data so at the beginning of the function we can now see this value in data. We'll continue twice:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 2 | {1, 2, .} | recursing
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 3 | {1, 2, 3} | printing (index == r)
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
New case: index == r i.e. we'll loop over data, print its current content and return, one level higher we'll step into the next recursive call so let's look at the next steps:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 3 | {1, 2, 3} | recursing
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 4 | {1, 2, 4} | printing
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 4 | {1, 2, 4} | recursing
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 5 | {1, 2, 5} | printing
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 5 | {1, 2, 5} | returning (i >= n)
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 2 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
Can you see how deep the call stack got until we printed 125? If you've been drawing the call graph along you should have something along the following now:
[] (0, 0)
/
1 (1, 1)
/
12 (2, 2)
/ \
123 12 (2, 3)
/ \
124 12 (2, 4)
/ \
125 ret (2, 5)
We're currently at ret and I've marked the arguments (index, i) from our current callstack along the root path (from [] to ret). We'll be returing in ret because i == n, we'll then return from each call up the tree until we reach 1 (index=1, i=1) where we return from its first recursion (index+1, i+1) so its next is (index, i+1) and the following next 6 steps together look like:
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 1 | 2 | {1, 2, 5} | recursing
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 3 | {1, 3, 5} | recursing
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 4 | {1, 3, 4} | printing
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 4 | {1, 3, 4} | recursing
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 3 | 5 | {1, 3, 5} | printing
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
----------------------+-------+-------+-----------+------------------------------------
> combinationUtil | 2 | 5 | {1, 3, 5} | returning
combinationUtil | 2 | 4 | |
combinationUtil | 2 | 3 | |
combinationUtil | 1 | 2 | |
combinationUtil | 1 | 1 | |
combinationUtil | 0 | 0 | |
And the call graph up to this point looks like:
[] (0, 0)
/
1 (1, 1)
/-------^-------\
12 1 (1, 2)
/ \ /
123 12 13 (2, 3)
/ \ / \
124 12 134 13 (2, 4)
/ \ / \
125 ret 135 ret (2, 5)
Your code is an example of DFS (Depth First Search). For every node, it first recurs on the leftmost child, then when it's at the rightmost child, this "part" of the function ends, essentially going back to the previous node and recurring on its children. Basically, children have priority over siblings, and left has priority over right.
If any part of this post is unclear, tell me and I will elaborate. Welcome to Stack Overflow :)
Related
Conditional count Measure
I have data looking like this: | ID |OpID| | -- | -- | | 10 | 1 | | 10 | 2 | | 10 | 4 | | 11 |null| | 12 | 3 | | 12 | 4 | | 13 | 1 | | 13 | 2 | | 13 | 3 | | 14 | 2 | | 14 | 4 | Here OpID 4 means 1 and 2. I would like to count the different occurrences of 1, 2 and 3 in OpID of distinct ID. If the counts of OpID having 1 would be 4, 2 would be 4, 3 would be 2. If ID has OpID of 4 but already has data of 1, 2 it wouldn't be counted. But if 4 exists and only 1 (2) is there, count for 2 (1) would be incremented. The expected output would be: |OpID|Count| | 1 | 4 | | 2 | 4 | | 3 | 2 | (Going to be using the results in a column chart) Hope this makes sense... edit: there are other columns too and an ID and OpID can be duplicated hence need to do a groupby clause before.
How to increase the verbosity level of SCIP solver log
I have the following SCIP solver log time | node | left |LP iter|LP it/n| mem |mdpt |frac |vars |cons |cols |rows | 0.0s| 1 | 0 | 4 | - | 6k| 0 | 0 | 6 | 200 | 6 | 200 | 0.0s| 1 | 0 | 7 | - | 6k| 0 | 0 | 8 | 200 | 8 | 200 | 0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 | 0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 | 0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 | 0.0s| 1 | 0 | 10 | - | 6k| 0 | 0 | 9 | 200 | 9 | 200 | I want the log to be more verbose, as in display a new line at each LP iteration. So far I only came across SCIP_CALL( SCIPsetIntParam(scip, "display/verblevel", 5)); This is increasing but not as much as I want and not where I want. Essentially I would like to have lines at LP iter 4, 5, 6, 7, 8, 9 and 10 too.
You cannot print a line of SCIP output at every LP iteration. You can set display/freq to 1, then SCIP will display a line at every node. Additionally you can set display/lpinfo to true, then the LP solver will print additional information. I don't think any LP solver will print you a line for every LP iteration though . Do you use SCIP with SoPlex? Edit: Looked and you can set the SoPlex frequency to 1 with the parameter "--int:displayfreq". I don't think you can set this through the SCIP api though. If you only want to solve the LP you could just do it in SoPlex or you would have to edit the lpi_spx2 source code.
Maximum overlapping of arithmetic sequences with intervals
I have n number of arithmetic sequences with intervals. I need to find the first point at which most of this intervals overlap. The sequences are infinite. Let me give an example for finite sequences where I have total 8 sequences. Given, N=8. So, there are 8 sequences. The sequences are as follows: seq-1: [{1,2,3,4,5,6,7,8}..{17,18,19,20,21,22,23,24}] seq-2: [{9,10,11,12,13,14,15,16}..{25,26,27,28,29,30,31,32}] seq-3: [{1,2,3,4}..{9,10,11,12}..{17,18,19,20}..{25,26,27,28}] seq-4: [{5,6,7,8}..{13,14,15,16}..{21,22,23,24}..{29,30,31,32}] seq-5: [{5}..{13}..{21}..{29}] seq-6: [{4}..{8}..{12}..{16}..{20}..{24}..{28}..{32}] seq-7: [{9,11,13,15}..{25,27,29,31}] seq-8: [{2}..{18}] Here point 13 and 29 have the maximum overlap with 4 over laps. And the first point is 13. Can I solve it using some efficient algorithm like O(n),O(n^2), O(n^3), O(n^4), O(n log n) etc. Here, the value of n is 8.
I suggest applying Distribution Counting algorithm. Below is a simple demo for the algorithm with 3 sample sequences: seq-1: [{1,2,}..{9,10}] seq-2: [{1,2,3}..{5,7,8}] seq-3: [{2,3,4}..{6,7}..{9,10}] You need to find the maximum value in all sequences. Which is 10 in this case. Create an int array of 11 elements starting from 0 to 10. i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------- A[i]| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | Now we count the appearance of elements in all sequences by increasing its value by 1. seq-1: [{1,2,}..{9,10}] This sequence contains 1, 2, 9, and 10. Increase value at index 1, 2, 9, and 10 by 1. i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------- A[i]| 0 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | seq-2: [{1,2,3}..{5,7,8}] This sequence contains 1, 2, 3, 5, 7,and 8. Increase value at index 1, 2, 3, 5, 7, and 8 by 1. i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------- A[i]| 0 | 2 | 2 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | seq-3: [{2,3,4}..{6,7}..{9,10}] This sequence contains 2, 3, 4, 6, 7, 9, and 10. Increase value at index 2, 3, 4, 6, 7, 9, and 10 by 1. i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------- A[i]| 0 | 2 | 3 | 2 | 1 | 1 | 1 | 2 | 1 | 2 | 2 | In the end, it is obvious that number 2 has the maximum overlapping times of 3 between all the sequences. Hope my suggestion will help!
How to assemble wavefront .obj data into element and vertex arrays of minimal size?
I'm having trouble putting the data inside a wavefront .obj file together. These are the vec3 and vec2 definitions template <typename T> struct vec3 { T x; T y; T z; }; template <typename T> struct vec2 { T x; T y; }; Used in a vector: +-----------------------------------+--------------+--------------+-------+ | std::vector<vec3<uint32_t>> f_vec | 0 | 1 | (...) | +-----------------------------------+--------------+--------------+-------+ | | v_vec_index | v_vec_index | (...) | | +--------------+--------------+-------+ | | vt_vec_index | vt_vec_index | (...) | | +--------------+--------------+-------+ | | vn_vec_index | vn_vec_index | (...) | +-----------------------------------+--------------+--------------+-------+ Where: v_vec_index is an index of std::vector<vec3<float>> v_vec with its fields containing vertex x, y and z coordinates vt_vec_index is an index of std::vector<vec2<float>> vt_vec containing texture u and v coordinates vn_vec_index is an index of std::vector<vec3<float>> vn_vec with normal x, y and z coordinates Every f_vec field is used to create a sequence of vert_x, vert_y, vert_z, tex_u, tex_v, norm_x, norm_y, norm_z float values inside std::vector<float> vertex_array. Also, every index of f_vec's field is by default a value of std::vector<uint32_t>> element_array - that is it contains the range of integers from 0 to f_vec.size() - 1. The problem is vec3 fields inside f_vec may repeat. So in order to assemble only the unique sequences mentioned above I planned to turn something like this: +-----------------+---+---+---+---+---+ | f_vec | 0 | 1 | 2 | 3 | 4 | +-----------------+---+---+---+---+---+ | | 1 | 3 | 1 | 3 | 4 | | +---+---+---+---+---+ | | 2 | 2 | 2 | 2 | 5 | | +---+---+---+---+---+ | | 2 | 4 | 2 | 4 | 5 | +-----------------+---+---+---+---+---+ Into this: +------------------------+-----------------+---+---+---+---+---+ | whatever that would be | index | 0 | 1 | 2 | 3 | 4 | +------------------------+-----------------+---+---+---+---+---+ | | key | 0 | 1 | 0 | 1 | 2 | | +-----------------+---+---+---+---+---+ | | | 1 | 3 | 1 | 3 | 4 | | | +---+---+---+---+---+ | | vec3 of indices | 2 | 2 | 2 | 2 | 5 | | | +---+---+---+---+---+ | | | 2 | 4 | 2 | 4 | 5 | +------------------------+-----------------+---+---+---+---+---+ Where every time an element of f_vec would be put into the "whatever container" It would be checked if it is unique If it is then it would be pushed to the end of the container with its key being the next natural number after the biggest key - the key's value would be pushed to the element_array and new vertex would be created inside vertex_array If it isn't then it would be pushed to the end of the container with its key being the same as the key of its duplicate - the key's value would be pushed to the element_array but vertex_array would remain unchanged How am I supposed to do it?
bit wise addtion in C++
I am looking to following code at following link https://www.geeksforgeeks.org/divide-and-conquer-set-2-karatsuba-algorithm-for-fast-multiplication/ // The main function that adds two bit sequences and returns the addition string addBitStrings( string first, string second ) { string result; // To store the sum bits // make the lengths same before adding int length = makeEqualLength(first, second); int carry = 0; // Initialize carry // Add all bits one by one for (int i = length-1 ; i >= 0 ; i--) { int firstBit = first.at(i) - '0'; int secondBit = second.at(i) - '0'; // boolean expression for sum of 3 bits int sum = (firstBit ^ secondBit ^ carry)+'0'; result = (char)sum + result; // boolean expression for 3-bit addition carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry); } // if overflow, then add a leading 1 if (carry) result = '1' + result; return result; } I am having difficulty in understanding following expressions // boolean expression for sum of 3 bits int sum = (firstBit ^ secondBit ^ carry)+'0'; and other expression // boolean expression for 3-bit addition carry = (firstBit&secondBit) | (secondBit&carry) | (firstBit&carry); What is difference between two? What are they trying to achieve? Thanks
To understand this, a table with all possible combinations may help. (For our luck, the number of combinations is very limited for bits.) Starting with AND (&), OR (|), XOR (^): a | b | a & b | a | b | a ^ b ---+---+-------+-------+------- 0 | 0 | 0 | 0 | 0 0 | 1 | 0 | 1 | 1 1 | 0 | 0 | 1 | 1 1 | 1 | 1 | 1 | 0 Putting it together: a | b | carry | a + b + carry | a ^ b ^ carry | a & b | b & carry | a & carry | a & b | a & carry | b & carry ---+---+-------+---------------+---------------+-------+-----------+-----------+------------------------------- 0 | 0 | 0 | 00 | 0 | 0 | 0 | 0 | 0 0 | 0 | 1 | 01 | 1 | 0 | 0 | 0 | 0 0 | 1 | 0 | 01 | 1 | 0 | 0 | 0 | 0 0 | 1 | 1 | 10 | 0 | 0 | 1 | 0 | 1 1 | 0 | 0 | 01 | 1 | 0 | 0 | 0 | 0 1 | 0 | 1 | 10 | 0 | 0 | 0 | 1 | 1 1 | 1 | 0 | 10 | 0 | 1 | 0 | 0 | 1 1 | 1 | 1 | 11 | 1 | 1 | 1 | 1 | 1 Please, note, how the last digit of a + b resembles exactly the result of a ^ b ^ carry as well as a & b | a & carry | b & carry resembles the first digit of a + b. The last detail is, adding '0' (ASCII code of digit 0) to the resp. result (0 or 1) translates this to the corresponding ASCII character ('0' or '1') again.