0% found this document useful (0 votes)
10 views7 pages

Ex4 Adsa

The document provides source code implementations for Breadth-First Traversal (BFT) and Depth-First Traversal (DFT) of a graph represented using both an adjacency matrix and an adjacency list. It includes two classes, GraphMatrix and Graph, each containing methods to add edges and perform BFS and DFS. Example usage of these classes is demonstrated in the main function with sample graphs.

Uploaded by

codingclub52
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)
10 views7 pages

Ex4 Adsa

The document provides source code implementations for Breadth-First Traversal (BFT) and Depth-First Traversal (DFT) of a graph represented using both an adjacency matrix and an adjacency list. It includes two classes, GraphMatrix and Graph, each containing methods to add edges and perform BFS and DFS. Example usage of these classes is demonstrated in the main function with sample graphs.

Uploaded by

codingclub52
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/ 7

4.

Implement BFT and DFT for given graph,when graph is represented by

a)adjacency matrix b)adjacency list

4.a) SOURCE CODE:

#include <iostream>

#include <vector>

#include <queue>

#include <stack>

using namespace std;

class GraphMatrix {

vector<vector<int>> adjMatrix;

int numVertices;

public:

GraphMatrix(int vertices);

void addEdge(int src, int dest);

void BFS(int start);

void DFS(int start);

private:

void DFSUtil(int vertex, vector<bool>& visited);

};

GraphMatrix::GraphMatrix(int vertices) : numVertices(vertices) {

adjMatrix.resize(vertices, vector<int>(vertices, 0));

void GraphMatrix::addEdge(int src, int dest) {


adjMatrix[src][dest] = 1;

adjMatrix[dest][src] = 1; // For undirected graph

void GraphMatrix::BFS(int start) {

vector<bool> visited(numVertices, false);

queue<int> q;

visited[start] = true;

q.push(start);

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

while (!q.empty()) {

int vertex = q.front();

q.pop();

cout << vertex << " ";

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

if (adjMatrix[vertex][i] == 1 && !visited[i]) {

visited[i] = true;

q.push(i);

cout << endl;

void GraphMatrix::DFS(int start) {

vector<bool> visited(numVertices, false);

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

cout << endl;

}
void GraphMatrix::DFSUtil(int vertex, vector<bool>& visited) {

visited[vertex] = true;

cout << vertex << " ";


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

if (adjMatrix[vertex][i] == 1 && !visited[i]) {

DFSUtil(i, visited);

int main() {

cout << "Graph Representation using Adjacency Matrix:\n";

GraphMatrix graph(5); // Example with 5 vertices

graph.addEdge(0, 1);

graph.addEdge(0, 4);

graph.addEdge(1, 2);

graph.addEdge(1, 3);

graph.addEdge(2, 3);

graph.addEdge(3, 4);

graph.BFS(0); // Example BFS

graph.DFS(0); // Example DFS


return 0;

Output:

4.b) SOURCE CODE:

#include <iostream>

#include <vector>

#include <list>

#include <queue>

#include <stack>

#include <unordered_set>

using namespace std;

// Graph class using adjacency list representation

class Graph {

private:

vector<list<int>>adjList; // adjacency list

int numVertices; // number of vertices

public:

// Constructor

Graph(int vertices) : numVertices(vertices) {

adjList.resize(vertices);

}
// Add an edge to the graph

void addEdge(int u, int v) {

adjList[u].push_back(v);

adjList[v].push_back(u); // For undirected graph

// Breadth-First Traversal

void breadthFirstTraversal(int start) {

vector<bool>visited(numVertices, false);

queue<int> q;

visited[start] = true;

q.push(start);

while (!q.empty()) {

int node = q.front();

q.pop();

cout<< node << " ";

for (int neighbor :adjList[node]) {

if (!visited[neighbor]) {

visited[neighbor] = true;

q.push(neighbor);

cout<<endl;

}
// Depth-First Traversal

void depthFirstTraversal(int start) {

vector<bool>visited(numVertices, false);

stack<int> s;

s.push(start);

while (!s.empty()) {

int node = s.top();

s.pop();

if (!visited[node]) {

visited[node] = true;

cout<< node << " ";

// Push all unvisited neighbors onto the stack

for (auto it = adjList[node].rbegin(); it != adjList[node].rend(); ++it) {

if (!visited[*it]) {

s.push(*it);

cout<<endl;

};

int main() {
// Create a graph with 4 vertices (0, 1, 2, 3)

Graph g(4);

// Add edges

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(1, 2);

g.addEdge(1, 3);

g.addEdge(2, 3);

cout<< "Breadth-First Traversal (starting from vertex 0): ";

g.breadthFirstTraversal(0);

cout<< "Depth-First Traversal (starting from vertex 0): ";

g.depthFirstTraversal(0);

return 0;

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy