Add to Favorites    Make Home Page 1937 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » To find the BFS and DFS of the given graph

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title To find the BFS and DFS of the given graph
Author Anirudha Dhaneshwar
Author Email and_ya [at] hotmail.com
Description To find the BFS and DFS of the given graph

Category C++ » Data Structures
Hits 388379
Code Select and Copy the Code
/*TO FIND THE BFS AND DFS OF THE GIVEN GRAPH*/ #include<stdio.h> #include<conio.h> #define size 20 int a[10][10],vertex[10],n,e; /*STACK FUNCTIONS*/ #define bottom -1 int stack[size],top=bottom; int stackempty() { return(top=bottom) ? 1:0; } int stackfull() { return(top==size-1) ? 1:0; } void push(int item) { if(stackfull()) printf("7 STACK IS FULL"); else stack[++top]=item; } int pop() { if(stackempty()) { printf("7 STACK IS EMPTY"); return -1; } else return stack[top--]; } int peep() { if(stackempty()) { printf("7 STACK IS EMPTY"); return -1; } else return stack[top]; } /* QUEUE FUNCTIONS */ #define start -1 int q[size]; int f=start,r=start; int qempty(){ return(f==r)?1:0; } int qfull(){ return(r==size-1)?1:0;} void addq(int c) { if(qfull()) printf("7 QUEUE IS FULL"); else q[++r]=c; } int delq() { if(qempty()) { printf("7 QUEUE IS EMPTY"); return -1; } else return q[++f]; } // j is unvisited adjecent vertex to i int adjvertex(int i) { int j; for(j=0;j<n;j++) if(a[i][j]==1&&vertex[j]==0) return j; return n; } int visitall() { int i; for(i=0;i<n;i++) if(vertex[i]==0) return 0; return 1; } /*FUNCTION FOR BFS*/ void bfs() { int i,j,k,cur=0;//current vertex is startting vertex for(i=0;i<n;i++) vertex[i]=0;//not visited printf(" BFS path => V%d ",cur+1); addq(cur); vertex[cur]=1;//marking visited vertex while(!visitall()) { if(qempty()) { printf("7 GRAPH IS DISCONNECTED"); break; } cur=delq(); //visit all vertices which are adjecent to current vertex for(j=0;j<n;j++) { //adjecent are not visited if(a[cur][j]==1&&vertex[j]==0) { printf("V%d ",j+1); addq(j); //marking visited vertex vertex[j]=1; } } } } /*FUNCTION FOR DFS*/ void dfs() { int i,j,k,cur=0;//current vertex is startting vertex for(i=0;i<n;i++) vertex[i]=0;//not visited printf(" DFS path => V%d ",cur+1); push(cur); vertex[cur]=1;//marking visited vertex while(!visitall()) { do { cur=adjvertex(peep()); if(cur==n) pop(); } while(cur==n&&!stackempty()); if(stackempty()) { printf("7 GRAPH IS DISCONNECTED"); break; } if(cur!=n) { printf(" V%d ",cur+1); push(cur); vertex[cur]=1;//marking visited vertex } } } /*MAIN PROGRAM*/ void main() { int i,j,k; clrscr(); for(i=0;i<10;i++) for(j=0;j<10;j++) a[i][j]=0; printf(" ENTER NO OF VERTICES & EDGES OF UNDIRECTED GRAPH : "); scanf("%d%d",&n,&e); printf(" ENTER EDGES AS PAIR OF VERTICES "); for(k=1;k<=e;k++) { printf("EDGE %d =>",k); scanf("%d%d",&i,&j); //for undirected graph a[i-1][j-1]=1; } dfs(); bfs(); getch(); }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=951


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review