CS112b1
LAB 13 - Java Iterators and Breadth-First Search

Objectives

  1. Java Iterators: Concepts and Implementation
  2. Breadth-First Search (BFS) and applications in graphs
  3. Write some code

Preamble

In this Lab, we will first talk about Java Iterators. Specifically, we will introduce the concepts of iterators, then show how they are uesd in programming. We next discuss Breadth-First-Search (BFS), which is a very useful algorithm for traversing graphs. Lastly, we will do some exercise.


Java Iterators

Specification of Iterator Interface: Iterator.java


Using Java Iterators

Code: Comp.java


Implementing Java Iterators

  • An iterator's implementation requires the implementation of a class for the associated generator.
  • The generator class is a static inner class: it is nested inside the class containing the iterator and can access the private information of its containing class.
  • The generator class implements the Iterator interface.

Code: Poly.java

Code: Num.java


Breadth-First Search

Code: GraphBFSedge.java


Exercise

Based on the GraphBFSedge.java , implementing a main() method, which takes two vertices (V, W) as arguments and prints out one shortest path form V to W.


CS112b1