• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Breadth First and Depth First Search C Language

April 5, 2011 by ProjectsGeek Leave a Comment

Breadth First and Depth First Search C Language Graphs

Write a Breadth First and Depth First Search program to Represent a given graph using adjacency list and perform DFS and BFS .Use the map of the area around the college as the graph.

 Breadth First and Depth First Search Code

 #include  
 #include  
 #include  
 #include  
 #define MAX 20  
 typedef struct graph  
 {  
 char vertex[MAX];  
 struct graph *link;  
 }graph;  
 void breadth(graph *,int ,int );  
 graph *list(int);  
 void depth(graph *,int ,int *,int);  
 graph *freelist(graph *,int);  
 void displaylist(graph *,int);  
 void main()  
 {  
 int ch,*visited,s,n,i,n1,j;  
 graph *g=NULL;  
 char str2[MAX],*str;  
 do  
 {  
 clrscr();  
 printf("\n\t\tGRAPH\n\t\t2.ADJANCY LIST\n\t\t3.DISPLAY OF ADJ MAT\n\t\t4.DISPLAY OF ADJ LIST\n\t\t5.DST OF ADJ LIST\n\t\t6.BST OF ADJ LIST\n\t\t9.EXIT");  
 printf("\n\t\tENTER UR CHOICE:");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 2:  
 if(g!=NULL)  
 g=freelist(g,n);  
 printf("ENTER NO OF NODES");  
 scanf("%d",&n);  
 printf("\n\t\tADJANCY LIST");  
 g=list(n);  
 break;  
 case 4:  
 printf("DISPLAY OF ADJ LIST");  
 break;  
 case 5:  
 if(g==NULL)  
 printf("ENTER ADJENCY LIST FIRST");  
 else  
 {  
 printf("\n\t\tDST OF ADJ LIST");  
 while(1)  
 {  
 printf("\n\t\tENTER SOURCE");  
 j=0;  
 flushall();  
 gets(str2);  
 while(1)  
 {  
 if(strcmp(str2,(g+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(j<n) <br=""> break;  
 }  
 visited=(int *)calloc(1,sizeof(int)*n);  
 depth(g,j,visited,n);  
 printf("ISOLATED VERTEX");  
 for(i=0;i<n;i++) <br=""> if(*(visited+i)==0)  
 printf("\n\t%s",(g+i)->vertex);  
 free(visited);  
 }  
 getch();  
 break;  
 case 6:  
 if(g==NULL)  
 printf("ENTER ADJENCY LIST FIRST");  
 else  
 {  
 printf("\n\t\tBST OF ADJ LIST");  
 while(1)  
 {  
 printf("\n\t\tENTER SOURCE");  
 j=0;  
 flushall();  
 gets(str2);  
 while(1)  
 {  
 if(strcmp(str2,(g+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(j<n) <br=""> break;  
 }  
 breadth(g,j,n);  
 }  
 getch();  
 break;  
 case 9:  
 break;  
 }  
 }while(ch!=9);  
 }  
 void depth(graph *f,int i,int *visited,int n)  
 {  
 graph *t;  
 int j=0;  
 printf("\t%s",(f+i)->vertex);  
 *(visited+i)=1;  
 t=(f+i)->link;  
 while(t!=NULL)  
 {  
 j=0;  
 while(1)  
 {  
 if(strcmp(t->vertex,(f+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(*(visited+j)!=1)  
 depth(f,j,visited,n);  
 t=t->link;  
 }  
 }  
 graph *freelist(graph *g,int n)  
 {  
 int i;  
 graph *q,*p,*k;  
 for(i=0;i<n;i++) <br=""> {  
 k=(g+i);  
 //PRINTF("\n%d",k->vertex);  
 q=k->link;  
 while(q!=NULL)  
 {  
 //printf("\t%d",q->link);  
 p=q; //(remove at d tym of display)  
 q=q->link;  
 free(p);//(remove at d tym of display)  
 }  
 }  
 p=g;  
 free(p);  
 return NULL;  
 }  
 void breadth(graph *f,int i,int n)  
 {  
 graph *queue[MAX],*q,*t;  
 int *visited,front=0,rear=0,j=0;  
 visited=(int *)calloc(1,sizeof(int)*n);  
 queue[rear]=(f+i);  
 *(visited+i)=1;  
 //q=f+i;  
 while(front!=-1)  
 {  
 q=queue[front];  
 j=0;  
 while(1)  
 {  
 if(strcmp(q->vertex,(f+j)->vertex)==0)  
 break;  
 j++;  
 }  
 q=f+j;  
 if(front==rear)  
 front=rear=-1;  
 else  
 front=(front+1)%MAX;  
 printf("\t%s",q->vertex);  
 t=q->link;  
 while(t!=NULL)  
 {  
 j=0;  
 while(1)  
 {  
 if(strcmp(t->vertex,(f+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(*(visited+j)==0)  
 {  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t;  
 *(visited+j)=1;  
 }  
 t=t->link;  
 }  
 }  
 printf("ISOLATED VERTEX");  
 for(j=0;j<n;j++) <br=""> if(*(visited+j)==0)  
 printf("\n\t%s",(f+j)->vertex);  
 getch();  
 free(visited);  
 }  
 graph * list(int n)  
 {  
 graph *g=NULL,*p,*q,*nw;  
 char so[MAX],de[MAX];  
 int s,i,d;  
 g=(graph *)malloc(sizeof(graph)*n);  
 for(i=0;i<n;i++) <br=""> {  
 printf("ENTER DATA");  
 flushall();  
 gets(so);  
 strcpy((g+i)->vertex,so);  
 (g+i)->link=NULL;  
 }  
 while(1)  
 {  
 printf("\n\t\tIS THERE ANY EDGES:y/n");  
 if(getche()=='n')  
 break;  
 printf("ENTER SOURCE & DESTINATION");  
 flushall();  
 gets(so);  
 flushall();  
 gets(de);  
 for(s=0;s<n;s++) <br=""> if(strcmp((g+s)->vertex,so)==0)  
 break;  
 for(d=0;d<n;d++) <br=""> if(strcmp((g+d)->vertex,de)==0)  
 break;  
 if(s==n||strcmp(so,de)==0||d==n)  
 printf("INVALID");  
 else  
 {  
 p=(g+s);  
 q=p->link;  
 while(q!=NULL)  
 {  
 if(strcmp(q->vertex,de)==0)  
 {  
 printf("ALREADY PRESENT");  
 break;  
 }  
 p=q;  
 q=q->link;  
 }  
 if(q==NULL)  
 {  
 nw=(graph *)malloc(sizeof(graph));  
 nw->link=NULL;  
 strcpy(nw->vertex,de);  
 p->link=nw;  
 p=g+d;  
 while(p->link!=NULL)  
 p=p->link;  
 nw=(graph *)malloc(sizeof(graph));  
 nw->link=NULL;  
 strcpy(nw->vertex,so);  
 p->link=nw;  
 }  
 }  
 }  
 return g;  
 }

 

 

Other Projects to Try:

  1. BFS AND DFS Algorithm using C Language
  2. Prims algorithm Code in C Language
  3. Queue Operations using C Language
  4. Hash table code in C Language
  5. Hoffmans algorithm in C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy