Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 43 additions & 0 deletions Algorithms/Graphs/BFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

void BFS(int startNode, vector<vector<int>>& adjList, vector<bool>& visited) {
queue<int> q;
q.push(startNode);
visited[startNode] = true;

while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";

// Traverse all neighbors of the current node
for (int neighbor : adjList[node]) {
if (!visited[neighbor]) {
q.push(neighbor);
visited[neighbor] = true;
}
}
}
}

int main() {
int numNodes = 5; // Number of nodes in the graph
vector<vector<int>> adjList(numNodes);
vector<bool> visited(numNodes, false);

// Example adjacency list (undirected graph)
adjList[0] = {1, 2};
adjList[1] = {0, 3, 4};
adjList[2] = {0, 4};
adjList[3] = {1};
adjList[4] = {1, 2};

cout << "BFS starting from node 0: ";
BFS(0, adjList, visited);

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

using namespace std;

void DFS(int node, vector<vector<int>>& adjList, vector<bool>& visited) {
// Mark the current node as visited and print it
visited[node] = true;
cout << node << " ";

// Traverse all the neighbors of the current node
for (int neighbor : adjList[node]) {
if (!visited[neighbor]) {
DFS(neighbor, adjList, visited);
}
}
}

int main() {
int numNodes = 5; // Number of nodes in the graph
vector<vector<int>> adjList(numNodes);
vector<bool> visited(numNodes, false);

// Example adjacency list (undirected graph)
adjList[0] = {1, 2};
adjList[1] = {0, 3, 4};
adjList[2] = {0, 4};
adjList[3] = {1};
adjList[4] = {1, 2};

cout << "DFS starting from node 0: ";
DFS(0, adjList, visited);

return 0;
}
65 changes: 65 additions & 0 deletions Algorithms/Graphs/Dijkstra’sAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <vector>
#include <queue>
#include <limits>

using namespace std;

const int INF = numeric_limits<int>::max();

void dijkstra(int startNode, vector<vector<pair<int, int>>>& adjList, vector<int>& distances) {
// Priority queue to store (distance, node)
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
distances[startNode] = 0;
pq.push({0, startNode});

while (!pq.empty()) {
int currentDistance = pq.top().first;
int currentNode = pq.top().second;
pq.pop();

// Skip if we found a better way to this node already
if (currentDistance > distances[currentNode]) continue;

// Check all neighbors of the current node
for (auto& neighbor : adjList[currentNode]) {
int nextNode = neighbor.first;
int weight = neighbor.second;
int newDistance = currentDistance + weight;

// If a shorter path to nextNode is found
if (newDistance < distances[nextNode]) {
distances[nextNode] = newDistance;
pq.push({newDistance, nextNode});
}
}
}
}

int main() {
int numNodes = 5; // Number of nodes in the graph
vector<vector<pair<int, int>>> adjList(numNodes);
vector<int> distances(numNodes, INF);

// Example adjacency list (directed graph with weights)
adjList[0].push_back({1, 2});
adjList[0].push_back({2, 4});
adjList[1].push_back({2, 1});
adjList[1].push_back({3, 7});
adjList[2].push_back({4, 3});
adjList[3].push_back({4, 1});

int startNode = 0;
dijkstra(startNode, adjList, distances);

cout << "Shortest distances from node " << startNode << ":\n";
for (int i = 0; i < numNodes; i++) {
cout << "Node " << i << ": ";
if (distances[i] == INF)
cout << "Infinity\n";
else
cout << distances[i] << "\n";
}

return 0;
}