0% found this document useful (0 votes)
11 views2 pages

DFS

Uploaded by

sharshsharma462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

DFS

Uploaded by

sharshsharma462
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <stack>

using namespace std;

const int MAX_VERTICES = 6; // Maximum number of vertices in the graph

// Function to add an edge to the graph (adjacency matrix representation)


void addEdge(int adj[][MAX_VERTICES], int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1; // Comment this line if the graph is directed
}

// DFS function using an explicit stack


void DFS(int adj[][MAX_VERTICES], int start) {
bool visited[MAX_VERTICES] = {false}; // Array to keep track of visited nodes
stack<int> s;

// Start the DFS from the starting node


s.push(start);
visited[start] = true;

while (!s.empty()) {
int node = s.top();
s.pop();
cout << node << " "; // Process the node

// Visit all unvisited adjacent vertices, pushing them to the stack


for (int i = MAX_VERTICES - 1; i >= 0; i--) { // Reverse order for
consistent DFS order
if (adj[node][i] == 1 && !visited[i]) {
s.push(i);
visited[i] = true;
}
}
}
}

int main() {
int adj[MAX_VERTICES][MAX_VERTICES] = {0}; // Adjacency matrix initialized to 0

// Adding edges to the graph


addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 5);

int startNode = 0;
cout << "DFS starting from node " << startNode << ": ";
DFS(adj, startNode);

return 0;
}
// code with recursion

#include <iostream>
using namespace std;

const int MAX_VERTICES = 6; // Maximum number of vertices in the graph

// Function to add an edge to the graph (adjacency matrix representation)


void addEdge(int adj[][MAX_VERTICES], int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1; // Comment this line if the graph is directed
}

// DFS helper function


void DFSUtil(int adj[][MAX_VERTICES], int node, bool visited[]) {
visited[node] = true; // Mark the node as visited
cout << node << " "; // Process the node

// Recursively visit all unvisited adjacent vertices


for (int i = 0; i < MAX_VERTICES; i++) {
if (adj[node][i] == 1 && !visited[i]) {
DFSUtil(adj, i, visited);
}
}
}

// 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

// Adding edges to the graph


addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 5);

int startNode = 0;
cout << "DFS starting from node " << startNode << ": ";
DFS(adj, startNode);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy