Breadth-first search (graphical) exercise
Click to run World:
Breadth-first search at
Ancient Brain.
Keeps going after DEST
Look in the console and you see something odd:
Exercise: Print a message
Clone it and
Edit it to:
- Output a message (using console.log) when it finds DEST.
- Output a HTML message to the "run header" when it finds DEST.
See AB.msg.
Refactor the code
Can we make it immediately stop when it finds DEST?
-
It is awkward, because we are inside a "for" loop
which is inside an "if" statement
inside a "while" loop.
- We need to come back out of these cleanly, while still drawing the path to the goal.
- The way it is written, it is easier to just let it finish.
- Difficult exercise:
- Find a clean way to get out of the loop.
- Difficult exercise:
- Refactor
the code to avoid: (a) nested loops and (b) use of break.
- I like using lots of functions
with lots of return's
when code gets complex.
I find that easier to see where control is going.
Not everyone agrees.
Why You Shouldn't Nest Your Code.
Not everyone agrees. But I'm with this guy.
More exercises
- Make connections sparse, so often no path is found.
This is easy!
- Make the Force-directed graph more repellent, so it spreads out more.