Breadth-First Search
queue
struct
typedef struct {
int q[MAXV+1];
int first,last,count;
} queue;
void init_queue(queue *q) {
q->first=0; q->last=MAXV-1; q->count=0;
}
void enqueue(queue *q, int x) {
q->last=(q->last+1)%MAXV;
q->q[q->last]=x;
q->count++;
}
int dequeue(queue *q) {
int res=q->q[q->first];
q->first=(q->first+1)%MAXV;
q->count--; return res;
}
int empty_queue(queue *q) {
return (q->count<=0);
}
Before Searching and Processing
void initialize_search() {
rep (i,0,MAXV) discovered[i]=0;
}
void process_vertex_early(int v) {
}
void process_vertex_late(int v) {
}
void process_edge(int v, int y) {
}
Search
void bfs(graph *g, int start) {
edgenode *p; int v,y; queue q;
init_queue(&q); enqueue(&q,start);
discovered[start]=1;
while (empty_queue(&q)==0) {
v=dequeue(&q);
p=g->edges[x];
while (p!=NULL) {
y=p->y;
if (discovered[y]==0) {
enqueue(&q,y);
discovered[y]=1;
}
p=p->next;
}
}
}
CC BY-SA 4.0 Miguel Bustamante. Last modified: August 08, 2023. Website built with Franklin.jl and the Julia programming language.