Skip to content

Commit

Permalink
Merge pull request #13 from NicolaBernini/combinatorics_20190422_1032_1
Browse files Browse the repository at this point in the history
Combinatorics 20190422 1032 1
  • Loading branch information
NicolaBernini authored Apr 22, 2019
2 parents ea3c177 + 81ef5ad commit ee23ec8
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
67 changes: 67 additions & 0 deletions combinatorics/generation/permutations/permutations1.cpp
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;
}



31 changes: 31 additions & 0 deletions combinatorics/generation/permutations/readme.md
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

11 changes: 11 additions & 0 deletions combinatorics/generation/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Overview

The Generation related problems and solutions





Work in progress

24 changes: 24 additions & 0 deletions combinatorics/readme.md
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

0 comments on commit ee23ec8

Please sign in to comment.