Questions about anything?

Today I'd like to do more practice with searching trees and graphs.

Last time I went over "recursive depth-first search". Let's start by reviewing what we did then.

Quick quiz:

- Why is this "depth-first"?
- What is the difference between "search" and "traverse"?
- Let's draw a graph on the board, and trace how this search (or "traversal") would go.

This sort of recursive search shows up in many variations. For example, let's take a look at this peg solitaire solver, pegs.py , and see how it compares. (Quick quiz: what is the "backtracking" in this, and how does that compare with what we did before?)

Recursion is all very nice, but it turns out that it doesn't generalize easily to breadth-first searches. Often times we want to look at all the nearby options before going too far down the tree.

We can modify depth-first with a technique called iterative deepening, but there is also another important way.

We can write an iterative loop (rather than use recursion), using either a queue or stack to keep track of what to do next. The nodes to be looked at in the future are called the "fringe".

Here are two articles that describe the algorithm. It's an important one, so let's work our way through it carefully.

- breadth-first-search (with a fringe that's a queue)
- depth-first-search (with a fringe that's a stack)

Here's an implementation of this breadth-or-depth-with-queue-or-stack which I'd like us to work through.

Notice that even if we start with a graph, any of these traversals generate a tree, since we start from some given spot and create paths that visit everything without looping.

One way to visualize some of the different search approaches is to use these technique to generate 2D mazes. There are a number of variations, but the basic idea is to start with a rectangular grid of points, connecting nearby ones with edges, giving us a checkerboard pattern of lines. Each intersection is a node. We then start at a random point (a random intersection), and search outwards using depth-first or breadth-first or whatever, leaving us with a tree that looks something like a 2D maze.

I have some examples in code/graphs/maze/ which I'll wave my hands at in class.

I'll post a homework assignment for next Monday that will let you try some of this.

And on Thursday we'll do more practice.

Please continue reading through these topics in the resources.

https://cs.bennington.college /courses /spring2022 /algorithms /notes /april11

last modified Mon April 11 2022 12:35 am

last modified Mon April 11 2022 12:35 am