I'm trying to write depth first search in C. In the search instead of maintaing a set of all the reachable nodes I instead have to mark the isVisited field in Vertex as a 1 for visited. Here's my data structs and my algo attempt.
struct Vertex {
char label;
int isVisited;
int numNeighbors;
struct Vertex** neighbors;
};
typedef struct Vertex Vertex;
struct Graph {
int numEdges;
int numVertices;
Vertex* vertexSet;
};
typedef struct Graph Graph;
struct DLink {
TYPE value;
struct DLink * next;
struct DLink * prev;
};
struct cirListDeque {
int size;
struct DLink *last;
};
typedef struct cirListDeque cirListDeque;
Here's my attempt at a DFS implementation:
int DFS(Graph* g, Vertex* source, Vertex* destination) {
cirListDeque stack;
TYPE currentVertex;
int i;
initCirListDeque(&stack);
addBackCirListDeque(&stack, source);
while(!isEmptyCirListDeque(&stack)) {
currentVertex = backCirListDeque(&stack);
removeBackCirListDeque(&stack);
if(currentVertex->label == destination->label) {
return 1;
}
else {
if(currentVertex->isVisited == 0) {
currentVertex->isVisited = 1;
for(i = 0; i < currentVertex->numNeighbors; i++) {
if(currentVertex->neighbors[i]->label == destination->label) {
return 1;
}
else {
addBackCirListDeque(&stack, currentVertex->neighbors[i]);
if(currentVertex->neighbors[i]->isVisited == 0) {
addBackCirListDeque(&stack, currentVertex->neighbors[i]);
}
}
}
}
}
}
return 0;
}
The problem with this search is it never returns that a node is reachable even if it is. Does anybody know how I could correct this?