From d4fe4890981edcb10b6785115a49a60c9a5c84f9 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari <65031177+ShubhGurukul@users.noreply.github.com> Date: Wed, 13 Oct 2021 00:16:05 +0530 Subject: [PATCH 1/4] Add files via upload --- Activity Selection Problem.cpp | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Activity Selection Problem.cpp diff --git a/Activity Selection Problem.cpp b/Activity Selection Problem.cpp new file mode 100644 index 0000000..fd0afb0 --- /dev/null +++ b/Activity Selection Problem.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +void maxActivities(int s[], int f[], int n) +{ + int i, j; + + cout <<"Following activities are selected "<< endl; + + // The first activity always gets selected + i = 0; + cout <<" "<< i; + + // Consider rest of the activities + for (j = 1; j < n; j++) + { + // If this activity has start time greater than or + // equal to the finish time of previously selected + // activity, then select it + if (s[j] >= f[i]) + { + cout <<" " << j; + i = j; + } + } +} +int main() +{ + int n; + cout<<"Enter the number of Activity: "; + cin>>n; + cout<<"Enter the starting time and Finish time : "; + int s[n], f[n]; + for(int i=0; i>s[i]>>f[i]; + } + maxActivities(s,f,n); + + return 0; +} From 6ff3c8fa8cc660fbbf864053e30033377b1d87fb Mon Sep 17 00:00:00 2001 From: Shubham Tiwari <65031177+ShubhGurukul@users.noreply.github.com> Date: Wed, 13 Oct 2021 00:19:03 +0530 Subject: [PATCH 2/4] Create 8_Puzzle_Problem_BFS.cpp --- 8_Puzzle_Problem_BFS.cpp | 196 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 8_Puzzle_Problem_BFS.cpp diff --git a/8_Puzzle_Problem_BFS.cpp b/8_Puzzle_Problem_BFS.cpp new file mode 100644 index 0000000..346baf6 --- /dev/null +++ b/8_Puzzle_Problem_BFS.cpp @@ -0,0 +1,196 @@ +// Program to print path from root node to destination node +// for N*N -1 puzzle algorithm using Branch and Bound +// The solution assumes that instance of puzzle is solvable +#include +using namespace std; +#define N 3 + +// state space tree nodes +struct Node +{ + // stores the parent node of the current node + // helps in tracing path when the answer is found + Node* parent; + + // stores matrix + int mat[N][N]; + + // stores blank tile coordinates + int x, y; + + // stores the number of misplaced tiles + int cost; + + // stores the number of moves so far + int level; +}; + +// Function to print N x N matrix +int printMatrix(int mat[N][N]) +{ + for (int i = 0; i < N; i++) + { + for (int j = 0; j < N; j++) + printf("%d ", mat[i][j]); + printf("\n"); + } + return 1; +} + +// Function to allocate a new node +Node* newNode(int mat[N][N], int x, int y, int newX, + int newY, int level, Node* parent) +{ + Node* node = new Node; + + // set pointer for path to root + node->parent = parent; + + // copy data from parent node to current node + memcpy(node->mat, mat, sizeof node->mat); + + // move tile by 1 position + swap(node->mat[x][y], node->mat[newX][newY]); + + // set number of misplaced tiles + node->cost = INT_MAX; + + // set number of moves so far + node->level = level; + + // update new blank tile cordinates + node->x = newX; + node->y = newY; + + return node; +} + +// bottom, left, top, right +int row[] = { 1, 0, -1, 0 }; +int col[] = { 0, -1, 0, 1 }; + +// Function to calculate the number of misplaced tiles +// ie. number of non-blank tiles not in their goal position +int calculateCost(int initial[N][N], int final[N][N]) +{ + int count = 0; + for (int i = 0; i < N; i++) + for (int j = 0; j < N; j++) + if (initial[i][j] && initial[i][j] != final[i][j]) + count++; + return count; +} + +// Function to check if (x, y) is a valid matrix cordinate +int isSafe(int x, int y) +{ + return (x >= 0 && x < N && y >= 0 && y < N); +} + +// print path from root node to destination node +void printPath(Node* root) +{ + if (root == NULL) + return; + printPath(root->parent); + printMatrix(root->mat); + + printf("\n"); +} + +// Comparison object to be used to order the heap +struct comp +{ + bool operator()(const Node* lhs, const Node* rhs) const + { + return (lhs->cost + lhs->level) > (rhs->cost + rhs->level); + } +}; + +// Function to solve N*N - 1 puzzle algorithm using +// Branch and Bound. x and y are blank tile coordinates +// in initial state +void solve(int initial[N][N], int x, int y, + int final[N][N]) +{ + // Create a priority queue to store live nodes of + // search tree; + priority_queue, comp> pq; + + // create a root node and calculate its cost + Node* root = newNode(initial, x, y, x, y, 0, NULL); + root->cost = calculateCost(initial, final); + + // Add root to list of live nodes; + pq.push(root); + + // Finds a live node with least cost, + // add its childrens to list of live nodes and + // finally deletes it from the list. + while (!pq.empty()) + { + // Find a live node with least estimated cost + Node* min = pq.top(); + + // The found node is deleted from the list of + // live nodes + pq.pop(); + + // if min is an answer node + if (min->cost == 0) + { + // print the path from root to destination; + printPath(min); + return; + } + + // do for each child of min + // max 4 children for a node + for (int i = 0; i < 4; i++) + { + if (isSafe(min->x + row[i], min->y + col[i])) + { + // create a child node and calculate + // its cost + Node* child = newNode(min->mat, min->x, + min->y, min->x + row[i], + min->y + col[i], + min->level + 1, min); + child->cost = calculateCost(child->mat, final); + + // Add child to list of live nodes + pq.push(child); + } + } + } +} + +// Driver code +int main() +{ + // Initial configuration + // Value 0 is used for empty space + int initial[N][N] = + { + {1, 2, 3}, + {8, 0, 4}, + {7, 6, 5} + }; + + // Solvable Final configuration + // Value 0 is used for empty space + int final[N][N] = + { + {2, 8, 1}, + {0, 4, 2}, + {7, 6, 5} + }; + + // Blank tile coordinates in initial + // configuration + int x = 1, y = 1; + + solve(initial, x, y, final); + + return 0; +} From 471135ee0be2ad15b3c8346e92666b4da479d218 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari <65031177+ShubhGurukul@users.noreply.github.com> Date: Wed, 13 Oct 2021 00:20:15 +0530 Subject: [PATCH 3/4] Delete 8_Puzzle_Problem_BFS.cpp --- 8_Puzzle_Problem_BFS.cpp | 196 --------------------------------------- 1 file changed, 196 deletions(-) delete mode 100644 8_Puzzle_Problem_BFS.cpp diff --git a/8_Puzzle_Problem_BFS.cpp b/8_Puzzle_Problem_BFS.cpp deleted file mode 100644 index 346baf6..0000000 --- a/8_Puzzle_Problem_BFS.cpp +++ /dev/null @@ -1,196 +0,0 @@ -// Program to print path from root node to destination node -// for N*N -1 puzzle algorithm using Branch and Bound -// The solution assumes that instance of puzzle is solvable -#include -using namespace std; -#define N 3 - -// state space tree nodes -struct Node -{ - // stores the parent node of the current node - // helps in tracing path when the answer is found - Node* parent; - - // stores matrix - int mat[N][N]; - - // stores blank tile coordinates - int x, y; - - // stores the number of misplaced tiles - int cost; - - // stores the number of moves so far - int level; -}; - -// Function to print N x N matrix -int printMatrix(int mat[N][N]) -{ - for (int i = 0; i < N; i++) - { - for (int j = 0; j < N; j++) - printf("%d ", mat[i][j]); - printf("\n"); - } - return 1; -} - -// Function to allocate a new node -Node* newNode(int mat[N][N], int x, int y, int newX, - int newY, int level, Node* parent) -{ - Node* node = new Node; - - // set pointer for path to root - node->parent = parent; - - // copy data from parent node to current node - memcpy(node->mat, mat, sizeof node->mat); - - // move tile by 1 position - swap(node->mat[x][y], node->mat[newX][newY]); - - // set number of misplaced tiles - node->cost = INT_MAX; - - // set number of moves so far - node->level = level; - - // update new blank tile cordinates - node->x = newX; - node->y = newY; - - return node; -} - -// bottom, left, top, right -int row[] = { 1, 0, -1, 0 }; -int col[] = { 0, -1, 0, 1 }; - -// Function to calculate the number of misplaced tiles -// ie. number of non-blank tiles not in their goal position -int calculateCost(int initial[N][N], int final[N][N]) -{ - int count = 0; - for (int i = 0; i < N; i++) - for (int j = 0; j < N; j++) - if (initial[i][j] && initial[i][j] != final[i][j]) - count++; - return count; -} - -// Function to check if (x, y) is a valid matrix cordinate -int isSafe(int x, int y) -{ - return (x >= 0 && x < N && y >= 0 && y < N); -} - -// print path from root node to destination node -void printPath(Node* root) -{ - if (root == NULL) - return; - printPath(root->parent); - printMatrix(root->mat); - - printf("\n"); -} - -// Comparison object to be used to order the heap -struct comp -{ - bool operator()(const Node* lhs, const Node* rhs) const - { - return (lhs->cost + lhs->level) > (rhs->cost + rhs->level); - } -}; - -// Function to solve N*N - 1 puzzle algorithm using -// Branch and Bound. x and y are blank tile coordinates -// in initial state -void solve(int initial[N][N], int x, int y, - int final[N][N]) -{ - // Create a priority queue to store live nodes of - // search tree; - priority_queue, comp> pq; - - // create a root node and calculate its cost - Node* root = newNode(initial, x, y, x, y, 0, NULL); - root->cost = calculateCost(initial, final); - - // Add root to list of live nodes; - pq.push(root); - - // Finds a live node with least cost, - // add its childrens to list of live nodes and - // finally deletes it from the list. - while (!pq.empty()) - { - // Find a live node with least estimated cost - Node* min = pq.top(); - - // The found node is deleted from the list of - // live nodes - pq.pop(); - - // if min is an answer node - if (min->cost == 0) - { - // print the path from root to destination; - printPath(min); - return; - } - - // do for each child of min - // max 4 children for a node - for (int i = 0; i < 4; i++) - { - if (isSafe(min->x + row[i], min->y + col[i])) - { - // create a child node and calculate - // its cost - Node* child = newNode(min->mat, min->x, - min->y, min->x + row[i], - min->y + col[i], - min->level + 1, min); - child->cost = calculateCost(child->mat, final); - - // Add child to list of live nodes - pq.push(child); - } - } - } -} - -// Driver code -int main() -{ - // Initial configuration - // Value 0 is used for empty space - int initial[N][N] = - { - {1, 2, 3}, - {8, 0, 4}, - {7, 6, 5} - }; - - // Solvable Final configuration - // Value 0 is used for empty space - int final[N][N] = - { - {2, 8, 1}, - {0, 4, 2}, - {7, 6, 5} - }; - - // Blank tile coordinates in initial - // configuration - int x = 1, y = 1; - - solve(initial, x, y, final); - - return 0; -} From b5e52a51a5fdbf71509666d3763fb03d819caf8f Mon Sep 17 00:00:00 2001 From: Shubham Tiwari <65031177+ShubhGurukul@users.noreply.github.com> Date: Wed, 13 Oct 2021 00:22:21 +0530 Subject: [PATCH 4/4] Create DP_MatrixChainMultiplication.cpp --- DP_MatrixChainMultiplication.cpp | 66 ++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 DP_MatrixChainMultiplication.cpp diff --git a/DP_MatrixChainMultiplication.cpp b/DP_MatrixChainMultiplication.cpp new file mode 100644 index 0000000..2f3dac3 --- /dev/null +++ b/DP_MatrixChainMultiplication.cpp @@ -0,0 +1,66 @@ +#include +#define inf 10000 +using namespace std; + +void print(int *a, int n ) +{ + for (int i=0; i>n; + int N = n+1; + int p[N]; + cout<<"Enter the array of MCM : "; + for(int i=0; i>p[i]; + + cout<<"Matrices different sizes are \n "; + for(int i=0; i