0% found this document useful (0 votes)
8 views4 pages

Dsa 7 Aliya

The document presents a C++ program that implements Depth First Search (DFS) and Breadth First Search (BFS) algorithms using an adjacency matrix to represent a graph of cities. It prompts the user to input the number of cities, their names, and the distances between them, then displays the adjacency matrix and performs both search algorithms starting from a user-defined vertex. The program outputs the order of cities visited in both DFS and BFS traversals.

Uploaded by

Sadiya Sayed
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)
8 views4 pages

Dsa 7 Aliya

The document presents a C++ program that implements Depth First Search (DFS) and Breadth First Search (BFS) algorithms using an adjacency matrix to represent a graph of cities. It prompts the user to input the number of cities, their names, and the distances between them, then displays the adjacency matrix and performs both search algorithms starting from a user-defined vertex. The program outputs the order of cities visited in both DFS and BFS traversals.

Uploaded by

Sadiya Sayed
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/ 4

Experiment No:7

Aliya Sayyed

SE(A)

Roll NO:4

Code:

#include <iostream>

#include <queue>

using namespace std;

int adj_mat[50][50] = {0}; // Adjacency matrix

int visited[50] = {0}; // For DFS

void dfs(int s, int n, string arr[]) {

visited[s] = 1;

cout << arr[s] << " ";

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

if (adj_mat[s][i] && !visited[i])

dfs(i, n, arr);

void bfs(int n, int s, string arr[]) {

bool bfs_visited[50] = {false};

queue<int> bfsq;

cout << arr[s] << " ";

bfs_visited[s] = true;

bfsq.push(s);
while (!bfsq.empty()) {

int v = bfsq.front();

bfsq.pop();

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

if (adj_mat[v][i] && !bfs_visited[i]) {

cout << arr[i] << " ";

bfs_visited[i] = true;

bfsq.push(i);

int main() {

int n, u;

cout << "Enter the number of cities: ";

cin >> n;

string cities[50];

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

cout << "Enter city #" << i << " (Airport code): ";

cin >> cities[i];

cout << "\nYour cities are:\n";

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

cout << " City #" << i << ": " << cities[i] << endl;

// Input adjacency matrix (distances)

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

for (int j = i + 1; j < n; j++) {


cout << "Enter distance between " << cities[i] << " and " << cities[j] << ": ";

cin >> adj_mat[i][j];

adj_mat[j][i] = adj_mat[i][j];

// Display adjacency matrix

cout << "\nAdjacency Matrix:\n\t";

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

cout << cities[i] << "\t";

cout << endl;

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

cout << cities[i] << "\t";

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

cout << adj_mat[i][j] << "\t";

cout << endl;

cout << "Enter the starting vertex (index): ";

cin >> u;

cout << "\nDFS: ";

dfs(u, n, cities);

cout << "\nBFS: ";

bfs(n, u, cities);

cout << endl;

return 0;

You might also like