1

At the minute I have an object that looks a bit like this.

C#
public class Step {
  int id;
  List<Step> nextSteps;
}

And I'm trying to convert it into another object that looks pretty similar apart from the fact that it doesn't allow loops.

It should handle loops by not expanding the children of nodes that have already appeared at a higher depth. Iterative deepening solves this (depth first search implementation but breadth first search order) but I'm struggling with an implementation using the following structure.

All implementations I found rely on finding some sort of goal node, whereas I need the whole tree expanded.

Any help would be appreciated. :D

Phil
  • 13
  • 4
  • 1
    http://en.wikipedia.org/wiki/Depth-first_search#Pseudocode – sehe Aug 09 '11 at 08:56
  • Uhm, that doesn't exactly help :( – Phil Aug 09 '11 at 09:20
  • Uhm... there is http://en.wikipedia.org/wiki/Breadth-first_search#Pseudocode too? You know they both DO traverse the whole graph (reachable from the source node) AND do not target a specific node? If you have a 'forest' just keep iterating all nodes until there is no unseen node and you'll end up with a forest of trees that cover the entire graph – sehe Aug 09 '11 at 09:25

1 Answers1

1

Add a Dictionary<Step, int> and each time you expand a node, add it with its depth.

void ExpandStep(Step s, int d)
{
    int prevDepth;
    if (lookup.TryGetValue(s, out prevDepth) && prevDepth <= d)
      return;
    lookup.Add(s, d);
    ... 
}
H H
  • 263,252
  • 30
  • 330
  • 514
  • I think you have read the question better than me. I'm still not sure though – sehe Aug 09 '11 at 09:31
  • Just to ask, would it be "prevDepth **<** d" as if the node is equal to the depth we last saw it we should still expand it? – Phil Aug 09 '11 at 14:14
  • Yes, I assumed each node should only be expanded once. If you want to re-expand from the same level, use `< d`. Or even `> d`, depending on how you count, up or down. – H H Aug 09 '11 at 14:20
  • I'm persisting the dictionary across depth incrementation so it will have the depth of previous loops in it. – Phil Aug 09 '11 at 16:04