0th Element of Arrays Left Uninitialized Only 19 Useable: Depth First Search
0th Element of Arrays Left Uninitialized Only 19 Useable: Depth First Search
501254
I.T. III
DEPTH FIRST SEARCH
#include <stdio.h>
#include <stdlib.h>
#define MAX 20//0th element of arrays left uninitialized only 19
useable
int n, top = -1, ajMat[MAX][MAX], vis[MAX],sumvisit;
char source, b[MAX], stack[ MAX ];
void dfs (char source, int n);
void push (int item);
char pop (void);
int main()
{
int i, j, choice;
char v;
printf("Enter the number of nodes in graph [MAX=19]: ");
scanf("%d",&n);
fflush(stdin);
printf("Enter the value of node of graph (don't press Enter)\n");
for(i=1;i<=n;i++)
{
printf("%c ",b[i]=getch());
}
printf("\n\n");
printf("Enter Adjancency matrix for graph\n");
printf("If there exits an edge between two vertices then type 'y'
otherwise 'n'\n");
printf(" ");
for(i=1; i<=n; i++)
printf("%c ",b[i]);
for(i=1;i<=n; i++)
{
printf("\n%c ",b[i]);
for(j=1;j<=n;j++)
{
printf("%c ",v=getch());
if(v=='y'||v=='Y')
ajMat[i][j]=1;
if(v=='n'||v=='N')
ajMat[i][j]=0;
}
}
do
{//Initially all vertices are unvisited
for (i=1;i<=n;i++ )
vis[i]=0;
printf( "\n_______________" );
printf( "\nMENU" );
printf( "\n(1)DFS" );
printf( "\n(2)EXIT" );
printf( "\nENTER YOUR CHOICE: " );
scanf( "%d", &choice );
fflush(stdin);
switch ( choice )
{
case 1:
printf( "ENTER THE SOURCE VERTEX: " );
scanf( "%c", &source );
fflush(stdin);
sumvisit=0;
printf("\nDepth First Search for given Graph:\n");
dfs( source, n );
break;
case 2:
printf( "PROGRAM WILL EXIT___" );
exit(0);
default:
printf("Incorrect Input. Try Again!");
}
}while ((choice!=2));
return (0);
}
void dfs (char source, int n)
{
int x=0, y=0, i=0;
char pT;
while (b[x]!=source)
{
// printf("%c not@%d\n", source, x);
x++;
if(b[x]==source)
{
// printf( "\n%c located at %d.\n", b[x],x );
if((b[x]==source) && (vis[x]==0))
{
push(x);
vis[x]=1;
printf( "%c ", b[x] );
y=x;
}
else if((b[x]==source) && (vis[x]==1))
{
y=x;
goto round;
}
round:
for (i=1;i<=n;i++)
if ((ajMat[y][i]!=0) && (vis[i]==0))
{
// printf("y=%d and i=%d", y, i);
push(i);
vis[i]=1;
// printf( "\n%c@%d visited through source %c@%d
.\n",b[i],i, b[y],y );
printf( "%c ", b[i] );
if (y!=i)
y=i;
goto round;
}
}
}
pT=pop();
// if (pT!=NULL)
// printf("\n\n%c POPED\n",pT);
// printf("\n%c is at top now and %c at
bottom\n",stack[top],stack[0]);
if (pT!=NULL)
dfs(stack[top], n);
else
{
for (i=1;i<=n;i++)
{
if ((vis[i]==0) && (pT==NULL) && sumvisit!=0)
dfs(b[i], n);
}
}
}
void push (int i)
{
if (top==MAX)//dont use top==MAX-1
printf( "Stack overflow " );
else
{
top=top+1;
stack[top]=b[i];
sumvisit=sumvisit+1;
}
}
char pop (void)
{
char pT;
if (top==-1)
return NULL;
else
{
pT=stack[top];
top=top-1;
return (pT);
}
OUTPUT
Enter the number of nodes in graph [MAX=19]: 9
Enter the value of node of graph (don't press Enter)
a b c d e f g h i
Enter Adjancency matrix for graph
If there exits an edge between two vertices then type 'y' otherwise 'n'
a b c d e f g h i
a n y n n y n n n n
b y n y y n n n n n
c n y n n n n y n n
d n y n n y y n n n
e y n n y n n n n n
f n n n y n n n n y
g n n y n n n n y y
h n n n n n n y n n
i n n n n n y y n n
_______________
MENU
(1)DFS
(2)EXIT
ENTER YOUR CHOICE: 1
ENTER THE SOURCE VERTEX: a
Depth First Search for given Graph:
a b c g h i f d e
_______________
MENU
(1)DFS
(2)EXIT
ENTER YOUR CHOICE: 1
ENTER THE SOURCE VERTEX: i
Depth First Search for given Graph:
i f d b a e c g h
_______________
MENU
(1)DFS
(2)EXIT
ENTER YOUR CHOICE: 2
PROGRAM WILL EXIT___