-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from NicolaBernini/combinatorics_20190422_1032_1
Combinatorics 20190422 1032 1
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
#include <vector> | ||
using namespace std; | ||
|
||
// remove_item :: [Int] -> Int -> [Int] | ||
vector<int> remove_item (const vector<int>& a, const unsigned int id) | ||
{ | ||
vector<int> res; | ||
for(unsigned int i=0; i<a.size(); ++i) if(i!=id) res.push_back(a[i]); | ||
return res; | ||
} | ||
|
||
|
||
// add_item :: [Int] -> Int -> [Int] | ||
vector<int> add_item(const vector<int>& a, const int b) | ||
{ | ||
vector<int> res=a; | ||
res.push_back(b); | ||
return res; | ||
} | ||
|
||
vector< vector<int> > merge(const vector< vector<int> >& a, const vector< vector<int> >& b) | ||
{ | ||
vector< vector<int> > res=a; | ||
for(const auto& e : b) res.push_back(e); | ||
return res; | ||
} | ||
|
||
|
||
// perm_comp :: [Int] -> [Int] -> [[Int]] | ||
// Rel 1:N : at each layer, choose N parallel paths | ||
// Design | ||
// Complexity needs to be O(N!) = O(N*(N-1)...) so Recursive | ||
// State / Args | ||
// - Partial Items List | ||
// - Remaining Items List | ||
vector< vector<int> > perm_comp(const vector<int>& a, const vector<int>& b) | ||
{ | ||
|
||
if(b.size()==0) return { a }; | ||
|
||
vector< vector<int> > res; | ||
for(unsigned int i=0; i<b.size(); ++i) res=merge(res, perm_comp(add_item(a, b[i]), remove_item(b,i))); | ||
return res; | ||
} | ||
|
||
|
||
vector< vector<int> > permutations(const vector<int>& v) | ||
{ | ||
return perm_comp({}, v); | ||
} | ||
|
||
int main() { | ||
// your code goes here | ||
|
||
auto res = permutations({1,2,3,4,5}); | ||
cout << "Sol Num = " << res.size() << endl; | ||
for(const auto& a : res) | ||
{ | ||
for(const auto& b : a) cout << to_string(b) << " "; | ||
cout << endl; | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
# Overview | ||
|
||
Some Permutations Computation related material | ||
|
||
# Basic Permutation | ||
|
||
## Problem Statement | ||
|
||
> Given a vector of int, compute all the possible vectors resulting from the permutation of the elements | ||
## Solutions | ||
|
||
Analysis | ||
|
||
- Equivalent to drawing elements from a container | ||
- Recursive + Iterative | ||
- Recursion i --> Recursion i+1 = Remove an element | ||
- Iterations inside a Recursion = Run one recursion for each of the remaining elements | ||
|
||
|
||
Implementations | ||
|
||
- [CPP](permutations1.cpp) | ||
|
||
|
||
|
||
|
||
|
||
Work in progress | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
# Overview | ||
|
||
The Generation related problems and solutions | ||
|
||
|
||
|
||
|
||
|
||
Work in progress | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
# Overview | ||
|
||
This section is related to Combinatorics Problems in Discrete Math hence including the following categories | ||
|
||
- Generation | ||
- Examples | ||
- Compute all the possible strings resulting from the permutation of a given set of chars | ||
- Compute all the possible substrings for a given string | ||
- Compute all the possible submatrixes for a given matrix | ||
|
||
- Enumaration / Counting | ||
- Examples | ||
- Count all the possible paths connecting 2 given points on a given graph | ||
- Count the number of prime numbers in a given interval | ||
|
||
|
||
- Search | ||
- Examples | ||
- Search for a given element in a sorted array | ||
- Search a pair of numbers in a given array which sum up to a certain given value | ||
|
||
|
||
Work in progress |