Practical tips debugging deep recursion?

前端 未结 8 1526
灰色年华
灰色年华 2021-02-13 06:44

I\'m working on a board game algorithm where a large tree is traversed using recursion, however, it\'s not behaving as expected. How do I handle this and what are you experience

相关标签:
8条回答
  • 2021-02-13 07:03

    I know what a pain this can be. At my job, we are currently working with a 3rd party application that basically behaves as a black box, so we have to devise some interesting debugging techniques to help us work around issues.

    When I was taking a compiler theory course in college, we used a software library to visualize our trees; this might help you as well, as it could help you see what the tree looks like. In fact, you could build yourself a WinForms/WPF application to dump the contents of your tree into a TreeView control--it's messy, but it'll get the job done.

    You might want to consider some kind of debug output, too. I know you mentioned that your tree is large, but perhaps debug statements or breaks at key point during execution that you're having trouble visualizing would lend you a hand.

    Bear in mind, too, that intelligent debugging using Visual Studio can work wonders. It's tough to see how state is changing across multiple breaks, but Visual Studio 2010 should actually help with this.

    Unfortunately, it's not particularly easy to help you debug without further information. Have you identified the first depth at which it starts to break? Does it continue to break with higher search depths? You might want to evaluate your working cases and try to determine how it's different.

    0 讨论(0)
  • 2021-02-13 07:08

    I would start by instrumenting the function(s). At each recursive call log the data structures and any other info that will be useful in helping you identify the problem.

    Print out the dump along with the source code then get away from the computer and have a nice paper-based debugging session over a cup of coffee.

    0 讨论(0)
  • 2021-02-13 07:09

    Maybe you could convert the recursion into an iteration with an explicit stack for the parameters. Testing is easier in this way because you can directly log values, access the stack and don't have to pass data/variables in each self-evaluation or prevent them from falling out of scope.

    0 讨论(0)
  • 2021-02-13 07:11

    I would normally unit-test such algorithms with one or more predefined datasets that have well-defined outcomes. I would typically make several such tests in increasing order of complexity.

    If you insist on debugging, it is sometimes useful to doctor the code with statements that check for a given value, so you can attach a breakpoint at that time and place in the code:

      if ( depth = X && item.id = 32) {
         // Breakpoint here
      }
    
    0 讨论(0)
  • 2021-02-13 07:12

    Logging. Log in your code extensively. In my experience, logging is THE solution for these types of problems. when it's hard to figure out what your code is doing, logging it extensively is a very good solution, as it lets you output from within your code what the internal state is; it's really not a perfect solution, but as far as I've seen, it works better than using any other method.

    0 讨论(0)
  • 2021-02-13 07:15

    One thing I have done in the past is to format your logs to reflect the recursion depth. So you may do a new indention for every recurse, or another of some other delimiter. Then make a debug dll that logs everything you need to know about a each iteration. Between the two, you should be able to read the execution path and hopefully tell whats wrong.

    0 讨论(0)
提交回复
热议问题