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

DSAL6Word

The document contains C++ code for a Graph class that implements both Depth First Search (DFS) and Breadth First Search (BFS) algorithms. It initializes a graph with a specified number of vertices, allows adding edges, and performs traversals starting from a given vertex. The main function demonstrates the creation of a graph with edges and executes both traversal methods from vertex 2.

Uploaded by

Pragati Shirole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

DSAL6Word

The document contains C++ code for a Graph class that implements both Depth First Search (DFS) and Breadth First Search (BFS) algorithms. It initializes a graph with a specified number of vertices, allows adding edges, and performs traversals starting from a given vertex. The main function demonstrates the creation of a graph with edges and executes both traversal methods from vertex 2.

Uploaded by

Pragati Shirole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>

#include <list>

using namespace std;

class Graph {

int v;

list<int> *adjL;

int **adjM;

vector<bool> visited;

public:

Graph(int);

void addEdge(int, int);

void DFS(int);

void BFS(int);

};

Graph::Graph(int v) {

this->v = v;

adjL = new list<int>[v];

adjM = new int*[v];

visited.assign(v, false);

for (int row = 0; row < v; row++) {

adjM[row] = new int[v];

for (int column = 0; column < v; column++) {

adjM[row][column] = 0;

void Graph::addEdge(int x, int y) {

adjL[x].push_back(y);

adjL[y].push_back(x);

adjM[x][y] = 1;
adjM[y][x] = 1;

void Graph::DFS(int start) {

cout << start << " ";

visited[start] = true;

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

if (adjM[start][i] == 1 && (!visited[i])) {

DFS(i);

void Graph::BFS(int start) {

visited.assign(v, false);

list<int> queue;

visited[start] = true;

queue.push_back(start);

while(!queue.empty()) {

start = queue.front();

cout << start << " ";

queue.pop_front();

for (int i : adjL[start]) {

if (!visited[i]) {

visited[i] = true;

queue.push_back(i);

int main() {

int v = 8;

Graph G(v);
int edges[][2] = {{0, 1}, {0, 6}, {0, 5}, {1, 2}, {1, 6}, {2, 3}, {2, 4},

{2, 6}, {2, 7}, {3, 4}, {3, 7}, {4, 5}, {4, 6}, {4, 7}, {5, 6}};

for (auto edge : edges) {

G.addEdge(edge[0], edge[1]);

cout << "\nOperation on following Graph ->\n\n\n";

cout << "\n\n Depth First Traversal (starting from vertex 2) : ";

G.DFS(2);

cout << "\n\nBreadth First Traversal (starting from vertex 2) : ";

G.BFS(2);

return 0;

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