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

ds10

Uploaded by

buktarshubham
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)
5 views

ds10

Uploaded by

buktarshubham
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/ 8

Vidyavardhini’s College of Engineering & Technology

K.T. MARG, VASAI ROAD (WEST)


Department of Computer Science and Engineering(Data Science)

EXPERIMENT ASSESSMENT

ACADEMIC YEAR 2024-25

Course: Data Structures Lab

Course code: CSL301

Year: SE SEM: III


Experiment No. 10

AIM:- Implementation of Graph traversal techniques - Breadth First


Search and Depth First Search
Name: Shubham Buktar

Roll Number: 19

Date of Performance: 8/10/2024

Date of Submission:18/10/2024

Evaluation
Performance Indicator Max. Marks Marks Obtained
Performance 5
Understanding 5
Journal work and timely submission. 10
Total 20

Performance Indicator Exceed Expectations (EE) Meet Expectations (ME) Below Expectations (BE)
Performance 5 3 2
Understanding 5 3 2
Journal work and timely
10 8 4
submission.

Checked by

Name of Faculty : Mrs. Maya Varghese


Signature :
Date :
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

Experiment No. 10: Depth First Search and Breath First Search
Aim : Implementation of DFS and BFS traversal of graph.
Objective:
1. Understand the Graph data structure and its basic operations.
2. Understand the method of representing a graph.
3. Understand the method of constructing the Graph ADT and defining its operations
Theory:
A graph is a collection of nodes or vertices, connected in pairs by lines referred to as
edges. A graph can be directed or undirected.
One method of traversing through nodes is depth first search. Here we traverse from
the starting node and proceed from top to bottom. At a moment we reach a dead end from
where the further movement is not possible and we backtrack and then proceed according to
left right order. A stack is used to keep track of a visited node which helps in backtracking.

DFS Traversal –0 1 2 3 4
Algorithm
Algorithm: DFS_LL(V)
Input: V is a starting vertex
Output : A list VISIT giving order of visited vertices during traversal.
Description: linked structure of graph with gptr as pointer
1. if gptr = NULL then
print “Graph is empty” exit
2. u=v
3. OPEN.PUSH(u)
4. while OPEN.TOP !=NULL do
u=OPEN.POP()
if search(VISIT,u) = FALSE then
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

INSERT_END(VISIT,u)
Ptr = gptr(u)
While ptr.LINK != NULL do
Vptr = ptr.LINK
OPEN.PUSH(vptr.LABEL)
End while
End if
End while
5. Return VISIT

6. Stop

BFS Traversal

BFS Traversal – 0 1 4 2 3
Algorithm
Algorithm: DFS()
i=0
count=1
visited[i]=1
print("Visited vertex i")

repeat this till queue is empty or all nodes visited


repeat this for all nodes from first till last
if(g[i][j]!=0&&visited[j]!=1)
{
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

push(j)
}
i=pop()
print("Visited vertex i")
visited[i]=1
count++
Algorithm: BFS()
i=0
count=1
visited[i]=1
print("Visited vertex i")

repeat this till queue is empty or all nodes visited


repeat this for all nodes from first till last
if(g[i][j]!=0&&visited[j]!=1)
{
enqueue(j)
}

i=dequeue()
print("Visited vertex i")
visited[i]=1
count++

Code:

BREADTH FIRST SEARCH

#include <stdio.h>

#define MAX 10
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

void breadth_first_search(int adj[][MAX], int visited[], int start) {

int queue[MAX], rear = -1, front = -1, i;

queue[++rear] = start; // Enqueue the starting node

visited[start] = 1; // Mark the starting node as visited

while (rear != front) {

start = queue[++front]; // Dequeue the front element

// Display the node, adjusting for the ASCII representation of letters

printf("%c \t", start + 65);

for (i = 0; i < MAX; i++) {

// If there is an edge and the node is not visited

if (adj[start][i] == 1 && visited[i] == 0) {

queue[++rear] = i; // Enqueue the node

visited[i] = 1; // Mark the node as visited

int main() {

int visited[MAX] = {0}; // Visited array initialized to 0

int adj[MAX][MAX], i, j;
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

// Input adjacency matrix

printf("\nEnter the adjacency matrix: \n");

for (i = 0; i < MAX; i++) {

for (j = 0; j < MAX; j++) {

scanf("%d", &adj[i][j]);

// Perform BFS starting from node 0 (which corresponds to 'A')

breadth_first_search(adj, visited, 0);

return 0;

DEPTH FIRST SEARCH

Output:
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

Conclusion:

BREADTH FIRST SEARCH

This corrected C program performs a breadth-first search (BFS) on a graph


represented by an adjacency matrix. It starts from a given node and prints each visited
node using letters ('A' to 'J'). The queue is used to explore nodes level by level, ensuring
each node is visited only once.

DEPTH FIRST SEARCH

The corrected C program performs a depth-first search (DFS) on a graph represented


by an adjacency matrix. Starting from a specified node, it explores as far as possible
along each branch before backtracking. The program prints the traversal order using
letters ('A' to 'E'), representing each node, and efficiently manages the exploration using
a stack.

Write the graph representation used by your program and explain why you choose that.

Write the applications of BFS and DFS other than finding connected nodes and explain how
it is attained?
Vidyavardhini’s College of Engineering & Technology
K.T. MARG, VASAI ROAD (WEST)
Department of Computer Science and Engineering(Data Science)

You might also like