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

Assignment No:: Problem Statement:-Theory

The document describes a C program to perform depth-first search (DFS) traversal on a graph. It includes the problem statement, theory, algorithm, source code, input/output examples, and discussion. The algorithm uses an adjacency matrix and visited array to traverse the graph without repeating nodes. It calls a DFS function recursively to print each visited vertex and explore its neighbors.

Uploaded by

Souradeep Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Assignment No:: Problem Statement:-Theory

The document describes a C program to perform depth-first search (DFS) traversal on a graph. It includes the problem statement, theory, algorithm, source code, input/output examples, and discussion. The algorithm uses an adjacency matrix and visited array to traverse the graph without repeating nodes. It calls a DFS function recursively to print each visited vertex and explore its neighbors.

Uploaded by

Souradeep Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Page No.

 ASSIGNMENT NO:
 PROBLEM STATEMENT:- C program to find the DFS traversal of a
given graph.
 THEORY:- Depth First Traversal(or search) for a graph is similar to
Depth First Traversal of a tree . The only catch here is, unlike trees, graphs
may contain cycles, so we may come to the same node again. To avoid
processing a node more than once, we use a Boolean visited array.
For example, in the following graph, we start traversal from vertex 2. When we come
to vertex 0, we look for all adjacent vertices of it. 2 is also an adjacent vertex of 0. If
we don’t mark visited vertices, then 2 will be processed again and it will become a
non-terminating process. A Depth First Traversal of the following graph is 2, 0, 1, 3.

 ALGORITHM:-
Variable Name Type Purpose

G[][] int It indicates the adjacency


matrix of the graph.
visited[] int It checks whether a node is
visited or not.
i,j int It is used for iteration in
looping structure.
n int It takes the no of vertices of
the graph as input.

Method to call the function prototype- int DFS(int):-


STEP 1:- Input G[10][10],visited[10].
STEP 2:- Stop.
[End of function]
Start of main() function:-
STEP 1:- Input i,j,n

Date:
Page No.

STEP 2:- Print “enter the no of vertices”


STEP 3:- Read n
STEP 4:- Display “enter the adjacency matrix of the graph”
STEP 5:- Repeat through step 6 to step 9 for i=1 to n
STEP 6:- Repeat through step 7 to step 8 for j=1 to n
STEP 7:- Read G[i][j]
STEP 8:- Increment j by 1
[End of inner for loop]
STEP 9:- Increment i by 1
[End of outer for loop]
STEP 10:- Repeat through step 11 to step 14 for i=1 to n
STEP 11:- Set visited[i]=0
STEP 12:- Display “required DFS for above graph is”
STEP 13:- call the function DFS with return value 0
STEP 14:- Increment i by 1
[End of for loop]
STEP 15:- stop
Steps for calling the declared function-int DFS(int i):-
STEP 1:- Input j
STEP 2:- display “i”
STEP 3:- Set visited[i]=1
STEP 4:- Repeat through step 5 to step 7 for j=1 to n
STEP 5:- if(!visited[j]&&G[i][j]=1) then
STEP 6:- call the function DFS with argument j
STEP 7:- Increment j by 1
[End of for loop]
STEP 8:- stop.

 SOURCE CODE: -
#include<stdio.h>

Date:
Page No.

int DFS(int);
int G[10][10],visited[10],n;
int main()
{
int i,j,n;

printf("\n enter the no of veritces");


scanf("%d",&n);

printf("\n enter adjency matrix of the graph");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{

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

}
for(i=0;i<n;i++)
{
visited[i]=0;
printf("\n\n required DFS for above graph is");
DFS(0);
return 0;

}
}
int DFS(int i)

Date:
Page No.

{
int j;
printf("\n %d",i);
visited[i]=1;
for(j=0;j<n;i++)
{
if(!visited[j]&&G[i][j]==1)
DFS(j);

 INPUT & OUTPUT: -


Set 1:- Enter the no of vertices: 9
Enter elements for adjacency matrix of the graph: 0 1 1 1 1 0 0 0 0
100001000
100000000
100000100
100000000
010000010
000100001
000001000
000000100
Required DFS for above graph is
0
1
5

Date:
Page No.

7
2
3
6
8
4

Set 2:- Enter the no of vertices : 8


Enter elements for adjacency matrix of the graph:
01100000
10011000
10000110
01000001
01000001
00100001
00100001
00011110
Required DFS for above graph is-
1
2
4
8
5
6
3
7

 DISCUSSIONS: -
1. The above code traverses only the vertices reachable from a given source vertex. All
the vertices may not be reachable from a given vertex (example Disconnected
graph). To do complete DFS traversal of such graphs, we must call DFS() for every
vertex. 

Date:
Page No.

2. Also, before calling DFS(), we should check if it is already printed by some other call
of DFS(). Above implementation does the complete graph traversal even if the nodes
are unreachable.

3. Time Complexity of the above traversal is O(V+E) where V is number of vertices in


the graph and E is number of edges in the graph.

Date:

You might also like