BFS Implementation in C | C Code For Breadth First Search

preview_player
Показать описание
BFS Graph Traversal: A simple and easy implementation of BFS in C Language. This video will explain how you can use Queues to implement BFS

python, C, C++, Java, JavaScript and Other Cheetsheets [++]:

►Learn in One Video[++]:

►Complete course [playlist]:

Follow Me On Social Media
Comment "#HarryBhai" if you read this 😉😉
Рекомендации по теме
Комментарии
Автор

ये Diwali code wish🙏🙏🙏 you many many happy returns of the And once again very very happy Diwali🕯🕯🕯🕯🕯🕯 🥰🥰🥰🥰🥰🥰❤❤❤❤❤❤ love you all and love❤❤ coding 💯💯💯💯

mandeepmalik
Автор

Happy diwali Harry bhai .bas tere bare main hi soch raha tha kyuki maine ajj firse ek software banaya diwali ke upar(coding se) or tabhi apne teacher ki bat yaad ayi.

gamingworldss
Автор

lodi bhai
incomplete information dene ke liye dhanyawad, puri playlist fir dekhna padegi kisi or se

rishb.
Автор

Harry bhai thanks a log for such great and valuable content all the time. specially you always find easy way to do complex problems. here it's BFS Spanning tree, before this there was a trick to find PreOrder, InOrder and PostOrder of an binary tree.

wp-status
Автор

Harry bhai aap ki वजह se class ki girls meri fan ho gye मेरे लिए तो ye Diwali🕯🕯🕯🕯 happy wali है thank you❤❤❤❤ Harry bhai for HTML tutorial love ❤❤❤ you 😘😘😘😘

mandeepmalik
Автор

Harry bhaiya pls Cloud computing using AWS isper ek detailed video bannao like dedo Dil Saath Harry Bhai ✌

anitaverma
Автор

Harry Bhai please cloud computing Request using Aws isper course bannao.
pls big fan reply dedo Harry Bhai pls Dil bhi Chahiye 🙁

pokemonfanbeast
Автор

Your DS tutorial is placement oriented

Prakashvishwakarma
Автор

theory part was good but the way you represent graph in code is not ok. You could use hash map or struct with structure like this

struct GraphNode{
int data;
struct GraphNode**; // Array of graph node
}

sanjaybalnad
Автор

Happy Diwali Harry Bhai Kya aap Cloud computing Aws per course ya detailed video bannado please request I watch on ytube but I didn't like pls aap hi bannado

dhananjaypatil
Автор

Harry Bhai, Your YouTube Shorts Video is awesome!!!!

soumyadipash
Автор

bhai is back here in this playlist...happy diwali bhai🕊️

harshjaiswal
Автор

This code might be helpful for some having doubts about the adjacency list indexing.

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

struct node {
int data;
struct node* next;
};

struct queue {
struct node* f;
struct node* r;
int maxSize;
int size;
};

struct node* createNode(int data) {
struct node* n = (struct node*)malloc(sizeof(struct node));
n->data = data;
n->next = NULL;
return n;
}

int isEmpty(struct queue* q) {
if (q->f == NULL) {
return 1;
}
return 0;
}

int isFull()
{
struct node *n = (struct node *)malloc(sizeof(struct node));
if (n == NULL)
{
free(n);
return 1;
}
free(n);
return 0;
}

void enqueue(struct queue* q, int data) {
if (isFull(q)) {
printf("Queue Overflow\n");
}
else {
struct node* n = createNode(data);
if (isEmpty(q)) {
q->f = q->r = n;
}
else {
q->r->next = n;
q->r = n;
}
q->size++;
}
}

int dequeue(struct queue* q) {
if (!isEmpty(q)) {
struct node* ptr = q->f;
q->f = q->f->next;
int val = ptr->data;
free(ptr);
q->size--;
return val;
}
else {
printf("Queue Underflow\n");
return -1;
}
}

struct Graph {
int numVertices;
int* visitArr;
struct node** adjList;
};

struct Graph* createGraph(int num) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->numVertices = num;
graph->visitArr = (int*)malloc(num * sizeof(int));
graph->adjList = (struct node**)malloc(num * sizeof(struct node*));

for (int i = 0; i < graph->numVertices; i++) {
graph->visitArr[i] = 0;
graph->adjList[i] = NULL;
}
return graph;
}

void addEdge(struct Graph* graph, int src, int dest) {
struct node* newNode = createNode(dest);
newNode->next = graph->adjList[src];
graph->adjList[src] = newNode;

newNode = createNode(src);
newNode->next = graph->adjList[dest];
graph->adjList[dest] = newNode;
}

void BFS(struct Graph* graph, int startVertex) {
struct queue* exploreQueue = (struct queue));
exploreQueue->f = exploreQueue->r = NULL;

graph->visitArr[startVertex] = 1;
enqueue(exploreQueue, startVertex);
int vertex;

while (!isEmpty(exploreQueue)) {
vertex = dequeue(exploreQueue);
printf("%d ", vertex);

struct node* ptr = graph->adjList[vertex];
while (ptr != NULL) {
if (graph->visitArr[ptr->data] == 0) {
graph->visitArr[ptr->data] = 1;
enqueue(exploreQueue, ptr->data);
}
ptr = ptr->next;
}
}
}

int main() {

struct Graph* graph = createGraph(11);
addEdge(graph, 0, 10);
addEdge(graph, 0, 7);
addEdge(graph, 0, 5);
addEdge(graph, 7, 5);
addEdge(graph, 7, 2);
addEdge(graph, 10, 5);
addEdge(graph, 2, 5);
addEdge(graph, 5, 1);
addEdge(graph, 1, 4);

BFS(graph, 0);

return 0;
}
For safer side, use a larger graph size (than you highest element in the graph) to avoid any indexing issues. Please feel free to correct/improve me upon this.

sauryanpandey
Автор

Harry, I have a question🙋. How do you enjoy coding almost everyday nd that too with various languages? Don't you get headache?

guddi
Автор

Nice video bro
Happy diwali to all of u

pranjalvidyarthi
Автор

what if data node data is not same as the array index as you have taken?

SUNNYKUMARPANDIT-im
Автор

Harry bhai me web development ki beginner level pr hu mene aapki web development ki 103 videos wali series ko CSS tk complete kr liya hai or just channle scroll krte krte flask ki web development series dekhi ... To bhai please please mujhe batao ki website development kisse krna chahiye frameworks use kr ke ya fir hr chiz type kr ke? is pr ek short video bana lo ya fir mujhe please reply kro

rajshah
Автор

Harry Bhai, iske baad please Heaps batana please 🙏🙏🙏🙏

souradeepbanerjee
Автор

Guys like and share this video to thank Harry for such fantastic free video

ayushgupta
Автор

Harry bhai please hashing aur heaps pai video upload kar do . By the way only few videos left to complete my dsa from you .♥♥♥

ayushverma
visit shbcf.ru