From 55952288747dbf6a1d7b148af7815f7d0b93ce79 Mon Sep 17 00:00:00 2001 From: Ankit Bhadage Date: Sun, 13 Oct 2019 15:59:15 +0530 Subject: [PATCH 1/2] new DP --- Contributors.md | 1 + Dynamic Programming/lis.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 Dynamic Programming/lis.cpp diff --git a/Contributors.md b/Contributors.md index bb585a2..fa5b727 100644 --- a/Contributors.md +++ b/Contributors.md @@ -38,3 +38,4 @@ * [Muskan](https://github.com/Muskan-goyal6) * [Tarannum](https://github.com/giTan7) * [HCamberos](https://github.com/HCamberos) +* [Ankit Bhadage](https://github.com/Ankitmb125) diff --git a/Dynamic Programming/lis.cpp b/Dynamic Programming/lis.cpp new file mode 100644 index 0000000..1e484a5 --- /dev/null +++ b/Dynamic Programming/lis.cpp @@ -0,0 +1,31 @@ +/* +The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}. +*/ + +#include +using namespace std; + +int lis( int arr[], int n ) +{ + int lis[n]; + + lis[0] = 1; + + for (int i = 1; i < n; i++ ) + { + lis[i] = 1; + for (int j = 0; j < i; j++ ) + if ( arr[i] > arr[j] && lis[i] < lis[j] + 1) + lis[i] = lis[j] + 1; + } + + return *max_element(lis, lis+n); +} + +int main() +{ + int arr[] = { 10, 22, 9, 33, 21, 50, 41, 60 }; + int n = sizeof(arr)/sizeof(arr[0]); + printf("Length of lis is %d\n", lis( arr, n ) ); + return 0; +} From db87b16004408f09a8ba3e11f153a1ae70018567 Mon Sep 17 00:00:00 2001 From: Ankit Bhadage Date: Sun, 13 Oct 2019 16:07:52 +0530 Subject: [PATCH 2/2] new DP-floydWarshall --- Dynamic Programming/floydWarshall.cpp | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Dynamic Programming/floydWarshall.cpp diff --git a/Dynamic Programming/floydWarshall.cpp b/Dynamic Programming/floydWarshall.cpp new file mode 100644 index 0000000..dbe917b --- /dev/null +++ b/Dynamic Programming/floydWarshall.cpp @@ -0,0 +1,61 @@ +/*The Floyd Warshall Algorithm is for solving the All Pairs Shortest Path problem. The problem is to find shortest distances between every pair of vertices in a given edge weighted directed Graph.*/ + +#include +using namespace std; + +#define V 4 + +#define INF 99999 + +void printSolution(int dist[][V]); + +void floydWarshall (int graph[][V]) +{ + int dist[V][V], i, j, k; + + for (i = 0; i < V; i++) + for (j = 0; j < V; j++) + dist[i][j] = graph[i][j]; + for (k = 0; k < V; k++) + { + for (i = 0; i < V; i++) + { + for (j = 0; j < V; j++) + { + if (dist[i][k] + dist[k][j] < dist[i][j]) + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + + printSolution(dist); +} + +void printSolution(int dist[][V]) +{ + cout<<"The following matrix shows the shortest distances" + " between every pair of vertices \n"; + for (int i = 0; i < V; i++) + { + for (int j = 0; j < V; j++) + { + if (dist[i][j] == INF) + cout<<"INF"<<" "; + else + cout<