answersLogoWhite

0

#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

14y ago

What else can I help you with?

Related Questions

What is the space complexity of breadth first search algorithm?

The space complexity of the breadth-first search algorithm is O(V), where V is the number of vertices in the graph being traversed.


What is the space complexity of Breadth-First Search (BFS) algorithm?

The space complexity of the Breadth-First Search (BFS) algorithm is O(V), where V is the number of vertices in the graph being traversed.


What is the space complexity of the Breadth-First Search (BFS) algorithm?

The space complexity of the Breadth-First Search (BFS) algorithm is O(V), where V is the number of vertices in the graph being traversed.


What is the runtime complexity of Breadth-First Search (BFS) algorithm?

The runtime complexity of the Breadth-First Search (BFS) algorithm is O(V E), where V is the number of vertices and E is the number of edges in the graph.


What is the relationship between Dijkstra's algorithm and breadth-first search in graph traversal?

Dijkstra's algorithm is a more advanced version of breadth-first search in graph traversal. While both algorithms explore nodes in a graph, Dijkstra's algorithm considers the weight of edges to find the shortest path, whereas breadth-first search simply explores nodes in a level-by-level manner.


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 are the differences between depth-first and breadth-first search algorithms in terms of their approach to traversing a graph or tree structure?

Depth-first search algorithm explores as far as possible along each branch before backtracking, while breadth-first search algorithm explores all neighbors of a node before moving on to the next level.


How can the Breadth-First Search (BFS) algorithm be implemented using recursion?

The Breadth-First Search (BFS) algorithm can be implemented using recursion by using a queue data structure to keep track of the nodes to visit. The algorithm starts by adding the initial node to the queue and then recursively visits each neighbor of the current node, adding them to the queue. This process continues until all nodes have been visited.


What are the differences between Dijkstra's algorithm and Breadth-First Search (BFS) when it comes to finding the shortest path in a graph?

Dijkstra's algorithm and Breadth-First Search (BFS) are both used to find the shortest path in a graph, but they have key differences. Dijkstra's algorithm considers the weight of edges, making it suitable for graphs with weighted edges, while BFS treats all edges as having the same weight. Additionally, Dijkstra's algorithm guarantees the shortest path, but BFS may not always find the shortest path in weighted graphs.


Can you implement Breadth-First Search (BFS) recursively?

Yes, Breadth-First Search (BFS) can be implemented recursively by using a queue data structure to keep track of the nodes to visit next. The algorithm involves visiting each node at the current level before moving on to the next level.