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

AI_Assignment1

The document presents an assignment on implementing Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph using an adjacency matrix. It includes a C++ code implementation that defines a Graph class with methods for adding edges and performing both DFS and BFS traversals. The main function allows user input for the number of vertices, edges, and the starting vertex for the traversals.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

AI_Assignment1

The document presents an assignment on implementing Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms for a graph using an adjacency matrix. It includes a C++ code implementation that defines a Graph class with methods for adding edges and performing both DFS and BFS traversals. The main function allows user input for the number of vertices, edges, and the starting vertex for the traversals.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ArtificiAl intelligence

&
Soft computing

ASSignment

nit KuruKSHetrA

NAME : NASIB
ROLL NO : 123103058
BRANCH : IT – A
SECTION : 04
Problem : Implementation of Depth-First Search (DFS) and Breadth-First
Search (BFS) for a graph using an adjacency matrix representation.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Graph {
private:
int vertices;
vector<vector<int>> adj_matrix;

void dfs_util(int v, vector<bool>& visited, vector<int>& result) {


visited[v] = true;
result.push_back(v);

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


if (adj_matrix[v][i] == 1 && !visited[i]) {
dfs_util(i, visited, result);
}
}
}

public:
Graph(int v) : vertices(v) {
adj_matrix.resize(v, vector<int>(v, 0));
}

void add_edge(int u, int v) {


adj_matrix[u][v] = 1;
adj_matrix[v][u] = 1; // Since the graph is undirected
}

void dfs(int start) {


vector<bool> visited(vertices, false);
vector<int> result;
dfs_util(start, visited, result);

cout << "DFS Traversal: ";


for (int v : result) {
cout << v << " -> ";
}
cout << "END" << endl;
}

void bfs(int start) {


vector<bool> visited(vertices, false);
queue<int> q;
vector<int> result;

q.push(start);
visited[start] = true;

while (!q.empty()) {
int v = q.front();
q.pop();
result.push_back(v);

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


if (adj_matrix[v][i] == 1 && !visited[i]) {
q.push(i);
visited[i] = true;
}
}
}

cout << "BFS Traversal: ";


for (int v : result) {
cout << v << " -> ";
}
cout << "END" << endl;
}
};

int main() {
int vertices, edges;
cout << "Enter number of vertices: ";
cin >> vertices;
cout << "Enter number of edges: ";
cin >> edges;

Graph graph(vertices);

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


for (int i = 0; i < edges; i++) {
int u, v;
cin >> u >> v;
graph.add_edge(u, v);
}

int start_vertex;
cout << "Enter starting vertex for traversal: ";
cin >> start_vertex;
graph.dfs(start_vertex);
graph.bfs(start_vertex);

return 0;
}

Output :

You might also like