You are on page 1of 5

BFS & DFS

Breadth First Search #include <stdio.h> #define N 10

Page 1 of 5

void bfs(int adj[][N],int visited[],int start) { int q[N],rear=-1,front=-1,i; q[++rear]=start; visited[start]=1; while(rear != front) { start = q[++front]; if(start==9) printf("10\t"); else printf("%c \t",start+49); //change to 65 in case of alphabets for(i=0;i<N;i++) { if(adj[start][i] && !visited[i]) { q[++rear]=i; visited[i]=1; } }

} }

int main() { int visited[N]={0}; int adj[N][N]={{0,1,1,0,0,0,0,0,0,1}, {0,0,0,0,1,0,0,0,0,1}, {0,0,0,0,1,0,1,0,0,0}, {1,0,1,0,0,1,1,0,0,1}, {0,0,0,0,0,0,1,1,0,0}, {0,0,0,1,0,0,0,1,0,0}, {0,0,0,0,0,0,0,1,1,1}, {0,0,1,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0,0}, {0,0,1,0,0,0,0,1,1,0}}; bfs(adj,visited,0); return 0; }

0. visited[start]=1.0.1. visited[i]=1.0. } int main() { Page 2 of 5 int adj[MAX][MAX]={{0.0.0). } } if(i==MAX) top--.0}.{0.{0.h> #define MAX 5 int dfs(int adj[][MAX]. while(top!=-1) { start=stack[top].0}.0. int visited[MAX]={0}.{0.i<MAX. int top=-1.{0.1.0.0.1.1}.0.int visited[].0}}.i. printf("\n"). break.1. } return 0.1.0. for(i=0. printf("%c-". printf("%c-". dfs(adj.i+65).1}. } return 0.BFS & DFS Depth First Search #include<stdio.0. stack[++top]=start.i++) { if(adj[start][i]&&visited[i]==0) { stack[++top]=i. printf("DFS Traversal : ").visited.start+65). .int start) { int stack[MAX].

getch( ) . v1 -> next = v2 = getnode_write ( 6 ) . front = rear = -1 . v ) . *v4 . void addqueue ( int ) . i < MAX . printf ( "%d\t".BFS & DFS Page 3 of 5 BFS – LIST /*Program that implements breadth first search algorithm*/ #include <stdio. visited[v . i++ ) del ( arr[i] ) .1] = TRUE . int visited[MAX] . arr[1] = v1 . v1 = getnode_write ( 2 ) . v1 = getnode_write ( 1 ) . bfs ( 1. int front. int isempty( ) . struct node **p ) { struct node *u . rear . v1 -> next = v2 = getnode_write ( 8 ) . v4 -> next = NULL . arr[3] = v1 . u=*(p+v-1). v1 = getnode_write ( 2 ) . v2 -> next = v3 = getnode_write ( 6 ) . v2 -> next = v3 = getnode_write ( 5 ) .h> #include <alloc. v1 = getnode_write ( 2 ) . for ( i = 0 . void del ( struct node * ) . v2 -> next = NULL . v1 -> next = v2 = getnode_write ( 4 ) . v1 -> next = v2 = getnode_write ( 8 ) . arr[5] = v1 . void main( ) { struct node *arr[MAX] . v2 -> next = NULL . v3 -> next = NULL . v1 = getnode_write ( 1 ) . while ( u != NULL ) . v3 -> next = v4 = getnode_write ( 7 ) . v1 = getnode_write ( 4 ) . struct node * getnode_write ( int ) . struct node ** ) . struct node *v1. int i . int q[8] . v1 -> next = v2 = getnode_write ( 8 ) . v1 -> next = v2 = getnode_write ( 8 ) . void bfs ( int. arr[6] = v1 .h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data . arr[0] = v1 . v2 -> next = NULL . struct node *next . v2 -> next = NULL . v1 = getnode_write ( 3 ) . arr[2] = v1 . clrscr( ) . v1 -> next = v2 = getnode_write ( 5 ) . v1 = getnode_write ( 3 ) . while ( isempty( ) == FALSE ) { v = deletequeue( ) . v3 -> next = NULL . } void bfs ( int v. v2 -> next = v3 = getnode_write ( 7 ) . arr ) .h> #include <conio. *v2. arr[4] = v1 . arr[7] = v1 . int deletequeue( ) . *v3. }. v1 -> next = v2 = getnode_write ( 3 ) . v2 -> next = NULL . addqueue ( v ) .

" ) .1 ) { printf ( "\nQueue Overflow. n = temp . } } DFS. if ( front == -1 ) { printf ( "\nQueue Underflow. printf("\n\n\nLEVEL ORDER TRAVERSAL\n\n"). add_node(-20). add_node(int value). q[rear] = vertex . exit( ) . mynode *root. add_node(5). return data . newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) . struct node *left. Page 4 of 5 void levelOrderTraversal(mynode *root). return FALSE . struct node *right. } } } struct node * getnode_write ( int val ) { struct node *newnode ." ) . if ( front == rear ) front = rear = -1 .1 ] == FALSE ) { addqueue ( u -> data ) .h> typedef struct node { int value. } int isempty( ) { if ( front == -1 ) return TRUE . u -> data ) . add_node(67). int main(int argc. } rear++ . add_node(23). exit( ) . } void del ( struct node *n ) { struct node *temp . } u = u -> next . } mynode.LIST #include <stdio. } int deletequeue( ) { int data . add_node(100).1 ] = TRUE . free ( n ) . } data = q[front] .BFS & DFS { if ( visited [ u -> data . levelOrderTraversal(root). } void addqueue ( int vertex ) { if ( rear == MAX . else front++ . return newnode . if ( front == -1 ) front = 0 . printf ( "%d\t". add_node(13). add_node(1). getch(). char* argv[]) { root = NULL. visited [ u -> data . newnode -> data = val . . while ( n != NULL ) { temp = n -> next .

if(root->left) { queue[size++] = root->left. return.. while(root) { printf("[%d] ". *temp. if(root == NULL) { printf("\nCreating the root.\n"). } } if(value < prev->value) { prev->left = temp. temp = malloc(sizeof(mynode)). temp->right = NULL. while(cur != NULL) { prev = cur. void levelOrderTraversal(mynode *root) { mynode *queue[100] = {(mynode *)0}. *cur. } else { prev->right = temp. } } // Level order traversal. int queue_pointer = 0.. temp->left = NULL.. add_node(int value) { mynode *prev. } } .BFS & DFS } // Function to add a new node. // Important to initialize! int size = 0. cur = root. } if(root->right) { queue[size++] = root->right.. root->value). } else { cur = cur->right. temp->value = value. //cur = (value < cur->value) ? cur->left:cur>right. } prev = NULL. root = temp. if(value < cur->value) { cur = cur->left. Page 5 of 5 } root = queue[queue_pointer++].