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

DS Graph Assignment: DFS Program

The document describes code for implementing depth-first search (DFS) and breadth-first search (BFS) algorithms on graphs. It defines a Graph class with methods to add edges and perform DFS and BFS. The DFS method recursively visits unvisited neighbors, printing each vertex. The BFS method uses a queue to iteratively visit neighbors of the start vertex, printing visited vertices. Main tests the code by running DFS and BFS on a sample graph.

Uploaded by

Abhishek Singhal
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)
50 views6 pages

DS Graph Assignment: DFS Program

The document describes code for implementing depth-first search (DFS) and breadth-first search (BFS) algorithms on graphs. It defines a Graph class with methods to add edges and perform DFS and BFS. The DFS method recursively visits unvisited neighbors, printing each vertex. The BFS method uses a queue to iteratively visit neighbors of the start vertex, printing visited vertices. Main tests the code by running DFS and BFS on a sample graph.

Uploaded by

Abhishek Singhal
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

Sahil Pewekar 20070122175 CS C2

DS Graph Assignment
DFS Program –

#include <iostream>
#include <list>
using namespace std;

class Graph {
int numVertices;
list<int> *adjLists;
bool *visited;

public:
Graph(int V);
void addEdge(int src, int dest);
void DFS(int vertex);
};

Graph::Graph(int vertices) {
numVertices = vertices;
adjLists = new list<int>[vertices];
visited = new bool[vertices];
}
Sahil Pewekar 20070122175 CS C2

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


adjLists[src].push_front(dest);
}

void Graph::DFS(int vertex) {


visited[vertex] = true;
list<int> adjList = adjLists[vertex];

cout << vertex << " ";

list<int>::iterator i;
for (i = adjList.begin(); i != adjList.end(); ++i)
if (!visited[*i])
DFS(*i);
}

int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
Sahil Pewekar 20070122175 CS C2

g.DFS(2);

return 0;
}

OUTPUT –

BFS Program

#include <iostream>
#include <list>

using namespace std;

class Graph {
int numVertices;
list<int>* adjLists;
bool* visited;
Sahil Pewekar 20070122175 CS C2

public:
Graph(int vertices);
void addEdge(int src, int dest);
void BFS(int startVertex);
};

Graph::Graph(int vertices) {
numVertices = vertices;
adjLists = new list<int>[vertices];
}

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


adjLists[src].push_back(dest);
adjLists[dest].push_back(src);
}

void Graph::BFS(int startVertex) {


visited = new bool[numVertices];
for (int i = 0; i < numVertices; i++)
visited[i] = false;

list<int> queue;

visited[startVertex] = true;
Sahil Pewekar 20070122175 CS C2

queue.push_back(startVertex);

list<int>::iterator i;

while (!queue.empty()) {
int currVertex = queue.front();
cout << "Visited " << currVertex << " ";
queue.pop_front();

for (i = adjLists[currVertex].begin(); i != adjLists[currVertex].end(); ++i) {


int adjVertex = *i;
if (!visited[adjVertex]) {
visited[adjVertex] = true;
queue.push_back(adjVertex);
}
}
}
}

int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
Sahil Pewekar 20070122175 CS C2

g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

g.BFS(2);

return 0;
}

OUTPUT –

You might also like