Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre traverse fix #38

Merged
merged 3 commits into from Sep 15, 2014
Merged

Pre traverse fix #38

merged 3 commits into from Sep 15, 2014

Conversation

ghost
Copy link

@ghost ghost commented Jun 28, 2014

Hi!

After reading the code of pre-traverse i thought that there was a problem and i think was able to find an example that proves it.

The problem i saw was that after visiting a node the algorithms was marking all of its successors as visited(it put them in the seen set), which is common for BFS but not DFS. Therefore if we take that example graph:

    :a
   / | \
  v  v  v
 :e :b :c
  |     ^
  |    /
  |   /
  |  /
  | /
  |/
  v
 :d

and start the traversal from :a, what the algorithm will do is actually:

  1. visit :a and mark :e, :b, and :c as visited
  2. visit :e
  3. visit :d
  4. try visiting :c but its already in the seen set, so its visited
  5. backtrack to :a
  6. visit :b
  7. visit :c

So it produces the result (:a :e :d :b :c) which is incorrect.

I chose the names of the vertices specifically for this example in order to force the algorithm to go through the branches in this exact order, otherwise it could just go through the branch from :e, go to :d, backtrack to :a and then choose :c and then :b. In that case the result is (:a :e :d :c :b) which is correct but the :c vertex was not visited when it had to be.

George Shopov added 3 commits June 28, 2014 20:15
There are special cases such as this:

    :a
   / | \
  v  v  v
 :e :b :c
  |     ^
  |    /
  |   /
  |  /
  | /
  |/
  v
 :d

in which the function yields false traverse. The error comes from the
fact that after discovering :a the algorithm marks its direct successors
:e, :b and :c as discovered. Therefore if the algorithm starts at :a
then chooses to visit :e, then :d it will start backtracking instead of
visiting :c because the vertex :c is marked as visited. If the
successors of :a are visited in the order (:e :b :c) then the result
would be (:a :e :d :b :c), which is obviously incorrect.
@aysylu
Copy link
Owner

aysylu commented Sep 15, 2014

Great catch! Thanks for the fix and the test case revealing it.

aysylu pushed a commit that referenced this pull request Sep 15, 2014
@aysylu aysylu merged commit e6b0754 into aysylu:master Sep 15, 2014
@ghost ghost deleted the pre-traverse-fix branch October 4, 2014 06:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant