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

ADSA Experiment - No3.1

The document describes algorithms and code implementations for breadth-first search (BFS) and depth-first search (DFS) on graphs. It includes: 1) Pseudocode and explanations of the BFS and DFS algorithms. 2) Analysis of the time and space complexity of BFS and DFS as O(V+E) and O(V) respectively. 3) C++ code implementations of BFS and DFS using an adjacency list representation of graphs. 4) Screenshots of sample outputs from the BFS and DFS code on a test graph.

Uploaded by

js9118164
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)
12 views

ADSA Experiment - No3.1

The document describes algorithms and code implementations for breadth-first search (BFS) and depth-first search (DFS) on graphs. It includes: 1) Pseudocode and explanations of the BFS and DFS algorithms. 2) Analysis of the time and space complexity of BFS and DFS as O(V+E) and O(V) respectively. 3) C++ code implementations of BFS and DFS using an adjacency list representation of graphs. 4) Screenshots of sample outputs from the BFS and DFS code on a test graph.

Uploaded by

js9118164
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/ 9

Course Name: ADSA Lab Course code: 23CSH-622

Experiment No. –3.1

Aim: Write a program to traverse a graph using:


a. Breadth First Search Algorithm.
b. Depth First Search Algorithm.

A. Write a program to traverse a graph using Breadth First Search Algorithm.

1. Problem Description:
Writing a program that traverses a graph using the Breadth-First Search (BFS) algorithm. BFS is a graph
traversal algorithm that explores a graph level by level, visiting all the neighbors of a node before moving
on to the next level.

2. Algorithm:
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
Step 6: EXIT

3.Complexity Analysis:
Time Complexity: O(V+E) is the time complexity of the Breadth first search algorithm.

Space Complexity: O(V) is the space complexity of the Breadth first search algorithm.

4.Pseudo Code :
procedure BFS(graph, start_node):
queue = empty queue
visited = set()
enqueue(queue, start_node)
add start_node to visited set
while queue is not empty:
current_node = dequeue(queue)
print(current_node)
for neighbor in graph.adjacent_nodes(current_node):
if neighbor not in visited:
enqueue(queue, neighbor)
add neighbor to visited set

NAME: Harsh Singh UID: 23MAI10028


Course Name: ADSA Lab Course code: 23CSH-622

5.Source Code:
// C++ code to print BFS traversal from a given
// source vertex
#include <bits/stdc++.h>
using namespace std;
// This class represents a directed graph using
// adjacency list representation
class Graph {
// No. of vertices
int V;

// Pointer to an array containing adjacency lists


vector<list<int> > adj;

public:
// Constructor
Graph(int V);
// Function to add an edge to graph
void addEdge(int v, int w);
// Prints BFS traversal from a given source s
void BFS(int s);
};
Graph::Graph(int V)
{
this->V = V;
adj.resize(V);
}
void Graph::addEdge(int v, int w)
{
// Add w to v’s list.
adj[v].push_back(w);
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
vector<bool> visited;

NAME: Harsh Singh UID: 23MAI10028


Course Name: ADSA Lab Course code: 23CSH-622
visited.resize(V, false);
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
while (!queue.empty()) {
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s.
// If an adjacent has not been visited,
// then mark it visited and enqueue it
for (auto adjacent : adj[s]) {
if (!visited[adjacent]) {
visited[adjacent] = true;
queue.push_back(adjacent);
}}}}
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);

NAME: Harsh Singh UID: 23MAI10028


Course Name: ADSA Lab Course code: 23CSH-622
g.addEdge(3, 3);
cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.BFS(2);
return 0;
}
6.Screenshot of Outputs:

7.Learning Outcomes:

In this we learn about how to implement a program to implement Breadth First Search algorithm along
with its complexity analysis.
B. Write a program to traverse a graph using Depth First Search Algorithm.

1.Problem Description: Write a program that traverses a graph using the Depth-First Search (DFS)
algorithm. DFS is a graph traversal algorithm that explores as far as possible along each branch before
backtracking.

2.Algorithm:
Step 1: SET STATUS = 1 (ready state) for each node in G
Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state)
Step 3: Repeat Steps 4 and 5 until STACK is empty
Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state)
Step 5: Push on the stack all the neighbours of N that are in the ready state (whose STATUS = 1) and set their
STATUS = 2 (waiting state)
[END OF LOOP]
Step 6: EXIT
NAME: Harsh Singh UID: 23MAI10028
Course Name: ADSA Lab Course code: 23CSH-622
3.Complexity Analysis:
Time Complexity: O(V+E) is the time complexity of the Depth first search algorithm.
Space Complexity: O(V) is the space complexity of the Depth first search algorithm.

4.Pseudo Code:

DFS(G,v) ( v is the vertex where the search starts )


Stack S := {}; ( start with an empty stack )
for each vertex u, set visited[u] := false;
push S, v;
while (S is not empty) do
u := pop S;

if (not visited[u]) then

visited[u] := true;

for each unvisited neighbour w of uu

push S, w;
end if
end while
END DFS()

5.Source Code(C/C++):
// C++ program to print DFS traversal from
// a given vertex in a given graph
#include <bits/stdc++.h>
using namespace std;
// Graph class represents a directed graph
// using adjacency list representation

NAME: Harsh Singh UID: 23MAI10028


Course Name: ADSA Lab Course code: 23CSH-622
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
// Function to add an edge to graph
void addEdge(int v, int w);
// DFS traversal of the vertices
// reachable from v
void DFS(int v);
};
void Graph::addEdge(int v, int w)

// Add w to v’s list.

adj[v].push_back(w);

void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
NAME: Harsh Singh UID: 23MAI10028
Course Name: ADSA Lab Course code: 23CSH-622
if (!visited[*i])
DFS(*i);
}
// Driver code
int main()
{
// Create a graph given in the above diagram
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";
// Function call

g.DFS(2);
return 0;
}

6.Screenshot of Outputs:

7.Learning Outcomes
NAME: Harsh Singh UID: 23MAI10028
Course Name: ADSA Lab Course code: 23CSH-622
In this we learn about how to implement a program to implement Depth first search algorithm along with
its complexity analysis.

NAME: Harsh Singh UID: 23MAI10028


Course Name: ADSA Lab Course code: 23CSH-622

NAME: Harsh Singh UID: 23MAI10028

You might also like