From 3ea5a4aa1cbcd55e113383d75ad6f01c3d498308 Mon Sep 17 00:00:00 2001 From: amanagr6697 Date: Mon, 31 Oct 2022 00:32:57 +0530 Subject: [PATCH 1/2] Update readme.txt --- graph/readme.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graph/readme.txt b/graph/readme.txt index 7024d0b0..af99d4f1 100644 --- a/graph/readme.txt +++ b/graph/readme.txt @@ -17,4 +17,5 @@ 17. shortest path algorithm (Bellman Ford algorithm) (works with negative weights) 18. Strongly connected components (Kosaraju's algorithm) 20. Articulation Point -21. Bridges in graph \ No newline at end of file +21. Bridges in graph +22. Bellman ford with negative cyc included From 70b6dd8d2b7e2fe089737717fd55ff148f1db6f6 Mon Sep 17 00:00:00 2001 From: amanagr6697 Date: Mon, 31 Oct 2022 00:34:39 +0530 Subject: [PATCH 2/2] Create Bellman ford with negative cyc included.cpp --- ...ellman ford with negative cyc included.cpp | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 graph/Bellman ford with negative cyc included.cpp diff --git a/graph/Bellman ford with negative cyc included.cpp b/graph/Bellman ford with negative cyc included.cpp new file mode 100644 index 00000000..3366d854 --- /dev/null +++ b/graph/Bellman ford with negative cyc included.cpp @@ -0,0 +1,96 @@ +// Bellman ford algorithm +// It basically finds after the ith iteration the shortest distance from src to every other node +// and hence promises to find the shortest distance to a node j with atmost i lengths or i-1 stops +// after the ith iteration + +// Proof is quite simple though: +// Consider an arbitrary vertex a to which there is a path from the starting vertex v, and consider a shortest path to it(p0=v,p1,,,,pk=a). Before the first phase, the shortest path to the vertex p0=v was found correctly. During the first phase, the edge (p0,p1) has been checked by the algorithm, and therefore, the distance to the vertex p1 was correctly calculated after the first phase. Repeating this statement k times, we see that after kth phase the distance to the vertex pk=a gets calculated correctly, which we wanted to prove. + +// Initially 0th and 1st versions are same + +// dis 0th version contains distances to all veritces after jth iteration that is having at max j edges +// For full bellman ford just use k=n-1 + +// Maintaining a parent array for full bellman ford to retrieve cycle + + + + +int par[n + 1]; +memset(par, -1, sizeof(par)); +int dis[n + 1][2]; +for (int i = 1; i <= n; i++) + dis[i][0] = 1e9, dis[i][1] = 1e9; +dis[src][0] = 0; +dis[src][1] = 0; + + +for (int j = 1; j <= k; j++) +{ + for (auto i : edges) + { + if (dis[i[1]][1] > dis[i[0]][0] + i[2]) + { + dis[i[1]][1] = dis[i[0]][0] + i[2]; + par[i[1]] = i[0]; + } + } + for (int i = 0; i < n; i++) + dis[i][0] = dis[i][1]; +} + + + + + +// Printing parent +vector path; +int t = dest; +for (int cur = t; cur != -1; cur = par[cur]) + path.push_back(cur); +reverse(path.begin(), path.end()); + +for (int i = 0; i < path.size(); ++i) + cout << path[i] << ' '; + + + + +bool neg_cycle_found = 0; +int node = -1; +// Detection of negative cycle +for (auto i : edges) +{ + if (dis[i[1]][1] > dis[i[0]][0] + i[2]) + { + neg_cycle_found = 1; + node = i[1]; + } +} + + + +// Printing negative cycle +if (neg_cycle_found) +{ + int y = node; + for (int i = 0; i < n; ++i) // It guarantees that after n iterations we would definitely be present + y = par[y]; // at any node insice cycle as max distance from any node in cycle to + // max farthest node could be n-1 only. + + + + + vector path; + for (int cur = y;; cur = par[cur]) + { + path.push_back(cur); + if (cur == y && path.size() > 1) + break; + } + reverse(path.begin(), path.end()); + + cout << "Negative cycle: "; + for (int i = 0; i < path.size(); ++i) + cout << path[i] << ' '; +}