change my recursive solution into a dynamic programming solution - c++

here I am trying to convert my recursive solution into a dynamic solution but I am getting problem in converting. I am trying to count minimum possible coin to make value. what I am doing is I am taking all possible coin and putting inside a vector and finally in main function I will find and minimum of my vector and that will be my answer
int rec(vector<int>coins,int n,int sum,int counter)
{
if(sum==0)
{
return 1;
}
if(n==0)
{
return 0;
}
int total=rec(coins,n-1,sum,counter);
if(sum-coins[n-1]>=0)
{
total+=rec(coins,n,sum-coins[n-1],counter+1);
if(sum-coins[n-1]==0)
{
vec.push_back(counter+1);
}
}
return total;
}

you should first try to solve this type of problems by yourself.
BTW:
#define BIG 2147483647
int min_coin(int *coins, int m, int desire_value)
{
int dp[desire_value+1];
dp[0] = 0;
for (int i=1; i<=desire_value; i++)
dp[i] = BIG;
for (int i=1; i<=desire_value; i++)
{
for (int j=0; j<m; j++)
if (coins[j] <= i)
{
int diff = dp[i-coins[j]];
if (diff != BIG && diff + 1 < dp[i])
dp[i] = diff + 1;
}
}
if(dp[desire_value]==BIG)
return -1;
return dp[desire_value];
}
you can easily convert dp and coins to vector. vector is like array.(beware for allocate vector for dp you should reserve space in vector see here.)

Related

Speeding up calculation using vectors in C++ by using pointers/references

Currently, I am making a C++ program that solves a sudoku. In order to do this, I calculate the "energy" of the sudoku (the number of faults) frequently. This calculation unfortunately takes up a lot of computation time. I think that it can be sped up significantly by using pointers and references in the calculation, but have trouble figuring out how to implement this.
In my solver class, I have a vector<vector<int> data-member called _sudoku, that contains the values of each site. Currently, when calculating the energy I call a lot of functions with pass-by-value. I tried adding a & in the arguments of the functions and a * when making the variables, but this did not work. How can I make this program run faster by using pass-by-reference?
Calculating the energy should not change the vector anyway so that would be better.
I used the CPU usage to track down 80% of the calculation time to the function where vectors are called.
int SudokuSolver::calculateEnergy() {
int energy = 243 - (rowUniques() + colUniques() + blockUniques());//count number as faults
return energy;
}
int SudokuSolver::colUniques() {
int count = 0;
for (int col = 0; col < _dim; col++) {
vector<int> colVec = _sudoku[col];
for (int i = 1; i <= _dim; i++) {
if (isUnique(colVec, i)) {
count++;
}
}
}
return count;
}
int SudokuSolver::rowUniques() {
int count = 0;
for (int row = 0; row < _dim; row++) {
vector<int> rowVec(_dim);
for (int i = 0; i < _dim; i++) {
rowVec[i] = _sudoku[i][row];
}
for (int i = 1; i <= _dim; i++) {
if (isUnique(rowVec, i)) {
count++;
}
}
}
return count;
}
int SudokuSolver::blockUniques() {
int count = 0;
for (int nBlock = 0; nBlock < _dim; nBlock++) {
vector<int> blockVec = blockMaker(nBlock);
for (int i = 1; i <= _dim; i++) {
if (isUnique(blockVec, i)) {
count++;
}
}
}
return count;
}
vector<int> SudokuSolver::blockMaker(int No) {
vector<int> block(_dim);
int xmin = 3 * (No % 3);
int ymin = 3 * (No / 3);
int col, row;
for (int i = 0; i < _dim; i++) {
col = xmin + (i % 3);
row = ymin + (i / 3);
block[i] = _sudoku[col][row];
}
return block;
}
bool SudokuSolver::isUnique(vector<int> v, int n) {
int count = 0;
for (int i = 0; i < _dim; i++) {
if (v[i] == n) {
count++;
}
}
if (count == 1) {
return true;
} else {
return false;
}
}
The specific lines that use a lot of computatation time are the ones like:
vector<int> colVec = _sudoku[col];
and every time isUnique() is called.
I expect that if I switch to using pass-by-reference, my code will speed up significantly. Could anyone help me in doing so, if that would indeed be the case?
Thanks in advance.
If you change your SudokuSolver::isUnique to take vector<int> &v, that is the only change you need to do pass-by-reference instead of pass-by-value. Passing with a pointer will be similar to passing by reference, with the difference that pointers could be re-assigned, or be NULL, while references can not.
I suspect you would see some performance increase if you are working on a sufficiently large-sized problem where you would be able to distinguish a large copy (if your problem is small, it will be difficult to see minor performance increases).
Hope this helps!
vector<int> colVec = _sudoku[col]; does copy/transfer all the elements, while const vector<int>& colVec = _sudoku[col]; would not (it only creates an alias for the right hand side).
Same with bool SudokuSolver::isUnique(vector<int> v, int n) { versus bool SudokuSolver::isUnique(const vector<int>& v, int n) {
Edited after Jesper Juhl's suggestion: The const addition makes sure that you don't change the reference contents by mistake.
Edit 2: Another thing to notice is that vector<int> rowVec(_dim); these vectors are continuously allocated and unallocated at each iteration, which might get costly. You could try something like
int SudokuSolver::rowUniques() {
int count = 0;
vector<int> rowVec(_maximumDim); // Specify maximum dimension
for (int row = 0; row < _dim; row++) {
for (int i = 0; i < _dim; i++) {
rowVec[i] = _sudoku[i][row];
}
for (int i = 1; i <= _dim; i++) {
if (isUnique(rowVec, i)) {
count++;
}
}
}
return count;
}
if that doesn't mess up with your implementation.

Finding number of prime numbers in an array

I'm trying to write a function that finds the number of prime numbers in an array.
int countPrimes(int a[], int size)
{
int numberPrime = 0;
int i = 0;
for (int j = 2; j < a[i]; j++)
{
if(a[i] % j == 0)
numbPrime++;
}
return numPrime;
}
I think what I'm missing is I have to redefine i after every iteration, but I'm not sure how.
You need 2 loops: 1 over the array, 1 checking all possible divisors. I'd suggest separating out the prime check into a function. Code:
bool primeCheck(int p) {
if (p<2) return false;
// Really slow way to check, but works
for(int d = 2; d<p; ++d) {
if (0==p%d) return false; // found a divisor
}
return true; // no divisors found
}
int countPrimes(const int *a, int size) {
int numberPrime = 0;
for (int i = 0; i < size; ++i) {
// For each element in the input array, check it,
// and increment the count if it is prime.
if(primeCheck(a[i]))
++numberPrime;
}
return numberPrime;
}
You can also use std::count_if like this:
std::count_if(std::begin(input), std::end(input), primeCheck)
See it live here.

The most occuring number in structure(array)

I cant find out whats wrong with this part of my program, i want to find out most occuring number in my structure(array), but it finds only the last number :/
void Daugiausiai(int n)
{
int max = 0;
int sk;
for(int i = 0; i < n; i++){
int kiek = 0;
for(int j=0; j < n; j++){
if(A[i].datamet == A[j].datamet){
kiek++;
if(kiek > max){
max = kiek;
sk = A[i].datamet;
}
}
}
}
}
ps. its only a part of my code
You haven't shown us enough of your code, but it is likely that you are not looking at the real result of your function. The result, sk is local to the function and you don't return it. If you have global variable that is also named sk, it will not be touched by Daugiausiai.
In the same way, you pass the number of elements in your struct array, but work on a global struct. It is good practice to "encapsulate" functions so that they receive the data they work on as arguments and return a result. Your function should therefore pass both array length and array and return the result.
(Such an encapsulation doesn't work in all cases, but here, it has the benefit that you can use the same function for many different arrays of the same structure tape.)
It is also enough to test whether the current number of elements is more than the maximum so far after your counting loop.
Putting all this together:
struct Data {
int datamet;
};
int Daugiausiai(const struct Data A[], int n)
{
int max = 0;
int sk;
for (int i = 0; i < n; i++){
int kiek = 0;
// Count occurrences
for(int j = 0; j < n; j++){
if(A[i].datamet == A[j].datamet) kiek++;
}
// Check for maximum
if (kiek > max) {
max = kiek;
sk = A[i].datamet;
}
}
return sk;
}
And you call it like this:
struct Data A[6] = {{1}, {2}, {1}, {4}, {1}, {2}};
int n = Daugiausiai(A, 6);
printf("%d\n", n); // 1
It would be nice if you had english variable names, so I could read them a bit better ^^. What should your paramter n do? Is that the array-length? And what should yout funtion do? It has no return value or something.
int getMostOccuring(int array[], int length)
{
int current_number;
int current_count = 0;
int most_occuring_number;
int most_occuring_count = 0;
for (int i = 0; i < length; i++)
{
current_number = array[i];
current_count = 0;
for (int j = i; j < length; j++)
{
int test_number = array[j];
if (test_number == current_number)
{
current_count ++;
if (current_count > most_occuring_count)
{
most_occuring_number = current_number;
most_occuring_count = current_count;
}
}
}
}
return most_occuring_number;
}
this should work and return the most occuring number in the given array (it has a bad runtime, but is very simple and good to understand).

Matrix determinant algorithm C++

I'm new to programming and I was looking for a way to find the determinant of a matrix. I found this code online, but I have trouble understanding the algorithm in place here. I have no problems for the base of the recursion , but the continue and main loop I have trouble understanding. Big thanks to anyone who can explain to me the algorithm.
int determ(int a[MAX][MAX],int n) {
int det=0, p, h, k, i, j, temp[MAX][MAX];
if(n==1) {
return a[0][0];
} else if(n==2) {
det=(a[0][0]*a[1][1]-a[0][1]*a[1][0]);
return det;
} else {
for(p=0;p<n;p++) {
h = 0;
k = 0;
for(i=1;i<n;i++) {
for( j=0;j<n;j++) {
if(j==p) {
continue;
}
temp[h][k] = a[i][j];
k++;
if(k==n-1) {
h++;
k = 0;
}
}
}
det=det+a[0][p]*pow(-1,p)*determ(temp,n-1);
}
return det;
}
}
This algorithm uses a divide-conquer approach for solving the problem (finding the determinant of an N*N Matrix).
The algorithm uses a recursive pattern which is one of divide and conquer approaches. You can find out this by noticing the algorithm is calling itself in the third condition statement.
Every recursive algorithm have an exit condition which is the first if-statement in your code. and they also contain a section which is the solution to the most convenient problem or an atomic problem of the main big problem which is hard to solve in the first place. The atomic problem or the most-divided problem can be solved easily as you can see the the second if-statement of your code. In your case it is actually solving the determinant of a 2*2 Matrix.
The most important part of your code to understand which is challenging a little bit too is the part you do the dividing (which is recursive too!).
This part has the key to conquering either. By doing a little back trace and numerical examples you can find it out:
det = det + a[0][p] * pow(-1,p) * determ(temp,n-1);
For the final suggestion try a 3*3 Matrix which only needs one dividing.
Good luck with that.
This book is a great one to start studying and understanding algorithms
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int **submatrix(int **matrix, unsigned int n, unsigned int x, unsigned int y) {
int **submatrix = new int *[n - 1];
int subi = 0;
for (int i = 0; i < n; i++) {
submatrix[subi] = new int[n - 1];
int subj = 0;
if (i == y) {
continue;
}
for (int j = 0; j < n; j++) {
if (j == x) {
continue;
}
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
return submatrix;
}
int determinant(int **matrix, unsigned int n) {
int det = 0;
if (n == 2) {
return matrix[0][0] * matrix[1][1] - matrix[1][0] * matrix[0][1];
}
for (int x = 0; x < n; ++x) {
det += ((x % 2 == 0 ? 1 : -1) * matrix[0][x] * determinant(submatrix(matrix, n, x, 0), n - 1));
}
return det;
}
int main() {
int n;
cin >> n;
int **matrix = new int *[n];
for (int i = 0; i < n; ++i) {
matrix[i] = new int[n];
for (int j = 0; j < n; ++j) {
cin >> matrix[i][j];
}
}
cout << determinant(matrix, n);
return 0;
}

How to use selection sort algorithm correctly to sort a list?

I cannot get this to work, seems like whatever I do it never sorts correctly.
I am trying to sort in a descending order based on number of points.
Bryan_Bickell 2 5 +2
Brandon_Bolig 0 3 0
Dave_Bolland 4 2 -1
Sheldon_Brookbank 0 4 -1
Daniel_Carcillo 0 1 +3
The middle column is the amount of points.
I am using 4 arrays to store all of those values, how would I correctly utilize the array selection sort to get it to order in the right way?
I had tried all the answers below but none of them seemed to work, this is what i have so far
void sortArrays( string playerNames[], int goals[], int assists[], int rating[], int numPlayers )
{
int temp, imin;
int points[numPlayers];
for(int j = 0; j < numPlayers; j++)
{
points[j] = goals[j] + assists[j];
}
imin = points[0];
for(int i = 0; i < numPlayers; i++)
{
if (points[i] < imin)
{
imin = points[i];
}
}
for(int j = 1; j < numPlayers; j++)
{
if (points[j] > imin)
{
temp = points[j];
points[j] = points[j-1];
points[j-1] = temp;
}
}
}
it should go like this...
void selsort(int *a,int size)
{
int i,j,imin,temp;
//cnt++;
for(j=0;j<size;j++)
{
//cnt+=2;
imin=j;
for(i=j+1;i<size;i++)
{
//cnt+=2;
if(a[i]<a[imin])
{
//cnt++;
imin=i;
}
}
if(imin!=j)
{
//cnt+=3;
temp=a[j];
a[j]=a[imin];
a[imin]=temp;
}
}
}
You don't need 4 arrays to store those records if only the middle column is used for sorting, i.e, keys used for sorting the records. From my understanding, you are trying to sort those records of people based on the number of points with selection sort. Code should look like the following: assuming records is your array of records
void selectionSort(RECORD records[], int n) {
int i, j, minIndex, tmp;
for (i = 0; i < n - 1; i++) {
maxIndex = i;
for (j = i + 1; j < n; j++) //find the current max
{
if (records[j].point > records[minIndex].point)
{
//assume point is the number of point, middle column
minIndex = j;
}
}
//put current max point record at correct position
if (minIndex != i) {
tmp = records[i];
records[i] = records[minIndex];
records[minIndex] = tmp;
}
}
}
It will sort all your records in "descending order" as you want
how about store the data into a std::vector then sort it
int compare(int a, int b){
return (a>b);
}
void sort(std::vector<int> &data){
std::sort(data.begin(), data.end(), compare);
}
try to use vector as much possible, they have been heavy optimized for performance and better memory usage