answersLogoWhite

0


Best Answer

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#define TRUE 1

#define FALSE 0

#define MAX 8

struct node

{

int data ;

struct node *next ;

} ;

int visited[MAX] ;

int q[8] ;

int front, rear ;

void bfs ( int, struct node ** ) ;

struct node * getnode_write ( int ) ;

void addqueue ( int ) ;

int deletequeue( ) ;

int isempty( ) ;

void del ( struct node * ) ;

int main( )

{

struct node *arr[MAX] ;

struct node *v1, *v2, *v3, *v4 ;

int i ;

v1 = getnode_write ( 2 ) ;

arr[0] = v1 ;

v1 -> next = v2 = getnode_write ( 3 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 1 ) ;

arr[1] = v1 ;

v1 -> next = v2 = getnode_write ( 4 ) ;

v2 -> next = v3 = getnode_write ( 5 ) ;

v3 -> next = NULL ;

v1 = getnode_write ( 1 ) ;

arr[2] = v1 ;

v1 -> next = v2 = getnode_write ( 6 ) ;

v2 -> next = v3 = getnode_write ( 7 ) ;

v3 -> next = NULL ;

v1 = getnode_write ( 2 ) ;

arr[3] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 2 ) ;

arr[4] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 3 ) ;

arr[5] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 3 ) ;

arr[6] = v1 ;

v1 -> next = v2 = getnode_write ( 8 ) ;

v2 -> next = NULL ;

v1 = getnode_write ( 4 ) ;

arr[7] = v1 ;

v1 -> next = v2 = getnode_write ( 5 ) ;

v2 -> next = v3 = getnode_write ( 6 ) ;

v3 -> next = v4 = getnode_write ( 7 ) ;

v4 -> next = NULL ;

front = rear = -1 ;

bfs ( 1, arr ) ;

for ( i = 0 ; i < MAX ; i++ )

del ( arr[i] ) ;

getch( ) ;

return 0;

}

void bfs ( int v, struct node **p )

{

struct node *u ;

visited[v - 1] = TRUE ;

printf ( "%d\t", v ) ;

addqueue ( v ) ;

while ( isempty( ) -1 )

return TRUE ;

return FALSE ;

}

void del ( struct node *n )

{

struct node *temp ;

while ( n != NULL )

{

temp = n -> next ;

free ( n ) ;

n = temp ;

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago
  1. #include
  2. #define MAX5
  3. int dfs(int adj[][MAX], int visited[], int start)
  4. {
  5. int stack[MAX];
  6. int top=-1,i;
  7. printf("%c-",start+65);
  8. visited[start]=1;
  9. stack[++top]=start;
  10. while(top!=-1)
  11. {
  12. start=stack[top];
  13. for(i=0;i
  14. {
  15. if(adj[start][i]&&visited[i]==0)
  16. {
  17. stack[++top]=i;
  18. printf('%c-",i+65);
  19. visited[i]=1;
  20. break;
  21. }
  22. }
  23. if(i==MAX)
  24. top--;
  25. }
  26. return 0;
  27. }
  28. int main()
  29. {
  30. int adj[MAX][MAX]={{0,0,1,1,0},{0,0,0,0,0},{0,1,0,1,1},{0,0,0,0,1},{0,0,0,1,0}};
  31. int visited[MAX]={0};
  32. printf("DFS Traversal : ");
  33. dfs(adj,visited,0);
  34. printf("\n");
  35. return 0;
  36. }
By Malek Alatomi
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program that implements breadth first search algorithm?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What graph traversal algorithm uses a queue to keep track of vertices which need to be processed?

Breadth-first search


How do you print all data in a Binary Search Tree?

By using Depth First Search or Breadth First search Tree traversal algorithm we can print data in Binary search tree.


Among best and breadth first search which algorithm has the property that if a wrong path is selected it can be corrected afterwards?

Best-first.


What is search algorithm?

The linear search algorithm is a special case of the brute force search.


Which search algorithm requires that the arrays contents be sorted?

Binary Search Algorithm


What is the program of breadth first search in c plus plus?

The algorithm for breadth first search is to start at the root node or at an arbitrary node within the tree. First, push this node onto a queue. Then proceed as follows 1. If the queue is empty, quit the search and return a "not found" result. 2. Pop the first node from the queue. 3. If this node contains the value you seek, quit the search and return the node. 4. Enumerate the child nodes (if any), and push them onto the queue. 5. Go to step 1.


Breadth First search is used in?

stacks


What is the algorithm DFS and BFS in graph traversal?

DFS and BFS stands for Depth First Search and Breadth First Search respectively. In DFS algorithm every node is explored in depth; tracking back upon hitting an already visited node and starts visiting from a node which has any adjacent nodes unvisited. In BFS, the nodes are visited level wise. These algorithms are used to traverse the nodes on a connected digraph. Primal


Is breadth first search bidirectional?

It can be. It depends on the structure and how it is implemented.


How to detect binary search tree as depth first search or breadth first search?

O(N-1)


What is the difference between breadth first search and depth first search in C programming?

Both algoritms can be build very similary. The difference between breadth-first search and depth-first search is order in which elements ar added to OPEN list. In breadth-first search new nodes are appended to the end of OPEN list In depth-first search new nodes are inserted in the begining of OPEN list


An algorithm to find whether a directed graph is connected or not?

You can use a The Depth-First Search algorithm.