Skip to content

Commit 4c9aaf9

Browse files
committed
Added the implementations of Depth_First_Search, Breadth_First_Search…
…,BellmanFord_Algo , Floyyd_Warshall_Algo , KruskalAlgo , MergeSort , Prims_Algo ,TopologicalSort ,DSU_Template among others.
1 parent 0dc0e41 commit 4c9aaf9

File tree

6 files changed

+442
-0
lines changed

6 files changed

+442
-0
lines changed

Graph_Algorithms/BellmanFord.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
5+
using namespace std;
6+
7+
const int INF = INT_MAX;
8+
9+
struct Edge {
10+
int src, dest, weight;
11+
};
12+
13+
class Graph {
14+
public:
15+
int V, E; // Number of vertices and edges
16+
vector<Edge> edges;
17+
18+
Graph(int vertices, int edgesCount) {
19+
V = vertices;
20+
E = edgesCount;
21+
edges.resize(E);
22+
}
23+
24+
void addEdge(int u, int v, int w, int edgeIndex) {
25+
edges[edgeIndex].src = u;
26+
edges[edgeIndex].dest = v;
27+
edges[edgeIndex].weight = w;
28+
}
29+
30+
void bellmanFord(int src) {
31+
vector<int> dist(V, INF);
32+
dist[src] = 0;
33+
34+
for (int i = 0; i < V - 1; i++) {
35+
for (int j = 0; j < E; j++) {
36+
int u = edges[j].src;
37+
int v = edges[j].dest;
38+
int weight = edges[j].weight;
39+
40+
if (dist[u] != INF && dist[u] + weight < dist[v]) {
41+
dist[v] = dist[u] + weight;
42+
}
43+
}
44+
}
45+
46+
// Check for negative-weight cycles
47+
for (int j = 0; j < E; j++) {
48+
int u = edges[j].src;
49+
int v = edges[j].dest;
50+
int weight = edges[j].weight;
51+
52+
if (dist[u] != INF && dist[u] + weight < dist[v]) {
53+
cout << "Negative-weight cycle detected!" << endl;
54+
return;
55+
}
56+
}
57+
58+
cout << "Shortest distances from vertex " << src << ":\n";
59+
for (int i = 0; i < V; i++) {
60+
cout << "Vertex " << i << ": " << dist[i] << "\n";
61+
}
62+
}
63+
};
64+
65+
int main() {
66+
int V, E; // Number of vertices and edges
67+
cin >> V >> E;
68+
Graph g(V, E);
69+
70+
for (int i = 0; i < E; i++) {
71+
int u, v, w;
72+
cin >> u >> v >> w;
73+
g.addEdge(u, v, w, i);
74+
}
75+
76+
int src; // Source vertex for Bellman-Ford
77+
cin >> src;
78+
79+
g.bellmanFord(src);
80+
81+
return 0;
82+
}

Graph_Algorithms/DSU.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
class DSU {
7+
public:
8+
vector<int> parent, rank;
9+
10+
DSU(int n) {
11+
parent.resize(n);
12+
rank.resize(n, 0);
13+
for (int i = 0; i < n; i++) {
14+
parent[i] = i;
15+
}
16+
}
17+
18+
int find(int x) {
19+
if (x != parent[x]) {
20+
parent[x] = find(parent[x]);
21+
}
22+
return parent[x];
23+
}
24+
25+
void unionSets(int x, int y) {
26+
int rootX = find(x);
27+
int rootY = find(y);
28+
29+
if (rootX != rootY) {
30+
if (rank[rootX] < rank[rootY]) {
31+
parent[rootX] = rootY;
32+
} else if (rank[rootX] > rank[rootY]) {
33+
parent[rootY] = rootX;
34+
} else {
35+
parent[rootY] = rootX;
36+
rank[rootX]++;
37+
}
38+
}
39+
}
40+
};
41+
42+
int main() {
43+
int n; // Number of elements
44+
cin >> n;
45+
DSU dsu(n);
46+
47+
// Example usage: unionSets and find operations
48+
dsu.unionSets(0, 1);
49+
dsu.unionSets(2, 3);
50+
dsu.unionSets(0, 3);
51+
52+
cout << "Are 1 and 2 in the same set? " << (dsu.find(1) == dsu.find(2) ? "Yes" : "No") << endl;
53+
cout << "Are 0 and 3 in the same set? " << (dsu.find(0) == dsu.find(3) ? "Yes" : "No") << endl;
54+
55+
return 0;
56+
}

Graph_Algorithms/Dijkstra.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <climits>
5+
6+
using namespace std;
7+
8+
const int INF = INT_MAX;
9+
10+
// Define a class to represent a weighted graph
11+
class Graph {
12+
public:
13+
int V; // Number of vertices
14+
vector<vector<pair<int, int>>> adj; // Adjacency list
15+
16+
Graph(int vertices) {
17+
V = vertices;
18+
adj.resize(V);
19+
}
20+
21+
// Function to add an edge to the graph
22+
void addEdge(int u, int v, int w) {
23+
adj[u].push_back({v, w});
24+
adj[v].push_back({u, w}); // If the graph is undirected
25+
}
26+
27+
// Dijkstra's algorithm to find the shortest path
28+
vector<int> dijkstra(int src) {
29+
vector<int> dist(V, INF);
30+
dist[src] = 0;
31+
32+
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
33+
pq.push({0, src});
34+
35+
while (!pq.empty()) {
36+
int u = pq.top().second;
37+
int weight = pq.top().first;
38+
pq.pop();
39+
40+
if (weight > dist[u]) continue;
41+
42+
for (pair<int, int>& edge : adj[u]) {
43+
int v = edge.first;
44+
int w = edge.second;
45+
46+
if (dist[u] + w < dist[v]) {
47+
dist[v] = dist[u] + w;
48+
pq.push({dist[v], v});
49+
}
50+
}
51+
}
52+
53+
return dist;
54+
}
55+
};
56+
57+
int main() {
58+
int V, E; // Number of vertices and edges
59+
cin >> V >> E;
60+
61+
Graph g(V);
62+
63+
for (int i = 0; i < E; i++) {
64+
int u, v, w;
65+
cin >> u >> v >> w;
66+
g.addEdge(u, v, w);
67+
}
68+
69+
int src; // Source vertex for Dijkstra's algorithm
70+
cin >> src;
71+
72+
vector<int> shortestDistances = g.dijkstra(src);
73+
74+
cout << "Shortest distances from vertex " << src << ":\n";
75+
for (int i = 0; i < V; i++) {
76+
cout << "Vertex " << i << ": " << shortestDistances[i] << "\n";
77+
}
78+
79+
return 0;
80+
}

Graph_Algorithms/Floyyd_Warshall.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <climits>
4+
5+
using namespace std;
6+
7+
const int INF = INT_MAX;
8+
9+
class Graph {
10+
public:
11+
int V; // Number of vertices
12+
vector<vector<int>> dist;
13+
14+
Graph(int vertices) {
15+
V = vertices;
16+
dist.resize(V, vector<int>(V, INF));
17+
18+
// Initialize diagonal elements to 0
19+
for (int i = 0; i < V; i++) {
20+
dist[i][i] = 0;
21+
}
22+
}
23+
24+
void addEdge(int u, int v, int w) {
25+
dist[u][v] = w;
26+
}
27+
28+
void floydWarshall() {
29+
for (int k = 0; k < V; k++) {
30+
for (int i = 0; i < V; i++) {
31+
for (int j = 0; j < V; j++) {
32+
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) {
33+
dist[i][j] = dist[i][k] + dist[k][j];
34+
}
35+
}
36+
}
37+
}
38+
39+
cout << "Shortest distances between all pairs of vertices:\n";
40+
for (int i = 0; i < V; i++) {
41+
for (int j = 0; j < V; j++) {
42+
if (dist[i][j] == INF) {
43+
cout << "INF ";
44+
} else {
45+
cout << dist[i][j] << " ";
46+
}
47+
}
48+
cout << "\n";
49+
}
50+
}
51+
};
52+
53+
int main() {
54+
int V, E; // Number of vertices and edges
55+
cin >> V >> E;
56+
Graph g(V);
57+
58+
for (int i = 0; i < E; i++) {
59+
int u, v, w;
60+
cin >> u >> v >> w;
61+
g.addEdge(u, v, w);
62+
}
63+
64+
g.floydWarshall();
65+
66+
return 0;
67+
}

Graph_Algorithms/Kruskal.cpp

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
struct Edge {
8+
int u, v, weight;
9+
};
10+
11+
bool compareEdges(const Edge& a, const Edge& b) {
12+
return a.weight < b.weight;
13+
}
14+
15+
class UnionFind {
16+
public:
17+
vector<int> parent, rank;
18+
19+
UnionFind(int n) {
20+
parent.resize(n);
21+
rank.resize(n, 0);
22+
for (int i = 0; i < n; i++) {
23+
parent[i] = i;
24+
}
25+
}
26+
27+
int find(int x) {
28+
if (x != parent[x]) {
29+
parent[x] = find(parent[x]);
30+
}
31+
return parent[x];
32+
}
33+
34+
void unionSets(int x, int y) {
35+
int rootX = find(x);
36+
int rootY = find(y);
37+
38+
if (rootX != rootY) {
39+
if (rank[rootX] < rank[rootY]) {
40+
parent[rootX] = rootY;
41+
} else if (rank[rootX] > rank[rootY]) {
42+
parent[rootY] = rootX;
43+
} else {
44+
parent[rootY] = rootX;
45+
rank[rootX]++;
46+
}
47+
}
48+
}
49+
};
50+
51+
vector<Edge> kruskalMST(vector<Edge>& edges, int n) {
52+
sort(edges.begin(), edges.end(), compareEdges);
53+
vector<Edge> minimumSpanningTree;
54+
UnionFind uf(n);
55+
56+
for (Edge edge : edges) {
57+
if (uf.find(edge.u) != uf.find(edge.v)) {
58+
minimumSpanningTree.push_back(edge);
59+
uf.unionSets(edge.u, edge.v);
60+
}
61+
}
62+
63+
return minimumSpanningTree;
64+
}
65+
66+
int main() {
67+
int n, m; // Number of vertices and edges
68+
cin >> n >> m;
69+
vector<Edge> edges(m);
70+
71+
for (int i = 0; i < m; i++) {
72+
cin >> edges[i].u >> edges[i].v >> edges[i].weight;
73+
}
74+
75+
vector<Edge> minimumSpanningTree = kruskalMST(edges, n);
76+
77+
cout << "Minimum Spanning Tree Edges:\n";
78+
for (Edge edge : minimumSpanningTree) {
79+
cout << edge.u << " - " << edge.v << " : " << edge.weight << "\n";
80+
}
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)