/*==========================================================================================
**
** File Name : dfs.c
** Creation Date : Fri 13 Oct 2017 02:58:26 PM IST
** Last Modified : Fri 13 Oct 2017 07:54:15 PM IST
** Compiler : gcc
** Author : Manoj Kumar Patra, Asst. Professor
** Organization : Vignan University, Guntur, India.
**
**=========================================================================================*/
#include<stdio.h>
void DepthFirstSearch(int);
int graph[100][100],visited[100],n;
void main()
{
int i,j;
printf("\n= = = = = = = Depth First Search = = = = = = = = = =\n");
printf("Enter total number of vertices in the graph: ");
scanf("%d",&n);
printf("\nEnter adjecency matrix of the graph:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&graph[i][j]);
}
printf("\n");
}
printf("\nThe adjecency matrix of the graph is:\n");
printf("= = = = = = = = = = = = = = = = = = = =\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",graph[i][j]);
}
printf("\n");
}
printf("= = = = = = = = = = = = = = = = = = = =\n");
//initially mark all verteces as unvisited
for(i=0;i<n;i++)
visited[i]=0;
printf("\nThe Visited Sequence is: ");
DepthFirstSearch(0);
printf("\n\n");
}
void DepthFirstSearch(int i)
{
int j;
printf("V%d\t",i);
visited[i]=1;
for(j=0;j<n;j++)
{
if(visited[j]==0 && graph[i][j]==1)
{
DepthFirstSearch(j);
}
}
}
OutPut:
manoj@ubuntu:~$ gcc dfs.c -o dfs
manoj@ubuntu:~$ ./dfs
= = = = = = = Depth First Search = = = = = = = = = =
Enter total number of vertices in the graph: 5
Enter adjecency matrix of the graph:
0
1
1
0
0
1
0
0
1
1
1
0
0
1
0
0
1
1
0
0
0
1
0
1
0
The adjecency matrix of the graph is:
= = = = = = = = = = = = = = = = = = = =
0 1 1 0 0
1 0 0 1 1
1 0 0 1 0
0 1 1 0 0
0 1 0 1 0
= = = = = = = = = = = = = = = = = = = =
The Visited Sequence is: V0 V1 V3 V2 V4
manoj@ubuntu:~$
**
** File Name : dfs.c
** Creation Date : Fri 13 Oct 2017 02:58:26 PM IST
** Last Modified : Fri 13 Oct 2017 07:54:15 PM IST
** Compiler : gcc
** Author : Manoj Kumar Patra, Asst. Professor
** Organization : Vignan University, Guntur, India.
**
**=========================================================================================*/
#include<stdio.h>
void DepthFirstSearch(int);
int graph[100][100],visited[100],n;
void main()
{
int i,j;
printf("\n= = = = = = = Depth First Search = = = = = = = = = =\n");
printf("Enter total number of vertices in the graph: ");
scanf("%d",&n);
printf("\nEnter adjecency matrix of the graph:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&graph[i][j]);
}
printf("\n");
}
printf("\nThe adjecency matrix of the graph is:\n");
printf("= = = = = = = = = = = = = = = = = = = =\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",graph[i][j]);
}
printf("\n");
}
printf("= = = = = = = = = = = = = = = = = = = =\n");
//initially mark all verteces as unvisited
for(i=0;i<n;i++)
visited[i]=0;
printf("\nThe Visited Sequence is: ");
DepthFirstSearch(0);
printf("\n\n");
}
void DepthFirstSearch(int i)
{
int j;
printf("V%d\t",i);
visited[i]=1;
for(j=0;j<n;j++)
{
if(visited[j]==0 && graph[i][j]==1)
{
DepthFirstSearch(j);
}
}
}
OutPut:
manoj@ubuntu:~$ gcc dfs.c -o dfs
manoj@ubuntu:~$ ./dfs
= = = = = = = Depth First Search = = = = = = = = = =
Enter total number of vertices in the graph: 5
Enter adjecency matrix of the graph:
0
1
1
0
0
1
0
0
1
1
1
0
0
1
0
0
1
1
0
0
0
1
0
1
0
The adjecency matrix of the graph is:
= = = = = = = = = = = = = = = = = = = =
0 1 1 0 0
1 0 0 1 1
1 0 0 1 0
0 1 1 0 0
0 1 0 1 0
= = = = = = = = = = = = = = = = = = = =
The Visited Sequence is: V0 V1 V3 V2 V4
manoj@ubuntu:~$
No comments:
Post a Comment