DFS
DFS
#include <stack>
while (!s.empty()) {
int node = s.top();
s.pop();
cout << node << " "; // Process the node
int main() {
int adj[MAX_VERTICES][MAX_VERTICES] = {0}; // Adjacency matrix initialized to 0
int startNode = 0;
cout << "DFS starting from node " << startNode << ": ";
DFS(adj, startNode);
return 0;
}
// code with recursion
#include <iostream>
using namespace std;
// DFS function
void DFS(int adj[][MAX_VERTICES], int start) {
bool visited[MAX_VERTICES] = {false}; // Array to keep track of visited nodes
DFSUtil(adj, start, visited); // Start DFS traversal from the starting node
}
int main() {
int adj[MAX_VERTICES][MAX_VERTICES] = {0}; // Adjacency matrix initialized to 0
int startNode = 0;
cout << "DFS starting from node " << startNode << ": ";
DFS(adj, startNode);
return 0;
}