bfs c

Solutions on MaxInterview for bfs c by the best coders in the world

showing results for - "bfs c"
Sandro
26 Jan 2020
1// BFS algorithm in C
2
3#include <stdio.h>
4#include <stdlib.h>
5#define SIZE 40
6
7struct queue {
8  int items[SIZE];
9  int front;
10  int rear;
11};
12
13struct queue* createQueue();
14void enqueue(struct queue* q, int);
15int dequeue(struct queue* q);
16void display(struct queue* q);
17int isEmpty(struct queue* q);
18void printQueue(struct queue* q);
19
20struct node {
21  int vertex;
22  struct node* next;
23};
24
25struct node* createNode(int);
26
27struct Graph {
28  int numVertices;
29  struct node** adjLists;
30  int* visited;
31};
32
33// BFS algorithm
34void bfs(struct Graph* graph, int startVertex) {
35  struct queue* q = createQueue();
36
37  graph->visited[startVertex] = 1;
38  enqueue(q, startVertex);
39
40  while (!isEmpty(q)) {
41    printQueue(q);
42    int currentVertex = dequeue(q);
43    printf("Visited %d\n", currentVertex);
44
45    struct node* temp = graph->adjLists[currentVertex];
46
47    while (temp) {
48      int adjVertex = temp->vertex;
49
50      if (graph->visited[adjVertex] == 0) {
51        graph->visited[adjVertex] = 1;
52        enqueue(q, adjVertex);
53      }
54      temp = temp->next;
55    }
56  }
57}
58
59// Creating a node
60struct node* createNode(int v) {
61  struct node* newNode = malloc(sizeof(struct node));
62  newNode->vertex = v;
63  newNode->next = NULL;
64  return newNode;
65}
66
67// Creating a graph
68struct Graph* createGraph(int vertices) {
69  struct Graph* graph = malloc(sizeof(struct Graph));
70  graph->numVertices = vertices;
71
72  graph->adjLists = malloc(vertices * sizeof(struct node*));
73  graph->visited = malloc(vertices * sizeof(int));
74
75  int i;
76  for (i = 0; i < vertices; i++) {
77    graph->adjLists[i] = NULL;
78    graph->visited[i] = 0;
79  }
80
81  return graph;
82}
83
84// Add edge
85void addEdge(struct Graph* graph, int src, int dest) {
86  // Add edge from src to dest
87  struct node* newNode = createNode(dest);
88  newNode->next = graph->adjLists[src];
89  graph->adjLists[src] = newNode;
90
91  // Add edge from dest to src
92  newNode = createNode(src);
93  newNode->next = graph->adjLists[dest];
94  graph->adjLists[dest] = newNode;
95}
96
97// Create a queue
98struct queue* createQueue() {
99  struct queue* q = malloc(sizeof(struct queue));
100  q->front = -1;
101  q->rear = -1;
102  return q;
103}
104
105// Check if the queue is empty
106int isEmpty(struct queue* q) {
107  if (q->rear == -1)
108    return 1;
109  else
110    return 0;
111}
112
113// Adding elements into queue
114void enqueue(struct queue* q, int value) {
115  if (q->rear == SIZE - 1)
116    printf("\nQueue is Full!!");
117  else {
118    if (q->front == -1)
119      q->front = 0;
120    q->rear++;
121    q->items[q->rear] = value;
122  }
123}
124
125// Removing elements from queue
126int dequeue(struct queue* q) {
127  int item;
128  if (isEmpty(q)) {
129    printf("Queue is empty");
130    item = -1;
131  } else {
132    item = q->items[q->front];
133    q->front++;
134    if (q->front > q->rear) {
135      printf("Resetting queue ");
136      q->front = q->rear = -1;
137    }
138  }
139  return item;
140}
141
142// Print the queue
143void printQueue(struct queue* q) {
144  int i = q->front;
145
146  if (isEmpty(q)) {
147    printf("Queue is empty");
148  } else {
149    printf("\nQueue contains \n");
150    for (i = q->front; i < q->rear + 1; i++) {
151      printf("%d ", q->items[i]);
152    }
153  }
154}
155
156int main() {
157  struct Graph* graph = createGraph(6);
158  addEdge(graph, 0, 1);
159  addEdge(graph, 0, 2);
160  addEdge(graph, 1, 2);
161  addEdge(graph, 1, 4);
162  addEdge(graph, 1, 3);
163  addEdge(graph, 2, 4);
164  addEdge(graph, 3, 4);
165
166  bfs(graph, 0);
167
168  return 0;
169}
Ronan
25 Oct 2020
1#include<stdio.h>
2#include<stdlib.h>
3 
4#define MAX 100  
5 
6#define initial 1
7#define waiting 2
8#define visited 3
9 
10int n;    
11int adj[MAX][MAX];
12int state[MAX]; 
13void create_graph();
14void BF_Traversal();
15void BFS(int v);
16 
17int queue[MAX], front = -1,rear = -1;
18void insert_queue(int vertex);
19int delete_queue();
20int isEmpty_queue();
21 
22int main()
23{
24	create_graph();
25	BF_Traversal();
26	return 0;
27}
28 
29void BF_Traversal()
30{
31	int v;
32	
33	for(v=0; v<n; v++) 
34		state[v] = initial;
35	
36	printf("Enter Start Vertex for BFS: \n");
37	scanf("%d", &v);
38	BFS(v);
39}
40 
41void BFS(int v)
42{
43	int i;
44	
45	insert_queue(v);
46	state[v] = waiting;
47	
48	while(!isEmpty_queue())
49	{
50		v = delete_queue( );
51		printf("%d ",v);
52		state[v] = visited;
53		
54		for(i=0; i<n; i++)
55		{
56			if(adj[v][i] == 1 && state[i] == initial) 
57			{
58				insert_queue(i);
59				state[i] = waiting;
60			}
61		}
62	}
63	printf("\n");
64}
65 
66void insert_queue(int vertex)
67{
68	if(rear == MAX-1)
69		printf("Queue Overflow\n");
70	else
71	{
72		if(front == -1) 
73			front = 0;
74		rear = rear+1;
75		queue[rear] = vertex ;
76	}
77}
78 
79int isEmpty_queue()
80{
81	if(front == -1 || front > rear)
82		return 1;
83	else
84		return 0;
85}
86 
87int delete_queue()
88{
89	int delete_item;
90	if(front == -1 || front > rear)
91	{
92		printf("Queue Underflow\n");
93		exit(1);
94	}
95	
96	delete_item = queue[front];
97	front = front+1;
98	return delete_item;
99}
100 
101void create_graph()
102{
103	int count,max_edge,origin,destin;
104 
105	printf("Enter number of vertices : ");
106	scanf("%d",&n);
107	max_edge = n*(n-1);
108 
109	for(count=1; count<=max_edge; count++)
110	{
111		printf("Enter edge %d( -1 -1 to quit ) : ",count);
112		scanf("%d %d",&origin,&destin);
113 
114		if((origin == -1) && (destin == -1))
115			break;
116 
117		if(origin>=n || destin>=n || origin<0 || destin<0)
118		{
119			printf("Invalid edge!\n");
120			count--;
121		}
122		else
123		{
124			adj[origin][destin] = 1;
125		}
126	}
127}
128
queries leading to this page
write a c program to traverse the below graph using depth first traversal and breadth first traversalwhen we use bfs and dfsbfs code with graph output pythonbfs traversalbfs using pythonbfs dfs examplesdfs and bfsgraph traversal code cbfs traversal algorithm in cdfs and bfs inwhich is best bfs or dfsuses of bfs and dfswhen to use dfs or bfsbfs implementation in cexample for bfs and dfsbfs code data structuresrbreadth first searchwrite a program to implement breadth first search algorithm using adjacency matrix in cbfs level order traversal graphdiscuss 2fillustrate traversing a graph using bfs algorithmimplement bfs algorithm bfs on cc program for breadth first search traversal of a graphwhat is dfs and bfswhere to use bfs and dfsalgorithm for bfs and dfswhen to use bfs 26 when to use dfsbreadth first graph traversalbfs dfs examplebfs and dfs program in cbfs and dfs pythonis a 2a dfs or bfsbfs and dfs in c 2b 2bbfs graph traversal program in c 2b 2bbfs and dfs algorithm in javabfs and dfs algorithm in pythonbfs traversal of directed graphbfs algorithm cbfs pseudocode in cinsert function of bfs graph javadfs and bfs with examplegraph breadth first search in cbfs in python in graphapplication of bfs and dfsgenerating a graph for bfs pythonbfs in pythongraph search algorithm in cimplementation of dfs and bfsbfs c linked list bfs code in cbfs dfs questionsbfs or dfswhen to use bfs or dfsbfs in data structure in cbfs linked list in c 2b 2bbfs c programdefine bfs and dfs 3a bfs and dfsbfs algorithm in cpython bfs searchbfs in the cbfs dfs explainedbfs traversal of graph c 2b 2bbreadth first traversal of a graph in cbfs traversal of graph in cbinar first search cide in cwhen to use bfs vs dfsbfs in cbfs and dfs algorithm codescopebfs search algorithm in cbfs dfs algorithmbfs traversal of graphtthe breadth first traversal algorithm is implemented on given graph using queue one of the possible order for visiting node on given graph 3aimplement breadth first search graph traversal using cbfs algorithm graph in cbfs dfs implementationwhat is bfs and dfsexample for dfs and bfsimplement bfs and dfs in cbfs dfs a 2a why we use dfs and bfsuse of bfs and dfsbfs implementation cbfs traversal of graph in cppwrite a program to traverse a graph using breadth first search 28bfs 29bfs and dfsbfs algorithm pythondfs and bfs in pythongraph in python bfsbfs and dfs c 2b 2b codebfs traversal of tree program in cbfs traversal of graph gfgbfs and dfs using pythondfs and bfs algorithmwhat bfs and dfsdfs and bfs program in cbfs queue cbfs dfs data structurebfs graph traversal algorithmimplement breadth first search graph traversal technique on a graph develop a program to implement bfs and dfs traversal of graphbfs in graph in cbreadth first search algorithm in cwrite the program for traversal of graph by using bfswhen to use bfs and when dfscpp program to implement bfs graphbfs and dfs of graph in cbfs dfs codehow to impliment bfs in c 2b 2bwrite algorithm for bfs graph traversalbfs examplebfs 2c dfsbfs pythonbfs and dfs operationsbfs traversal in a graphsimple graph bfs traversal in cppbfs algorithm c codebfs traversal gfgbreath irst searchhow to bfs c 2b 2bbreadth first search program in c 2b 2bgraph traversal in cdfs and bfs usesbfs pseudocode pythonbfs c implementationbfs dfsbfs dfs program in cbfs for graphs in cbfs traversal and dfs traversal graphbfs using adjacency list in cwhen to use bfs and when to use dfswho created dfs and bfs algorithmimplementation of bfs and dfs in cwhat are dfs and bfs 3fbfs and dfs examplesbfs traversal of treeimplement breadth first search graph traversal technique on a graph in cbfs 2c dfs algorithmsbfs and dfs data structurecretae agraph and perform bfs from each node in cpython breadth first searchbfs and dfs graph traversal program in cusing both dfs and bfsbfs traversal of tree cahow to print in bfs c programmingbfs 26 dfs algorithmbreadth first searchbfs traversal codehow to traverse bfs and dfsbfs graph traversal in data structurebfs and dfs geeksforgeekspython graphs 22breadth first traversal 22bfs traversal of graphbfs traversal in graphbfs 2fdfs are example of 2abfs directed graph c 23bfs graph traversal code in cppgraph traversal bfs questionexplain bfs and dfs with examplebfs traversal graphbreadth first order using adjacency matrix cbfs cgraph to bfs traversalbfs vs dfsalgorithm of bfs and dfsdfs and bfs exampledfs and bfs in cbfs 26 dfswhat is bfs or dfsbfs and dfs program bfs codencodebfs in c language using adjacency listbfs codewhich one to use dfs or bfswhere we use bfs and dfs in best first searchbreadth first traversal of a tree in cppbfs using queue program in c 2b 2bbfs and dfs gfgbredth first searchbreathfirst algorithm to codebfs and dfs codebreadth search and breadth first searchbfs using cbfs and dfs in data structurebreadth for searchhow to traverse a graph using bfsbfs in directed graphhow to do breadth first traversalbfs and dfs program in c 2b 2b with outputbfs using adjacency matrix in cbfs search for graph traversal algorithmbfs and dfs techniques bfs and dfs commonbfs algos using queuesgraph representation and breadth first traversal program in cwhen should we use dfs and bfsdata structure suitable for implementing bfs and bound strategy4breadth first search in cbfs and dfs program in c 2b 2bbfs algorithm output examplebfs graph traversal program in c output givenbfs and dfs algorithm in cbreadth first search c 2b 2b code of bfsc program for bfs and dfswho has made dfs and bfs algorithmbfs and dfs applicationbfs 2c dfs 2c dls which is done firstgraph bfs traversalbfs and dfs examplee breadth first search javabreadth first search ccode for bfs in cbread first search alogorithm bfs and dfs algorithmdfs and bfs abfs graphbreadth first search on the grahp using linked list bfs and dfs best explanationc program implementation of graph traversal depth first searchalgorithm dfs and bfsbfs undirected graph in javadfs and bfs programizbfs traversal in tree cpp 28bfs 29 python codebfs algorithm graph list cbreathfirst searchwrite a program to implement breadth first search algorithm using queuewhen bfs and dfs are usebreadth first search algorithmbreath first searchhow to find all bfs traversals of a graphwhich of the following data structures is best to implement bfswhen to use dfs and bfsdfs vs bfstraversing graph by bfshow to code bfs graphsimple bfs program in pythonbfs traversal algorithm graphbfs and dfswhen to use bfs and dfsbreathe first searchgraph bfs in pythona breadth first searchbfs and dfs in c programmingmcqs on bfs and dfsuse of dfs and bfsbfs traversal of undirected graphdfs or bfsbfs traversal in cgenerating a graph for bfsbfs program in c with explanationc bfspossible of number bfs traversal from graphbfs and dfs graph traversal program in c graphbfs program in cbfs and dfs definitionexample of dfs and bfsbfs and dfs in cbfs vs dfs in aiexplain bfs and dfs with examplesbreadth first search c exemplebfs and dfs data structuresbfs and dfs ordercreate breadth first search tree pythonbfs su cbfs traversal code c 2b 2bbfs program explanation using cexplain bfs and dfsbfs traversal of a graphprogramiz bfs dfswhat is bfs in a graphbfs graph traversal examplehow to traversing a graph by bfsbfs graph traversal program in cbreadth first search directed graph c 3d 2bbfs linked list cbfs graph traversalc code bfsbreadth first search algorithm can be used to find the graph is connected or not bfs dfs algorithmsbfs in data structure c programbfs dfs c programbfs c