mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-03 10:35:34 +08:00
Major rework to improve code quality and add automation checks (#805)
* delete secant method - it is identical to regula falsi * document + improvize root finding algorithms * attempt to document gaussian elimination * added file brief * commented doxygen-mainpage, added files-list link * corrected files list link path * files-list link correction - this time works :) * document successive approximations * cleaner equation * updating DIRECTORY.md * documented kmp string search * document brute force string search * document rabin-karp string search * fixed mainpage readme * doxygen v1.8.18 will suppress out the #minipage in the markdown * cpplint correction for header guard style * github action to auto format source code per cpplint standard * updated setting to add 1 space before `private` and `public` keywords * auto rename files and auto format code * added missing "run" for step * corrected asignmemt operation * fixed trim and assign syntax * added git move for renaming bad filenames * added missing pipe for trim * added missing space * use old and new fnames * store old fname using echo * move files only if there is a change in filename * put old filenames in quotes * use double quote for old filename * escape double quotes * remove old_fname * try escape characters and echo" * add file-type to find * cleanup echo * ensure all trim variables are also in quotes * try escape -quote again * remove second escpe quote * use single quote for first check * use carets instead of quotes * put variables in brackets * remove -e from echo * add debug echos * try print0 flag * find command with while instead of for-loop * find command using IFS instead * 🎉 IFS fix worked - escaped quotes for git mv * protetc each word in git mv .. * filename exists in lower cases - renamed * 🎉 git push enabled * updating DIRECTORY.md * git pull & then push * formatting filenamesd7af6fdc8c* formatting source-code ford7af6fdc8c* remove allman break before braces * updating DIRECTORY.md * added missing comma lost in previous commit * orchestrate all workflows * fix yml indentation * force push format changes, add title to DIRECTORY.md * pull before proceeding * reorganize pull commands * use master branches for actions * rename .cc files to .cpp * added class destructor to clean up dynamic memory allocation * rename to awesome workflow * commented whole repo cpplint - added modified files lint check * removed need for cpplint * attempt to use actions/checkout@master * temporary: no dependency on cpplint * formatting filenames153fb7b8a5* formatting source-code for153fb7b8a5* updating DIRECTORY.md * fix diff filename * added comments to the code * added test case * formatting source-code fora850308fba* updating DIRECTORY.md * added machine learning folder * added adaline algorithm * updating DIRECTORY.md * fixed issue [LWG2192](https://cplusplus.github.io/LWG/issue2192) for std::abs on MacOS * add cmath for same bug: [LWG2192](https://cplusplus.github.io/LWG/issue2192) for std::abs on MacOS * formatting source-code forf8925e4822* use STL's inner_product * formatting source-code forf94a330594* added range comments * define activation function * use equal initial weights * change test2 function to predict * activation function not friend * previous commit correction * added option for predict function to return value before applying activation function as optional argument * added test case to classify points lying within a sphere * improve documentation for adaline * formatting source-code for15ec4c3aba* added cmake to geometry folder * added algorithm include for std::max * add namespace - machine_learning * add namespace - statistics * add namespace - sorting * added sorting algos to namespace sorting * added namespace string_search * formatting source-code forfd69530515* added documentation to string_search namespace * feat: Add BFS and DFS algorithms to check for cycle in a directed graph * Remove const references for input of simple types Reason: overhead on access * fix bad code sorry for force push * Use pointer instead of the non-const reference because apparently google says so. * Remove a useless and possibly bad Graph constuctor overload * Explicitely specify type of vector during graph instantiation * updating DIRECTORY.md * find openMP before adding subdirectories * added kohonen self organizing map * updating DIRECTORY.md * remove older files and folders from gh-pages before adding new files * remove chronos library due to inacceptability by cpplint * use c++ specific static_cast instead * initialize radom number generator * updated image links with those from CPP repository * rename computer.... folder to numerical methods * added durand kerner method for root computation for arbitrarily large polynomials * fixed additional comma * fix cpplint errors * updating DIRECTORY.md * convert to function module * update documentation * move openmp to main loop * added two test cases * use INT16_MAX * remove return statement from omp-for loop and use "break" * run tests when no input is provided and skip tests when input polynomial is provided * while loop cannot have break - replaced with continue and check is present in the main while condition * (1) break while loop (2) skip runs on break_loop instead of hard-break * add documentation images * use long double for errors and tolerance checks * make iterator variable i local to threads * add critical secions to omp threads * bugfix: move file writing outside of the parallel loop othersie, there is no gurantee of the order of roots written to file * rename folder to data_structures * updating DIRECTORY.md * fix ambiguous symbol `size` * add data_structures to cmake * docs: enable tree view, add timestamp in footer, try clang assistaed parsing * doxygen - open links in external window * remove invalid parameter from function docs * use HTML5 img tag to resize images * move file to proper folder * fix documentations and cpplint * formatting source-code foraacaf9828c* updating DIRECTORY.md * cpplint: add braces for multiple statement if * add explicit link to badges * remove duplicate line Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * remove namespace indentation * remove file associations in settings * add author name * enable cmake in subfolders of data_structures * create and link object file * cpp lint fixes and instantiate template classes * cpp lint fixes and instantiate template classes Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * cpplint - ignore `build/include` Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * disable redundant gcc compilation in cpplint workflow Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * template header files contain function codes as well and removed redundant subfolders Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * updating DIRECTORY.md * remove semicolons after functions in a class Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * cpplint header guard style Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * remove semilon Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * added LU decomposition algorithm Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * added QR decomposition algorithm Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * use QR decomposition to find eigen values Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * updating DIRECTORY.md * use std::rand for thread safety Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * move srand to main() Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * cpplint braces correction Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * updated eigen value documentation Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * fix matrix shift doc Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * rename CONTRIBUTION.md to CONTRIBUTING.md #836 * remove 'sort alphabetical order' check * added documentation check * remove extra paranthesis * added gitpod * added gitpod link from README * attempt to add vscode gitpod extensions * update gitpod extensions * add gitpod extensions cmake-tools and git-graph * remove gitpod init and add commands * use init to one time install doxygen, graphviz, cpplint * use gitpod dockerfile * add ninja build system to docker * remove configure task * add github prebuild specs to gitpod * disable gitpod addcommit * update documentation for kohonen_som * added ode solve using forward euler method * added mid-point euler ode solver * fixed itegration step equation * added semi-implicit euler ODE solver * updating DIRECTORY.md * fix cpplint issues - lines 117 and 124 * added documentation to ode group * corrected semi-implicit euler function * updated docs and test cases better structure * replace `free` with `delete` operator * formatting source-code forf55ab50cf2* updating DIRECTORY.md * main function must return * added machine learning group * added kohonen som topology algorithm * fix graph image path * updating DIRECTORY.md * fix braces * use snprintf instead of sprintf * use static_cast * hardcode character buffer size * fix machine learning groups in documentation * fix missing namespace function * replace kvedala fork references to TheAlgorithms * fix bug in counting_sort Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Anmol3299 <mittalanmol22@gmail.com>
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
//0-1 Knapsack problem - Dynamic programming
|
||||
//#include <bits/stdc++.h>
|
||||
#include <iostream>
|
||||
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 res[20][20];
|
||||
for (int i = 0; i < n + 1; ++i)
|
||||
{
|
||||
for (int j = 0; j < capacity + 1; ++j)
|
||||
{
|
||||
if (i == 0 || j == 0)
|
||||
res[i][j] = 0;
|
||||
else if (weight[i - 1] <= j)
|
||||
res[i][j] = max(value[i - 1] + res[i - 1][j - weight[i - 1]], res[i - 1][j]);
|
||||
else
|
||||
res[i][j] = res[i - 1][j];
|
||||
}
|
||||
}
|
||||
// Print(res, n, capacity, capacity);
|
||||
// cout<<"\n";
|
||||
return res[n][capacity];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cout << "Enter number of items: ";
|
||||
cin >> n;
|
||||
int weight[n], value[n];
|
||||
cout << "Enter weights: ";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin >> weight[i];
|
||||
}
|
||||
cout << "Enter values: ";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin >> value[i];
|
||||
}
|
||||
int capacity;
|
||||
cout << "Enter capacity: ";
|
||||
cin >> capacity;
|
||||
cout << Knapsack(capacity, n, weight, value);
|
||||
return 0;
|
||||
}
|
||||
66
dynamic_programming/0_1_knapsack.cpp
Normal file
66
dynamic_programming/0_1_knapsack.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
// 0-1 Knapsack problem - Dynamic programming
|
||||
//#include <bits/stdc++.h>
|
||||
#include <iostream>
|
||||
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 res[20][20];
|
||||
for (int i = 0; i < n + 1; ++i) {
|
||||
for (int j = 0; j < capacity + 1; ++j) {
|
||||
if (i == 0 || j == 0)
|
||||
res[i][j] = 0;
|
||||
else if (weight[i - 1] <= j)
|
||||
res[i][j] = max(value[i - 1] + res[i - 1][j - weight[i - 1]],
|
||||
res[i - 1][j]);
|
||||
else
|
||||
res[i][j] = res[i - 1][j];
|
||||
}
|
||||
}
|
||||
// Print(res, n, capacity, capacity);
|
||||
// cout<<"\n";
|
||||
return res[n][capacity];
|
||||
}
|
||||
int main() {
|
||||
int n;
|
||||
cout << "Enter number of items: ";
|
||||
cin >> n;
|
||||
int weight[n], value[n];
|
||||
cout << "Enter weights: ";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> weight[i];
|
||||
}
|
||||
cout << "Enter values: ";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> value[i];
|
||||
}
|
||||
int capacity;
|
||||
cout << "Enter capacity: ";
|
||||
cin >> capacity;
|
||||
cout << Knapsack(capacity, n, weight, value);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,128 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//Wrapper class for storing an edge
|
||||
class Edge
|
||||
{
|
||||
public:
|
||||
int src, dst, weight;
|
||||
};
|
||||
|
||||
//Wrapper class for storing a graph
|
||||
class Graph
|
||||
{
|
||||
public:
|
||||
int vertexNum, edgeNum;
|
||||
Edge *edges;
|
||||
|
||||
//Constructs a graph with V vertices and E edges
|
||||
Graph(int V, int E)
|
||||
{
|
||||
this->vertexNum = V;
|
||||
this->edgeNum = E;
|
||||
this->edges = (Edge *)malloc(E * sizeof(Edge));
|
||||
}
|
||||
|
||||
//Adds the given edge to the graph
|
||||
void addEdge(int src, int dst, int weight)
|
||||
{
|
||||
static int edgeInd = 0;
|
||||
if (edgeInd < this->edgeNum)
|
||||
{
|
||||
Edge newEdge;
|
||||
newEdge.src = src;
|
||||
newEdge.dst = dst;
|
||||
newEdge.weight = weight;
|
||||
this->edges[edgeInd++] = newEdge;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//Utility function to print distances
|
||||
void print(int dist[], int V)
|
||||
{
|
||||
cout << "\nVertex Distance" << endl;
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
if (dist[i] != INT_MAX)
|
||||
cout << i << "\t" << dist[i] << endl;
|
||||
else
|
||||
cout << i << "\tINF" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
//The main function that finds the shortest path from given source
|
||||
//to all other vertices using Bellman-Ford.It also detects negative
|
||||
//weight cycle
|
||||
void BellmanFord(Graph graph, int src)
|
||||
{
|
||||
int V = graph.vertexNum;
|
||||
int E = graph.edgeNum;
|
||||
int dist[V];
|
||||
|
||||
//Initialize distances array as INF for all except source
|
||||
//Intialize source as zero
|
||||
for (int i = 0; i < V; i++)
|
||||
dist[i] = INT_MAX;
|
||||
dist[src] = 0;
|
||||
|
||||
//Calculate shortest path distance from source to all edges
|
||||
//A path can contain maximum (|V|-1) edges
|
||||
for (int i = 0; i <= V - 1; i++)
|
||||
for (int j = 0; j < E; j++)
|
||||
{
|
||||
int u = graph.edges[j].src;
|
||||
int v = graph.edges[j].dst;
|
||||
int w = graph.edges[j].weight;
|
||||
|
||||
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
|
||||
dist[v] = dist[u] + w;
|
||||
}
|
||||
|
||||
//Iterate inner loop once more to check for negative cycle
|
||||
for (int j = 0; j < E; j++)
|
||||
{
|
||||
int u = graph.edges[j].src;
|
||||
int v = graph.edges[j].dst;
|
||||
int w = graph.edges[j].weight;
|
||||
|
||||
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
|
||||
{
|
||||
cout << "Graph contains negative weight cycle. Hence, shortest distance not guaranteed." << endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
print(dist, V);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//Driver Function
|
||||
int main()
|
||||
{
|
||||
int V, E, gsrc;
|
||||
int src, dst, weight;
|
||||
cout << "Enter number of vertices: ";
|
||||
cin >> V;
|
||||
cout << "Enter number of edges: ";
|
||||
cin >> E;
|
||||
Graph G(V, E);
|
||||
for (int i = 0; i < E; i++)
|
||||
{
|
||||
cout << "\nEdge " << i + 1 << "\nEnter source: ";
|
||||
cin >> src;
|
||||
cout << "Enter destination: ";
|
||||
cin >> dst;
|
||||
cout << "Enter weight: ";
|
||||
cin >> weight;
|
||||
G.addEdge(src, dst, weight);
|
||||
}
|
||||
cout << "\nEnter source: ";
|
||||
cin >> gsrc;
|
||||
BellmanFord(G, gsrc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
|
||||
// Function to find the Minimum number of coins required to get Sum S
|
||||
int findMinCoins(int arr[], int n, int N)
|
||||
{
|
||||
// dp[i] = no of coins required to get a total of i
|
||||
int dp[N + 1];
|
||||
|
||||
// 0 coins are needed for 0 sum
|
||||
|
||||
dp[0] = 0;
|
||||
|
||||
for (int i = 1; i <= N; i++)
|
||||
{
|
||||
// initialize minimum number of coins needed to infinity
|
||||
dp[i] = INT_MAX;
|
||||
int res = INT_MAX;
|
||||
|
||||
// do for each coin
|
||||
for (int c = 0; c < n; c++)
|
||||
{
|
||||
if (i - arr[c] >= 0) // check if coins doesn't become negative by including it
|
||||
res = dp[i - arr[c]];
|
||||
|
||||
// if total can be reached by including current coin c,
|
||||
// update minimum number of coins needed dp[i]
|
||||
if (res != INT_MAX)
|
||||
dp[i] = min(dp[i], res + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// The Minimum No of Coins Required for N = dp[N]
|
||||
return dp[N];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// No of Coins We Have
|
||||
int arr[] = {1, 2, 3, 4};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
// Total Change Required
|
||||
int N = 15;
|
||||
|
||||
cout << "Minimum Number of Coins Required " << findMinCoins(arr, n, N) << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
/*Given a rod of length n inches and an array of prices that
|
||||
contains prices of all pieces of size smaller than n. Determine
|
||||
the maximum value obtainable by cutting up the rod and selling
|
||||
the pieces.*/
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int cutrod(int p[], int n)
|
||||
{
|
||||
int r[n + 1];
|
||||
r[0] = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
int q = INT_MIN;
|
||||
for (int i = 0; i <= j; i++)
|
||||
{
|
||||
q = max(q, p[i] + r[j - i]);
|
||||
}
|
||||
r[j + 1] = q;
|
||||
}
|
||||
return r[n];
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int price[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
|
||||
cout << cutrod(price, 30);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int fib(int n)
|
||||
{
|
||||
int res[3];
|
||||
res[0] = 0;
|
||||
res[1] = 1;
|
||||
for (int i = 2; i <= n; i++)
|
||||
{
|
||||
res[2] = res[1] + res[0];
|
||||
res[0] = res[1];
|
||||
res[1] = res[2];
|
||||
}
|
||||
return res[1];
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int n;
|
||||
cout << "Enter n: ";
|
||||
cin >> n;
|
||||
cout << "Fibonacci number is ";
|
||||
cout << fib(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int arr[1000000];
|
||||
int fib(int n)
|
||||
{
|
||||
if (arr[n] == -1)
|
||||
{
|
||||
if (n <= 1)
|
||||
arr[n] = n;
|
||||
else
|
||||
arr[n] = fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
return arr[n];
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int n;
|
||||
cout << "Enter n: ";
|
||||
cin >> n;
|
||||
for (int i = 0; i < n + 1; ++i)
|
||||
{
|
||||
arr[i] = -1;
|
||||
}
|
||||
cout << "Fibonacci number is " << fib(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//Wrapper class for storing a graph
|
||||
class Graph
|
||||
{
|
||||
public:
|
||||
int vertexNum;
|
||||
int **edges;
|
||||
|
||||
//Constructs a graph with V vertices and E edges
|
||||
Graph(int V)
|
||||
{
|
||||
this->vertexNum = V;
|
||||
this->edges = (int **)malloc(V * sizeof(int *));
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
this->edges[i] = (int *)malloc(V * sizeof(int));
|
||||
for (int j = 0; j < V; j++)
|
||||
this->edges[i][j] = INT_MAX;
|
||||
this->edges[i][i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Adds the given edge to the graph
|
||||
void addEdge(int src, int dst, int weight)
|
||||
{
|
||||
this->edges[src][dst] = weight;
|
||||
}
|
||||
};
|
||||
|
||||
//Utility function to print distances
|
||||
void print(int dist[], int V)
|
||||
{
|
||||
cout << "\nThe Distance matrix for Floyd - Warshall" << endl;
|
||||
for (int i = 0; i < V; i++)
|
||||
{
|
||||
for (int j = 0; j < V; j++)
|
||||
{
|
||||
|
||||
if (dist[i * V + j] != INT_MAX)
|
||||
cout << dist[i * V + j] << "\t";
|
||||
else
|
||||
cout << "INF"
|
||||
<< "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
//The main function that finds the shortest path from a vertex
|
||||
//to all other vertices using Floyd-Warshall Algorithm.
|
||||
void FloydWarshall(Graph graph)
|
||||
{
|
||||
int V = graph.vertexNum;
|
||||
int dist[V][V];
|
||||
|
||||
//Initialise distance array
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++)
|
||||
dist[i][j] = graph.edges[i][j];
|
||||
|
||||
//Calculate distances
|
||||
for (int k = 0; k < V; k++)
|
||||
//Choose an intermediate vertex
|
||||
|
||||
for (int i = 0; i < V; i++)
|
||||
//Choose a source vertex for given intermediate
|
||||
|
||||
for (int j = 0; j < V; j++)
|
||||
//Choose a destination vertex for above source vertex
|
||||
|
||||
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX && dist[i][k] + dist[k][j] < dist[i][j])
|
||||
//If the distance through intermediate vertex is less than direct edge then update value in distance array
|
||||
dist[i][j] = dist[i][k] + dist[k][j];
|
||||
|
||||
//Convert 2d array to 1d array for print
|
||||
int dist1d[V * V];
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++)
|
||||
dist1d[i * V + j] = dist[i][j];
|
||||
|
||||
print(dist1d, V);
|
||||
}
|
||||
|
||||
//Driver Function
|
||||
int main()
|
||||
{
|
||||
int V, E;
|
||||
int src, dst, weight;
|
||||
cout << "Enter number of vertices: ";
|
||||
cin >> V;
|
||||
cout << "Enter number of edges: ";
|
||||
cin >> E;
|
||||
Graph G(V);
|
||||
for (int i = 0; i < E; i++)
|
||||
{
|
||||
cout << "\nEdge " << i + 1 << "\nEnter source: ";
|
||||
cin >> src;
|
||||
cout << "Enter destination: ";
|
||||
cin >> dst;
|
||||
cout << "Enter weight: ";
|
||||
cin >> weight;
|
||||
G.addEdge(src, dst, weight);
|
||||
}
|
||||
FloydWarshall(G);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
//Longest common subsequence - Dynamic Programming
|
||||
#include <iostream>
|
||||
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 m = a.length(), n = b.length();
|
||||
int res[m + 1][n + 1];
|
||||
int trace[20][20];
|
||||
|
||||
// fills up the arrays with zeros.
|
||||
for (int i = 0; i < m + 1; i++)
|
||||
{
|
||||
for (int j = 0; j < n + 1; j++)
|
||||
{
|
||||
res[i][j] = 0;
|
||||
trace[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m + 1; ++i)
|
||||
{
|
||||
for (int j = 0; j < n + 1; ++j)
|
||||
{
|
||||
if (i == 0 || j == 0)
|
||||
{
|
||||
res[i][j] = 0;
|
||||
trace[i][j] = 0;
|
||||
}
|
||||
|
||||
else if (a[i - 1] == b[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
|
||||
{
|
||||
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];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
cout << lcs(a, b);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
//Program to calculate length of longest increasing subsequence in an array
|
||||
// in O(n log n)
|
||||
// tested on : https://cses.fi/problemset/task/1145/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
int LIS(int arr[], int n)
|
||||
{
|
||||
set < int > active; // The current built LIS.
|
||||
active.insert(arr[0]);
|
||||
// Loop through every element.
|
||||
for (int i = 1; i < n; ++i)
|
||||
{
|
||||
auto get = active.lower_bound(arr[i]);
|
||||
if (get == active.end())
|
||||
{
|
||||
active.insert(arr[i]);
|
||||
} // current element is the greatest so LIS increases by 1.
|
||||
else
|
||||
{
|
||||
int val = * get; // we find the position where arr[i] will be in the LIS. If it is in the LIS already we do nothing
|
||||
if (val > arr[i])
|
||||
{
|
||||
// else we remove the bigger element and add a smaller element (which is arr[i]) and continue;
|
||||
active.erase(get);
|
||||
active.insert(arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return active.size(); // size of the LIS.
|
||||
}
|
||||
int main(int argc, char const * argv[])
|
||||
{
|
||||
int n;
|
||||
cout << "Enter size of array: ";
|
||||
cin >> n;
|
||||
int a[n];
|
||||
cout << "Enter array elements: ";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << LIS(a, n) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
//Program to calculate length of longest increasing subsequence in an array
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int LIS(int a[], int n)
|
||||
{
|
||||
int lis[n];
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
lis[i] = 1;
|
||||
}
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
for (int j = 0; j < i; ++j)
|
||||
{
|
||||
if (a[i] > a[j] && lis[i] < lis[j] + 1)
|
||||
lis[i] = lis[j] + 1;
|
||||
}
|
||||
}
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
res = max(res, lis[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int n;
|
||||
cout << "Enter size of array: ";
|
||||
cin >> n;
|
||||
int a[n];
|
||||
cout << "Enter array elements: ";
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << LIS(a, n) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
using namespace std;
|
||||
|
||||
#define MAX 10
|
||||
|
||||
// dp table to store the solution for already computed sub problems
|
||||
int dp[MAX][MAX];
|
||||
|
||||
// Function to find the most efficient way to multiply the given sequence of matrices
|
||||
int MatrixChainMultiplication(int dim[], int i, int j)
|
||||
{
|
||||
// base case: one matrix
|
||||
if (j <= i + 1)
|
||||
return 0;
|
||||
|
||||
// stores minimum number of scalar multiplications (i.e., cost)
|
||||
// needed to compute the matrix M[i+1]...M[j] = M[i..j]
|
||||
int min = INT_MAX;
|
||||
|
||||
// if dp[i][j] is not calculated (calculate it!!)
|
||||
|
||||
if (dp[i][j] == 0)
|
||||
{
|
||||
// take the minimum over each possible position at which the
|
||||
// sequence of matrices can be split
|
||||
|
||||
for (int k = i + 1; k <= j - 1; k++)
|
||||
{
|
||||
// recur for M[i+1]..M[k] to get a i x k matrix
|
||||
int cost = MatrixChainMultiplication(dim, i, k);
|
||||
|
||||
// recur for M[k+1]..M[j] to get a k x j matrix
|
||||
cost += MatrixChainMultiplication(dim, k, j);
|
||||
|
||||
// cost to multiply two (i x k) and (k x j) matrix
|
||||
cost += dim[i] * dim[k] * dim[j];
|
||||
|
||||
if (cost < min)
|
||||
min = cost; // store the minimum cost
|
||||
}
|
||||
dp[i][j] = min;
|
||||
}
|
||||
|
||||
// return min cost to multiply M[j+1]..M[j]
|
||||
return dp[i][j];
|
||||
}
|
||||
|
||||
// main function
|
||||
int main()
|
||||
{
|
||||
// Matrix i has Dimensions dim[i-1] & dim[i] for i=1..n
|
||||
// input is 10 x 30 matrix, 30 x 5 matrix, 5 x 60 matrix
|
||||
int dim[] = {10, 30, 5, 60};
|
||||
int n = sizeof(dim) / sizeof(dim[0]);
|
||||
|
||||
// Function Calling: MatrixChainMultiplications(dimensions_array, starting, ending);
|
||||
|
||||
cout << "Minimum cost is " << MatrixChainMultiplication(dim, 0, n - 1) << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,21 +1,21 @@
|
||||
// Program to check whether a number is an armstrong number or not
|
||||
#include <iostream>
|
||||
|
||||
using std::cout;
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
|
||||
int main() {
|
||||
int n, k, d, s = 0;
|
||||
cout << "Enter a number:";
|
||||
cin >> n;
|
||||
k = n;
|
||||
while (k != 0) {
|
||||
d = k % 10;
|
||||
s += d * d * d;
|
||||
k /= 10;
|
||||
}
|
||||
if (s == n)
|
||||
cout << n << "is an armstrong number";
|
||||
else
|
||||
cout << n << "is not an armstrong number";
|
||||
int n, k, d, s = 0;
|
||||
cout << "Enter a number:";
|
||||
cin >> n;
|
||||
k = n;
|
||||
while (k != 0) {
|
||||
d = k % 10;
|
||||
s += d * d * d;
|
||||
k /= 10;
|
||||
}
|
||||
if (s == n)
|
||||
cout << n << "is an armstrong number";
|
||||
else
|
||||
cout << n << "is not an armstrong number";
|
||||
}
|
||||
|
||||
116
dynamic_programming/bellman_ford.cpp
Normal file
116
dynamic_programming/bellman_ford.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <limits.h>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Wrapper class for storing an edge
|
||||
class Edge {
|
||||
public:
|
||||
int src, dst, weight;
|
||||
};
|
||||
|
||||
// Wrapper class for storing a graph
|
||||
class Graph {
|
||||
public:
|
||||
int vertexNum, edgeNum;
|
||||
Edge *edges;
|
||||
|
||||
// Constructs a graph with V vertices and E edges
|
||||
Graph(int V, int E) {
|
||||
this->vertexNum = V;
|
||||
this->edgeNum = E;
|
||||
this->edges = (Edge *)malloc(E * sizeof(Edge));
|
||||
}
|
||||
|
||||
// Adds the given edge to the graph
|
||||
void addEdge(int src, int dst, int weight) {
|
||||
static int edgeInd = 0;
|
||||
if (edgeInd < this->edgeNum) {
|
||||
Edge newEdge;
|
||||
newEdge.src = src;
|
||||
newEdge.dst = dst;
|
||||
newEdge.weight = weight;
|
||||
this->edges[edgeInd++] = newEdge;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Utility function to print distances
|
||||
void print(int dist[], int V) {
|
||||
cout << "\nVertex Distance" << endl;
|
||||
for (int i = 0; i < V; i++) {
|
||||
if (dist[i] != INT_MAX)
|
||||
cout << i << "\t" << dist[i] << endl;
|
||||
else
|
||||
cout << i << "\tINF" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// The main function that finds the shortest path from given source
|
||||
// to all other vertices using Bellman-Ford.It also detects negative
|
||||
// weight cycle
|
||||
void BellmanFord(Graph graph, int src) {
|
||||
int V = graph.vertexNum;
|
||||
int E = graph.edgeNum;
|
||||
int dist[V];
|
||||
|
||||
// Initialize distances array as INF for all except source
|
||||
// Intialize source as zero
|
||||
for (int i = 0; i < V; i++) dist[i] = INT_MAX;
|
||||
dist[src] = 0;
|
||||
|
||||
// Calculate shortest path distance from source to all edges
|
||||
// A path can contain maximum (|V|-1) edges
|
||||
for (int i = 0; i <= V - 1; i++)
|
||||
for (int j = 0; j < E; j++) {
|
||||
int u = graph.edges[j].src;
|
||||
int v = graph.edges[j].dst;
|
||||
int w = graph.edges[j].weight;
|
||||
|
||||
if (dist[u] != INT_MAX && dist[u] + w < dist[v])
|
||||
dist[v] = dist[u] + w;
|
||||
}
|
||||
|
||||
// Iterate inner loop once more to check for negative cycle
|
||||
for (int j = 0; j < E; j++) {
|
||||
int u = graph.edges[j].src;
|
||||
int v = graph.edges[j].dst;
|
||||
int w = graph.edges[j].weight;
|
||||
|
||||
if (dist[u] != INT_MAX && dist[u] + w < dist[v]) {
|
||||
cout << "Graph contains negative weight cycle. Hence, shortest "
|
||||
"distance not guaranteed."
|
||||
<< endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
print(dist, V);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Driver Function
|
||||
int main() {
|
||||
int V, E, gsrc;
|
||||
int src, dst, weight;
|
||||
cout << "Enter number of vertices: ";
|
||||
cin >> V;
|
||||
cout << "Enter number of edges: ";
|
||||
cin >> E;
|
||||
Graph G(V, E);
|
||||
for (int i = 0; i < E; i++) {
|
||||
cout << "\nEdge " << i + 1 << "\nEnter source: ";
|
||||
cin >> src;
|
||||
cout << "Enter destination: ";
|
||||
cin >> dst;
|
||||
cout << "Enter weight: ";
|
||||
cin >> weight;
|
||||
G.addEdge(src, dst, weight);
|
||||
}
|
||||
cout << "\nEnter source: ";
|
||||
cin >> gsrc;
|
||||
BellmanFord(G, gsrc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -9,10 +9,9 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int *cat; // global array to hold catalan numbers
|
||||
int *cat; // global array to hold catalan numbers
|
||||
|
||||
unsigned long int catalan_dp(int n)
|
||||
{
|
||||
unsigned long int catalan_dp(int n) {
|
||||
/** Using the tabulation technique in dynamic programming,
|
||||
this function computes the first `n+1` Catalan numbers
|
||||
|
||||
@@ -29,19 +28,17 @@ unsigned long int catalan_dp(int n)
|
||||
cat[0] = cat[1] = 1;
|
||||
|
||||
// Compute the remaining numbers from index 2 to index n, using tabulation
|
||||
for (int i = 2; i <= n; i++)
|
||||
{
|
||||
for (int i = 2; i <= n; i++) {
|
||||
cat[i] = 0;
|
||||
for (int j = 0; j < i; j++)
|
||||
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
|
||||
cat[i] += cat[j] * cat[i - j - 1]; // applying the definition here
|
||||
}
|
||||
|
||||
// Return the result
|
||||
return cat[n];
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
int n;
|
||||
cout << "Enter n: ";
|
||||
cin >> n;
|
||||
@@ -49,8 +46,7 @@ int main(int argc, char *argv[])
|
||||
cat = new int[n + 1];
|
||||
|
||||
cout << "Catalan numbers from 0 to " << n << " are:\n";
|
||||
for (int i = 0; i <= n; i++)
|
||||
{
|
||||
for (int i = 0; i <= n; i++) {
|
||||
cout << "catalan (" << i << ") = " << catalan_dp(i) << endl;
|
||||
// NOTE: Since `cat` is a global array, calling `catalan_dp`
|
||||
// repeatedly will not recompute the the values already computed
|
||||
48
dynamic_programming/coin_change.cpp
Normal file
48
dynamic_programming/coin_change.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// Function to find the Minimum number of coins required to get Sum S
|
||||
int findMinCoins(int arr[], int n, int N) {
|
||||
// dp[i] = no of coins required to get a total of i
|
||||
int dp[N + 1];
|
||||
|
||||
// 0 coins are needed for 0 sum
|
||||
|
||||
dp[0] = 0;
|
||||
|
||||
for (int i = 1; i <= N; i++) {
|
||||
// initialize minimum number of coins needed to infinity
|
||||
dp[i] = INT_MAX;
|
||||
int res = INT_MAX;
|
||||
|
||||
// do for each coin
|
||||
for (int c = 0; c < n; c++) {
|
||||
if (i - arr[c] >=
|
||||
0) // check if coins doesn't become negative by including it
|
||||
res = dp[i - arr[c]];
|
||||
|
||||
// if total can be reached by including current coin c,
|
||||
// update minimum number of coins needed dp[i]
|
||||
if (res != INT_MAX)
|
||||
dp[i] = min(dp[i], res + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// The Minimum No of Coins Required for N = dp[N]
|
||||
return dp[N];
|
||||
}
|
||||
|
||||
int main() {
|
||||
// No of Coins We Have
|
||||
int arr[] = {1, 2, 3, 4};
|
||||
int n = sizeof(arr) / sizeof(arr[0]);
|
||||
|
||||
// Total Change Required
|
||||
int N = 15;
|
||||
|
||||
cout << "Minimum Number of Coins Required " << findMinCoins(arr, n, N)
|
||||
<< "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
dynamic_programming/cut_rod.cpp
Normal file
25
dynamic_programming/cut_rod.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
/*Given a rod of length n inches and an array of prices that
|
||||
contains prices of all pieces of size smaller than n. Determine
|
||||
the maximum value obtainable by cutting up the rod and selling
|
||||
the pieces.*/
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int cutrod(int p[], int n) {
|
||||
int r[n + 1];
|
||||
r[0] = 0;
|
||||
for (int j = 0; j < n; j++) {
|
||||
int q = INT_MIN;
|
||||
for (int i = 0; i <= j; i++) {
|
||||
q = max(q, p[i] + r[j - i]);
|
||||
}
|
||||
r[j + 1] = q;
|
||||
}
|
||||
return r[n];
|
||||
}
|
||||
int main() {
|
||||
int price[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30, 31, 32, 33, 34, 35,
|
||||
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
|
||||
cout << cutrod(price, 30);
|
||||
return 0;
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
* a. Insert
|
||||
* b. Remove
|
||||
* c. Replace
|
||||
* All of the above operations are
|
||||
* All of the above operations are
|
||||
* of equal cost
|
||||
*/
|
||||
|
||||
@@ -15,31 +15,27 @@
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
int min(int x, int y, int z)
|
||||
{
|
||||
return min(min(x, y), z);
|
||||
}
|
||||
int min(int x, int y, int z) { return min(min(x, y), z); }
|
||||
|
||||
/* A Naive recursive C++ program to find
|
||||
* minimum number of operations to convert
|
||||
* str1 to str2.
|
||||
* O(3^m)
|
||||
*/
|
||||
int editDist(string str1, string str2, int m, int n)
|
||||
{
|
||||
int editDist(string str1, string str2, int m, int n) {
|
||||
if (m == 0)
|
||||
return n;
|
||||
if (n == 0)
|
||||
return m;
|
||||
|
||||
//If last characters are same then continue
|
||||
//for the rest of them.
|
||||
// If last characters are same then continue
|
||||
// for the rest of them.
|
||||
if (str1[m - 1] == str2[n - 1])
|
||||
return editDist(str1, str2, m - 1, n - 1);
|
||||
|
||||
//If last not same, then 3 possibilities
|
||||
//a.Insert b.Remove c. Replace
|
||||
//Get min of three and continue for rest.
|
||||
// If last not same, then 3 possibilities
|
||||
// a.Insert b.Remove c. Replace
|
||||
// Get min of three and continue for rest.
|
||||
return 1 + min(editDist(str1, str2, m, n - 1),
|
||||
editDist(str1, str2, m - 1, n),
|
||||
editDist(str1, str2, m - 1, n - 1));
|
||||
@@ -48,33 +44,29 @@ int editDist(string str1, string str2, int m, int n)
|
||||
/* A DP based program
|
||||
* O(m x n)
|
||||
*/
|
||||
int editDistDP(string str1, string str2, int m, int n)
|
||||
{
|
||||
|
||||
//Create Table for SubProblems
|
||||
int editDistDP(string str1, string str2, int m, int n) {
|
||||
// Create Table for SubProblems
|
||||
int dp[m + 1][n + 1];
|
||||
|
||||
//Fill d[][] in bottom up manner
|
||||
for (int i = 0; i <= m; i++)
|
||||
{
|
||||
for (int j = 0; j <= n; j++)
|
||||
{
|
||||
//If str1 empty. Then add all of str2
|
||||
// Fill d[][] in bottom up manner
|
||||
for (int i = 0; i <= m; i++) {
|
||||
for (int j = 0; j <= n; j++) {
|
||||
// If str1 empty. Then add all of str2
|
||||
if (i == 0)
|
||||
dp[i][j] = j;
|
||||
|
||||
//If str2 empty. Then add all of str1
|
||||
// If str2 empty. Then add all of str1
|
||||
else if (j == 0)
|
||||
dp[i][j] = i;
|
||||
|
||||
//If character same. Recur for remaining
|
||||
// If character same. Recur for remaining
|
||||
else if (str1[i - 1] == str2[j - 1])
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
|
||||
else
|
||||
dp[i][j] = 1 + min(dp[i][j - 1], //Insert
|
||||
dp[i - 1][j], //Remove
|
||||
dp[i - 1][j - 1] //Replace
|
||||
dp[i][j] = 1 + min(dp[i][j - 1], // Insert
|
||||
dp[i - 1][j], // Remove
|
||||
dp[i - 1][j - 1] // Replace
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -82,8 +74,7 @@ int editDistDP(string str1, string str2, int m, int n)
|
||||
return dp[m][n];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
string str1 = "sunday";
|
||||
string str2 = "saturday";
|
||||
|
||||
@@ -1,35 +1,29 @@
|
||||
/* Function to get minimun number of trials needed
|
||||
* in worst case with n eggs and k floors
|
||||
/* Function to get minimun number of trials needed
|
||||
* in worst case with n eggs and k floors
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int eggDrop(int n, int k)
|
||||
{
|
||||
int eggDrop(int n, int k) {
|
||||
int eggFloor[n + 1][k + 1];
|
||||
int result;
|
||||
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
eggFloor[i][1] = 1; //n eggs..1 Floor
|
||||
eggFloor[i][0] = 0; //n eggs..0 Floor
|
||||
for (int i = 1; i <= n; i++) {
|
||||
eggFloor[i][1] = 1; // n eggs..1 Floor
|
||||
eggFloor[i][0] = 0; // n eggs..0 Floor
|
||||
}
|
||||
|
||||
// Only one egg available
|
||||
for (int j = 1; j <= k; j++)
|
||||
{
|
||||
for (int j = 1; j <= k; j++) {
|
||||
eggFloor[1][j] = j;
|
||||
}
|
||||
|
||||
for (int i = 2; i <= n; i++)
|
||||
{
|
||||
for (int j = 2; j <= k; j++)
|
||||
{
|
||||
for (int i = 2; i <= n; i++) {
|
||||
for (int j = 2; j <= k; j++) {
|
||||
eggFloor[i][j] = INT_MAX;
|
||||
for (int x = 1; x <= j; x++)
|
||||
{
|
||||
for (int x = 1; x <= j; x++) {
|
||||
// 1+max(eggBreak[one less egg, lower floors],
|
||||
// eggDoesntBreak[same # of eggs, upper floors]);
|
||||
result = 1 + max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]);
|
||||
@@ -42,8 +36,7 @@ int eggDrop(int n, int k)
|
||||
return eggFloor[n][k];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n, k;
|
||||
cout << "Enter number of eggs and floors: ";
|
||||
cin >> n >> k;
|
||||
21
dynamic_programming/fibonacci_bottom_up.cpp
Normal file
21
dynamic_programming/fibonacci_bottom_up.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int fib(int n) {
|
||||
int res[3];
|
||||
res[0] = 0;
|
||||
res[1] = 1;
|
||||
for (int i = 2; i <= n; i++) {
|
||||
res[2] = res[1] + res[0];
|
||||
res[0] = res[1];
|
||||
res[1] = res[2];
|
||||
}
|
||||
return res[1];
|
||||
}
|
||||
int main(int argc, char const *argv[]) {
|
||||
int n;
|
||||
cout << "Enter n: ";
|
||||
cin >> n;
|
||||
cout << "Fibonacci number is ";
|
||||
cout << fib(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
22
dynamic_programming/fibonacci_top_down.cpp
Normal file
22
dynamic_programming/fibonacci_top_down.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int arr[1000000];
|
||||
int fib(int n) {
|
||||
if (arr[n] == -1) {
|
||||
if (n <= 1)
|
||||
arr[n] = n;
|
||||
else
|
||||
arr[n] = fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
return arr[n];
|
||||
}
|
||||
int main(int argc, char const *argv[]) {
|
||||
int n;
|
||||
cout << "Enter n: ";
|
||||
cin >> n;
|
||||
for (int i = 0; i < n + 1; ++i) {
|
||||
arr[i] = -1;
|
||||
}
|
||||
cout << "Fibonacci number is " << fib(n) << endl;
|
||||
return 0;
|
||||
}
|
||||
107
dynamic_programming/floyd_warshall.cpp
Normal file
107
dynamic_programming/floyd_warshall.cpp
Normal file
@@ -0,0 +1,107 @@
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
// Wrapper class for storing a graph
|
||||
class Graph {
|
||||
public:
|
||||
int vertexNum;
|
||||
int **edges;
|
||||
|
||||
// Constructs a graph with V vertices and E edges
|
||||
Graph(int V) {
|
||||
this->vertexNum = V;
|
||||
this->edges = new int *[V];
|
||||
for (int i = 0; i < V; i++) {
|
||||
this->edges[i] = new int[V];
|
||||
for (int j = 0; j < V; j++) this->edges[i][j] = INT_MAX;
|
||||
this->edges[i][i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
~Graph() {
|
||||
for (int i = 0; i < vertexNum; i++) delete[] edges[i];
|
||||
delete[] edges;
|
||||
}
|
||||
|
||||
// Adds the given edge to the graph
|
||||
void addEdge(int src, int dst, int weight) {
|
||||
this->edges[src][dst] = weight;
|
||||
}
|
||||
};
|
||||
|
||||
// Utility function to print distances
|
||||
void print(int dist[], int V) {
|
||||
cout << "\nThe Distance matrix for Floyd - Warshall" << endl;
|
||||
for (int i = 0; i < V; i++) {
|
||||
for (int j = 0; j < V; j++) {
|
||||
if (dist[i * V + j] != INT_MAX)
|
||||
cout << dist[i * V + j] << "\t";
|
||||
else
|
||||
cout << "INF"
|
||||
<< "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
// The main function that finds the shortest path from a vertex
|
||||
// to all other vertices using Floyd-Warshall Algorithm.
|
||||
void FloydWarshall(Graph graph) {
|
||||
int V = graph.vertexNum;
|
||||
int dist[V][V];
|
||||
|
||||
// Initialise distance array
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++) dist[i][j] = graph.edges[i][j];
|
||||
|
||||
// Calculate distances
|
||||
for (int k = 0; k < V; k++)
|
||||
// Choose an intermediate vertex
|
||||
|
||||
for (int i = 0; i < V; i++)
|
||||
// Choose a source vertex for given intermediate
|
||||
|
||||
for (int j = 0; j < V; j++)
|
||||
// Choose a destination vertex for above source vertex
|
||||
|
||||
if (dist[i][k] != INT_MAX && dist[k][j] != INT_MAX &&
|
||||
dist[i][k] + dist[k][j] < dist[i][j])
|
||||
// If the distance through intermediate vertex is less than
|
||||
// direct edge then update value in distance array
|
||||
dist[i][j] = dist[i][k] + dist[k][j];
|
||||
|
||||
// Convert 2d array to 1d array for print
|
||||
int dist1d[V * V];
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int j = 0; j < V; j++) dist1d[i * V + j] = dist[i][j];
|
||||
|
||||
print(dist1d, V);
|
||||
}
|
||||
|
||||
// Driver Function
|
||||
int main() {
|
||||
int V, E;
|
||||
int src, dst, weight;
|
||||
cout << "Enter number of vertices: ";
|
||||
cin >> V;
|
||||
cout << "Enter number of edges: ";
|
||||
cin >> E;
|
||||
Graph G(V);
|
||||
for (int i = 0; i < E; i++) {
|
||||
cout << "\nEdge " << i + 1 << "\nEnter source: ";
|
||||
cin >> src;
|
||||
cout << "Enter destination: ";
|
||||
cin >> dst;
|
||||
cout << "Enter weight: ";
|
||||
cin >> weight;
|
||||
G.addEdge(src, dst, weight);
|
||||
}
|
||||
FloydWarshall(G);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
#include<iostream>
|
||||
#include<climits>
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
|
||||
int maxSubArraySum(int a[], int size) {
|
||||
int max_so_far = INT_MIN, max_ending_here = 0;
|
||||
@@ -15,7 +15,6 @@ int maxSubArraySum(int a[], int size) {
|
||||
return max_so_far;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int n, i;
|
||||
std::cout << "Enter the number of elements \n";
|
||||
|
||||
@@ -1,65 +1,53 @@
|
||||
#include <iosrteam>
|
||||
using namespace std;
|
||||
|
||||
int max(int a,int b)
|
||||
{
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
int max(int a, int b) { return (a > b) ? a : b; }
|
||||
|
||||
int main()
|
||||
{
|
||||
char str1[]="DEFBCD";
|
||||
char str2[]="ABDEFJ";
|
||||
int i,j,k;
|
||||
int n=strlen(str1)+1;
|
||||
int m=strlen(str2)+1;
|
||||
//cout<<n<<" "<<m<<"\n";
|
||||
int main() {
|
||||
char str1[] = "DEFBCD";
|
||||
char str2[] = "ABDEFJ";
|
||||
int i, j, k;
|
||||
int n = strlen(str1) + 1;
|
||||
int m = strlen(str2) + 1;
|
||||
// cout<<n<<" "<<m<<"\n";
|
||||
int a[m][n];
|
||||
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
{
|
||||
if(i==0 || j==0)
|
||||
a[i][j]=0;
|
||||
for (i = 0; i < m; i++) {
|
||||
for (j = 0; j < n; j++) {
|
||||
if (i == 0 || j == 0)
|
||||
a[i][j] = 0;
|
||||
|
||||
else if(str1[i-1] == str2[j-1])
|
||||
a[i][j]=a[i-1][j-1]+1;
|
||||
else if (str1[i - 1] == str2[j - 1])
|
||||
a[i][j] = a[i - 1][j - 1] + 1;
|
||||
|
||||
else
|
||||
a[i][j]=0;
|
||||
a[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
cout<<a[i][j]<<" ";
|
||||
cout<<"\n";
|
||||
}*/
|
||||
|
||||
|
||||
int ma=-1;
|
||||
int indi,indj;
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=0;j<n;j++)
|
||||
/*for(i=0;i<m;i++)
|
||||
{
|
||||
if(a[i][j]>ma)
|
||||
{
|
||||
ma=a[i][j];
|
||||
indi=i;
|
||||
indj=j;
|
||||
for(j=0;j<n;j++)
|
||||
cout<<a[i][j]<<" ";
|
||||
cout<<"\n";
|
||||
}*/
|
||||
|
||||
int ma = -1;
|
||||
int indi, indj;
|
||||
for (i = 0; i < m; i++) {
|
||||
for (j = 0; j < n; j++) {
|
||||
if (a[i][j] > ma) {
|
||||
ma = a[i][j];
|
||||
indi = i;
|
||||
indj = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout<<str1<<"\n";
|
||||
cout<<str2<<"\n";
|
||||
cout << str1 << "\n";
|
||||
cout << str2 << "\n";
|
||||
|
||||
cout<<"longest string size = "<<ma/*<<" "<<indi<<" "<<indj*/<<"\n";
|
||||
for(i=indi-3;i<indi;i++)
|
||||
cout<<str1[i];
|
||||
cout<<"\n";
|
||||
cout << "longest string size = " << ma /*<<" "<<indi<<" "<<indj*/ << "\n";
|
||||
for (i = indi - 3; i < indi; i++) cout << str1[i];
|
||||
cout << "\n";
|
||||
}
|
||||
|
||||
65
dynamic_programming/longest_common_subsequence.cpp
Normal file
65
dynamic_programming/longest_common_subsequence.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
// Longest common subsequence - Dynamic Programming
|
||||
#include <iostream>
|
||||
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 m = a.length(), n = b.length();
|
||||
int res[m + 1][n + 1];
|
||||
int trace[20][20];
|
||||
|
||||
// fills up the arrays with zeros.
|
||||
for (int i = 0; i < m + 1; i++) {
|
||||
for (int j = 0; j < n + 1; j++) {
|
||||
res[i][j] = 0;
|
||||
trace[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < m + 1; ++i) {
|
||||
for (int j = 0; j < n + 1; ++j) {
|
||||
if (i == 0 || j == 0) {
|
||||
res[i][j] = 0;
|
||||
trace[i][j] = 0;
|
||||
}
|
||||
|
||||
else if (a[i - 1] == b[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 {
|
||||
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];
|
||||
}
|
||||
|
||||
int main() {
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
cout << lcs(a, b);
|
||||
return 0;
|
||||
}
|
||||
32
dynamic_programming/longest_increasing_subsequence.cpp
Normal file
32
dynamic_programming/longest_increasing_subsequence.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Program to calculate length of longest increasing subsequence in an array
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int LIS(int a[], int n) {
|
||||
int lis[n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
lis[i] = 1;
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < i; ++j) {
|
||||
if (a[i] > a[j] && lis[i] < lis[j] + 1)
|
||||
lis[i] = lis[j] + 1;
|
||||
}
|
||||
}
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
res = max(res, lis[i]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
int main(int argc, char const *argv[]) {
|
||||
int n;
|
||||
cout << "Enter size of array: ";
|
||||
cin >> n;
|
||||
int a[n];
|
||||
cout << "Enter array elements: ";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << LIS(a, n) << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Program to calculate length of longest increasing subsequence in an array
|
||||
// in O(n log n)
|
||||
// tested on : https://cses.fi/problemset/task/1145/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
int LIS(int arr[], int n) {
|
||||
set<int> active; // The current built LIS.
|
||||
active.insert(arr[0]);
|
||||
// Loop through every element.
|
||||
for (int i = 1; i < n; ++i) {
|
||||
auto get = active.lower_bound(arr[i]);
|
||||
if (get == active.end()) {
|
||||
active.insert(arr[i]);
|
||||
} // current element is the greatest so LIS increases by 1.
|
||||
else {
|
||||
int val = *get; // we find the position where arr[i] will be in the
|
||||
// LIS. If it is in the LIS already we do nothing
|
||||
if (val > arr[i]) {
|
||||
// else we remove the bigger element and add a smaller element
|
||||
// (which is arr[i]) and continue;
|
||||
active.erase(get);
|
||||
active.insert(arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return active.size(); // size of the LIS.
|
||||
}
|
||||
int main(int argc, char const* argv[]) {
|
||||
int n;
|
||||
cout << "Enter size of array: ";
|
||||
cin >> n;
|
||||
int a[n];
|
||||
cout << "Enter array elements: ";
|
||||
for (int i = 0; i < n; ++i) {
|
||||
cin >> a[i];
|
||||
}
|
||||
cout << LIS(a, n) << endl;
|
||||
return 0;
|
||||
}
|
||||
61
dynamic_programming/matrix_chain_multiplication.cpp
Normal file
61
dynamic_programming/matrix_chain_multiplication.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <climits>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
#define MAX 10
|
||||
|
||||
// dp table to store the solution for already computed sub problems
|
||||
int dp[MAX][MAX];
|
||||
|
||||
// Function to find the most efficient way to multiply the given sequence of
|
||||
// matrices
|
||||
int MatrixChainMultiplication(int dim[], int i, int j) {
|
||||
// base case: one matrix
|
||||
if (j <= i + 1)
|
||||
return 0;
|
||||
|
||||
// stores minimum number of scalar multiplications (i.e., cost)
|
||||
// needed to compute the matrix M[i+1]...M[j] = M[i..j]
|
||||
int min = INT_MAX;
|
||||
|
||||
// if dp[i][j] is not calculated (calculate it!!)
|
||||
|
||||
if (dp[i][j] == 0) {
|
||||
// take the minimum over each possible position at which the
|
||||
// sequence of matrices can be split
|
||||
|
||||
for (int k = i + 1; k <= j - 1; k++) {
|
||||
// recur for M[i+1]..M[k] to get a i x k matrix
|
||||
int cost = MatrixChainMultiplication(dim, i, k);
|
||||
|
||||
// recur for M[k+1]..M[j] to get a k x j matrix
|
||||
cost += MatrixChainMultiplication(dim, k, j);
|
||||
|
||||
// cost to multiply two (i x k) and (k x j) matrix
|
||||
cost += dim[i] * dim[k] * dim[j];
|
||||
|
||||
if (cost < min)
|
||||
min = cost; // store the minimum cost
|
||||
}
|
||||
dp[i][j] = min;
|
||||
}
|
||||
|
||||
// return min cost to multiply M[j+1]..M[j]
|
||||
return dp[i][j];
|
||||
}
|
||||
|
||||
// main function
|
||||
int main() {
|
||||
// Matrix i has Dimensions dim[i-1] & dim[i] for i=1..n
|
||||
// input is 10 x 30 matrix, 30 x 5 matrix, 5 x 60 matrix
|
||||
int dim[] = {10, 30, 5, 60};
|
||||
int n = sizeof(dim) / sizeof(dim[0]);
|
||||
|
||||
// Function Calling: MatrixChainMultiplications(dimensions_array, starting,
|
||||
// ending);
|
||||
|
||||
cout << "Minimum cost is " << MatrixChainMultiplication(dim, 0, n - 1)
|
||||
<< "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,36 +1,36 @@
|
||||
/*
|
||||
*this program is use to find any elemet in any row with variable array size
|
||||
*aplication of pointer is use in it
|
||||
*important point start from here to:
|
||||
*the index value of array can be go to 1 to 100000
|
||||
*check till array[1000]
|
||||
*end here
|
||||
*how to work example:
|
||||
**Question:
|
||||
***number of array 2
|
||||
***quarry 3
|
||||
***array 1 is {1 2 3 4 5}
|
||||
***array 2 is {6 7}
|
||||
****i) what is 2nd element in 1st array
|
||||
****ii) what is 1st element in 2nd array
|
||||
****iii) what is 5th element in 1st array
|
||||
*****output:
|
||||
*****Enter Number of array you want to Store : 2
|
||||
*****Enter Number of Question or Quary you want to do Related to Array : 3
|
||||
*****Enter number of element in 1 rows : 5
|
||||
*****Enter the element of Array 1 2 3 4 5
|
||||
*****Enter number of element in 2 rows : 2
|
||||
*****Enter the element of Array 6 7
|
||||
*****enter the number of row which element You want to find : 1
|
||||
*****enter the position of element which You want to find : 2
|
||||
*****The element is 2
|
||||
*****enter the number of row which element You want to find : 2
|
||||
*****enter the position of element which You want to find : 1
|
||||
*****The element is 6
|
||||
*****enter the number of row which element You want to find : 1
|
||||
*****enter the position of element which You want to find : 5
|
||||
*****The element is 5
|
||||
*/
|
||||
*this program is use to find any elemet in any row with variable array size
|
||||
*aplication of pointer is use in it
|
||||
*important point start from here to:
|
||||
*the index value of array can be go to 1 to 100000
|
||||
*check till array[1000]
|
||||
*end here
|
||||
*how to work example:
|
||||
**Question:
|
||||
***number of array 2
|
||||
***quarry 3
|
||||
***array 1 is {1 2 3 4 5}
|
||||
***array 2 is {6 7}
|
||||
****i) what is 2nd element in 1st array
|
||||
****ii) what is 1st element in 2nd array
|
||||
****iii) what is 5th element in 1st array
|
||||
*****output:
|
||||
*****Enter Number of array you want to Store : 2
|
||||
*****Enter Number of Question or Quary you want to do Related to Array : 3
|
||||
*****Enter number of element in 1 rows : 5
|
||||
*****Enter the element of Array 1 2 3 4 5
|
||||
*****Enter number of element in 2 rows : 2
|
||||
*****Enter the element of Array 6 7
|
||||
*****enter the number of row which element You want to find : 1
|
||||
*****enter the position of element which You want to find : 2
|
||||
*****The element is 2
|
||||
*****enter the number of row which element You want to find : 2
|
||||
*****enter the position of element which You want to find : 1
|
||||
*****The element is 6
|
||||
*****enter the number of row which element You want to find : 1
|
||||
*****enter the position of element which You want to find : 5
|
||||
*****The element is 5
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
// this is main fuction
|
||||
@@ -46,7 +46,7 @@ int main() {
|
||||
// create a Array in run time because use can
|
||||
// change the size of each array which he/she is going to store
|
||||
// create a 2D array
|
||||
int** ar = new int* [x]();
|
||||
int** ar = new int*[x]();
|
||||
// this for loop is use for entering different variable size array
|
||||
// ***
|
||||
for (r = 0; r < x; r++) {
|
||||
@@ -75,6 +75,6 @@ int main() {
|
||||
std::cin >> q1;
|
||||
q1 = q1 - 1;
|
||||
// use this to find desire position of element in desire array
|
||||
std::cout <<"The element is "<< ar[r1][q1] <<std::endl;
|
||||
std::cout << "The element is " << ar[r1][q1] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,18 +8,18 @@
|
||||
* 1 2
|
||||
* 1 3
|
||||
* 2 4
|
||||
* which can be represented as
|
||||
* 1
|
||||
* / \
|
||||
* 2 3
|
||||
* |
|
||||
* 4
|
||||
*
|
||||
* which can be represented as
|
||||
* 1
|
||||
* / \
|
||||
* 2 3
|
||||
* |
|
||||
* 4
|
||||
*
|
||||
* Height of the tree : - 2
|
||||
*/
|
||||
*/
|
||||
|
||||
#include<iostream>
|
||||
#include<vector>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
// global declarations
|
||||
// no of nodes max limit.
|
||||
@@ -37,7 +37,7 @@ void depth_first_search(int u) {
|
||||
depth_first_search(v);
|
||||
|
||||
// select maximum sub-tree height from all children.
|
||||
child_height = std::max(child_height, dp[v]+1);
|
||||
child_height = std::max(child_height, dp[v] + 1);
|
||||
}
|
||||
}
|
||||
// assigned the max child height to current visited node.
|
||||
@@ -61,9 +61,9 @@ int main() {
|
||||
adj[v].push_back(u);
|
||||
}
|
||||
// initialize all nodes as unvisited.
|
||||
visited.assign(number_of_nodes+1, false);
|
||||
visited.assign(number_of_nodes + 1, false);
|
||||
// initialize depth of all nodes to 0.
|
||||
dp.assign(number_of_nodes+1, 0);
|
||||
dp.assign(number_of_nodes + 1, 0);
|
||||
// function call which will initialize the height of all nodes.
|
||||
depth_first_search(1);
|
||||
std::cout << "Height of the Tree : " << dp[1] << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user