My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.

Monday, 15 August 2011

Decision Of Graph

/* Graph */
# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>
# include <process.h>
# include <alloc.h>
# define MAX 50
static int mat[MAX][MAX],que[MAX],visit[MAX];
int stk[MAX];
int top=-1,rear=-1,front=-1;
int vert;
void push(int ele)
{ if(top==MAX)
{ printf("\nstack is full cannot push into stack"); }
else { top++; stk[top]=ele; }
}
int pop()
{ int ele;
if(top<=-1)
{ printf("\nstack is empty cannot pop from stack"); }
else { ele=stk[top]; top--; }
return(ele);
}
void addq(int ele)
{ if(rear==MAX)
{ printf("\nqueue is full.cannot add in queue"); }
else
{ que[++rear]=ele; }
}
int delq()
{ int ele;
if(front==rear)
{ printf("\nqueue is empty cannot delete from stack");
return(rear);
}
else
{ ele=que[++front]; }
return(ele);
}
void print()
{ int i,j;
printf("\nThe adjacency matrix is::\n");
for(i=0;i<vert;i++)
{ for(j=0;j<vert;j++)
{ printf(" %d",mat[i][j]); }
}
}
 
 
 
 
 
 
void create()
{ int i,j,ans,direct;
printf("\nEnter:: 1: for directed matrix\n\t0:for non-directed:: ");
scanf("%d",&direct);
printf("\nEnter the no. of vertices::");
scanf("%d",&vert);
for(i=0;i<vert;i++)
{ while(1)
{ printf("\n\nVERTEX %d",i);
printf("\nEnter 1 if there is any adjacent vertex for %d:: ",i);
scanf("%d",&ans);
if(ans!=1)
{ break; }
else { printf("\nEnter the adjacent vertex:: ");
scanf("%d",&j);
mat[i][j]=1;
if(direct==0)
{ mat[j][i]=1; }
}
}
}
}
void bfs()
{ int start,ele,j,flag=-1;
printf("\nEnter the vertex from which you want to start:: ");
scanf("%d",&start);
addq(start);
visit[start]=1;
while(front!=rear)
{ ele=delq();
printf("\n%d ",ele);
for(j=0;j<vert;j++)
{ if(mat[ele][j]==1 && visit[j]!=1)
{ addq(j); visit[j]=1; }
}
}
for(j=0;j<vert;j++)
{ if(visit[j]!=1) { flag=j;}
}
if(flag!=-1)
{ printf("\nThe graph is not a connected graph!"); }
}
void main()
{ create();
print();
bfs();
getch();
}

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls