problem of the day rich old man tells his 2 children he will hold a race to decide who gets his...

37
Problem of the Day Rich old man tells his 2 children he will hold a race to decide who gets his fortune. Winner is one who owns SLOWEST horse. 1 st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise man Upon leaving the wise man, kids

Upload: estella-fields

Post on 28-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Problem of the Day

Rich old man tells his 2 children he will hold a race to decide who gets his fortune. Winner is one who owns SLOWEST horse.

1st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise

man Upon leaving the wise man, kids jump

on horses Race at top speed to the city.

What did the wise man say?

Page 2: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Problem of the Day

Rich old man tells his 2 children he will hold a race to decide who gets his fortune. Winner is one who owns SLOWEST horse.

1st two weeks, race is aimless mess, never to end Missing their families, kids consult a wise

man Upon leaving the wise man, kids jump

on horses Race at top speed to the city.

What did the wise man say? He told them to ride the other's horse!

Page 3: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

LECTURE 32:SEQUENCE ADT

CSC 212 – Data Structures

Page 4: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

All elements available when stored in a List Without removing a thing can access all elements Add element anywhere in the List For existing location, set changes element in-

place Lists implement Iterable interface

iterator() returns Iterator over elements Instance of 2nd class returned by this method

Iterable makes it easy to access elements Use for-each to handle any Iterable instance

List ADT

Page 5: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods Could use both absolute & relative ordering Conversion methods enable jumping back-

and-forth

Identical Access w/Iterable?

Page 6: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods

Identical Access w/Iterable?

Page 7: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods

Identical Access w/Iterable?

Page 8: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

But Lists still differ in how they are organized To use ArrayList need absolute index (an int)

Positions (references) used by NodeList Ideally have ADT with both sets of

methods

Identical Access w/Iterable?

Page 9: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Page 10: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Page 11: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Page 12: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Sequence ADT

Combines DEQUE, INDEXLIST, & POSITIONLIST Includes all methods defined by these

interfaces Adds 2 methods to convert between

systems Get Position at index using atIndex(i) indexOf(pos) returns index of a Position

Page 13: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

interface Sequence<E> extends IndexList<E>, PositionList<E>,

Deque<E> {public Position<E> atIndex(int r) throws BoundaryViolationException;

public int indexOf(Position<E> p) throws InvalidPositionException;

}

As ADT, must be implementation independent Specify Position okay, but not it's implementation SEQUENCE class free to use array or linked list

SEQUENCE Interface

Page 14: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Actually used by all of Java’s List classes Combines everything so far into one simple

idea Can serve as basis of STACK, QUEUE, or DEQUE Small, simple databases built from Sequences

Basic building block for many other structures Many use SEQUENCE somewhere in

implementation Need to know how Position & index works Use big-Oh costs to select which implementation

Why Use a Sequence?

Page 15: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Actually used by all of Java’s List classes Combines everything so far into one simple

idea Can serve as basis of STACK, QUEUE, or DEQUE Small, simple databases built from Sequences

Basic building block for many other structures Many use SEQUENCE somewhere in implementation Need to know how Position & index works Use big-Oh costs to select which

implementation

Why Use a Sequence?

Page 16: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Easier & faster if nodes doubly-linked Position-based methods come from

NODELIST To find indices, need to traverse Nodes

Could use Nodes via getNext() & getPrev()

next() & prev() if want Position traversal

Linked list-based Sequence

Nodes

List

Page 17: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Implementing atIndex(i)

Nodes

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 18: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Suppose call was:atIndex(3)

i 3trav

Implementing atIndex(i)

Nodes

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 19: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Suppose call was:atIndex(3)

i 3trav

Implementing atIndex(i)

Nodes

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 20: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Nodes

Suppose call was:atIndex(3)

i 3 2trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 21: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Nodes

Suppose call was:atIndex(3)

i 3 2 1trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 22: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Nodes

Suppose call was:atIndex(3)

i 3 2 1 0trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 23: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Position

Suppose call was:atIndex(3)

i 3 2 1 0trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfi

Page 24: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Nodes

Suppose call was:atIndex(3)

i 3 2 1 0trav

Implementing atIndex(i)

List

Algorithm atIndex(i)if (i < 0) OR (i ≥ size()) then

throw BoundaryViolationExceptionfitrav first()while (i > 0) do

trav next(trav) i--

endwhilereturn trav

Page 25: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Based on IndexList Use and resize

array Easy to implement Store elements in

arrayS

0 1 2 3 N-1

List

Page 26: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Based on IndexList Use and resize

array Easy to implement Store elements in

array

What about Positions? NodeList's

methods needs to use them

atIndex() returns it

We do not have them!

S0 1 2 3 N-1

List

Page 27: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Array of Positions Can be used in

methods

Position contains: Element Index in array

No next or prev links Instead use the

index Update index when Position shifted

0 1 2 3

S0 1 2 3 N-1

List

Position

Page 28: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm next(pos)if (pos == S[size()-1]) then throw BoundaryViolation…else idx pos.getIndex() return S[idx+1]fi

end0 1 2 3

S0 1 2 3 N-1

List

Position

Page 29: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

0 1 2

S0 1 2 3 N-1

List

Position

Page 30: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

0 1 3

S0 1 2 3 N-1

List

Position

Page 31: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

0 2 3

S0 1 2 3 N-1

List

Position

Page 32: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

1 2 3

S0 1 2 3 N-1

List

Position

Page 33: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */

1 2 3

S0 1 2 3 N-1

List

Position

0

Page 34: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */for i size() downto 1 do S[i] S[i-1] S[i].setIndex(i)endforS[0] new Node(0, e)size++

end

1 2 3

S0 1 2 3 N-1

List

Position

0

Page 35: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Array-based Sequence

Algorithm addFirst(e)/* Resize array if needed */for i size() downto 1 do S[i] S[i-1]

S[i].setIndex(i)endforS[0] new Node(0, e)size++

end

1 2 3

S0 1 2 3 N-1

List

Position

0

Page 36: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

Your Turn

Get into your groups and complete activity

Page 37: Problem of the Day  Rich old man tells his 2 children he will hold a race to decide who gets his fortune. SLOWEST  Winner is one who owns SLOWEST horse

For Next Lecture

Review GT Chapter 6 for Wednesday’s Quiz Do your best to understand what all this is

about How do we use these in real-world problems What are the big-Oh complexities of these

methods?

As usual, week #11 assignment due tomorrow

Programming Assignment #2 due in 5 days If you have not started do not delay, will

take time