University of Virginia, Department of Computer Science
CS201J: Engineering Software, Fall 2003

Notes: Thursday 11 September 2003
Assignments Due

Notes and Questions
StringSet Specification
public class StringSet
    // OVERVIEW: StringSets are unbounded, mutable sets of
    //     Strings.  A typical StringSet is { x1, ..., xn }
    public StringSet () 
        // EFFECTS: Initializes this to be empty: { }
    public void insert (String s) 
        // MODIFIES: this
        // EFFECTS: Adds x to the elements of this: 
        //       this_post = this_pre U { s }
    public boolean isIn (String s) {
        // EFFECTS: Returns true iff s is an element of this.
    public int size () 
        // EFFECTS: Returns the number of elements in this.

Representation Invariant

The Representation Invariant expresses properties all legitimate objects of the ADT must satisfy. It is a function from concrete representation to boolean:

I: C → boolean

To check correctness we assume all objects passed in to a procedure satisfy the invariant and prove all objects satisfy the invariant before leaving the implementation code.

public class StringSet {
   // OVERVIEW: StringSets are unbounded, mutable sets of Strings.
   //    A typical StringSet is {x1, ..., xn}

   // Representation:
   private Vector rep;

   // RepInvariant (c) = c.rep contains no duplicates && c.rep != null
   //                        && all elements of c.rep are of non-null Strings

   //@invariant rep != null
   //@invariant rep.containsNull == false
   //@invariant rep.elementType == \type(String)
Abstraction Function

The Abstraction Function maps a concrete state to an abstract state:

It is a function from concrete representation to the abstract notation introduced in overview specification.
   // AF (c) = { AFString (c.els[i]) | 0 <= i < c.els.size () }

Friday's Section

In section on Friday, you will work on implementing the Graph data abstraction specified below:

public class Graph {
   // OVERVIEW: 
   //      A Graph is a mutable type that represents an undirected
   //      graph.  It consists of nodes that are named by Strings,
   //      and edges that connect a pair of nodes.
   //      A typical Graph is: < Nodes, Edges >
   //       where
   //         Nodes = { n1, n2, , nm }
   //       and 
   //         Edges = { {from_1, to_1}, ..., {from_n, to_n} }

   // Creator
   public Graph () 
      // EFFECTS: Initializes this to a graph
      //      with no nodes or edges: < {}, {} >.

   // Mutators
   public void addNode (String name)
      // REQUIRES: name is not the name of a node in this
      //  MODIFIES: this
      // EFFECTS: adds a node named name to this:
      //     this_post = < this_pre.nodes U { name }, this_pre.edges >

   public void addEdge (String fnode, String tnode)
      // REQUIRES: fnode and tnode are names of nodes in this.
      // MODIFIES: this
      // EFFECTS: Adds an edge from fnode to tnode to this:
      //       this_post = < this_pre.nodes, this_pre.edges U { {fnode, tnode} } >

   // Observers
   public boolean hasNode (String node)
      // EFFECTS: Returns true iff node is a node in this.

   public StringIterator nodes ()
      // EFFECTS: Returns the StringIterator that
      //      yields all nodes in this in arbitrary order.

   StringSet getNeighbors (String node)
      // REQUIRES: node is a node in this
      // EFFECTS: Returns the StringSet consisting of all nodes in this
      //      that are directly connected to node:
      //         \result =  { n | {node, n} is in this.edges }
To implement the Graph data abstraction:
  1. Select a representation for Graph. Consider carefully several different possible representations, and what their advantages and disadvantages will be.
  2. Determine the rep invariant and abstraction function
  3. Implement Graph (), addNode and hasHode
  4. Implement addEdge and getNeighbors

CS201J University of Virginia
Department of Computer Science
CS 201J: Engineering Software
Sponsored by the
National Science Foundation