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

Home » C++ Home » Data Structures Home » Graph Traversal,DFS and BFS

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

Search Projects & Source Codes:

Title Graph Traversal,DFS and BFS
Author Arun Vishnu
Author Email arun_vishnu [at] hotmail.com
Description Program to create a graph and use Deapth First Search(DFS) and Breadth First Search(BFS) Traversal.
Category C++ » Data Structures
Hits 430990
Code Select and Copy the Code
#include<conio.h> #include<iostream.h> #include<stdlib.h> void create(); // For creating a graph void dfs(); // For Deapth First Search(DFS) Traversal. void bfs(); // For Breadth First Search(BFS) Traversal. struct node // Structure for elements in the graph { int data,status; struct node *next; struct link *adj; }; struct link // Structure for adjacency list { struct node *next; struct link *adj; }; struct node *start,*p,*q; struct link *l,*k; int main() { int choice; clrscr(); create(); while(1) { cout<<" -----------------------"; cout<<" 1: DFS 2: BSF 3: Exit Enter your choice: "; cin>>choice; switch(choice) { case 1: dfs(); break; case 2: bfs(); break; case 3: exit(0); break; default: cout<<" Incorrect choice! Re-enter your choice."; getch(); } } return 0; } void create() // Creating a graph { int dat,flag=0; start=NULL; cout<<" Enter the nodes in the graph(0 to end): "; while(1) { cin>>dat; if(dat==0) break; p=new node; p->data=dat; p->status=0; p->next=NULL; p->adj=NULL; if(flag==0) { start=p; q=p; flag++; } else { q->next=p; q=p; } } p=start; while(p!=NULL) { cout<<"Enter the links to "<<p->data<<" (0 to end) : "; flag=0; while(1) { cin>>dat; if(dat==0) break; k=new link; k->adj=NULL; if(flag==0) { p->adj=k; l=k; flag++; } else { l->adj=k; l=k; } q=start; while(q!=NULL) { if(q->data==dat) k->next=q; q=q->next; } } p=p->next; } cout<<" -------------------------"; return; } // Deapth First Search(DFS) Traversal. void bfs() { int qu[20],i=1,j=0; p=start; while(p!=NULL) { p->status=0; p=p->next; } p=start; qu[0]=p->data; p->status=1; while(1) { if(qu[j]==0) break; p=start; while(p!=NULL) { if(p->data==qu[j]) break; p=p->next; } k=p->adj; while(k!=NULL) { q=k->next; if(q->status==0) { qu[i]=q->data; q->status=1; qu[i+1]=0; i++; } k=k->adj; } j++; } j=0; cout<<" Breadth First Search Results "; cout<<" --------------------------- "; while(qu[j]!=0) { cout<<qu[j]<<" "; j++; } getch(); return; } // Breadth First Search(BFS) Traversal. void dfs() { int stack[25],top=1; cout<<" Deapth First Search Results "; cout<<" --------------------------- "; p=start; while(p!=NULL) { p->status=0; p=p->next; } p=start; stack[0]=0; stack[top]=p->data; p->status=1; while(1) { if(stack[top]==0) break; p=start; while(p!=NULL) { if(p->data==stack[top]) break; p=p->next; } cout<<stack[top]<<" "; top--; k=p->adj; while(k!=NULL) { q=k->next; if(q->status==0) { top++; stack[top]=q->data; q->status=1; } k=k->adj; } } getch(); return; }

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=834


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