ADSA Experiment - No3.1
ADSA Experiment - No3.1
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
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;
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;
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:
visited[u] := true;
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
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.