CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only Submit...

28
CSC 212 Vectors, Lists, & Sequences
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    212
  • download

    0

Transcript of CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only Submit...

Page 1: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

CSC 212

Vectors, Lists, & Sequences

Page 2: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Announcement

Daily quizzes accepted electronically onlySubmit via one or other DropboxE-mail also accepted, if necessary

Next homework assignment due Thursday Midterm in a little over one week

Will cover through chapter 5 of bookMidterm will be open book, open note (but, closed

computer)Thursday’s lecture includes a review

Page 3: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Rank of Element

Describes the position of element in a list Item at front of list has rank of 02nd item has rank of 1; 3rd item has rank of 2Generally, nth item has rank of n – 1

Page 4: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Ranking Details

Ranks do not depend on arraysThey specify how to order lists of elements

independent of implementation details Arrays can maintain ranks, but so can:

Linked listsDoubly linked listsSpecially trained monkeys…

Page 5: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Vector ADT

Vector is first type which uses ranks Similar to previous ADTs in that it defines

int size()boolean isEmpty()

Differs from stacks, queues, and dequesCan insert and remove any elementCan also replace a specific element

Page 6: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Item Retrieval

Any element in a Vector can be retrieved using Object elemAtRank(int r)

Note: this is different than using an arrayObject[] arr;Vector vect;...Object o = arr[4]; // legalo = vect[4]; // No!o = vect.elemAtRank(4) // legalo = arr.elemAtRank(4) // No go!

Page 7: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Insertion into Vectors

Insertion can occur anywherevoid insertAtRank(int r, Object e)

Inserts object e at rank r Increases rank of objects now after it

What exceptions do we need to include?

Page 8: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Removal from Vectors

Can also remove any elementObject removeAtRank(int r)

Removes object at rank r Decreases rank of elements after object being

removed

Could this throw any exceptions beyond those defined by insertAtRank()?

Page 9: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

New Option Excitement!

Vectors can also replace elementsObject replaceAtRank(int r, Object e)

Throws same exceptions as previous methods Returns element previously at rank r Ranks of remaining elements are unchanged

Page 10: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Vector Implementation

What could we use to implement vectors and how?

Page 11: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Array-based Vector

Arrays make implementing Vectors simple, except…

… inserting an element. What is big-Oh?

Vector 0 1 2 r

Vector 0 1 2 r

Vector0 1 2

er

Page 12: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Array-based Vector

Removing from a Vector is not easy either…

Vector 0 1 2 r

Vector0 1 2

er

Vector 0 1 2 r

Page 13: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Array-based Vector

How would you implement size()?Vector 0 1 2 r

Page 14: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Linked Lists Not a Panacea

What is complexity of insertAtRank, removeAtRank, and replaceAtRank?

Page 15: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Vector Growth

Using linked lists removes size limitBut we could get unbounded size using arrays

Whenever array fills, allocate larger arrayObject[] temp = new Object[...];for (int i = 0; i < arr.size; i++)

temp[i] = arr[i];arr = temp;

Page 16: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Growth Strategies

Two different strategies for growth:Grow by constant, c, each timeDouble size each time

Both have same big-Oh complexity.What is it?When does it occur?

Instead compare amortized (average) timeConsider inserting n elements

Page 17: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Constant Growth

Need to grow k = n/c timesEach time we grow we must copy entire arrayTotal copies is:

1 + (c+1) + (2c+1) + • • • + ((k-1)*c)+1 + k*c+1= k*c+2 + k*c+2 + • • • + k*c+2= k/2 * ((k*c)+2)= ½ck2 + 1= O(n2)

For each of n inserts, avg complexity is O(n)

Page 18: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Doubling Growth

Array grows k = log n timesEach time we grow we must copy entire arrayTotal copies:

1 + 2 + 4 + • • • + 2k-1 + 2k

= 2k + 2k

= 2k+1

= 2 * 2log n

= 2 * nFor each insert, average complexity only O(1)

Page 19: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Adapter Pattern

Implementation using other class instanceExample: Implementing Stack with Deque

Page 20: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Adapter Pattern

public class Stack { protected Deque deque; public Stack() {deque = new Deque();} public int size() { return deque.size(); } public boolean isEmpty() { return deque.isEmpty(); } public void push(Object e) {

deque.insertFirst(e); } •••

Page 21: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Positions

Sometimes rankings may not make senseElements may not have absolute positionsMay not want to pay time overhead

Instead use relative rankingsRetrieve elements before or after current oneAdd new element before or after current oneReplace/remove current elementGet first/last elements

Page 22: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Position ADT

ADT implementing these relative rankings

public interface Position { public Object element();}

Page 23: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

List ADT

Simplest collection of Positions is List Defines usual int size() & boolean isEmpty() methods

Page 24: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

List ADT

Also defines accessors:Position first()

Position last()

Position next(Position p) throws InvalidPositionException, BoundaryViolationException

Position prev(Position p) throws InvalidPositionException, BoundaryViolationException

Page 25: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

List ADT

Defines update methods:insertFirst, insertLast, insertBefore, insertAfter

Takes an object and returns the Position

remove Removes the position passed in as parameter

replace Substitutes object for element currently at position

Page 26: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Implementation of List

How could we implement List & Position?

Page 27: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Sequences

Sequences combine List and Vector ADTs Also enables conversion between the two

atRank – returns the position object for a given rank

rankOf – given a position, returns its rankpublic interface Sequence extends List, Vector {public Position atRank(int r) throws •••

public int rankOf(Position p) throws •••

}

Page 28: CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  E-mail also accepted,

Daily Quiz

Use adapter pattern to write a Deque implementationAdapt using an implementation of VectorA vector class, LLVector, the exceptions

LLVector needs, and the Deque & Vector interfaces available on quiz page.

Useful for student looking to practice Java skills by compiling/testing solutions