writing depth first search in c
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?
In this piece of code
else {
addBackCirListDeque(&stack, currentVertex->neighbors[i]);
if(currentVertex->neighbors[i]->isVisited == 0) {
addBackCirListDeque(&stack, currentVertex->neighbors[i]);
}
}
you for some reason are adding the neighboring vertex currentVertex->neighbors[i]
to the DFS stack twice. Why are you doing it twice???
Moreover, the first addition is done without even checking if the neighbor has already been visited. Why? If you do it that way (i.e. add without checking if visited already) in a cyclical graph the algorithm will go into an endless cycle. It will loop around the same cycle forever, never reaching other parts of the graph.
P.S. The if(currentVertex->isVisited == 0)
check before that will probably prevent the endless loop from happening, but still the above repeated addition makes no sense to me.
P.P.S. Oh, I think I'm starting to get it. It is added twice intentionally: the first, (unconditional) addition, for backtracking, the second addition (with checking) for forward DFS progress. Hmm... Might even work, although I wouldn't do it that way. Are you sure your input is correct? Is the graph connected, i.e. is the vertex really reachable?
精彩评论