Here is my breadth-first search (bfs
) and depth-first search (dfs
) code for graph traversal. Please give me some constructive reviews on it.
#include<stdio.h>
#include<stdlib.h>
#define null 0
typedef struct node//node of a linked list
{
struct vertex* info;//info part of node
struct node* next;// next pointer of a node
}*nodeptr;
typedef struct edge
{
int weight;
struct vertex * point;
struct edge *next;
}*eptr;
typedef struct vertex
{
int degree;
int info;
int status;
struct vertex * next;
struct edge *point;
struct vertex *parent;
}* vptr;
typedef struct graph
{
struct vertex *start;
int *directed;
}Graph;
typedef struct queue
{
nodeptr front;//front pointer of a queue
nodeptr rear;//rear pointer of a queue
} *QUEUE;
QUEUE qwe;
void insert_node(Graph *g,int x)
{
vptr p=(vptr)malloc(sizeof(struct vertex));
p->degree=0;
p->info=x;
p->point=null;
p->status=1;
p->next=g->start;
g->start=p;
return;
}
vptr find(vptr p,int x)
{
while(p!=null)
{
if(p->info==x)
return p;
p=p->next;
}
return null;
}
void insert_edge(Graph *g,int a,int b,int directed)
{
vptr loca;
vptr locb;
eptr p;
eptr q;
loca=find(g->start,a);
locb=find(g->start,b);
if(loca==null||locb==null)
{
printf("void insertion");
return;
}
p=loca->point;
q=null;
while(p!=null)
{
q=p;
p=p->next;
}
p=(eptr)malloc(sizeof(struct edge));
p->point=locb;
p->next=null;
if(q!=null)
q->next=p;
else
loca->point=p;
if(directed)
return;
else
insert_edge(g,b,a,1);
}
void delete_edge(Graph *g,int a,int b,int directed)
{
vptr loca=find(g->start,a);
vptr locb=find(g->start,b);
eptr p=loca->point;
eptr q=null;
while(p->point!=locb)
{
q=p;
p=p->next;
}
if (q==null)
loca->point=null;
else
q->next=p->next;
free(p);
return;
}
void process_vertex_early(vptr x)
{
printf("%d ",x->info);
}
void process_vertex_late(vptr x)
{
}
void process_edge(vptr x,vptr y)
{
//printf("%d %d\n",x->info,y->info);
}
int empty_queue()//if queue is empty then return 1
{
if (qwe->front==null)
return 1;
else
return 0;
}
void insert_queue(vptr x)
{
nodeptr p=(nodeptr)malloc(sizeof(struct node));//allocate new memory space to be added to queue
p->next=null;
p->info=x;
if(empty_queue())//if the queue is empty,front and rear point to the new node
{
qwe->rear=p;
qwe->front=p;
return;
}
qwe->rear->next=p;
qwe->rear=p;
//rear points to the new node
return;
}
vptr delete_queue()
{
vptr x;
if(empty_queue())//if queue is empty then it is the condition for underflow
{
printf("underflow\n");
return;
}
nodeptr p=qwe->front;//p points to node to be deleted
x=p->info;//x is the info to be returned
qwe->front=p->next;
if(qwe->front==null)//if the single element present was deleted
qwe->rear=null;
free(p);
return x;
}
BFS
void bfs(Graph *g)
{
vptr q=g->start;
vptr x;
vptr y;
eptr p;
while(q!=null)
{
q->status=1;
q=q->next;
}
g->start->status=2;
insert_queue(g->start);
while(!(empty_queue()))
{
x=delete_queue();
process_vertex_early(x);
x->status=3;
p=x->point;
while(p!=null)
{
y=p->point;
if(y->status!=3||g->directed)
process_edge(x,y);
if(y->status==1)
{
y->status=2;
y->parent=x;
insert_queue(y);
}
p=p->next;
}
process_vertex_late(x);
}
return;
}
DFS
void dfs(Graph *g,vptr x)
{
// if(finished)return;
// x->entry_time=++time;
eptr p=x->point;
vptr y;
process_vertex_early(x);
while(p!=null)
{
y=p->point;
if(y->status==1)
{
process_edge(x,y);
y->status=2;
y->parent=x;
dfs(g,y);
}
else if((y->status==2&&x->parent!=y||g->directed))
{
process_edge(x,y);
}
// if(finished)return;
p=p->next;
}
process_vertex_late(x);
x->status=3;
//x->exit_time=++time;
return;
}
node/edge/vertex/graph
hang together to build a graph. \$\endgroup\$delete_edge
you haveif (q=null)
which your compiler should warn you about. \$\endgroup\$main()
program that created a graph and then ran the traversal functions over it. It is a lot harder to work out what's required and expected when you don't provide an SSCCE (Short, Self-Contained, Correct Example). \$\endgroup\$