From a1cd111acbab6637ba93b0f9e69cab186b278fb3 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Tue, 28 Dec 2021 00:54:12 +0530 Subject: [PATCH 1/5] Implemented Warshall Algorithm In C++ --- c++/graph/warshall.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 c++/graph/warshall.cpp diff --git a/c++/graph/warshall.cpp b/c++/graph/warshall.cpp new file mode 100644 index 0000000..20cc61d --- /dev/null +++ b/c++/graph/warshall.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +using namespace std; +#define N 4 +#define M INT_MAX +void printpath(int path[][N], int v, int u) +{ + if (path[v][u] == v) + return; + + printpath(path, v, path[v][u]); + cout << path[v][u] << " "; +} +void solution(int cost[N][N], int path[N][N]) +{ + for (int v = 0; v < N; v++) + { + for (int u = 0; u < N; u++) + { + if (u != v && path[v][u] != -1) + { + cout << "Shortest Path from " << v << " -> " << u << " is (" + << v << " "; + printpath(path, v, u); + cout << u << ")" << endl; + } + } + } +} + +void WarshallAlgo(int adjMatrix[][N]) +{ + int cost[N][N], path[N][N]; + for (int v = 0; v < N; v++) + { + for (int u = 0; u < N; u++) + { + cost[v][u] = adjMatrix[v][u]; + + if (v == u) + path[v][u] = 0; + else if (cost[v][u] != INT_MAX) + path[v][u] = v; + else + path[v][u] = -1; + } + } + + for (int k = 0; k < N; k++) + { + for (int v = 0; v < N; v++) + { + for (int u = 0; u < N; u++) + { + if (cost[v][k] != INT_MAX && cost[k][u] != INT_MAX && cost[v][k] + cost[k][u] < cost[v][u]) + { + cost[v][u] = cost[v][k] + cost[k][u]; + path[v][u] = path[k][u]; + } + } + + if (cost[v][v] < 0) + { + cout << "Negative Weight Cycle Found!!"; + return; + } + } + } + solution(cost, path); +} + +int main() +{ + int adjMatrix[N][N] = + { + {0, M, -2, M}, + {4, 0, 3, M}, + {M, M, 0, 2}, + {M, -1, M, 0}}; + WarshallAlgo(adjMatrix); + + return 0; +} \ No newline at end of file From 1e49747e3a757c9ba7f8d0b7d5596bc0734d9994 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 02:17:58 +0530 Subject: [PATCH 2/5] Segment Tree implementation in different languages Fixes #29 --- java/tree/segmentree.java | 147 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 java/tree/segmentree.java diff --git a/java/tree/segmentree.java b/java/tree/segmentree.java new file mode 100644 index 0000000..c995751 --- /dev/null +++ b/java/tree/segmentree.java @@ -0,0 +1,147 @@ +class segmentree { + final int MAX = 1000; // Max tree size + int tree[] = new int[MAX]; // stors segment tree + int lazy[] = new int[MAX]; // stores pending updates + + void updateRangeUtil(int si, int ss, int se, int us, int ue, int diff) { + + if (lazy[si] != 0) { + // Makes pdates using value stored in lazy nodes + tree[si] += (se - ss + 1) * lazy[si]; + + if (ss != se) { + + lazy[si * 2 + 1] += lazy[si]; + lazy[si * 2 + 2] += lazy[si]; + } + + // Assigned the lazy value for current node as 0 as it has been updated + lazy[si] = 0; + } + + // out of range + if (ss > se || ss > ue || se < us) + return; + + // Current segment is fully in range + if (ss >= us && se <= ue) { + // Add the difference to current node + tree[si] += (se - ss + 1) * diff; + + if (ss != se) { + + lazy[si * 2 + 1] += diff; + lazy[si * 2 + 2] += diff; + } + return; + } + + int mid = (ss + se) / 2; + updateRangeUtil(si * 2 + 1, ss, mid, us, ue, diff); + updateRangeUtil(si * 2 + 2, mid + 1, se, us, ue, diff); + + tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2]; + } + + // Function to update a range of values in segment tree + + void updateRange(int n, int us, int ue, int diff) { + updateRangeUtil(0, 0, n - 1, us, ue, diff); + } + + int getSumUtil(int ss, int se, int qs, int qe, int si) { + + if (lazy[si] != 0) { + + tree[si] += (se - ss + 1) * lazy[si]; + + if (ss != se) { + + lazy[si * 2 + 1] += lazy[si]; + lazy[si * 2 + 2] += lazy[si]; + } + + lazy[si] = 0; + } + + // Out of range + if (ss > se || ss > qe || se < qs) + return 0; + + if (ss >= qs && se <= qe) + return tree[si]; + + int mid = (ss + se) / 2; + return getSumUtil(ss, mid, qs, qe, 2 * si + 1) + + getSumUtil(mid + 1, se, qs, qe, 2 * si + 2); + } + + // Returns the sum of elements in range from index qs (query start) to qe (query + // end). + int getSum(int n, int qs, int qe) { + + if (qs < 0 || qe > n - 1 || qs > qe) { + System.out.println("Invalid Input"); + return -1; + } + + return getSumUtil(0, n - 1, qs, qe, 0); + } + + /* + * A recursive function that constructs Segment Tree for + * array[ss..se]. si is index of current node in segment + * tree st. + */ + void constructSTUtil(int arr[], int ss, int se, int si) { + + if (ss > se) + return; + + if (ss == se) { + tree[si] = arr[ss]; + return; + } + + int mid = (ss + se) / 2; + constructSTUtil(arr, ss, mid, si * 2 + 1); + constructSTUtil(arr, mid + 1, se, si * 2 + 2); + + tree[si] = tree[si * 2 + 1] + tree[si * 2 + 2]; + } + + /* + * Function to construct segment tree from given array. + * This function allocates memory for segment tree and + * calls constructSTUtil() to fill the allocated memory + */ + void constructST(int arr[], int n) { + + constructSTUtil(arr, 0, n - 1, 0); + } + + // Driver program to test above functions + public static void main(String args[]) { + int arr[] = { 1, 3, 5, 7, 9, 11 }; + int n = arr.length; + segmentree tree = new segmentree(); + + // Build segment tree from given array + tree.constructST(arr, n); + + // Print sum of values in array from index 1 to 4 + System.out.println("Sum of values in given range = " + tree.getSum(n, 1, 4)); + + // Add 10 to all nodes at indexes from 1 to 6. + tree.updateRange(n, 1, 6, 10); + + // Find sum after the value is updated + System.out.println("Updated sum of values in given range = " + tree.getSum(n, 1, 4)); + } +} +/* + * output:- + * Sum of values in given range = 24 + * Updated sum of values in given range = 64 + * Time complexity= O(log(n)) + */ \ No newline at end of file From 91a92f982299cbd0a867429d3b75a2cb0cdd4353 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 02:18:22 +0530 Subject: [PATCH 3/5] New Commit --- c++/graph/warshall.cpp | 84 ------------------------------------------ 1 file changed, 84 deletions(-) delete mode 100644 c++/graph/warshall.cpp diff --git a/c++/graph/warshall.cpp b/c++/graph/warshall.cpp deleted file mode 100644 index 20cc61d..0000000 --- a/c++/graph/warshall.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include -#include -using namespace std; -#define N 4 -#define M INT_MAX -void printpath(int path[][N], int v, int u) -{ - if (path[v][u] == v) - return; - - printpath(path, v, path[v][u]); - cout << path[v][u] << " "; -} -void solution(int cost[N][N], int path[N][N]) -{ - for (int v = 0; v < N; v++) - { - for (int u = 0; u < N; u++) - { - if (u != v && path[v][u] != -1) - { - cout << "Shortest Path from " << v << " -> " << u << " is (" - << v << " "; - printpath(path, v, u); - cout << u << ")" << endl; - } - } - } -} - -void WarshallAlgo(int adjMatrix[][N]) -{ - int cost[N][N], path[N][N]; - for (int v = 0; v < N; v++) - { - for (int u = 0; u < N; u++) - { - cost[v][u] = adjMatrix[v][u]; - - if (v == u) - path[v][u] = 0; - else if (cost[v][u] != INT_MAX) - path[v][u] = v; - else - path[v][u] = -1; - } - } - - for (int k = 0; k < N; k++) - { - for (int v = 0; v < N; v++) - { - for (int u = 0; u < N; u++) - { - if (cost[v][k] != INT_MAX && cost[k][u] != INT_MAX && cost[v][k] + cost[k][u] < cost[v][u]) - { - cost[v][u] = cost[v][k] + cost[k][u]; - path[v][u] = path[k][u]; - } - } - - if (cost[v][v] < 0) - { - cout << "Negative Weight Cycle Found!!"; - return; - } - } - } - solution(cost, path); -} - -int main() -{ - int adjMatrix[N][N] = - { - {0, M, -2, M}, - {4, 0, 3, M}, - {M, M, 0, 2}, - {M, -1, M, 0}}; - WarshallAlgo(adjMatrix); - - return 0; -} \ No newline at end of file From e860ad7fd474621a4070b30e18739efa32ee7733 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 02:20:47 +0530 Subject: [PATCH 4/5] new Commit From 0c57e766b0bcba82b26c94840dd69d93bd969570 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Fri, 31 Dec 2021 04:40:21 +0530 Subject: [PATCH 5/5] Segment Tree implementation in different languages Fixes #29 --- java/tree/segmentree.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/java/tree/segmentree.java b/java/tree/segmentree.java index c995751..9c68bc9 100644 --- a/java/tree/segmentree.java +++ b/java/tree/segmentree.java @@ -1,3 +1,10 @@ +/*Segment Tree is a basically a binary tree used for storing the intervals or segments. +Each node in the segment tree represents an interval.When there is need to update an interval +in the range in Lazy propagation method, instead of updating each element one by one which will +turn complexity O(n) we will update a node and mark its child that it needs to be updated and update it when needed. +For this we need an array of the same size as that of segment tree. Initially all the elements of the array will be 0 representing that there is no pending update. +If there is non-zero element then this element needs to update node in the segment tree before making any query operation. */ + class segmentree { final int MAX = 1000; // Max tree size int tree[] = new int[MAX]; // stors segment tree