Skip to content

Commit ba2fe54

Browse files
Merge pull request #592 from 221fa04732/main
added Johnson's algorithm
2 parents a9d5960 + a7b8542 commit ba2fe54

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <limits>
4+
#include <algorithm>
5+
6+
#define INF std::numeric_limits<int>::max()
7+
8+
using namespace std;
9+
10+
// Function to find the vertex with the minimum distance
11+
// that has not yet been included in the shortest path tree
12+
int Min_Distance(const vector<int>& dist, const vector<bool>& visited) {
13+
int min = INF, min_index;
14+
for (int v = 0; v < dist.size(); ++v) {
15+
if (!visited[v] && dist[v] <= min) {
16+
min = dist[v];
17+
min_index = v;
18+
}
19+
}
20+
return min_index;
21+
}
22+
23+
// Function to perform Dijkstra's algorithm on the modified graph
24+
void Dijkstra_Algorithm(const vector<vector<int>>& graph, const vector<vector<int>>& altered_graph, int source) {
25+
int V = graph.size(); // Number of vertices
26+
vector<int> dist(V, INF); // Distance from source to each vertex
27+
vector<bool> visited(V, false); // Track visited vertices
28+
29+
dist[source] = 0; // Distance to source itself is 0
30+
31+
// Compute shortest path for all vertices
32+
for (int count = 0; count < V - 1; ++count) {
33+
// Select the vertex with the minimum distance that hasn't been visited
34+
int u = Min_Distance(dist, visited);
35+
visited[u] = true; // Mark this vertex as visited
36+
37+
// Update the distance value of the adjacent vertices of the selected vertex
38+
for (int v = 0; v < V; ++v) {
39+
if (!visited[v] && graph[u][v] != 0 && dist[u] != INF && dist[u] + altered_graph[u][v] < dist[v]) {
40+
dist[v] = dist[u] + altered_graph[u][v];
41+
}
42+
}
43+
}
44+
45+
// Print the shortest distances from the source
46+
cout << "Shortest Distance from vertex " << source << ":\n";
47+
for (int i = 0; i < V; ++i) {
48+
cout << "Vertex " << i << ": " << (dist[i] == INF ? "INF" : to_string(dist[i])) << endl;
49+
}
50+
}
51+
52+
// Function to perform Bellman-Ford algorithm to find shortest distances
53+
// from a source vertex to all other vertices
54+
vector<int> BellmanFord_Algorithm(const vector<vector<int>>& edges, int V) {
55+
vector<int> dist(V + 1, INF); // Distance from source to each vertex
56+
dist[V] = 0; // Distance to the new source vertex (added vertex) is 0
57+
58+
// Add a new source vertex to the graph and connect it to all original vertices with 0 weight edges
59+
vector<vector<int>> edges_with_extra(edges);
60+
for (int i = 0; i < V; ++i) {
61+
edges_with_extra.push_back({V, i, 0});
62+
}
63+
64+
// Relax all edges |V| - 1 times
65+
for (int i = 0; i < V; ++i) {
66+
for (const auto& edge : edges_with_extra) {
67+
if (dist[edge[0]] != INF && dist[edge[0]] + edge[2] < dist[edge[1]]) {
68+
dist[edge[1]] = dist[edge[0]] + edge[2];
69+
}
70+
}
71+
}
72+
return vector<int>(dist.begin(), dist.begin() + V); // Return distances excluding the new source vertex
73+
}
74+
75+
// Function to implement Johnson's Algorithm
76+
void JohnsonAlgorithm(const vector<vector<int>>& graph) {
77+
int V = graph.size(); // Number of vertices
78+
vector<vector<int>> edges;
79+
80+
// Collect all edges from the graph
81+
for (int i = 0; i < V; ++i) {
82+
for (int j = 0; j < V; ++j) {
83+
if (graph[i][j] != 0) {
84+
edges.push_back({i, j, graph[i][j]});
85+
}
86+
}
87+
}
88+
89+
// Get the modified weights from Bellman-Ford algorithm
90+
vector<int> altered_weights = BellmanFord_Algorithm(edges, V);
91+
vector<vector<int>> altered_graph(V, vector<int>(V, 0));
92+
93+
// Modify the weights of the edges to remove negative weights
94+
for (int i = 0; i < V; ++i) {
95+
for (int j = 0; j < V; ++j) {
96+
if (graph[i][j] != 0) {
97+
altered_graph[i][j] = graph[i][j] + altered_weights[i] - altered_weights[j];
98+
}
99+
}
100+
}
101+
102+
// Print the modified graph with re-weighted edges
103+
cout << "Modified Graph:\n";
104+
for (const auto& row : altered_graph) {
105+
for (int weight : row) {
106+
cout << weight << ' ';
107+
}
108+
cout << endl;
109+
}
110+
111+
// Run Dijkstra's algorithm for every vertex as the source
112+
for (int source = 0; source < V; ++source) {
113+
cout << "\nShortest Distance with vertex " << source << " as the source:\n";
114+
Dijkstra_Algorithm(graph, altered_graph, source);
115+
}
116+
}
117+
118+
// Main function to test the Johnson's Algorithm implementation
119+
int main() {
120+
// Define a graph with possible negative weights
121+
vector<vector<int>> graph = {
122+
{0, -5, 2, 3},
123+
{0, 0, 4, 0},
124+
{0, 0, 0, 1},
125+
{0, 0, 0, 0}
126+
};
127+
128+
// Execute Johnson's Algorithm
129+
JohnsonAlgorithm(graph);
130+
131+
// ALGO INFO
132+
133+
cout<<endl<<endl;
134+
cout<<"=================\n";
135+
cout<<" ALGORITHM INFO \n";
136+
cout<<"=================\n\n";
137+
cout<<"Time COmplexity : O(V2log V + VE)\n";
138+
cout<<"Space Complexity : O(V2)\n";
139+
return 0;
140+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
vector<int> zFunction(string s) {
9+
int n = s.length();
10+
vector<int> z(n);
11+
int l = 0, r = 0;
12+
13+
for (int i = 1; i < n; i++) {
14+
if (i <= r) {
15+
int k = i - l;
16+
17+
// Case 2: reuse the previously computed value
18+
z[i] = min(r - i + 1, z[k]);
19+
}
20+
21+
// Try to extend the Z-box beyond r
22+
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) {
23+
z[i]++;
24+
}
25+
26+
// Update the [l, r] window if extended
27+
if (i + z[i] - 1 > r) {
28+
l = i;
29+
r = i + z[i] - 1;
30+
}
31+
}
32+
33+
return z;
34+
}
35+
36+
int main(){
37+
string s = "aabxaab";
38+
39+
vector<int> z = zFunction(s);
40+
41+
for(int i=0; i < z.size(); ++i){
42+
cout<<z[i]<<" ";
43+
}
44+
45+
// ALGO INFO
46+
47+
cout<<endl<<endl;
48+
cout<<"=================\n";
49+
cout<<" ALGORITHM INFO \n";
50+
cout<<"=================\n\n";
51+
cout<<"Time COmplexity : O(n)\n";
52+
cout<<"Space Complexity : O(n)\n";
53+
return 0;
54+
}

0 commit comments

Comments
 (0)