|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <stack> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +class Graph { |
| 7 | +public: |
| 8 | + vector<vector<int>> adjacencyList; // Adjacency list representation of the graph |
| 9 | + |
| 10 | + // Constructor to initialize the graph with the given number of nodes |
| 11 | + Graph(int totalNodes) { |
| 12 | + adjacencyList.resize(totalNodes); // Resize the adjacency list to fit the total nodes |
| 13 | + } |
| 14 | + |
| 15 | + // Method to add an edge to the graph |
| 16 | + void addEdge(int sourceNode, int destinationNode, bool isDirected = false) { |
| 17 | + adjacencyList[sourceNode].push_back(destinationNode); // Add destination node to source node's adjacency list |
| 18 | + if (!isDirected) { |
| 19 | + adjacencyList[destinationNode].push_back(sourceNode); // For undirected graph, add the reverse edge |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // Recursive DFS implementation |
| 24 | + void dfsRecursive(int startNode, vector<bool>& visited) { |
| 25 | + visited[startNode] = true; // Mark the node as visited |
| 26 | + cout << startNode << " "; // Print the visited node |
| 27 | + |
| 28 | + // Visit all the unvisited neighbors of the current node |
| 29 | + for (int neighbor : adjacencyList[startNode]) { |
| 30 | + if (!visited[neighbor]) { |
| 31 | + dfsRecursive(neighbor, visited); // Recursively visit the neighbor |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Iterative DFS implementation using a stack |
| 37 | + void dfsIterative(int startNode) { |
| 38 | + vector<bool> visited(adjacencyList.size(), false); // Keep track of visited nodes |
| 39 | + stack<int> s; // Stack to manage the nodes |
| 40 | + |
| 41 | + s.push(startNode); // Push the starting node into the stack |
| 42 | + |
| 43 | + while (!s.empty()) { |
| 44 | + int currentNode = s.top(); // Get the top node from the stack |
| 45 | + s.pop(); // Remove it from the stack |
| 46 | + |
| 47 | + if (!visited[currentNode]) { |
| 48 | + cout << currentNode << " "; // Print the current node |
| 49 | + visited[currentNode] = true; // Mark it as visited |
| 50 | + } |
| 51 | + |
| 52 | + // Push all unvisited neighbors of the current node into the stack |
| 53 | + for (int neighbor : adjacencyList[currentNode]) { |
| 54 | + if (!visited[neighbor]) { |
| 55 | + s.push(neighbor); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +int main() { |
| 63 | + int totalNodes = 6; // Total number of nodes in the graph |
| 64 | + |
| 65 | + Graph g(totalNodes); // Create a graph object |
| 66 | + |
| 67 | + // Adding edges to the graph |
| 68 | + g.addEdge(0, 1); |
| 69 | + g.addEdge(0, 2); |
| 70 | + g.addEdge(1, 3); |
| 71 | + g.addEdge(1, 4); |
| 72 | + g.addEdge(2, 5); |
| 73 | + |
| 74 | + cout << "DFS Traversal (Recursive): "; |
| 75 | + vector<bool> visited(totalNodes, false); // Visited array to track nodes |
| 76 | + g.dfsRecursive(0, visited); // Perform DFS starting from node 0 |
| 77 | + cout << endl; |
| 78 | + |
| 79 | + cout << "DFS Traversal (Iterative): "; |
| 80 | + g.dfsIterative(0); // Perform iterative DFS starting from node 0 |
| 81 | + cout << endl; |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments