3

I know that you use a recursive implementation of DFS where all nodes start as white, are colored gray when they are first encountered, and are colored black after all of their children are explored, you know that there is a cycle if you ever encounter a gray node.

But how do you do this with a stack implementation?

user1090859
  • 31
  • 1
  • 1
  • 3
  • 1
    Welcome to Stackoverflow ! Your first coding attempt ? What's you programming language ? You will likely get more valuable answer by avoiding to put vague question. Please elaborate! – menjaraz Dec 10 '11 at 10:04

1 Answers1

-1

This answer on Stackoverflow is pointing to a Java code sample implementing DFS with stack.

Besides, if you feel comfortable with C and I hope you like chess as me too, I strongly advise you to study Andy DuPlain's fbk2rbm's source code released in the public domain.

It's a handy utility to convert Fritz power-books to Rebel 7 format (Opening libraries in the chess parlance).

It makes use of stack, chess move beeing seen as node.

Excerpt :

...

/* Used to hold move */
struct Move {
  char FromFile, FromRank;
  char ToFile, ToRank;
  unsigned char Eval;
  char StartVar;    
};

/* List of moves seen */
struct Move Moves[256]; 

...

Extending the program to address position cylcing detection (given that different move sequences may lead to the same position / collision) would be an excellent exercise related to your question.

Community
  • 1
  • 1
menjaraz
  • 7,551
  • 4
  • 41
  • 81
  • All you did was say that extending DFS to answer the question would be an excellent exercise which is what the question was asking! – theannouncer Jul 11 '18 at 18:34