Algorithms
and
Data
Structures

Spring 2022
course
site
-->

Mon April 11

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:

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?)

breadth-first and depth-first with stacks and queues

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.

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.

maze generation

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.

coming next

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