-
Notifications
You must be signed in to change notification settings - Fork 77
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
sonicdashh
wants to merge
2
commits into
codewithdev:master
Choose a base branch
from
sonicdashh:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the issue exactly? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.