Section 6.4: Traversal Algorithms

Abstract:

First of all, note that we're only reading 6.4 through p. 449 (Practice 16).

We compare breadth-first and depth-first strategies for traversing a graph, by which we mean writing down the nodes in some reasonable way.

For the problems, we should stick with the convention that, given a choice, we should choose nodes in alphabetic order.

Depth-First versus Breadth-First Search

The idea behind the depth-first strategy is to burrow down into the graph, rather than spread out as one does in a breadth-first search. The depth-first algorithm is recursive. Have a look at the algorithm on page 443.

Practice #16a, p. 449

Practice #14, p. 444

Examine the breadth-first algorithm on p. 446. It uses a queue to order the nodes, and then recursively determines the adjacent nodes to each which are yet unmarked.

Practice #16b, p. 449

Practice #15, p. 448

(Please indulge your professor now: he spent a lot of time on this, and he'd hate to think that it was wasted!)

These traversal algorithms are useful for operating on graphs. For example, I wrote this lisp code to find the shortest distance between two nodes x and y, based on the idea we generated in class. I use a mixed breadth-first and depth-first algorithm, with recursion. The idea was this:

Note that I used the adjacency matrix representation, which is good for ``full'' graphs, but wasteful for sparse ones.

To test my algorithm, I ran it on the graph of Exercise #13, p. 439 for every pair of nodes (to compare the result with Floyd's algorithm). You can try out this procedure using this this web script.

Interestingly enough, I've not had an (easy) time of getting the path: I thought it would be easier!



LONG ANDREW E
Thu Mar 28 19:31:59 EST 2002