mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-06-14 22:27:45 +08:00
81
Backtracking/Graph Coloring.cpp
Normal file
81
Backtracking/Graph Coloring.cpp
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
// Number of vertices in the graph
|
||||||
|
#define V 4
|
||||||
|
|
||||||
|
void printSolution(int color[]);
|
||||||
|
|
||||||
|
/* A utility function to check if the current color assignment
|
||||||
|
is safe for vertex v */
|
||||||
|
bool isSafe (int v, bool graph[V][V], int color[], int c)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < V; i++)
|
||||||
|
if (graph[v][i] && c == color[i])
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A recursive utility function to solve m coloring problem */
|
||||||
|
void graphColoring(bool graph[V][V], int m, int color[], int v)
|
||||||
|
{
|
||||||
|
/* base case: If all vertices are assigned a color then
|
||||||
|
return true */
|
||||||
|
if (v == V){
|
||||||
|
printSolution(color);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Consider this vertex v and try different colors */
|
||||||
|
for (int c = 1; c <= m; c++)
|
||||||
|
{
|
||||||
|
/* Check if assignment of color c to v is fine*/
|
||||||
|
if (isSafe(v, graph, color, c))
|
||||||
|
{
|
||||||
|
color[v] = c;
|
||||||
|
|
||||||
|
/* recur to assign colors to rest of the vertices */
|
||||||
|
graphColoring (graph, m, color, v+1);
|
||||||
|
|
||||||
|
|
||||||
|
/* If assigning color c doesn't lead to a solution
|
||||||
|
then remove it */
|
||||||
|
color[v] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A utility function to print solution */
|
||||||
|
void printSolution(int color[])
|
||||||
|
{
|
||||||
|
printf(" Following are the assigned colors \n");
|
||||||
|
for (int i = 0; i < V; i++)
|
||||||
|
printf(" %d ", color[i]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// driver program to test above function
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
/* Create following graph and test whether it is 3 colorable
|
||||||
|
(3)---(2)
|
||||||
|
| / |
|
||||||
|
| / |
|
||||||
|
| / |
|
||||||
|
(0)---(1)
|
||||||
|
*/
|
||||||
|
bool graph[V][V] = {{0, 1, 1, 1},
|
||||||
|
{1, 0, 1, 0},
|
||||||
|
{1, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0},
|
||||||
|
};
|
||||||
|
int m = 3; // Number of colors
|
||||||
|
|
||||||
|
int color[V];
|
||||||
|
|
||||||
|
for (int i = 0; i < V; i++)
|
||||||
|
color[i] = 0;
|
||||||
|
|
||||||
|
graphColoring(graph, m, color, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
82
Backtracking/N Queens.cpp
Normal file
82
Backtracking/N Queens.cpp
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
#include<iostream>
|
||||||
|
#define N 4
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void printSolution(int board[N][N])
|
||||||
|
{
|
||||||
|
cout<<"\n";
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < N; j++)
|
||||||
|
cout<<""<<board[i][j];
|
||||||
|
cout<<"\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool isSafe(int board[N][N], int row, int col)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
/* Check this row on left side */
|
||||||
|
for (i = 0; i < col; i++)
|
||||||
|
if (board[row][i])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* Check upper diagonal on left side */
|
||||||
|
for (i=row, j=col; i>=0 && j>=0; i--, j--)
|
||||||
|
if (board[i][j])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* Check lower diagonal on left side */
|
||||||
|
for (i=row, j=col; j>=0 && i<N; i++, j--)
|
||||||
|
if (board[i][j])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void solveNQ(int board[N][N], int col)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (col >= N){
|
||||||
|
printSolution(board);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Consider this column and try placing
|
||||||
|
this queen in all rows one by one */
|
||||||
|
for (int i = 0; i < N; i++)
|
||||||
|
{
|
||||||
|
/* Check if queen can be placed on
|
||||||
|
board[i][col] */
|
||||||
|
if ( isSafe(board, i, col) )
|
||||||
|
{
|
||||||
|
/* Place this queen in board[i][col] */
|
||||||
|
// cout<<"\n"<<col<<"can place"<<i;
|
||||||
|
board[i][col] = 1;
|
||||||
|
|
||||||
|
/* recur to place rest of the queens */
|
||||||
|
solveNQ(board, col + 1);
|
||||||
|
|
||||||
|
|
||||||
|
board[i][col] = 0; // BACKTRACK
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
int board[N][N] = { {0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0}
|
||||||
|
};
|
||||||
|
|
||||||
|
solveNQ(board, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,8 +1,35 @@
|
|||||||
//0-1 Knapsack problem - Dynamic programming
|
//0-1 Knapsack problem - Dynamic programming
|
||||||
#include <bits/stdc++.h>
|
//#include <bits/stdc++.h>
|
||||||
|
#include<iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
//void Print(int res[20][20], int i, int j, int capacity)
|
||||||
|
//{
|
||||||
|
// if(i==0 || j==0)
|
||||||
|
// {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if(res[i-1][j]==res[i][j-1])
|
||||||
|
// {
|
||||||
|
// if(i<=capacity)
|
||||||
|
// {
|
||||||
|
// cout<<i<<" ";
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Print(res, i-1, j-1, capacity-i);
|
||||||
|
// }
|
||||||
|
// else if(res[i-1][j]>res[i][j-1])
|
||||||
|
// {
|
||||||
|
// Print(res, i-1,j, capacity);
|
||||||
|
// }
|
||||||
|
// else if(res[i][j-1]>res[i-1][j])
|
||||||
|
// {
|
||||||
|
// Print(res, i,j-1, capacity);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
int Knapsack(int capacity,int n,int weight[],int value[]){
|
int Knapsack(int capacity,int n,int weight[],int value[]){
|
||||||
int res[n+1][capacity+1];
|
int res[20][20];
|
||||||
for (int i = 0; i < n+1; ++i)
|
for (int i = 0; i < n+1; ++i)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < capacity+1; ++j)
|
for (int j = 0; j < capacity+1; ++j)
|
||||||
@@ -15,9 +42,11 @@ int Knapsack(int capacity,int n,int weight[],int value[]){
|
|||||||
res[i][j] = res[i-1][j];
|
res[i][j] = res[i-1][j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Print(res, n, capacity, capacity);
|
||||||
|
// cout<<"\n";
|
||||||
return res[n][capacity];
|
return res[n][capacity];
|
||||||
}
|
}
|
||||||
int main(int argc, char const *argv[])
|
int main()
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
cout<<"Enter number of items: ";
|
cout<<"Enter number of items: ";
|
||||||
@@ -38,4 +67,4 @@ int main(int argc, char const *argv[])
|
|||||||
cin>>capacity;
|
cin>>capacity;
|
||||||
cout<<Knapsack(capacity,n,weight,value);
|
cout<<Knapsack(capacity,n,weight,value);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,75 @@
|
|||||||
//Longest common subsequence - Dynamic Programming
|
//Longest common subsequence - Dynamic Programming
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
void Print(int trace[20][20], int m, int n, string a)
|
||||||
|
{
|
||||||
|
if (m==0 || n==0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (trace[m][n]==1)
|
||||||
|
{
|
||||||
|
Print(trace, m-1, n-1, a);
|
||||||
|
cout<<a[m-1];
|
||||||
|
}
|
||||||
|
else if(trace[m][n]==2)
|
||||||
|
{
|
||||||
|
Print(trace, m-1,n, a);
|
||||||
|
}
|
||||||
|
else if (trace[m][n]==3)
|
||||||
|
{
|
||||||
|
Print(trace, m,n-1, a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int lcs(string a, string b){
|
int lcs(string a, string b){
|
||||||
int m = a.length(), n = b.length();
|
int m = a.length(), n = b.length();
|
||||||
int res[m+1][n+1];
|
int res[m+1][n+1];
|
||||||
|
int trace[20][20];
|
||||||
for (int i = 0; i < m+1; ++i)
|
for (int i = 0; i < m+1; ++i)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < n+1; ++j)
|
for (int j = 0; j < n+1; ++j)
|
||||||
{
|
{
|
||||||
if(i==0||j==0)
|
if(i==0||j==0)
|
||||||
res[i][j] = 0;
|
{
|
||||||
|
res[i][j] = 0;
|
||||||
|
trace[i][j]=0;
|
||||||
|
}
|
||||||
|
|
||||||
else if(a[i-1]==b[j-1])
|
else if(a[i-1]==b[j-1])
|
||||||
|
{
|
||||||
res[i][j] = 1 + res[i-1][j-1];
|
res[i][j] = 1 + res[i-1][j-1];
|
||||||
|
trace[i][j]=1; // 1 means trace the matrix in upper left diagonal direction.
|
||||||
|
}
|
||||||
else
|
else
|
||||||
res[i][j] = max(res[i-1][j], res[i][j-1]);
|
{
|
||||||
|
if (res[i-1][j]>res[i][j-1])
|
||||||
|
{
|
||||||
|
res[i][j]=res[i-1][j];
|
||||||
|
trace[i][j]=2; // 2 means trace the matrix in upwards direction.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res[i][j]=res[i][j-1];
|
||||||
|
trace[i][j]=3; // means trace the matrix in left direction.
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Print(trace, m, n, a);
|
||||||
return res[m][n];
|
return res[m][n];
|
||||||
}
|
}
|
||||||
int main(int argc, char const *argv[])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
{
|
{
|
||||||
string a,b;
|
string a,b;
|
||||||
cin>>a>>b;
|
cin>>a>>b;
|
||||||
cout<<lcs(a,b);
|
cout<<lcs(a,b);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ struct Item
|
|||||||
|
|
||||||
float profitPerUnit(Item x)
|
float profitPerUnit(Item x)
|
||||||
{
|
{
|
||||||
// cout<<(float)x.profit/(float)x.weight<<"\n";
|
|
||||||
return (float)x.profit/(float)x.weight;
|
return (float)x.profit/(float)x.weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,13 +52,6 @@ void quickSort(Item arr[], int low, int high)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// void show(Item arr[], int size)
|
|
||||||
// {
|
|
||||||
// for (int i=0; i < size; i++)
|
|
||||||
// cout<<arr[i].weight<<"\t"<<arr[i].profit<<"\n";
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
cout<<"\nEnter the capacity of the knapsack : ";
|
cout<<"\nEnter the capacity of the knapsack : ";
|
||||||
|
|||||||
BIN
Greedy Algorithms/Knapsack.exe
Normal file
BIN
Greedy Algorithms/Knapsack.exe
Normal file
Binary file not shown.
60
Strassen Matrix Multiplication.cpp
Normal file
60
Strassen Matrix Multiplication.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
Multiply(int A[][], int B[][], int n)
|
||||||
|
{
|
||||||
|
if (n==2)
|
||||||
|
{
|
||||||
|
int p1= (a[0][0] + a[1][1])*(b[0][0]+b[1][1]);
|
||||||
|
int p2= (a[1][0]+a[1][1])*b[0][0];
|
||||||
|
int p3= a[0][0]*(b[0][1]-b[1][1]);
|
||||||
|
int p4= a[1][1]*(b[1][0]-b[0][0]);
|
||||||
|
int p5= (a[0][0]+a[0][1])*b[1][1];
|
||||||
|
int p6= (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
|
||||||
|
int p7= (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);
|
||||||
|
|
||||||
|
|
||||||
|
int c[n][n];
|
||||||
|
c[0][0]=p1+p4-p5+p7;
|
||||||
|
c[0][1]=p3+p5;
|
||||||
|
c[1][0]=p2+p4;
|
||||||
|
c[1][1]=p1-p2+p3+p6;
|
||||||
|
|
||||||
|
return c[][];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int p,q,r,s;
|
||||||
|
cout<<"Enter the dimensions of Matrices";
|
||||||
|
cin>>n;
|
||||||
|
int A[n][n],;
|
||||||
|
int B[n][n],;
|
||||||
|
cout<<"Enter the elements of Matrix A";
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j <n ; j++)
|
||||||
|
{
|
||||||
|
cin>>A[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
cout<<"Enter the elements of Matrix B";
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j <n ; j++)
|
||||||
|
{
|
||||||
|
cin>>B[i][j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Multiply(A, B, n);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user