Skip to content

Added the implementations of Depth_First_Search, Breadth_First_Search, BellmanFord_Algo , Floyyd_Warshall_Algo , KruskalAlgo , MergeSort , Prims_Algo ,TopologicalSort ,DSU_Template among others. #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Graph_Algorithms/BellmanFord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

const int INF = INT_MAX;

struct Edge {
int src, dest, weight;
};

class Graph {
public:
int V, E; // Number of vertices and edges
vector<Edge> edges;

Graph(int vertices, int edgesCount) {
V = vertices;
E = edgesCount;
edges.resize(E);
}

void addEdge(int u, int v, int w, int edgeIndex) {
edges[edgeIndex].src = u;
edges[edgeIndex].dest = v;
edges[edgeIndex].weight = w;
}

void bellmanFord(int src) {
vector<int> dist(V, INF);
dist[src] = 0;

for (int i = 0; i < V - 1; i++) {
for (int j = 0; j < E; j++) {
int u = edges[j].src;
int v = edges[j].dest;
int weight = edges[j].weight;

if (dist[u] != INF && dist[u] + weight < dist[v]) {
dist[v] = dist[u] + weight;
}
}
}

// Check for negative-weight cycles
for (int j = 0; j < E; j++) {
int u = edges[j].src;
int v = edges[j].dest;
int weight = edges[j].weight;

if (dist[u] != INF && dist[u] + weight < dist[v]) {
cout << "Negative-weight cycle detected!" << endl;
return;
}
}

cout << "Shortest distances from vertex " << src << ":\n";
for (int i = 0; i < V; i++) {
cout << "Vertex " << i << ": " << dist[i] << "\n";
}
}
};

int main() {
int V, E; // Number of vertices and edges
cin >> V >> E;
Graph g(V, E);

for (int i = 0; i < E; i++) {
int u, v, w;
cin >> u >> v >> w;
g.addEdge(u, v, w, i);
}

int src; // Source vertex for Bellman-Ford
cin >> src;

g.bellmanFord(src);

return 0;
}
56 changes: 56 additions & 0 deletions Graph_Algorithms/DSU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <vector>

using namespace std;

class DSU {
public:
vector<int> parent, rank;

DSU(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}

int find(int x) {
if (x != parent[x]) {
parent[x] = find(parent[x]);
}
return parent[x];
}

void unionSets(int x, int y) {
int rootX = find(x);
int rootY = find(y);

if (rootX != rootY) {
if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};

int main() {
int n; // Number of elements
cin >> n;
DSU dsu(n);

// Example usage: unionSets and find operations
dsu.unionSets(0, 1);
dsu.unionSets(2, 3);
dsu.unionSets(0, 3);

cout << "Are 1 and 2 in the same set? " << (dsu.find(1) == dsu.find(2) ? "Yes" : "No") << endl;
cout << "Are 0 and 3 in the same set? " << (dsu.find(0) == dsu.find(3) ? "Yes" : "No") << endl;

return 0;
}
80 changes: 80 additions & 0 deletions Graph_Algorithms/Dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <iostream>
#include <vector>
#include <queue>
#include <climits>

using namespace std;

const int INF = INT_MAX;

// Define a class to represent a weighted graph
class Graph {
public:
int V; // Number of vertices
vector<vector<pair<int, int>>> adj; // Adjacency list

Graph(int vertices) {
V = vertices;
adj.resize(V);
}

// Function to add an edge to the graph
void addEdge(int u, int v, int w) {
adj[u].push_back({v, w});
adj[v].push_back({u, w}); // If the graph is undirected
}

// Dijkstra's algorithm to find the shortest path
vector<int> dijkstra(int src) {
vector<int> dist(V, INF);
dist[src] = 0;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, src});

while (!pq.empty()) {
int u = pq.top().second;
int weight = pq.top().first;
pq.pop();

if (weight > dist[u]) continue;

for (pair<int, int>& edge : adj[u]) {
int v = edge.first;
int w = edge.second;

if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
pq.push({dist[v], v});
}
}
}

return dist;
}
};

int main() {
int V, E; // Number of vertices and edges
cin >> V >> E;

Graph g(V);

for (int i = 0; i < E; i++) {
int u, v, w;
cin >> u >> v >> w;
g.addEdge(u, v, w);
}

int src; // Source vertex for Dijkstra's algorithm
cin >> src;

vector<int> shortestDistances = g.dijkstra(src);

cout << "Shortest distances from vertex " << src << ":\n";
for (int i = 0; i < V; i++) {
cout << "Vertex " << i << ": " << shortestDistances[i] << "\n";
}

return 0;
}
67 changes: 67 additions & 0 deletions Graph_Algorithms/Floyyd_Warshall.cpp
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a quick introduction at the top (before line 1).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

const int INF = INT_MAX;

class Graph {
public:
int V; // Number of vertices
vector<vector<int>> dist;

Graph(int vertices) {
V = vertices;
dist.resize(V, vector<int>(V, INF));

// Initialize diagonal elements to 0
for (int i = 0; i < V; i++) {
dist[i][i] = 0;
}
}

void addEdge(int u, int v, int w) {
dist[u][v] = w;
}

void floydWarshall() {
for (int k = 0; k < V; k++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][k] != INF && dist[k][j] != INF && dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}

cout << "Shortest distances between all pairs of vertices:\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF) {
cout << "INF ";
} else {
cout << dist[i][j] << " ";
}
}
cout << "\n";
}
}
};

int main() {
int V, E; // Number of vertices and edges
cin >> V >> E;
Graph g(V);

for (int i = 0; i < E; i++) {
int u, v, w;
cin >> u >> v >> w;
g.addEdge(u, v, w);
}

g.floydWarshall();

return 0;
}
83 changes: 83 additions & 0 deletions Graph_Algorithms/Kruskal.cpp
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the issue exactly?

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Edge {
int u, v, weight;
};

bool compareEdges(const Edge& a, const Edge& b) {
return a.weight < b.weight;
}

class UnionFind {
public:
vector<int> parent, rank;

UnionFind(int n) {
parent.resize(n);
rank.resize(n, 0);
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}

int find(int x) {
if (x != parent[x]) {
parent[x] = find(parent[x]);
}
return parent[x];
}

void unionSets(int x, int y) {
int rootX = find(x);
int rootY = find(y);

if (rootX != rootY) {
if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
};

vector<Edge> kruskalMST(vector<Edge>& edges, int n) {
sort(edges.begin(), edges.end(), compareEdges);
vector<Edge> minimumSpanningTree;
UnionFind uf(n);

for (Edge edge : edges) {
if (uf.find(edge.u) != uf.find(edge.v)) {
minimumSpanningTree.push_back(edge);
uf.unionSets(edge.u, edge.v);
}
}

return minimumSpanningTree;
}

int main() {
int n, m; // Number of vertices and edges
cin >> n >> m;
vector<Edge> edges(m);

for (int i = 0; i < m; i++) {
cin >> edges[i].u >> edges[i].v >> edges[i].weight;
}

vector<Edge> minimumSpanningTree = kruskalMST(edges, n);

cout << "Minimum Spanning Tree Edges:\n";
for (Edge edge : minimumSpanningTree) {
cout << edge.u << " - " << edge.v << " : " << edge.weight << "\n";
}

return 0;
}
Loading