This exercise is for your own practice, not to be turned in. This is recommended for your study to test yourself that you are able to reconstruct the solution to graph traversal algorithms from class and the book
The goal of this project is to understand how graph traversal works: iterative breadth first, iterative depth first, and recursive depth first. For each of these, we package the algorithm both as an iterator (or "external iterator" to use the terminology in the Design Patterns book) and as a stand-alone algorithm (or "internal iterator").
Find the code at
/homes/tvandrun/Public/cs345/graph-practice
The code contains packages adt
,
alg
, impl
and test
.
The classes you will be completing are in alg
.
The classes BreadthFirstTraversal
,
DepthFirstTraversal
,
and DepthFirstTraveralRecursive
all implement the Traversal
interface.
It has three signatures:
external()
takes a graph and a starting vertex
identified by an integer, and returns a (traditional) iterator,
packaged in an iterable.
The iterator will return all the vertices of the graph reachable from
the given starting vertex in an order consistent with
the implemented traversal strategy.
internal()
takes a graph, starting index,
and an object that implements PerformOnVertex
,
which is a "Command" object (in the Design Patterns
terminology, also equivalent to a first-class function);
and performs the given operation on every vertex reachable from
the given starting vertex in an order consistent with
the implemented traversal strategy.
(As mentioned above,
this is essentially what the Design Patterns book calls an "internal iterator",
except that it is implemented externally to the structure being iterated over.)
parents()
returns the traversal tree of the
most recently performed traversal as an array whose
indices correspond to vertices and whose values correspond to
the vertices' parents in the traversal tree.
Finish the methods internal()
and external()
in alg.BreadthFirstTraversal
.
Some code to initialize structures is given for you.
Test using BFTALTest
and BFTAMTest
, which test BFT on
adjacency-list and adjacency-matrix implementations.
Do similarly for alg.DepthFirstTraversal
.
Test using DFTALTest
and DFTAMTest
.
Unfortunately, these also test the recursive version, so ignore those
unit tests for now.
Do similarly for alg.DepthFirstTraversalRecursive
,
but only for internal()
, and, more specifically,
you need only to write the helper method
internalR()
, which does the actual recursion.
The method internal()
sets things up and
starts the recursive process going by making the initial call to
internalR()
.
Test using the remaining unit tests in
DFTALTest
and DFTAMTest
.
(Sorry the test cases aren't organized conveniently.)
There is nothing to be turned in.