diff --git a/Algorithms/Graphs/BFS.cpp b/Algorithms/Graphs/BFS.cpp new file mode 100644 index 0000000..f17acb7 --- /dev/null +++ b/Algorithms/Graphs/BFS.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +using namespace std; + +void BFS(int startNode, vector>& adjList, vector& visited) { + queue 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> adjList(numNodes); + vector 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; +} diff --git a/Algorithms/Graphs/DFS.cpp b/Algorithms/Graphs/DFS.cpp new file mode 100644 index 0000000..0b04190 --- /dev/null +++ b/Algorithms/Graphs/DFS.cpp @@ -0,0 +1,35 @@ +#include +#include + +using namespace std; + +void DFS(int node, vector>& adjList, vector& 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> adjList(numNodes); + vector 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; +} diff --git "a/Algorithms/Graphs/Dijkstra\342\200\231sAlgo.cpp" "b/Algorithms/Graphs/Dijkstra\342\200\231sAlgo.cpp" new file mode 100644 index 0000000..452c97c --- /dev/null +++ "b/Algorithms/Graphs/Dijkstra\342\200\231sAlgo.cpp" @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +using namespace std; + +const int INF = numeric_limits::max(); + +void dijkstra(int startNode, vector>>& adjList, vector& distances) { + // Priority queue to store (distance, node) + priority_queue, vector>, greater>> 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>> adjList(numNodes); + vector 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; +}