0% found this document useful (0 votes)
26 views6 pages

DK 10

The document describes implementing breadth-first search (BFS) and depth-first search (DFS) algorithms on a graph. It includes code for a Graph class with methods to add edges to the adjacency matrix representation of a graph and perform BFS and DFS. The main function takes input to build the graph and calls the BFS and DFS methods.

Uploaded by

Payal Kori
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)
26 views6 pages

DK 10

The document describes implementing breadth-first search (BFS) and depth-first search (DFS) algorithms on a graph. It includes code for a Graph class with methods to add edges to the adjacency matrix representation of a graph and perform BFS and DFS. The main function takes input to build the graph and calls the BFS and DFS methods.

Uploaded by

Payal Kori
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/ 6

BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST

PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Data Structures
Course Name:_________________________ BH.USCSP304
Course Number: __________
Practical Number: _______ Page Number: _______

Aim:

Teacher’s Signature : ____________________________


SYCS13 PRACTICAL -10 DATE-__________

AIM:
Implement BFS and DFS in a graph.
CODE:
#include <iostream>
using namespace std;

class Graph {
private:
int** adjMatrix;
int vertices;

void dfsUtil(int vertex, bool* visited) {


cout << vertex << " ";
visited[vertex] = true;
for (int i = 0; i < vertices; i++) {
if (adjMatrix[vertex][i] && !visited[i]) {
dfsUtil(i, visited);
}
}
}

public:
Graph(int V) {
vertices = V;
adjMatrix = new int*[vertices];
for (int i = 0; i < vertices; i++) {
adjMatrix[i] = new int[vertices];
for (int j = 0; j < vertices; j++) {
adjMatrix[i][j] = 0;
}
}
}

void addEdge(int start, int end) {


if (start >= 0 && start < vertices && end >= 0 && end < vertices) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;
}
}

void bfs(int start) {


bool* visited = new bool[vertices];
for (int i = 0; i < vertices; i++) {
visited[i] = false;
}

visited[start] = true;
Page 1 of 3
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Data Structures
Course Name:_________________________ BH.USCSP304
Course Number: __________
Practical Number: _______ Page Number: _______

Aim:

Teacher’s Signature : ____________________________


SYCS13 PRACTICAL -10 DATE-__________

cout << "BFS starting from vertex " << start << ": ";
cout << start << " ";

int queue[vertices];
int front = 0, rear = 0;
queue[rear++] = start;

while (front != rear) {


int current = queue[front++];
for (int i = 0; i < vertices; i++) {
if (adjMatrix[current][i] && !visited[i]) {
cout << i << " ";
visited[i] = true;
queue[rear++] = i;
}
}
}

delete[] visited;
cout << endl;
}

void dfs(int start) {


bool* visited = new bool[vertices];
for (int i = 0; i < vertices; i++) {
visited[i] = false;
}

cout << "DFS starting from vertex " << start << ": ";
dfsUtil(start, visited);
cout << endl;

delete[] visited;
}

~Graph() {
for (int i = 0; i < vertices; i++) {
delete[] adjMatrix[i];
}
delete[] adjMatrix;
}
};

int main() {
int V, E;
cout << "Enter the number of vertices: ";

Page 2 of 3
BHAVANS COLLEGE AUTONOMOUS, ANDHERI WEST
PRACTICAL JOURNAL
SYCS
Class: _____________ 3
Sem: _____ Date of Performance: ___________
Data Structures
Course Name:_________________________ BH.USCSP304
Course Number: __________
Practical Number: _______ Page Number: _______

Aim:

Teacher’s Signature : ____________________________


SYCS13 PRACTICAL -10 DATE-__________

cin >> V;
cout << "Enter the number of edges: ";
cin >> E;

Graph g(V);

cout << "Enter " << E << " edges in the format (start end):" << endl;
for (int i = 0; i < E; i++) {
int start, end;
cin >> start >> end;
g.addEdge(start, end);
}

int startVertex;
cout << "Enter the starting vertex for BFS and DFS: ";
cin >> startVertex;

g.bfs(startVertex);
g.dfs(startVertex);

return 0;
}

OUTPUT:

Page 3 of 3

You might also like