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 734b4a9ad431038ad390dbadb15f9ab87648adcf Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 00:41:37 +0530 Subject: [PATCH 2/5] Warshall Algoritm Implementation in Different Languages Fixes #25 --- c++/graph/warshall.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/c++/graph/warshall.cpp b/c++/graph/warshall.cpp index 20cc61d..345adc8 100644 --- a/c++/graph/warshall.cpp +++ b/c++/graph/warshall.cpp @@ -2,6 +2,7 @@ #include #include using namespace std; +// defined number of vertices #define N 4 #define M INT_MAX void printpath(int path[][N], int v, int u) @@ -28,7 +29,7 @@ void solution(int cost[N][N], int path[N][N]) } } } - +// implemented Floyd warshall Algorithm void WarshallAlgo(int adjMatrix[][N]) { int cost[N][N], path[N][N]; @@ -46,7 +47,7 @@ void WarshallAlgo(int adjMatrix[][N]) path[v][u] = -1; } } - + // Adding vertices individually for (int k = 0; k < N; k++) { for (int v = 0; v < N; v++) @@ -77,8 +78,23 @@ int main() {0, M, -2, M}, {4, 0, 3, M}, {M, M, 0, 2}, - {M, -1, M, 0}}; - WarshallAlgo(adjMatrix); + {M, -1, M, 0}}; // graph input + WarshallAlgo(adjMatrix); // calling of function return 0; -} \ No newline at end of file +} +/* +Time Complexity:O(Ver^3) +Space Complexity:O(Ver^2) +Shortest Path from 0 -> 1 is (0 2 3 1) +Shortest Path from 0 -> 2 is (0 2) +Shortest Path from 0 -> 3 is (0 2 3) +Shortest Path from 1 -> 0 is (1 0) +Shortest Path from 1 -> 2 is (1 0 2) +Shortest Path from 1 -> 3 is (1 0 2 3) +Shortest Path from 2 -> 0 is (2 3 1 0) +Shortest Path from 2 -> 1 is (2 3 1) +Shortest Path from 2 -> 3 is (2 3) +Shortest Path from 3 -> 0 is (3 1 0) +Shortest Path from 3 -> 1 is (3 1) +Shortest Path from 3 -> 2 is (3 1 0 2)*/ \ No newline at end of file From ce692300b0414c4d54ad133352dc62deb1a368e6 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 00:44:47 +0530 Subject: [PATCH 3/5] Time and space Complexity Mentioned,Codeexplained --- java/tree/segmentree.class | Bin 0 -> 2505 bytes java/tree/segmentree.java | 141 +++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 java/tree/segmentree.class create mode 100644 java/tree/segmentree.java diff --git a/java/tree/segmentree.class b/java/tree/segmentree.class new file mode 100644 index 0000000000000000000000000000000000000000..42d3254c008ae182cee928e7af4745eee3fb2ea3 GIT binary patch literal 2505 zcmaJ?TW=Fb7(Hw6dL74!oxpOb*I-J3cukxZC}c!Xgbrq*~wXJ%9Ri}X*;%iT7thjIog6CT2zF>Xhm2; zsA^4@EXS=_mW1Ho+3PoRnep=yq>ONE4}XP0}2E4Jh2Sh!j03t}(NWSse$o3N{ijI&@C?Lr9quwTW2FuIYH zuw|2z64ZGSr|2+rw->vP9Ru6Wbe~sxDvX2ZVT)5`r|MQ}Q*Lf#wf-BTMbFJI+I%aF zw{b{9Ym+Z2=%%0NbtH^;aFiww2eBt2WI9Rbu`rI~goN!SbI$57J5#26&30!vtGcG+ zRz;DSo*Ww$)IhX0c}ZnX*7C`^yEg9~rP6Yp7P5 zv??R!WRaG(oNG?a4Vv?fwOlf70=2FC#>Ntoqog)hu2rV23${2MSyNV8AWwqMmCG*i zGv@~_ccxsZMgsUSgaHhy7!sXlC3LOH*LD`lb5^g%ga^h2bIL7O7A1V&%vHnD3wqIX z3Pr2hHBc_k)#m#)7TRQ4cW`8J-rB^yxmE9l>T4#T5u)EL7IU_1QHhr&q(^3~M735* zly4aD=!Z zI0m`LD~aPduFlX$;xlGEg=9nvPoQ#q9(sbd#aT#U|2my&&g3<4iuQ$L}F2R!VF}&QGMhjeNFYQeHE~{5-LQ zh^{G1Xo+j`L&%SHO@4sWabY^fbFY$UfkuKoO$oHXy5M5@SdZtG;!)M8*Pv^vCTofo zSix3dQSzE9=b38NCNhXLb#GCym?`-*1F>IU9i1Tp47Bjyid|^KUc!A89mrrShKZ4h zotT6U2fJ~HgnfZ7e8;=+Ar4>(27W;b&v~Q$i8TH~FWzUg5CSie2}H>gf4oQ8NN zVBc=|{V}K#~K{bOblcC zJ@g1KMN?j}#H|LBG_LaW%BU%R9$P|tOp})o)Z-7K_?Khx2k0POOK9~{rI%y6z&>Ea zv;YhD60Bf{p#|i;Xhm3*JT3WU*6N{q0_~h1Cn6__-D&KnwEHOGVVuWxT*M46VHSg^ zA&XCNh3Mvp>Ii<~Qy|#_xDl;+po|&-&{eL zZ`tq=_OqU~a=cezoD@%B2RdGGNE39VWc#L`V(t`gW>s$(R1+>Sl?YIL4)uB7Ns%GoU;^S5gJN-Md$e; z|Cj=+5VR`B;I391i0}kIjg4v#@T~a$v>GYDE*IFe@w%!OndCO{JdX>FLyve!fS8X4 zo`=T#N_I?Evtvq>Lm(Q;j;Yabb}Sfe%MPSoocbNXv8PB6^`!I_^b+PCT~=3c_!-^> z2soF)C=GqhTa-Vy)+#x~D-QUSO1Q(f=rh9oIa~OW`uK{v_>#?iO)cCd|KE_HyCmmZ hw)Fwe_^6c(pY5DoV*SJQ@d`b~+a}>Et`VM{{{jhY0p$Py literal 0 HcmV?d00001 diff --git a/java/tree/segmentree.java b/java/tree/segmentree.java new file mode 100644 index 0000000..e9aed2a --- /dev/null +++ b/java/tree/segmentree.java @@ -0,0 +1,141 @@ +public class segmentree +{ +final int MAX_SIZE = 100; +int t[] = new int[MAX_SIZE]; +int lazy[] = new int[MAX_SIZE]; +void updateRangeUtil(int currNode, int x, int y, int s, int e, int val) +{ + +if (lazy[currNode] != 0) +{ +t[currNode] += (y - x + 1) * lazy[currNode]; + +if (x != y) +{ + +lazy[2 * currNode + 1] += lazy[currNode]; +lazy[2 * currNode + 2] += lazy[currNode]; +} + +lazy[currNode] = 0; +} + +if (x > y || x > e || y < s) +{ +return; +} + +if (x >= s && y <= e) +{ + +t[currNode] += (y - x + 1) * val; + +if (x != y) +{ + +lazy[2 * currNode + 1] += val; +lazy[2 * currNode + 2] += val; +} +return; +} + +int mid = (x + y) / 2; +updateRangeUtil(2 * currNode + 1, x, mid, s, e, val); +updateRangeUtil(2 * currNode + 2, mid + 1, y, s, e, val); + +t[currNode] = t[2 * currNode + 1] + t[2 * currNode + 2]; +} + +void updateRange(int n, int x, int y, int val) +{ +updateRangeUtil(0, 0, n - 1, x, y, val); +} + +int getSumUtil(int x, int y, int s, int e, int si) +{ + +if (lazy[si] != 0) +{ + +t[si] += (y - x + 1) * lazy[si]; + +if (x != y) +{ + +lazy[2 * si + 1] += lazy[si]; +lazy[2 * si + 2] += lazy[si]; +} + +lazy[si] = 0; +} + +if (x > y || x > e || y < s) +{ +return 0; +} + +if (x >= s && y <= e) +{ +return t[si]; +} + +int mid = (x + y) / 2; +return getSumUtil(x, mid, s, e, 2 * si + 1) + +getSumUtil(mid + 1, y, s, e, 2 * si + 2); +} + +int getSum(int s, int x, int y) +{ + +if (x < 0 || y > s - 1 || x > y) +{ +System.out.println("Input is Invalid"); +return -1; +} +return getSumUtil(0, s - 1, x, y, 0); +} + +void constructSTUtil(int a[], int x, int y, int si) +{ + +if (x > y) +{ +return; +} + +if (x == y) +{ +t[si] = a[x]; +return; +} + +int m = (x + y) / 2; +constructSTUtil(a, x, m, 2 * si + 1); +constructSTUtil(a, m + 1, y, 2* si + 2); +t[si] = t[2 * si + 1] + t[2 * si + 2]; +} + +void constructST(int a[], int s) +{ + +constructSTUtil(a, 0, s - 1, 0); +} + +public static void main(String argvs[]) +{ +int a[] = {2, 4, 7, 10, 12, 13}; +int s = a.length; + +segmentree tObj = new segmentree(); + +tObj.constructST(a, s); + +System.out.println("The sum of the values in the given range is: " + +tObj.getSum(s, 1, 4)); + +tObj.updateRange(s, 1, 5, 7); + +System.out.println("The sum of the values, after updation, in the given range is: " + +tObj.getSum(s, 1, 4)); +} +} \ No newline at end of file From 70d5e6af9b0feadd56193048825a753c7f635f78 Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 01:37:06 +0530 Subject: [PATCH 4/5] Warshall Algoritm Implementation in Different Languages Fixes #25 --- java/tree/segmentree.class | Bin 2505 -> 0 bytes java/tree/segmentree.java | 141 ------------------------------------- 2 files changed, 141 deletions(-) delete mode 100644 java/tree/segmentree.class delete mode 100644 java/tree/segmentree.java diff --git a/java/tree/segmentree.class b/java/tree/segmentree.class deleted file mode 100644 index 42d3254c008ae182cee928e7af4745eee3fb2ea3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2505 zcmaJ?TW=Fb7(Hw6dL74!oxpOb*I-J3cukxZC}c!Xgbrq*~wXJ%9Ri}X*;%iT7thjIog6CT2zF>Xhm2; zsA^4@EXS=_mW1Ho+3PoRnep=yq>ONE4}XP0}2E4Jh2Sh!j03t}(NWSse$o3N{ijI&@C?Lr9quwTW2FuIYH zuw|2z64ZGSr|2+rw->vP9Ru6Wbe~sxDvX2ZVT)5`r|MQ}Q*Lf#wf-BTMbFJI+I%aF zw{b{9Ym+Z2=%%0NbtH^;aFiww2eBt2WI9Rbu`rI~goN!SbI$57J5#26&30!vtGcG+ zRz;DSo*Ww$)IhX0c}ZnX*7C`^yEg9~rP6Yp7P5 zv??R!WRaG(oNG?a4Vv?fwOlf70=2FC#>Ntoqog)hu2rV23${2MSyNV8AWwqMmCG*i zGv@~_ccxsZMgsUSgaHhy7!sXlC3LOH*LD`lb5^g%ga^h2bIL7O7A1V&%vHnD3wqIX z3Pr2hHBc_k)#m#)7TRQ4cW`8J-rB^yxmE9l>T4#T5u)EL7IU_1QHhr&q(^3~M735* zly4aD=!Z zI0m`LD~aPduFlX$;xlGEg=9nvPoQ#q9(sbd#aT#U|2my&&g3<4iuQ$L}F2R!VF}&QGMhjeNFYQeHE~{5-LQ zh^{G1Xo+j`L&%SHO@4sWabY^fbFY$UfkuKoO$oHXy5M5@SdZtG;!)M8*Pv^vCTofo zSix3dQSzE9=b38NCNhXLb#GCym?`-*1F>IU9i1Tp47Bjyid|^KUc!A89mrrShKZ4h zotT6U2fJ~HgnfZ7e8;=+Ar4>(27W;b&v~Q$i8TH~FWzUg5CSie2}H>gf4oQ8NN zVBc=|{V}K#~K{bOblcC zJ@g1KMN?j}#H|LBG_LaW%BU%R9$P|tOp})o)Z-7K_?Khx2k0POOK9~{rI%y6z&>Ea zv;YhD60Bf{p#|i;Xhm3*JT3WU*6N{q0_~h1Cn6__-D&KnwEHOGVVuWxT*M46VHSg^ zA&XCNh3Mvp>Ii<~Qy|#_xDl;+po|&-&{eL zZ`tq=_OqU~a=cezoD@%B2RdGGNE39VWc#L`V(t`gW>s$(R1+>Sl?YIL4)uB7Ns%GoU;^S5gJN-Md$e; z|Cj=+5VR`B;I391i0}kIjg4v#@T~a$v>GYDE*IFe@w%!OndCO{JdX>FLyve!fS8X4 zo`=T#N_I?Evtvq>Lm(Q;j;Yabb}Sfe%MPSoocbNXv8PB6^`!I_^b+PCT~=3c_!-^> z2soF)C=GqhTa-Vy)+#x~D-QUSO1Q(f=rh9oIa~OW`uK{v_>#?iO)cCd|KE_HyCmmZ hw)Fwe_^6c(pY5DoV*SJQ@d`b~+a}>Et`VM{{{jhY0p$Py diff --git a/java/tree/segmentree.java b/java/tree/segmentree.java deleted file mode 100644 index e9aed2a..0000000 --- a/java/tree/segmentree.java +++ /dev/null @@ -1,141 +0,0 @@ -public class segmentree -{ -final int MAX_SIZE = 100; -int t[] = new int[MAX_SIZE]; -int lazy[] = new int[MAX_SIZE]; -void updateRangeUtil(int currNode, int x, int y, int s, int e, int val) -{ - -if (lazy[currNode] != 0) -{ -t[currNode] += (y - x + 1) * lazy[currNode]; - -if (x != y) -{ - -lazy[2 * currNode + 1] += lazy[currNode]; -lazy[2 * currNode + 2] += lazy[currNode]; -} - -lazy[currNode] = 0; -} - -if (x > y || x > e || y < s) -{ -return; -} - -if (x >= s && y <= e) -{ - -t[currNode] += (y - x + 1) * val; - -if (x != y) -{ - -lazy[2 * currNode + 1] += val; -lazy[2 * currNode + 2] += val; -} -return; -} - -int mid = (x + y) / 2; -updateRangeUtil(2 * currNode + 1, x, mid, s, e, val); -updateRangeUtil(2 * currNode + 2, mid + 1, y, s, e, val); - -t[currNode] = t[2 * currNode + 1] + t[2 * currNode + 2]; -} - -void updateRange(int n, int x, int y, int val) -{ -updateRangeUtil(0, 0, n - 1, x, y, val); -} - -int getSumUtil(int x, int y, int s, int e, int si) -{ - -if (lazy[si] != 0) -{ - -t[si] += (y - x + 1) * lazy[si]; - -if (x != y) -{ - -lazy[2 * si + 1] += lazy[si]; -lazy[2 * si + 2] += lazy[si]; -} - -lazy[si] = 0; -} - -if (x > y || x > e || y < s) -{ -return 0; -} - -if (x >= s && y <= e) -{ -return t[si]; -} - -int mid = (x + y) / 2; -return getSumUtil(x, mid, s, e, 2 * si + 1) + -getSumUtil(mid + 1, y, s, e, 2 * si + 2); -} - -int getSum(int s, int x, int y) -{ - -if (x < 0 || y > s - 1 || x > y) -{ -System.out.println("Input is Invalid"); -return -1; -} -return getSumUtil(0, s - 1, x, y, 0); -} - -void constructSTUtil(int a[], int x, int y, int si) -{ - -if (x > y) -{ -return; -} - -if (x == y) -{ -t[si] = a[x]; -return; -} - -int m = (x + y) / 2; -constructSTUtil(a, x, m, 2 * si + 1); -constructSTUtil(a, m + 1, y, 2* si + 2); -t[si] = t[2 * si + 1] + t[2 * si + 2]; -} - -void constructST(int a[], int s) -{ - -constructSTUtil(a, 0, s - 1, 0); -} - -public static void main(String argvs[]) -{ -int a[] = {2, 4, 7, 10, 12, 13}; -int s = a.length; - -segmentree tObj = new segmentree(); - -tObj.constructST(a, s); - -System.out.println("The sum of the values in the given range is: " + -tObj.getSum(s, 1, 4)); - -tObj.updateRange(s, 1, 5, 7); - -System.out.println("The sum of the values, after updation, in the given range is: " + -tObj.getSum(s, 1, 4)); -} -} \ No newline at end of file From 2fc65bc19faf99c5a9832b4db996b194a5c02cac Mon Sep 17 00:00:00 2001 From: vidit21srivastava Date: Thu, 30 Dec 2021 01:37:44 +0530 Subject: [PATCH 5/5] Final Correct Commit