0% found this document useful (0 votes)
17 views

BFS and DFS

The document provides implementations and algorithms for Breadth-First Search (BFS) and Depth-First Search (DFS) in C++. It includes code examples for both algorithms, detailing how to initialize data structures, process nodes, and traverse a graph represented as an adjacency list. The BFS algorithm uses a queue while the DFS algorithm uses a stack for traversal.

Uploaded by

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

BFS and DFS

The document provides implementations and algorithms for Breadth-First Search (BFS) and Depth-First Search (DFS) in C++. It includes code examples for both algorithms, detailing how to initialize data structures, process nodes, and traverse a graph represented as an adjacency list. The BFS algorithm uses a queue while the DFS algorithm uses a stack for traversal.

Uploaded by

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

BFS::::

#include <iostream>

#include <vector>

#include <queue>

using namespace std;

// Function to perform BFS on a graph

void bfs(int start, const vector<vector<int>>& adjList) {

vector<bool> visited(adjList.size(), false); // Keep track of visited nodes

queue<int> q; // Queue for BFS traversal

visited[start] = true;

q.push(start);

cout << "BFS Traversal: ";

while (!q.empty()) {

int node = q.front();

q.pop();

cout << node << " ";

// Traverse all adjacent nodes

for (int neighbor : adjList[node]) {

if (!visited[neighbor]) {

visited[neighbor] = true;

q.push(neighbor);

cout << endl;

int main() {

int vertices, edges;

cout << "Enter the number of vertices and edges: ";


cin >> vertices >> edges;

vector<vector<int>> adjList(vertices);

cout << "Enter the edges (u v):" << endl;

for (int i = 0; i < edges; ++i) {

int u, v;

cin >> u >> v;

adjList[u].push_back(v);

adjList[v].push_back(u); // Comment this line for a directed graph

int start;

cout << "Enter the starting node: ";

cin >> start;

bfs(start, adjList);

return 0;

BFS Algorithm

Input:

 A graph GGG represented as an adjacency list or adjacency matrix.

 A starting vertex sss.

Output:

 A sequence of nodes in BFS traversal order.

1. Initialize Data Structures:

o Create a queue to manage nodes for processing.

o Create a visited list or array to track visited nodes. Initialize all entries to false.

2. Start with the Source Node:

o Mark the starting node sss as visited: visited[s] = true.

o Enqueue sss into the queue.


3. Process the Queue:

o While the queue is not empty:

1. Dequeue a node uuu from the front of the queue.

2. Process uuu (e.g., print it, store it in a result list, etc.).

3. For each unvisited neighbor vvv of uuu:

 Mark vvv as visited: visited[v] = true.

 Enqueue vvv into the queue.

4. Repeat:

o Continue until all reachable nodes from sss have been visited.

5. End:

o The traversal is complete. If desired, repeat the process for other components of the graph (if
disconnected).

DFS::::::

#include <iostream>

#include <vector>

#include <stack>

using namespace std;

// Function to perform DFS iteratively using a stack

void dfs(int start, const vector<vector<int>>& adjList) {

vector<bool> visited(adjList.size(), false);

stack<int> s;

s.push(start);

visited[start] = true;

cout << "DFS Traversal: ";

while (!s.empty()) {

int node = s.top();

s.pop();

cout << node << " ";


for (int neighbor : adjList[node]) {

if (!visited[neighbor]) {

visited[neighbor] = true;

s.push(neighbor);

cout << endl;

int main() {

int vertices, edges;

cout << "Enter the number of vertices and edges: ";

cin >> vertices >> edges;

vector<vector<int>> adjList(vertices);

cout << "Enter the edges (u v):" << endl;

for (int i = 0; i < edges; ++i) {

int u, v;

cin >> u >> v;

adjList[u].push_back(v);

adjList[v].push_back(u); // For an undirected graph

int start;

cout << "Enter the starting node: ";

cin >> start;

dfs(start, adjList);

return 0;

}
DFS Algorithm

Algorithm for DFS

1. Initialize:

o Mark all nodes as unvisited (using a boolean array visited).

o Create a stack or use the recursive call stack for traversal.

2. Start DFS from the Source Node:

o Mark the source node as visited.

o Process the node (e.g., print it).

3. Traverse Neighbors:

o For each unvisited neighbor of the current node:

 In recursive DFS, call the DFS function with the neighbor.

 In iterative DFS, push the neighbor onto the stack.

4. Continue Until All Connected Nodes are Visited:

o Continue the process until all nodes in the stack or call stack have been exhausted.

You might also like