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

PR 6 C-13

The document contains C++ code for implementing Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms using both adjacency matrix and adjacency list representations. It initializes the necessary data structures, takes user input for the number of landmarks and roads, and performs the traversals starting from specified landmarks. The code includes functions for both DFS and BFS, along with the display of the adjacency matrix.

Uploaded by

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

PR 6 C-13

The document contains C++ code for implementing Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms using both adjacency matrix and adjacency list representations. It initializes the necessary data structures, takes user input for the number of landmarks and roads, and performs the traversals starting from specified landmarks. The code includes functions for both DFS and BFS, along with the display of the adjacency matrix.

Uploaded by

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

#include <iostream> visited_bfs[start] = 1;

#include <queue> q.push(start);

#include <stack> cout << start << " ";

using namespace std;

while (!q.empty()) {

int node = q.front();

// Declare adjacency matrix and adjacency list q.pop();

int cost[MAX][MAX], visited[MAX], visit[MAX],


n, m;
// Visit all adjacent nodes (neighbors in the
int adj_list[MAX][MAX], visited_bfs[MAX]; adjacency list)

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

// Function for DFS using adjacency matrix if (adj_list[node][i] == 1 &&


!visited_bfs[i]) {
void dfs_matrix(int node) {
visited_bfs[i] = 1;
cout << node << " ";
q.push(i);
visited[node] = 1;
cout << i << " ";

}
for (int i = 0; i < n; i++) {
}
if (cost[node][i] == 1 && !visited[i]) {
}
dfs_matrix(i);
}
}

}
int main() {
}
cout << "Enter number of landmarks
(vertices): ";
// Function for BFS using adjacency list cin >> n;
void bfs_list(int start) { cout << "Enter number of roads (edges): ";
queue<int> q; cin >> m;
// Display adjacency matrix for DFS

// Initialize the adjacency matrix and cout << "\nAdjacency Matrix (for DFS):\n";
adjacency list
for (int i = 0; i < n; i++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int j = 0; j < n; j++) {
cout << cost[i][j] << " ";
cost[i][j] = 0;
}
adj_list[i][j] = 0; // adjacency list
cout << endl;
}
}
visited[i] = 0;

visited_bfs[i] = 0;
// Perform DFS
visit[i] = 0;
int start_dfs;
}
cout << "\nEnter the starting landmark for
DFS: ";

// Input roads (edges) and fill the adjacency cin >> start_dfs;
matrix and adjacency list
cout << "\nDFS traversal from landmark " <<
cout << "\nEnter the roads (pairs of start_dfs << ":\n";
landmarks connected by roads):\n";
dfs_matrix(start_dfs);
for (int k = 0; k < m; k++) {
cout << endl;
int i, j;

cin >> i >> j;


// Perform BFS
cost[i][j] = 1; // Adjacency matrix for DFS
int start_bfs;
cost[j][i] = 1; // Undirected graph
cout << "\nEnter the starting landmark for
BFS: ";

adj_list[i][j] = 1; // Adjacency list for BFS cin >> start_bfs;

adj_list[j][i] = 1; // Undirected graph cout << "\nBFS traversal from landmark " <<
start_bfs << ":\n";
}
bfs_list(start_bfs);
cout << endl;

return 0;

You might also like