AI_Assignment1
AI_Assignment1
&
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;
public:
Graph(int v) : vertices(v) {
adj_matrix.resize(v, vector<int>(v, 0));
}
q.push(start);
visited[start] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
result.push_back(v);
int main() {
int vertices, edges;
cout << "Enter number of vertices: ";
cin >> vertices;
cout << "Enter number of edges: ";
cin >> edges;
Graph graph(vertices);
int start_vertex;
cout << "Enter starting vertex for traversal: ";
cin >> start_vertex;
graph.dfs(start_vertex);
graph.bfs(start_vertex);
return 0;
}
Output :