25
6.170 Recitation Godfrey Tan

6.170 Recitation

  • Upload
    damara

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

6.170 Recitation. Godfrey Tan. Announcements. Quiz 1 Review Sunday, March 3, 7:30pm, 34-101 Problem Set 3 Tuesday, March 5 Quiz 1 (L1-L10): Wednesday, March 6 during class Locations 54-100 for usernames a*-j* 34-101 for usernames k*-z* Open book, but it is a short quiz!. - PowerPoint PPT Presentation

Citation preview

Page 1: 6.170 Recitation

6.170 Recitation

Godfrey Tan

Page 2: 6.170 Recitation

Announcements Quiz 1 Review

Sunday, March 3, 7:30pm, 34-101 Problem Set 3

Tuesday, March 5 Quiz 1 (L1-L10):

Wednesday, March 6 during class Locations

54-100 for usernames a*-j* 34-101 for usernames k*-z*

Open book, but it is a short quiz!

Page 3: 6.170 Recitation

Graph Basics Node

Station: id, name Edge

Line: type, (in station, out station) Nodes are connected via Edges Graph is a collection of nodes and

edges

Page 4: 6.170 Recitation

Graph Requirements Directed Multi-graph Dynamic: add and remove nodes Flexible: works for other

applications

Page 5: 6.170 Recitation

Graph Operations addNode, containsNode addEdge, containsEdge Traversal

Predecessors, successors

Page 6: 6.170 Recitation

Graph Implementation Centralized

Graph stores everything May or may not require Node class

Distributed Graph stores Nodes Node stores Edges

Page 7: 6.170 Recitation

Storing Nodes and Edges Adjacency lists

Nodes IN Edges, Out Edges for each Node

Adjacency matrices Two dimensional arrays

HashMap Stores (key, value)

Page 8: 6.170 Recitation

Traversal Depth first search Breadth first search Operations

Mark: found, visited Visit

Page 9: 6.170 Recitation

Inputs and outputs Input: Harvard Alewife Output:

[Harvard(14), Porter(10), Davis(7), Alewife(8)]

Page 10: 6.170 Recitation

Abstract Data Type: What? Abstraction by specification Operations (T = abstract type, t = some other

type) Constructors: t T Producers: T, t T Mutators: T, t void Observers: T, t t

Designing an Abstract Type Few, simple operations that can be combined in powerful

ways Operations should have well-defined purpose, coherent

behavior Set of operations should be adequate

Page 11: 6.170 Recitation

Abstract Data Type: Why?

Allows us to: abstract from the details of

how data objects are implemented how they behave

defer decisions about the data structures

Page 12: 6.170 Recitation

Implementing an ADT Select the rep (representation of

instances) Implement operations in terms of

that rep

Page 13: 6.170 Recitation

Rep Data structure plus Set of conventions defined by

rep invariant abstraction function

Page 14: 6.170 Recitation

A Rep example

We want a representation of a list of 3 integers sorted in ascending order

[ x1, x2, x3 ] where x1 <= x2 <= x3

Page 15: 6.170 Recitation

Sorted Listclass SortedList {

Vector v; Integer foo;

// requires: a <= b <= c

public SortedListA(Integer a, Integer b,

Integer c) {

v = new Vector();

foo = new Integer(666);

v.add(a); v.add(foo);

v.add(b); v.add(foo);

v.add(c); v.add(foo);

}…

}

Page 16: 6.170 Recitation

Rep Data structure plus Set of conventions defined by

rep invariant abstraction function

What is the data structure for SortedList?

Page 17: 6.170 Recitation

Rep Data structure plus Set of conventions defined by

rep invariant abstraction function

What is the data structure for SortedList?

Vector

Page 18: 6.170 Recitation

Abstraction Function

What is the abstraction function?

Page 19: 6.170 Recitation

Abstraction Function

What is the abstraction function?

Mapping from vector elements to abstract values

[ v[0], v[2], v[4] ] maps to [ x1, x2, x3 ]

Page 20: 6.170 Recitation

Is this implementation necessarily wrong?class SortedList {

Vector v; Integer foo;

// requires: a <= b <= c

public SortedListA(Integer a, Integer b,

Integer c) {

v = new Vector();

foo = new Integer(666);

v.add(b); v.add(foo);

v.add(a); v.add(foo);

v.add(c); v.add(foo);

}…

}

Page 21: 6.170 Recitation

Abstraction Function

Just redefine the abstraction function!

[ v[2], v[0], v[4] ] maps to [ x1, x2, x3 ]

Abstraction function defines the convention or the way we interpret the underlying data structure for the abstraction it represents.

Page 22: 6.170 Recitation

Representation InvariantIf I decide that my representation invariant is the following:

v != null &&

v.size() == 6 &&

all elements in v are Integers &&

foo == Integer(666)

Then all operations: constructors, producers, mutators, observers MUST maintain this property!!!

Page 23: 6.170 Recitation

Representation InvariantIf I decide that my representation invariant is the following:

v != null &&

v.size() == 6 &&

all elements in v are Integers &&

foo == Integer(666)

Suppose I take out the last clause, then what does this imply?

Then the operators: the constructors, producers, mutators, observers cannot always expect foo == Integer(666) in their implementation!

Page 24: 6.170 Recitation

Representation Invariant

The idea behind Representation Invariant is to allow the implementation of each operation to agree on a set of assumptions about the data structure!

Page 25: 6.170 Recitation

Quiz 1 Questions?

Good luck on the quiz!