I am trying to implement Depth first search in c, I have successfully built the program to make the adjacency list representation for a graph(with help). I understand the Pseudo code for Dfs in this manner
procedure DFS(G,v):
label v as discovered
for all edges from v to w in G.adjacentEdges(v) do
if vertex w is not labeled as discovered then
recursively call DFS(G,w)
I have built the code that compiles but there seems to be some logical inconsistencies with my code. Please help me out with DFS part. I have properly checked the rest of the code and it works fine without DFS however I have included the rest of the part anyway so as to make sure if the there was improper connection within the code.
When I enter the input
3
Enter the number of Edges
2
Enter the Edges
0 1
1 2
I get the output as just
1
I have here used examples for DFS where all vertices are connected . This is my code,please check out the void dfs function.
#include <stdlib.h>
#include <stdio.h>
struct grnode;
struct grconn;
struct grconn { /* Connection to node (linked list) */
struct grnode *dest;
struct grconn *next;
};
struct grnode { /* Node in graph */
int id;
struct grconn *conn;
};
struct graph {
int nnode;
struct grnode *node;
};
/*
* Create new connection to given node
*/
struct grconn *grconn_new(struct grnode *nd)
{
struct grconn *c = malloc(sizeof(*c));
if (c) {
c->dest = nd;
c->next = NULL;
}
return c;
}
/*
* Clean up linked list of connections
*/
void grconn_delete(struct grconn *c)
{
while (c) {
struct grconn *p = c->next;
free(c);
c = p;
}
}
/*
* Print connectivity list of a node
*/
void grnode_print(struct grnode *nd)
{
struct grconn *c;
printf("%d:", nd->id);
c = nd->conn;
while (c) {
printf(" %d", c->dest->id);
c = c->next;
}
printf("\n");
}
/*
* Create new graph with given number of nodes
*/
struct graph *graph_new(int n)
{
struct graph *g = malloc(sizeof(*g));
int i;
if (g == NULL) return g;
g->nnode = n;
g->node = malloc(n * sizeof(*g->node));
if (g->node == NULL) {
free(g);
return NULL;
}
for (i = 0; i < n; i++) {
g->node[i].id = i;
g->node[i].conn = NULL;
}
return g;
}
/*
* Delete graph and all dependent data
*/
void graph_delete(struct graph *g)
{
int i;
for (i = 0; i < g->nnode; i++) {
grconn_delete(g->node[i].conn);
}
free(g->node);
free(g);
}
/*
* Print connectivity of all nodes in graph
*/
void graph_print(struct graph *g)
{
int i;
for (i = 0; i < g->nnode; i++) {
grnode_print(&g->node[i]);
}
}
/*
* Create one-way connection from node a to node b
*/
void graph_connect(struct graph *g, int a, int b)
{
struct grnode *nd;
struct grconn *c;
if (a < 0 || a >= g->nnode) return;
if (b < 0 || b >= g->nnode) return;
nd = &g->node[a];
c = grconn_new(&g->node[b]);
c->next = nd->conn;
nd->conn = c;
}
/*
* Create two-way connection between nodes a and b
*/
void graph_connect_both(struct graph *g, int a, int b)
{
graph_connect(g, a, b);
graph_connect(g, b, a);
}
// The code above is for the functions for the adjacency list
// so now we have an array of integers which keeps whether we have visited something
void dfs(struct graph *g,int u, int *b,int v,struct grnode *nd)
{
int visited[v];
struct grconn *c;
visited[u]=1;
c = nd->conn;printf("%d",c->dest->id);
c=c->next;
while(c)
{
printf("%d",c->dest->id);
u=c->dest->id;
dfs(g,u,b,v,&g->node[0]);
}
}
// The code below is for the representation of something in the form of adjacency list
int main()
{
printf("Enter the number of Vertices\n");
int i,n,d,x,y;
scanf("%d",&n);
struct graph *g = graph_new(n);int b[n];
printf("Enter the number of Edges\n");
scanf("%d",&d);
printf("Enter the Edges\n");
for(i=0;i<d;i++)
{
scanf("%d %d",&x,&y);
graph_connect_both(g, x, y);
}
printf("\n");
for(i=0;i<n;i++)b[i]=0;
dfs(g,0, b,n,&g->node[0]);
graph_delete(g);
return 0;
}
dfs()and perhaps modifiedmain(). "there seems to be some logical inconsistencies" is not a proper description of an error, at what point is the output not what you expected?