ucd computer science comp-2001 1 vectors, lists and sequence stacks, queues, deques -- access...
Post on 19-Dec-2015
213 views
TRANSCRIPT
1UCD Computer Science COMP-2001
• Vectors, Lists and Sequence• Stacks, queues, deques -- access elements only at
the “ends”– Useful for applications that involve serially processing
incoming data in some specific order
• Sequences, lists, vectors -- access elements in the “middle”, too– Useful for storing items that might be needed in any order
Lectures 12-14-15-16
2UCD Computer Science COMP-2001
The Vector ADT• The rank of an item in a vector is a count of the number of items that
occur before it:Seattle Rome Montreal Paris
• Vectors support the following methods:- size(), isEmpty() as usual-elemAtRank(r): Return the element with rank r;
exception thrown if r<0 or r size()-replaceAtRank(r,e): Replace the element at rank r with e
and return the old element; exception thrown if r<0 or r size()
-insertAtRank(r,e): Insert a new element into S which will have rank r (and therefore ranksof subsequent elements will increase!);exception thrown if r<0 or r size()
-removeAtRank(r): Remove the element at rank r (and therefore ranks of subsequent elementswill decrease); exception if r<0, r size()
Rank = 0 1 2 3
“after”“before”
3UCD Computer Science COMP-2001
Vector interfacepublic interface Vector {
public int size();public boolean isEmpty();
public Object elemAtRank(int r)throws InvalidRankException;
public Object replaceAtRank(int r, Object o)throws InvalidRankException;
public void insertAtRank(int r, Object o)throws InvalidRankException;
public Object removeAtRank(int r)throws InvalidRankException;
}
4UCD Computer Science COMP-2001
Implementing Vectors with Arrays
public class ArrayVector implements Vector {…
protected int N; // max capacityprotected int n; // no. elements storedprotected Object S[];public ArrayVector(int capacity) { N = capacity; n = 0; S = new Object[N];}
…}
used
inactive
…
rank(S[i]) i
5UCD Computer Science COMP-2001
Some pseudo code
Algorithm insertAtRank(r,e):for i = n - 1, n - 2, ... , r do S[i+1] s[i]S[r] en n + 1
Algorithm removeAtRank(r):e S[r]for i = r, r + 1, ... , n - 2 do S[i] S[i + 1]n n - 1return e
shift elements to the rightin order to squeeze “e” in here
shift elements left tofill the gap left by e here
Algorithm elemAtRank(r):return S[r];
Algorithm replaceAtRank(r,e):x = elemAtRank(r);S[r]=e;return x;
For simplicity,exception-throwing
code not shown
6UCD Computer Science COMP-2001
Array-Based Implementation (contd.)
• Time complexity of the various methods:
Ouch!
remember, O(1) means “constant” (independent of n)
7UCD Computer Science COMP-2001
Problems with ArrayVector• 1. As just mentioned, some methods are
expensive [O(n) complexity instead of O(1)]; we’ll deal with this later
• 2. Predefined fixed capacity.One solution: “extendable (‘self-extending’) arrays”: if we ever run out of room, just create more!
8UCD Computer Science COMP-2001
Extendable arrays• 1. During initialization, use fixed capacity as
before (either application-supplied argument or some default value)
• 2. When run out of room, allocate a new array of size (say) double the current size; copy over all the old elements to the new array
• 3. Carry on as before….
• (4. Could also clean up wasted space if the vector is persistently under-capacity; but we won’t bother)
9UCD Computer Science COMP-2001
ExtendableArrayVectorclass ExtendableArrayVector extends ArrayVector {
// all we need to do is overload one method!public insertAtRank(int r, Object o) {
if (n = = N) { // over capacity!N *= 2;
Object S2[] = new Object[N];
for (int i=0; i<n; i++) S2[i]=S[i];S = S2;
}
// now the original implementation does the job!
super.insertAtRank(r,o); // call ArrayVector’s method
}
} See “Vector” demo for complete details
10UCD Computer Science COMP-2001
Linked-list implementation of Vector• A second problem with the Array implementation
is the need to slide elements during insert & remove -> causes O(n) [instead of O(1)] performance
• Sliding not needed if we use a doubly-linked listpublic class LinkedListVector implements Vector {
private DLNode header;private DLNode trailer;public LinkedListVector() {
header = new DLNode(null,null,null);trailer = new DLNode(null,header,null);header.setNext(trailer);
} }
…
11UCD Computer Science COMP-2001
Vector Implementation with a Doubly Linked List
1. the list before insertion
2. creating a new node
3. after insertion:
12UCD Computer Science COMP-2001
public void insertAtRank (int r, Object o) throws InvalidRankException { if (r < 0 || r > size()) throw new InvalidRankException() DLNode next = nodeAtRank(r); // the new node will be right before ‘next’ DLNode prev = next.getPrev(); // the new node willl be right after ‘prev’ DLNode node = new DLNode(o, prev, next);
// new node knows about its next & prev. Now// we tell next & prev about the new node.
next.setPrev(node); prev.setNext(node);
size++; }
Some Java
13UCD Computer Science COMP-2001
Deletion from Doubly Linked List
the list before deletion:
deleting a node
after deletion:
14UCD Computer Science COMP-2001
More Java
public Object removeAtRank (int r) throws InvalidRankException {if (r < 0 || r > size()-1) throw new InvalidRankException();DLNode node = nodeAtRank(rank); // node to be removedDLNode next = node.getNext(); // node before node to be removedDLNode prev = node.getPrev(); // node after node to be removedprev.setNext(next);next.setPrev(prev);size--;return node.getElement(); // returns the element of the deleted node
}
15UCD Computer Science COMP-2001
One last Java detail…• code for finding the node at a given rank r
private DLNode nodeAtRank (int r) { DLNode node; // start at the node closest to the desired rank if (r <= size()/2) { //scan forward from header
node = header.getNext();for (int i=0; i < r; i++) node = node.getNext();
} else { // scan backward from trailernode = trailer.getPrev(); for (int i=0; i < size()-r-1 ; i++) node = node.getPrev();
} return node;
}
very common“pointer walking”
expression
either way, as many as n/2 iterations of this loop,therefore this method is O(n). Therefore….
16UCD Computer Science COMP-2001
LinkedList Vector- Analysis• Time complexity of the various methods:
Ouch!O(n)O(n)
Compare with slide #7Oops, now all methods run slowly!
17UCD Computer Science COMP-2001
Vector Summary• Simple Vector ADT - access/remove elements by
“rank”• Leads to simple array-based implementation• 2 problems:
– Q1. Can we get unlimited capacity?- yes - use “extendable” array that creates
more space on demand as needed– Q2. Can we avoid shifting elements during
insert/remove?- ?? - linked lists save this shifting cost, but add
new cost of needing to walk list to find elements by rank; overall, linked-list implementation is worse!
18UCD Computer Science COMP-2001
Lists• Lists are a generalization of Vectors:
Instead of the “rank” of an element in a list; use the more generic notion of “position” of an element in a list
• The goal: ensure that access/insert/remove methods run in O(1) time!
Lecture 14
the dog sat on my cata list of 5 words
position(the) is the first positionposition(cat) is the last position
position(dog) is before position(sat)position(on) is after position(on)
19UCD Computer Science COMP-2001
Position ADT• Positions are very simple “helper” data-type
• One operation!
element() returns the data-value elementassociated with the position
(seem a bit like magic -- How can a Positionpossibly know?!?!! -- but it will all makesense soon…)
20UCD Computer Science COMP-2001
List ADTsize() & isEmpty() as usual…first() returns the Position of the first element
throws InvalidPositionException if emptylast() returns the Position of the last element
throws InvalidPositionException if emptyisFirst(p) is p the first position?isLast(p) is p the last position?before(p) return the Position immediately before p;
throws InvalidPositionException if p is firstafter(p)return the Position immediately after p
throws InvalidPositionException if p is lastinsertFirst(e) insert e at the first entry, and return the
Position of this new first entryinsertLast(e) insert e at the last entry, and return the
Position of this new last entryinsertBefore(p,e) insert e immediately before Position p
and return Position object for einsertAfter(p,e) insert e immediately before Position p
and return Position object for eremove(p) remove element at Position p; return the removed element Unlike ranks, note that Positions of ‘unaffected’ elements aren’t modified!
21UCD Computer Science COMP-2001
Implementing Lists with Doubly Linked-Lists
• pages 199-205:
class NodeList implements List
• Code in the book is somewhat more complicated than I’ve said so far– 3 kinds of exceptions instead of 1– Details like determining that a Position is “valid” (ie,
a member of this’s list, not some other list)
22UCD Computer Science COMP-2001
Detailsinterface Position {
Object element();}
our linked lists will be built from node objects which will “double” as our Position objects:
public class DNode implements Position { // page 199// first, stuff related to Positionprivate Object element;Object element() { return element; }// next, stuff related to linked list nodeprivate DNode next;private DNode prev;
…}
23UCD Computer Science COMP-2001
More detailsWhy so complicated??!? Where went the distinction between abstract Positions and concrete Nodes?!!?The intent is that your application shouldn’t need to know that List is implemented using a linked list. But obviously there is a direct correspondence between Positions in the List abstraction, and nodes in the linked list implementation. So we’ll use a single class that serves both purposes, and use ‘private’ to prevent application for learning implementation-specific details.
DNode
inside NodeList implementation
components for building linked lists
your application
your applicationcan interpretthese objects
only as Positions
24UCD Computer Science COMP-2001
NodeList analysis
all List methods(first, last, isFirst, isLast, before, after, replaceElement, swapElements, insertFirst, insertLast, insertBefore, insertAfter, remove)
O(1)
Horray!All methodsare fast(constant-time)
However, what if our application needs bothrank and position-based access.That’s where Sequence comes in…
25UCD Computer Science COMP-2001
Sequences
• Sequence = Vector List
• This week:- finish P6- start P7- finish Chapter 5
Next week:
- finish P7
- start P8
- start Chapter 6
Lec 15
26UCD Computer Science COMP-2001
Sequence• Provides both rank- and position-based access, and
“bridge” methods for converting between them
the dog sat on my cat“dog” is in this position
“cat” is in this position after rank 4
0 1 2 3 4 5
“sat” is at rank 2 “my” is at rank 4
(“bridge”)
27UCD Computer Science COMP-2001
Sequence ADT• Everything required for Vector and List,
plus two new methods:
– atRank(r) return the Position associated withthe given rank
– rankOf(p) return the rank associated withPosition p
28UCD Computer Science COMP-2001
Sequence Interfaceinterface Sequence extends List, Vector {
Position atRank(int r);int rankOf(Position p);
}
Multiple inheritance - this interface has two parents!?!?!Actually, Java permits only a very weak form: only
interfaces can have multiple super-classes - ie, only “promises” can be inherited.
Other languages (Lisp, C++, Smalltalk, …) allow classes to have multiple super-classes; ie, actual methods can be inherited from multiple parents.
29UCD Computer Science COMP-2001
Interface vs class multiple inheritanceinterface X { int x(); String y(int a); void z(String u);}
interface Y { String p(int a); double q(); void z(String u);}
class C implements X, Y { …}
C must implement allthese methods;
no problempromising z twice
class X { int x() {…} String y(int a) {…} void z(String u) {…}}
class Y { String p(int a) {…} double q() {…} void z(String u) {…}}
class C extends X, Y {}
C inherits allthese methods…
C c = …c.z(“fishfood”); which implementation of z should be called?!?!!
…but
30UCD Computer Science COMP-2001
Implementing Sequence with Doubly Linked ListG&T page 208
class NodeSequence extends NodeList implements Sequence
(why do we need to specify both extends and implements?)
L14,pp 199-205
•Inherited from NodeList• first, last, isFirst, isLast, before, after, replaceElement, swapElements, insertFirst, insertLast, insertBefore, insertAfter, remove
•Defined in NodeSequence (not inherited)
• atRank, rankOf, [next slide…]
elementAtRank, [next slide…]
insertAtRank, removeAtRank [p 208]
31UCD Computer Science COMP-2001
NodeSequence [cont]• bridge methods
Position atRank(int rank) [see p 208 for Java]repeat:
jump from header, to header.next, to header.next.next, …,until ‘rank’ nodes have been passedreturn currrent node
int rankOf(Position p)repeat
jump from from p to p.prev, to p.prev.prev, …,until header node is encounteredreturn the number of nodes passed
p3 2 1
atRank(2)0 1 2
both run
in time
O(n) !
32UCD Computer Science COMP-2001
rankOf - Java
int rankOf(Position p) {
DNode n = (DNode) p;int rank = 0;
while (n != header) {
n = n.getPrev();
rank++;
}
return rank;
}
we know this Positionis in fact a DNode, so thiscasting is OK
33UCD Computer Science COMP-2001
NodeSequence [cont]• With bridge methods in place, the methods
required by Vector are very simple:[exception-handling code omitted for simplicity]
Object elemAtRank(int rank) {return atRank(rank).element()
}void insertAtRank(int rank, Object element) {
insertBefore(atRank(rank), element);}Object removeAtRank(int rank) {
return remove(atRank(rank));}Object replaceAtRank(int rank, Object element) {
return replaceElement(atRank(rank),element);}
[see p 208for details]
34UCD Computer Science COMP-2001
Analysis of NodeSequence
O(n)O(n)
all List methods(first, last, isFirst, isLast, before, after, replaceElement, swapElements, insertFirst, insertLast, insertBefore, insertAfter, remove)
Vector methods
Slow because
bridge method
atRank is slow!
O(1)
35UCD Computer Science COMP-2001
Implementing Sequence with Array• The ArrayList -vs- NodeList implementations of List suggest that linked-lists
implementations are inherently slow for rank-based access, and array-based implements are inherently slow for position-based access. Is this O(n) -vs- O(1) tradeoff is inevitable?
• No … advanced data structures you’ll learn about in the future such as hash tables can give fast access in both cases… but they’re too complicated for now…
• As a suggestion of things to come… ArraySequenceuses arrays instead of linked lists, but with a clever special ‘trick’ to improve complexity of some of the methods
36UCD Computer Science COMP-2001
ArraySequence - naïve versionclass ArrayPosition implements Position{
Object A[];index i;ArrayPosition(Object[] B, int j) {A=B; I=j;}Object element() {return A[i];}
}
A: i: 3
the dog sat on my cat0 1 2 3 4 5
ArrayPosition object for “on”
A: i: 1
ArrayPosition object for “dog”
37UCD Computer Science COMP-2001
ArraySequence: inserting an element
A: i: 3
the dog sat on my cat0 1 2 3 4 5
A: i: 0
A: i: 2
A: i: 4
A: i: 5
A: i: 6
Sequence s = new ArraySequence();… other elements inserted …
Position p = s.insertLast(“cat”);s.insertBefore(p, “fat”);
pThese ArrayPositionsAren’t stored in theArraySequence; theapplication must storethem for later use.
38UCD Computer Science COMP-2001
Insertion, continuedA: i: 2
the dog sat on my cat0 1 2 3 4 5 6
A: i: 0
A: i: 1
A: i: 3
A: i: 4
A: i: 5
p
Slide existing elements(just like ArrayVector.insertAtRank)
A: i:2
the dog sat on my fat cat0 1 2 3 4 5 6
A: i: 0
A: i: 1
A: i: 3
A: i: 4
A: i: 5
A: i: 5
Yikes!Need toincrementranks…
But how??!?!
39UCD Computer Science COMP-2001
ArraySequence.insertAtRank• Algorithm insertBefore(Position p, Object o)
– For r = size(), size()-1, …, rankOf(p)+2, rankOf(p)+1:A[r] = A[r-1];
– A[r] = o;– size++– // yikes! We need to update Positions corresponding to ranks #size
down to #rankOf(p)+1 -- but the ArraySequence doesn’t keep a list of the Positions -- that’s the application’s responsibility!
– Return new ArrayPosition(A,r);
This naïve approach doesn’t work!!
40UCD Computer Science COMP-2001
Sequences, continued• Where are we?
Vector - rank-based accessarray-based implementation - insert/remove are slowlinked-list implementation - all methods slow!
List - position-based accesslinked-list implementation - all methods fast!
Sequence - both kinds of accesslinked-list implementation - as before, rank-based
methods are slowstarted array-based implementation but hit dead-endto do: a better array-based implementation
L16
41UCD Computer Science COMP-2001
ArraySequence: Cleverer approachclass ArrayPosition implements Position {
int rank;Object element;ArrayPosition(int r, Object e) {rank=r; element=e;}Object element() {return element;}
}
elemen
t: “th
e”
rank:
0
0 1 2 3 4 5
elemen
t: “d
og”
rank:
1
elemen
t: “s
at”
rank:
2
elemen
t: “o
n”
rank:
3
elemen
t: “m
y”
rank:
4
elemen
t: “c
at”
rank:
5
42UCD Computer Science COMP-2001
Cleverer ArraySequence• Still need to shift elements during insert/remove.• But now… since the SequenceArray “remembers” the
ArrayPositions it has “exported” to the application, there’s no problem to modify the ranks of the moved elements.
43UCD Computer Science COMP-2001
ArraySequence vs NodeSequence: Analysis
• Operation Array Nodesize, isEmpty O(1) O(1)atRank, rankOf, elemAtRank O(1) O(n)first, last, before, after O(1) O(1)replaceElement, swapElements O(1) O(1)replaceAtRank O(1) O(n)insertAtRank, removeAtRankO(n) O(n)insertFirst, insertLast O(1) O(1)insertAfter, insertBefore O(n) O(1)remove O(n) O(1)
There’s no free lunch…
If you need these operations, use ArraySequence
If you need these operations, use NodeSequence
44UCD Computer Science COMP-2001
Application example: Sorting• Sorting: Given some sequence (Sally, John, Dave, Ellen,
Pat), output a permutation of the elements in order (Dave, Ellen, John, Pat, Sally)
• Dozens of algorithms; person-centuries of research• Well known result: the fastest possible sorting algorithm
runs in time O(n · log n) to sort n items• A very simple algorithm: Bubble Sort - O(n2)• The point isn’t to build a great algorithm, but to show
how the Sequence ADT might be used in practice
45UCD Computer Science COMP-2001
Bubble sort10
8
1
4
6
First pass: Start at top, compare adjacent pairs, swap larger toward bottom if needed8
10
1
4
6
8
1
10
4
6
8
1
4
10
6
8
1
4
6
10
At the end of the first pass,largest element is guaranteedto be at bottom
2nd pass: Start at top, compare adjacent pairs, swap larger toward bottom if needed8
1
4
6
10
1
8
4
6
10
1
4
8
6
10
1
4
6
8
10
At the end of the 2nd pass,2nd-largest element is guaranteedto be at 2nd-from-bottom
1
4
6
8
10
1
4
6
8
10
1
4
6
8
10
1
4
6
8
10
1
4
6
8
10
3rdpass(no swapsneeded)
4thpass(no swapsneeded)
1
4
6
8
10
done
46UCD Computer Science COMP-2001
Bubble-sort using ranksvoid bubbleSort(Sequence S) {
int n = S.size();for (int i = 0; i<n; i++) { // i’th pass for (int j = 1; j<n-i; j++)
if (valAtRank(S,j-1) > valAtRank(S,j)) S.swapElements(S.atRank(j-1),
S.atRank(j));}
}
int valAtRank(Sequence S, int i) {return ((Integer) S.elemAtRank(i)).intValue();
} Assume S contains Integer objects
47UCD Computer Science COMP-2001
Bubble-sort using positionsvoid bubbleSort(Sequence S) {
int n = S.size();for (int i = 0; i<n; i++) { // i’th pass Position prec = S.first(), succ; for (int j = 1; j<n-i; j++) {
succ = S.after(prec);if (valAtPos(prec) > valAtPos(succ)) S.swapElements(prec,succ);prec = succ;
}}
}
int valAtPos(Position p) {return ((Integer) p.element()).intValue();
}
48UCD Computer Science COMP-2001
Iterators• A common operation on a sequence is to perform some
operation on each item in turn.– Print out each element, add 1 to each element in a list of
integers, remove spaces from each element in a list of strings, …
• Iterator (also known as Enumerator) is an ADT encapsulating this notion of “walking along the elements of a list”.Two operations:– hasNext() Are there more items?
– nextObject() Returns next object (if there is one)
49UCD Computer Science COMP-2001
SequenceIteratorinterface ObjectIterator {
boolean hasNext();Object nextIterator();
}
class SequenceIterator implements ObjectIterator {Sequence S; // the sequence over which we’re iteratingDNode node; // current position in SSequenceIterator(Sequence _S) {
S = _S;node = (DNode) S.first();
}boolean hasNext() { return node != S.trailer; }Object nextObject() {
Object o = node.element();node = node.getNext();return o;
}}
50UCD Computer Science COMP-2001
Iterator: example• Now you can say…
Sequence S = …
SequenceIterator si = new SequenceIterator(S);
while (si.hasNext()) {
System.out.println(si.nextObject() + “, “);
}
51UCD Computer Science COMP-2001
Iterators in java• Java has built-in iterators for java.util.Vector
• (Actually, a variety of iterators for all of the varied/complicated kinds of “containers”)
Import java.util.*;…
Vector v = …
Iterator i = v.iterator();
while (i.hasNext()) {System.out.println(i.next());
}
52UCD Computer Science COMP-2001
Lec 7 - Lec 16 - SummaryContainersize, isEmpty
Stackpush, pop
Queueenqueue, dequeue
Deque
VectorinsertAtRank, …
ListinsertBefore, …
SequencerankOf, atRank
ADTs
Implementation techniquesArrayextendable arrayLinked listsdoubly-linked listsPositions vs array indices
Complexity results exposetradeoffs between requirementsof ADTs and efficiencyof implementation