David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.

Slides:



Advertisements
Similar presentations
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Advertisements

Cs2220: Engineering Software Class 10: Generic Datatypes Fall 2010 University of Virginia David Evans.
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 16: Concurrent Programming.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES II Autumn 2011.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst, David Notkin,
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
Cs2220: Engineering Software Class 8: Implementing Data Abstractions Fall 2010 University of Virginia David Evans.
Data Abstraction CS 201j: Engineering Software University of Virginia Computer Science Nathanael Paul
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION MIDTERM REVIEW Autumn 2011.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Pointers OVERVIEW.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Data Abstractions EECE 310: Software Engineering.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Abstract Data Types – Examples / Summary (Based on slides by Mike Ernst and David Notkin)
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Data Abstraction SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Understanding ADTs CSE 331 University of Washington.
Representation invariants and abstraction functions CSE 331 UW CSE.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 9: Designing Exceptionally.
CSE 331 Software Design & Implementation Dan Grossman Fall 2014 Abstraction Functions (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION ABSTRACT DATA TYPES Autumn 2011.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 11: Subtyping and Inheritance.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Concurrent Programming Acknowledgements: Some slides adapted from David Evans, U. Virginia.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 6 Representation Invariants.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Data Abstraction: Abstract Data Types (ADTs) (Based on slides by Mike Ernst and David.
EECE 310: Software Engineering
EECE 310: Software Engineering
Design David Evans cs205: engineering software
CSE 331 Software Design & Implementation
Graphs.
CSE 331 Software Design & Implementation
CSE 331 Software Design and Implementation
Abstraction Functions and Representation Invariants
CSE 331 Software Design and Implementation
CS216: Program and Data Representation
Data Structures and Algorithms for Information Processing
Data Abstraction UW CSE 160 Winter 2016.
Data Abstraction UW CSE 160 Spring 2018.
Lecture 5: Code Red, Ariane 5, and Gambling
Data Abstraction David Evans cs205: engineering software
SWE 619 Software Construction Last Modified, Fall 2015 Paul Ammann
Method Verification CS/SWE 332 Paul Ammann.
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
EECE 310: Software Engineering
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Lecture 13: Subtyping Rules Killer Bear Climber
Reasoning about Data Abstractions
Data Abstraction UW CSE 160.
Method Verification Paul Ammann.
Presentation transcript:

David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and a tree)

23 September 2003CS 201J Fall 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: // // where // Nodes = { n1, n2, …, nm } // and // Edges = { {from_1, to_1}, // …, {from_n, to_n} } Graph ADT A B C D Nodes = { A, B, C, D } Edges = { { A, B }, { A, C }, { B, C }, { A, D } } { … } means its a set – order doesn’t matter

23 September 2003CS 201J Fall Representation Ideas Set of Nodes, Set of Edges e.g., Nodes = { A, B, C, D } Edges = {,,, } Set of Nodes and Neighbors e.g., Graph = {,,, } Each entry is pair of node name, and names of nodes it is connected to. A B C D

23 September 2003CS 201J Fall Representation Ideas Set of Nodes and Matrix of booleans e.g., Nodes = [ A, B, C, D ] Edges = [ [ ] [ ] [ ] [ ] ] A B C D No edge from A to A Edge from B to C

23 September 2003CS 201J Fall Implementation 1 class Edge { // OVERVIEW: Record type for representing an edge. String node1, node 2; Edge (String n1, String n2) { node1 = n1; node2 = n2; } } class Graph { // OVERVIEW: A Graph is a mutable type that represents an … Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … }

23 September 2003CS 201J Fall Rep Invariant class Edge { String node1, node 2; } class Graph { Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … } RI (c) = c.nodes != null && c.edges != null && !c.nodes.containsNull && !c.edges.containsNull && elements of c.nodes are String objects && elements of c.edges are Edge objects && no duplicates in c.nodes && no duplicates in c.edges && every node mentioned in c.edges is also in c.nodes Is this precise enough? Function from rep to boolean

23 September 2003CS 201J Fall Rep Invariant RI (c) = c.nodes != null && c.edges != null && !c.nodes.containsNull && !c.edges.containsNull && elements of c.nodes are String objects && elements of c.edges are Edge objects && no duplicates in c.nodes // No duplicate edges, node1/node2 are interchangable: && ((c.edges[i].node1 = c.edges[j].node1 && c.edges[i].node2 = c.edges[j].node2) || (c.edges[i].node1 = c.edges[j].node2 && c.edges[i].node2 = c.edges[j].node1))  i == j && every node mentioned in c.edges is also in c.nodes

23 September 2003CS 201J Fall Abstraction Function Function from rep to abstract notion (use notation from overview) AF (c) = where … 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: // // where // Nodes = { n1, n2, …, nm } // and // Edges = { {from_1, to_1}, // …, {from_n, to_n} }

23 September 2003CS 201J Fall Abstraction Function class Edge { String node1, node 2; } class Graph { Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … } Nodes = { c.nodes[i] | 0 <= i < c.nodes.size () } The set of nodes is the elements of the c.nodes Vector Edges = { { c.edges[i].node1, c.edges[i].node2 } | 0 <= i < c.edges.size () } AF (c) = where The set of edges is the elements of the c.edges Vector

23 September 2003CS 201J Fall Implementing Constructor public Graph () // EFFECTS: Initializes this to a graph with no nodes or // edges:. class Edge { String node1, node 2; } class Graph { Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … } nodes = new Vector (); edges = new Vector (); } How do we know this satisfies the rep invariant?

23 September 2003CS 201J Fall Implementing addNode 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 = class Edge { String node1, node 2; } class Graph { Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … } nodes.addElement (name); } How do we know this still satisfies the rep invariant?

23 September 2003CS 201J Fall Implementing addEdge 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} } > class Edge { String node1, node 2; } class Graph { Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … } edges.addElement (new Edge (fnode, tnode)); } Would edges.addElement (new Edge (tnode, fnode)); be correct? How do we know this still satisfies the rep invariant?

23 September 2003CS 201J Fall Implementing getNeighbors public 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 class Edge { String node1, node 2; } class Graph { Vector nodes; // A Vector of String objects Vector edges; // A Vector of Edge object … } StringSet res = new StringSet (); Enumeration edgeenum = edges.elements (); while (edgeenum.hasMoreElements ()) { Edge e = (Edge) edgeenum.nextElement (); if (e.node1.equals (node)) { res.insert (e.node2); } else if (e.node2.equals (node)) { res.insert (e.node1); } }

23 September 2003CS 201J Fall Representation Ideas Set of Nodes, Set of Edges e.g., Nodes = { A, B, C, D } Edges = {,,, } Set of Nodes and Neighbors e.g., Graph = {,,, } Each entry is pair of node name, and names of nodes it is connected to. A B C D

23 September 2003CS 201J Fall Implementation 2 class NodeNeighbors { // OVERVIEW: Record type for representing an edge. String node; StringSet neighbors; // A Set of String objects NodeNeighbors (String n) { node = n; neighbors = new StringSet (); } } class Graph { // OVERVIEW: A Graph is a mutable type that represents an … Vector nodes; // A Vector of NodeNeighbors objects … }

23 September 2003CS 201J Fall Rep Invariant class NodeNeighbors { String node; StringSet neighbors; } class Graph { Vector nodes; // A Vector of NodeNeighbors objects } RI (c) = c.nodes != null && !c.nodes.containsNull && elements of c.nodes are NodeNeighbors objects && no duplicates in c.nodes && for each node in c.nodes, each node in c.nodes[i].neighbors is a node in c.nodes c.nodes[i].neighbors does not contain duplicates Function from rep to boolean

23 September 2003CS 201J Fall Abstraction Function Nodes = { c.nodes[i].node | 0 <= i < c.nodes.size () } The set of nodes is the elements of the c.nodes Vector Edges = { { c.nodes[i].node, c.nodes[i].neighbors[e] } | 0 <= i < c.nodes.size (), 0 <= e <= c.nodes[i].neighbors.size () } AF (c) = where class NodeNeighbors { String node; StringSet neighbors; } class Graph { Vector nodes; // A Vector of NodeNeighbors objects }

23 September 2003CS 201J Fall Implementing Constructor public Graph () // EFFECTS: Initializes this to a graph with no nodes or // edges:. nodes = new Vector (); } class NodeNeighbors { String node; Vector neighbors; // A Vector of String objects } class Graph { Vector nodes; // A Vector of NodeNeighbors objects }

23 September 2003CS 201J Fall Implementing addNode 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 = nodes.addElement (new NodeNeighbors (name)); } How do we know this still satisfies the rep invariant? class NodeNeighbors { String node; StringSet neighbors; } class Graph { Vector nodes; // A Vector of NodeNeighbors objects }

23 September 2003CS 201J Fall Implementing addEdge 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} } > NodeNeighbors n1 = lookupNode (fnode); NodeNeighbors n2 = lookupNode (tnode); n1.neighbors.insert (tnode); n2.neighbors.insert (fnode); } How do we know this still satisfies the rep invariant? class NodeNeighbors { String node; StringSet neighbors; } class Graph { Vector nodes; // A Vector of NodeNeighbors objects } We need to implement lookupNode also.

23 September 2003CS 201J Fall Implementing getNeighbors public 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 NodeNeighbors n = lookupNode (node); return n.neighbors; } class NodeNeighbors { String node; StringSet neighbors; } class Graph { Vector nodes; // A Vector of NodeNeighbors objects } Almost…but we have exposed our rep!

23 September 2003CS 201J Fall Rep Exposure What if client does this? Graph g = new Graph (); g.addNode (“A”); g.addNode (“B”); g.addEdge (“A”, “B”); StringSet neighbors = g.getNeighbors (“A”); neighbors.insert (“C”); Does the rep invariant for g still hold?

23 September 2003CS 201J Fall Rep Exposure If mutable components of the representation are accessible to clients, the implementation exposes the rep! Clients can mutate the representation directly – without using data type operations Why is this bad?

23 September 2003CS 201J Fall Problems with Rep Exposure Client mutations could break the rep invariant Client code may break if ADT implementation changes No longer possible to reason about the invariant being true by just checking the ADT implementation

23 September 2003CS 201J Fall return n.neighbors; Implementing getNeighbors public 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 return n.neighbors.copy (); class NodeNeighbors { String node; StringSet neighbors; } class Graph { Vector nodes; // A Vector of NodeNeighbors objects } NodeNeighbors n = lookupNode (node); } If we return a copy, the client doesn’t have access to the actual neighbors object in the representation.

23 September 2003CS 201J Fall Which implementation is better? Depends what we care about Code complexity –Normally the most important criteria –Nodes/Edges: getNeighbors is harder –NodeNeighbors: toString is harder, addEdge a little harder Memory Use –Nodes/Edges: 2 vectors, each edge requires 2 strings –NodeNeighbors: 1 vector, number of nodes StringSets, each edge requires 1 string

23 September 2003CS 201J Fall Which implementation is better? Performance –Both have poor performance: linear search through all the nodes to find one –NodeNeighbors getNeighbors does less work –Other methods Nodes/Edges usually less work –If we expect clients to call getNeighbors a lot, NodeNeighbors might be better

23 September 2003CS 201J Fall Performance Comparison > time java GraphTest// Using Nodes/Edges impl 1.220u 0.020s 0: % > time java GraphTest// Using NodeNeighbors impl 0.660u 0.040s 0: % Very rough comparison…but NodeNeighbors appears to be twice as fast for this test case. What is the test case doing?

23 September 2003CS 201J Fall GraphTest.java public class GraphTest { static public void main (String args[]) { Graph g = new Graph (); int numnodes = 1000; for (int i = 0; i < numnodes; i++) { g.addNode ("node" + i); } for (int i = 0; i < numnodes - 1; i++) { g.addEdge ("node" + i, "node" + (i + 1)); } for (int i = 0; i < numnodes - 2; i++) { g.addEdge ("node" + i, "node" + (i + 2)); } for (int i = 0; i < numnodes; i++) { StringSet neighbors = g.getNeighbors ("node" + i); }

23 September 2003CS 201J Fall Charge When picking representations, focus on complexity of implementation Your time is (usually) more valuable than the computer’s!