c - How to implement a BFS with depth -
i stuck trying think of way to write function bfs depth. wrote working bfs search goes through entire graph thinking of making copy , editing it.
my bfs returns array of int -1 denoting end. thinking after every depth, make marker , put -2 onto array show new depth. example: 0, -2, 1, 4, 6, -2, 3, 5, -1
however, part stuck on. put -2 after first number, -2 after inserting of neighbors of beginning vertex, , on. how write function?
my function looks this:
vertex *bfs(graph *g, vertex v) { if(v < 0 || v > g->numvertices) return null; int size = g->numvertices + 1; int counter = 0; int = 0; queue *fq = createqueue(size); set *ds = createset(size); vertex currentv; vertex *returnarr = (vertex *)malloc(size * sizeof(vertex)); vertex *neighbors; if(addtoqueue(fq, v) != 1){ printf("error in adding queue\n"); return null; } if(addtoset(ds, v) != 1){ printf("error in added set\n"); return null; } returnarr[counter] = v; counter++; while(queue not empty){ currentv = top of queue; neighbors = neighbors(g, currentv); = 0; while(neighbors[i] != -1){ if(current element in ds){ addtoset(ds, neighbors[i]); returnarr[counter] = neighbors[i]; counter++; addtoqueue(fq, neighbors[i]); } i++; } free(neighbors); } returnarr[counter] = -1; return returnarr; }
Comments
Post a Comment