0% found this document useful (0 votes)
5 views3 pages

Dsal6 Print

The document outlines a practical exercise for representing a graph using an adjacency matrix and list to perform Depth-First Search (DFS) and Breadth-First Search (BFS). It includes a C++ program that prompts the user for the number of vertices and edges, initializes the adjacency matrix, and implements BFS and DFS algorithms. The program is designed to analyze a graph based on landmarks around a college.

Uploaded by

rohitgagare50
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)
5 views3 pages

Dsal6 Print

The document outlines a practical exercise for representing a graph using an adjacency matrix and list to perform Depth-First Search (DFS) and Breadth-First Search (BFS). It includes a C++ program that prompts the user for the number of vertices and edges, initializes the adjacency matrix, and implements BFS and DFS algorithms. The program is designed to analyze a graph based on landmarks around a college.

Uploaded by

rohitgagare50
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

Practical No:6

Name: KUNDAN DHOKE Date:


Roll No: 20
Practical :
Represent a given graph using adjacency matrix/list to perform DFS and using adjacency list to
perform BFS .Use the map of the area around the college as the graph.identify prominent land
marks as nodes and DFS and BFS on that.
Program:

#include <iostream>
#include <stdlib.h>
using namespace std;
int cost[10][10], i, j, k, n, qu[10], front = 0, rear = 0, v;
int visit[10] = {0}, visited[10] = {0};
int stk[10], top = -1, visit1[10] = {0}, visited1[10] = {0};

int main() {
int m;

cout << "Enter number of vertices: ";


cin >> n;

cout << "Enter number of edges: ";


cin >> m;

// Initializing adjacency matrix to 0


for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cost[i][j] = 0;

cout << "\nEnter edges (format: u v):\n";


for (k = 0; k < m; k++) {
cin >> i >> j;
cost[i][j] = 1;
cost[j][i] = 1;
}

// Printing adjacency matrix


cout << "The adjacency matrix of the graph is:\n";
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << cost[i][j] << " ";
cout << endl;
}

// *BFS Implementation*
cout << "Enter initial vertex for BFS: ";
cin >> v;
cout << "The BFS of the Graph is: ";

visited[v] = 1;
qu[rear++] = v;

while (front < rear) {


v = qu[front++];
cout << v << " ";
for (j = 0; j < n; j++) {
if (cost[v][j] != 0 && visited[j] == 0) {
visited[j] = 1;
qu[rear++] = j;
}
}
}
cout << endl;

// *DFS Implementation*
cout << "Enter initial vertex for DFS: ";
cin >> v;
cout << "The DFS of the Graph is: ";
visited1[v] = 1;
stk[++top] = v;

while (top != -1) {


v = stk[top--];
cout << v << " ";

for (j = n - 1; j >= 0; j--) {


if (cost[v][j] != 0 && visited1[j] == 0) {
visited1[j] = 1;
stk[++top] = j;
}
}
}
cout << endl;
return 0;
}
OUTPUT:

You might also like