Last active
April 25, 2017 00:11
-
-
Save aryan-gupta/9413ddb04aae157d86cb6feeeec9ed70 to your computer and use it in GitHub Desktop.
This algorithm creates combinations of different lengths using a tree.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
using namespace std; | |
typedef vector<int> IV; | |
IV master; // master vector of ints (or whatever) | |
void func(int start, int depth, const int &fDepth, IV vec, vector<IV> &pos) { | |
vec.push_back(master[start]); // store the current node in vector | |
if(depth == fDepth) { // if we are at the depth we need to go to then ... | |
pos.push_back(vec); // ... add the accumulated nodes ... | |
return; // ... and exit | |
} | |
for(int i = start + 1; i < master.size(); ++i) // for each node below us ... | |
func(i, depth + 1, fDepth, vec, pos); // ... spawn a func | |
// if we reached an end node then exit | |
} | |
int main() { | |
master = {1, 2, 3, 4, 5}; | |
vector<IV> pos; // stores all the combinations of master vector | |
IV vec; // stores the accumulated nodes | |
for(int maxDepth = 0; maxDepth < master.size(); ++maxDepth) | |
for(int start = 0; start < master.size(); ++start) | |
func(start, 0, maxDepth, vec, pos); | |
// output all the combinations of master | |
for(IV& tmpIV : pos) { | |
for(int tmpInt : tmpIV) | |
cout << tmpInt << " "; | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Example: for a master list of
It would create the following combinations: