CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic...
-
date post
21-Dec-2015 -
Category
Documents
-
view
225 -
download
0
Transcript of CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic...
CSC401 – Analysis of AlgorithmsCSC401 – Analysis of Algorithms Lecture Notes 3Lecture Notes 3
Basic Data StructuresBasic Data StructuresObjectives:Objectives:
Introduce basic data structures, including Introduce basic data structures, including – StacksStacks– QueuesQueues– VectorsVectors– ListsLists– SequencesSequences
Analyze the performance of operations on Analyze the performance of operations on basic data structuresbasic data structures
22
Abstract Data Types (ADTs)Abstract Data Types (ADTs)An abstract An abstract data type (ADT) data type (ADT) is an is an abstraction of a abstraction of a data structuredata structureAn ADT An ADT specifies:specifies:– Data storedData stored– Operations on Operations on
the datathe data– Error conditions Error conditions
associated with associated with operationsoperations
Example: ADT modeling Example: ADT modeling a simple stock trading a simple stock trading systemsystem– The data stored are The data stored are
buy/sell ordersbuy/sell orders– The operations supported The operations supported
areareorder order buybuy(stock, shares, (stock, shares, price)price)
order order sellsell(stock, shares, (stock, shares, price)price)
void void cancelcancel(order)(order)
– Error conditions:Error conditions:Buy/sell a nonexistent stockBuy/sell a nonexistent stock
Cancel a nonexistent orderCancel a nonexistent order
33
The Stack ADTThe Stack ADTThe The StackStack ADT stores arbitrary ADT stores arbitrary objectsobjectsInsertions and deletions follow Insertions and deletions follow the last-in first-out schemethe last-in first-out schemeThink of a spring-loaded plate Think of a spring-loaded plate dispenserdispenserMain stack operations:Main stack operations:– pushpush(object): inserts an element(object): inserts an element– object object poppop(): removes and (): removes and
returns the last inserted elementreturns the last inserted element
Auxiliary stack operations:Auxiliary stack operations:– object object toptop(): returns the last (): returns the last
inserted element without inserted element without removing itremoving it
– integer integer sizesize(): returns the (): returns the number of elements storednumber of elements stored
– boolean boolean isEmptyisEmpty(): indicates (): indicates whether no elements are storedwhether no elements are stored
Attempting the Attempting the execution of an execution of an operation of ADT may operation of ADT may sometimes cause an sometimes cause an error condition, called error condition, called an exceptionan exception
Exceptions are said to Exceptions are said to be “thrown” by an be “thrown” by an operation that cannot operation that cannot be executedbe executedIn the Stack ADT, In the Stack ADT, operations pop and top operations pop and top cannot be performed if cannot be performed if the stack is emptythe stack is empty
Attempting the execution Attempting the execution of pop or top on an of pop or top on an empty stack throws an empty stack throws an EmptyStackExceptionEmptyStackException
44
Applications of StacksApplications of StacksDirect applicationsDirect applications– Page-visited history in a Web Page-visited history in a Web
browserbrowser– Undo sequence in a text editorUndo sequence in a text editor– Chain of method calls in the Chain of method calls in the
Java Virtual MachineJava Virtual Machine
Indirect applicationsIndirect applications– Auxiliary data structure Auxiliary data structure
for algorithmsfor algorithms– Component of other Component of other
data structuresdata structures
The Java Virtual Machine (JVM) The Java Virtual Machine (JVM) keeps track of the chain of active keeps track of the chain of active methods with a stackmethods with a stackWhen a method is called, the JVM When a method is called, the JVM pushes on the stack a frame pushes on the stack a frame containingcontaining– Local variables and return valueLocal variables and return value– Program counter, keeping track of Program counter, keeping track of
the statement being executed the statement being executed
When a method ends, its frame is When a method ends, its frame is popped from the stack and control popped from the stack and control is passed to the method on top of is passed to the method on top of thethe stack stack
main() {int i = 5;foo(i);}
foo(int j) {int k;k = j+1;bar(k);}
bar(int m) {…}
bar PC = 1 m = 6
foo PC = 3 j = 5 k = 6
main PC = 2 i = 5
55
Array-based StackArray-based StackA simple way of A simple way of implementing the Stack implementing the Stack ADT uses an arrayADT uses an arrayWe add elements from We add elements from left to rightleft to rightA variable keeps track of A variable keeps track of the index of the top the index of the top elementelementThe array storing the The array storing the stack elements may stack elements may become fullbecome fullA push operation will then A push operation will then throw a throw a FullStackExceptionFullStackException– Limitation of the array-based Limitation of the array-based
implementation implementation– Not intrinsic to the Stack Not intrinsic to the Stack
ADTADT
Algorithm size()return t + 1
Algorithm pop()if isEmpty() then
throw EmptyStackException else
t t 1return S[t + 1]
Algorithm push(o)if t = S.length 1 then
throw FullStackException else
t t + 1S[t] o
PerformancePerformance– Let Let nn be the number of be the number of
elements in the stackelements in the stack– The space used is The space used is OO((nn))– Each operation runs in time Each operation runs in time OO(1)(1)
LimitationsLimitations– The fixed maximum sizeThe fixed maximum size– Trying to push a new Trying to push a new
element into a full stack element into a full stack causes an implementation-causes an implementation-specific exceptionspecific exception
66
Other Implementations Other Implementations of Stackof Stack – Extendable array-based Extendable array-based
stackstack– Linked list-based stack Linked list-based stack
Stack Interface & ArrayStack in JavaStack Interface & ArrayStack in Javapublic interface Stack {
public int size();
public boolean isEmpty();
public Object top()throws EmptyStackException;
public void push(Object o);
public Object pop() throws EmptyStackException;
}
public class ArrayStack implements Stack {private Object S[ ];private int top = -1;
public ArrayStack(int capacity) { S = new Object[capacity]);
}
public Object pop()throws EmptyStackException {
if isEmpty()throw new EmptyStackException
(“Empty stack: cannot pop”);Object temp = S[top];S[top] = null;top = top – 1;return temp;
}}
77
The Queue ADTThe Queue ADTThe The QueueQueue ADT stores ADT stores arbitrary objectsarbitrary objectsInsertions and deletions Insertions and deletions follow the first-in first-out follow the first-in first-out schemeschemeInsertions are at the rear Insertions are at the rear and removals at the front and removals at the front
Main queue operations:Main queue operations:– enqueueenqueue(object): inserts (object): inserts
an element at the end of an element at the end of the queuethe queue
– object object dequeuedequeue(): (): removes and returns the removes and returns the element at the front element at the front
Auxiliary queue operations:Auxiliary queue operations:– object object frontfront(): returns the (): returns the
element at the front without element at the front without removing itremoving it
– integer integer sizesize(): returns the (): returns the number of elements storednumber of elements stored
– boolean boolean isEmptyisEmpty(): indicates (): indicates whether no elements are storedwhether no elements are stored
ExceptionsExceptions– Attempting the execution of Attempting the execution of
dequeue or front on an empty dequeue or front on an empty queue throws an queue throws an EmptyQueueExceptionEmptyQueueException
Direct applicationsDirect applications– Waiting lists, bureaucracyWaiting lists, bureaucracy– Access to shared Access to shared
resources (e.g., printer)resources (e.g., printer)– MultiprogrammingMultiprogramming
Indirect applicationsIndirect applications– Auxiliary data structure for Auxiliary data structure for
algorithmsalgorithms– Component of other data Component of other data
structuresstructures
88
Array-based QueueArray-based Queue
Use an array of size Use an array of size NN in a circular fashion in a circular fashionTwo variables keep track of the front and rearTwo variables keep track of the front and rearff index of the front elementindex of the front elementrr index immediately past the rear elementindex immediately past the rear element
Array location Array location rr is kept empty is kept empty
Q0 1 2 rf
normal configuration
Q0 1 2 fr
wrapped-around configuration
99
Array-based Queue OperationsArray-based Queue OperationsWe use the modulo We use the modulo operator (remainder of operator (remainder of division)division)Operation enqueue Operation enqueue throws an exception if throws an exception if the array is fullthe array is fullThis exception is This exception is implementation-implementation-dependentdependentOperation dequeue Operation dequeue throws an exception if throws an exception if the queue is emptythe queue is emptyThis exception is This exception is specified in the queue specified in the queue ADTADT
Algorithm size()return (N f + r) mod N
Algorithm isEmpty()return (f r)
Algorithm enqueue(o)if size() = N 1 then
throw FullQueueException else
Q[r] or (r + 1) mod N
Algorithm dequeue()if isEmpty() then
throw EmptyQueueException else
o Q[f]f (f + 1) mod Nreturn o
1010
Other Implementations of QueueOther Implementations of Queue – Extendable array-based queue: The enqueue Extendable array-based queue: The enqueue
operation has amortized running time operation has amortized running time OO((nn)) with the incremental strategy with the incremental strategy OO(1)(1) with the doubling strategy with the doubling strategy
– Linked list-based queueLinked list-based queue
Queue Interface in JavaQueue Interface in Javapublic interface Queue {
public int size();
public boolean isEmpty();
public Object front()throws EmptyQueueException;
public void enqueue(Object o);
public Object dequeue() throws EmptyQueueException;
}
Java interface corresponding Java interface corresponding to our Queue ADTto our Queue ADTRequires the definition of Requires the definition of class class EmptyQueueExceptionEmptyQueueExceptionNo corresponding built-in No corresponding built-in Java classJava class
1111
The Vector ADTThe Vector ADTThe The VectorVector ADT extends the ADT extends the notion of array by storing a notion of array by storing a sequence of arbitrary objectssequence of arbitrary objectsAn element can be accessed, An element can be accessed, inserted or removed by inserted or removed by specifying its rank (number specifying its rank (number of elements preceding it)of elements preceding it)An exception is thrown if an An exception is thrown if an incorrect rank is specified incorrect rank is specified (e.g., a negative rank)(e.g., a negative rank)
Main vector operations:Main vector operations:– object object elemAtRankelemAtRank(integer r): (integer r):
returns the element at rank r returns the element at rank r without removing itwithout removing it
– object object replaceAtRankreplaceAtRank(integer (integer r, object o): replace the r, object o): replace the element at rank with o and element at rank with o and return the old elementreturn the old element
– insertAtRankinsertAtRank(integer r, object (integer r, object o): insert a new element o to o): insert a new element o to have rank rhave rank r
– object object removeAtRankremoveAtRank(integer (integer r): removes and returns the r): removes and returns the element at rank relement at rank r
Additional operations Additional operations sizesize() and () and isEmptyisEmpty()()
Direct applicationsDirect applications– Sorted collection of objects Sorted collection of objects
(elementary database)(elementary database)
Indirect applicationsIndirect applications– Auxiliary data structure for algorithmsAuxiliary data structure for algorithms– Component of other data structuresComponent of other data structures
1212
Array-based VectorArray-based VectorUse an array Use an array VV of size of size NNA variable A variable nn keeps track of the size of the vector keeps track of the size of the vector (number of elements stored)(number of elements stored)Operation Operation elemAtRankelemAtRank((rr) is implemented in ) is implemented in OO(1)(1) time by time by
returning returning VV[[rr]]V
0 1 2 nr
V0 1 2 nr
V0 1 2 n
or
V0 1 2 nr
In operation In operation insertAtRankinsertAtRank((rr,, o o), we need to make ), we need to make room for the new element by shifting forward the room for the new element by shifting forward the n n r r elements elements VV[[rr], …, ], …, VV[[n n 1]1]In the worst In the worst
case (case (r r 00), ), this takes this takes OO((nn)) time time
1313
Array-based VectorArray-based VectorIn operation In operation removeAtRankremoveAtRank((rr), we need to fill the ), we need to fill the hole left by the removed element by shifting hole left by the removed element by shifting backward the backward the n n r r 11 elements elements VV[[r r 1], …, 1], …, VV[[n n 1]1]In the worst In the worst
case (case (r r 00), ), this takes this takes OO((nn)) time time
V0 1 2 nr
V0 1 2 n
or
V0 1 2 nr
PerformancePerformance– In the array based implementation of a VectorIn the array based implementation of a Vector
The space used by the data structure is The space used by the data structure is OO((nn))sizesize, , isEmptyisEmpty, , elemAtRankelemAtRank and and replaceAtRankreplaceAtRank run in run in OO(1)(1) time timeinsertAtRankinsertAtRank and and removeAtRankremoveAtRank run in run in OO((nn)) time time
– If we use the array in a circular fashion,If we use the array in a circular fashion, insertAtRank insertAtRank(0)(0) and and removeAtRankremoveAtRank(0)(0) run in run in OO(1)(1) time time
– In an In an insertAtRankinsertAtRank operation, when the array is full, instead operation, when the array is full, instead of throwing an exception, we can replace the array with a of throwing an exception, we can replace the array with a larger one (extendable array)larger one (extendable array)
1414
Singly Linked ListSingly Linked ListA singly linked list is a concrete data A singly linked list is a concrete data structure consisting of a sequence of structure consisting of a sequence of nodesnodesEach node storesEach node stores– elementelement– link to the next nodelink to the next node
next
elem node
A B C D
Stack with singly linked listStack with singly linked list– The top element is stored at the first node of the listThe top element is stored at the first node of the list– The space used is The space used is OO((nn)) and each operation of the Stack ADT takes and each operation of the Stack ADT takes OO(1) (1)
time time
Queue with singly linked listQueue with singly linked list– The front element is stored at the first nodeThe front element is stored at the first node– The rear element is stored at the last nodeThe rear element is stored at the last node– The space used is The space used is OO((nn)) and each operation of the Queue ADT takes and each operation of the Queue ADT takes OO(1) (1)
timetime
1515
Position ADT & List ADTPosition ADT & List ADTThe The PositionPosition ADT ADT – models the notion of place within a data structure where a models the notion of place within a data structure where a
single object is storedsingle object is stored– gives a unified view of diverse ways of storing data, such asgives a unified view of diverse ways of storing data, such as
a cell of an arraya cell of an arraya node of a linked lista node of a linked list
– Just one method:Just one method:object object elementelement(): returns the element stored at the position(): returns the element stored at the position
The The ListList ADT ADT – models a sequence of positions storing arbitrary objectsmodels a sequence of positions storing arbitrary objects– establishes a before/after relation between positionsestablishes a before/after relation between positions– Generic methods:Generic methods: sizesize(), (), isEmptyisEmpty()()– Query methods:Query methods: isFirstisFirst(p), (p), isLastisLast(p)(p)– Accessor methods:Accessor methods: firstfirst(), (), lastlast(),(), beforebefore(p), (p), afterafter(p)(p)– Update methods: Update methods:
replaceElementreplaceElement(p, o), (p, o), swapElementsswapElements(p, q)(p, q)insertBeforeinsertBefore(p, o), (p, o), insertAfterinsertAfter(p, o) (p, o) insertFirstinsertFirst(o), (o), insertLastinsertLast(o)(o)removeremove(p)(p)
1616
Doubly Linked ListDoubly Linked ListA doubly linked list provides a natural A doubly linked list provides a natural implementation of the List ADTimplementation of the List ADT
Nodes implement Position and store:Nodes implement Position and store:– elementelement– link to the previous nodelink to the previous node– link to the next nodelink to the next node
Special trailer and header nodesSpecial trailer and header nodes
trailerheader nodes/positions
elements
prev next
elem node
1717
Doubly Linked List OperationsDoubly Linked List OperationsWe visualizeWe visualize insertAfter insertAfter(p, X), (p, X), which returns position qwhich returns position q
p
A B C
A B C
p
X
q
A B X C
p q
We visualize We visualize removeremove(p), (p), where p = where p = lastlast()()
A B C D
p
A B C
D
p
A B CPerformancePerformance– The space used by a doubly linked list with The space used by a doubly linked list with nn elements is elements is OO((nn))– The space used by each position of the list is The space used by each position of the list is OO(1)(1)– All the operations of the List ADT run in All the operations of the List ADT run in OO(1)(1) time time– Operation element() of the Position ADT runs in Operation element() of the Position ADT runs in OO(1)(1) time time
1818
Sequence ADTSequence ADTThe The SequenceSequence ADT is the ADT is the union of the Vector and union of the Vector and List ADTsList ADTsElements accessed byElements accessed by– Rank or PositionRank or Position
Generic methods:Generic methods:– sizesize(), (), isEmptyisEmpty()()
Vector-based methods:Vector-based methods:– elemAtRankelemAtRank(r), (r),
replaceAtRankreplaceAtRank(r, o), (r, o), insertAtRankinsertAtRank(r, o), (r, o), removeAtRankremoveAtRank(r)(r)
List-based methods:List-based methods:– firstfirst(), (), lastlast(), (),
beforebefore(p), (p), afterafter(p), (p), replaceElementreplaceElement(p, o), (p, o), swapElementsswapElements(p, q), (p, q), insertBeforeinsertBefore(p, o), (p, o), insertAfterinsertAfter(p, o), (p, o), insertFirstinsertFirst(o), (o), insertLastinsertLast(o), (o), removeremove(p)(p)
Bridge methods:Bridge methods:– atRankatRank(r), (r), rankOfrankOf(p)(p)
The Sequence ADT is a The Sequence ADT is a basic, general-purpose, basic, general-purpose, data structure for storing data structure for storing an ordered collection of an ordered collection of elementselements
Direct applications:Direct applications:– Generic replacement for stack, Generic replacement for stack,
queue, vector, or listqueue, vector, or list– small database small database
Indirect applications:Indirect applications:– Building block of more complex Building block of more complex
data structuresdata structures
1919
Array-based ImplementationArray-based Implementation
We use a We use a circular array circular array storing storing positions positions A position A position object stores:object stores:– ElementElement– RankRank
Indices Indices ff and and ll keep track of keep track of first and last first and last positionspositions
0 1 2 3
positions
elements
S
lf
2020
Sequence ImplementationsSequence Implementations
nnnninsertAtRank, removeAtRankinsertAtRank, removeAtRank
1111insertFirst, insertLastinsertFirst, insertLast
11nninsertAfter, insertBeforeinsertAfter, insertBefore
nn11replaceAtRankreplaceAtRank1111replaceElement, swapElementsreplaceElement, swapElements
nn11atRank, rankOf, elemAtRankatRank, rankOf, elemAtRank1111size, isEmptysize, isEmpty
11nnremoveremove
1111first, last, before, afterfirst, last, before, after
ListListArrayArrayOperationOperation
2121
Design PatternsDesign Patterns
AdaptorAdaptor
PositionPosition
CompositionComposition
IteratorIterator
ComparatorComparator
LocatorLocator
2222
Design Pattern: IteratorsDesign Pattern: IteratorsAn iterator abstracts the An iterator abstracts the process of scanning process of scanning through a collection of through a collection of elementselementsMethods of the Methods of the ObjectIterator ADT:ObjectIterator ADT:– object object object()object()– boolean boolean hasNext()hasNext()– object object nextObject()nextObject()– reset()reset()
Extends the concept of Extends the concept of Position by adding a Position by adding a traversal capabilitytraversal capabilityImplementation with an Implementation with an array or singly linked listarray or singly linked list
An iterator is typically An iterator is typically associated with an associated with an another data structureanother data structure
We can augment the We can augment the Stack, Queue, Vector, List Stack, Queue, Vector, List and Sequence ADTs with and Sequence ADTs with method:method:– ObjectIterator ObjectIterator elements()elements()
Two notions of iterator:Two notions of iterator:– snapshot: freezes the snapshot: freezes the
contents of the data contents of the data structure at a given timestructure at a given time
– dynamic: follows changes dynamic: follows changes to the data structureto the data structure
2323
The Tree StructureThe Tree Structure
In computer science, a In computer science, a tree is an abstract model tree is an abstract model of a hierarchical of a hierarchical structurestructure
A tree consists of nodes A tree consists of nodes with a parent-child with a parent-child relationrelation
Applications:Applications:– Organization chartsOrganization charts– File systemsFile systems– Programming Programming
environmentsenvironments
Computers”R”Us
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
2424
subtree
Tree TerminologyTree TerminologyRoot: node without parent (A)Root: node without parent (A)Internal node: node with at least Internal node: node with at least one child (A, B, C, F)one child (A, B, C, F)External node (a.k.a. leaf ): node External node (a.k.a. leaf ): node without children (E, I, J, K, G, H, D)without children (E, I, J, K, G, H, D)Ancestors of a node: parent, Ancestors of a node: parent, grandparent, grand-grandparent, grandparent, grand-grandparent, etc.etc.Depth of a node: number of Depth of a node: number of ancestorsancestorsHeight of a tree: maximum depth Height of a tree: maximum depth of any node (3)of any node (3)Descendant of a node: child, Descendant of a node: child, grandchild, grand-grandchild, etc.grandchild, grand-grandchild, etc.
A
B DC
G HE F
I J K
Subtree: tree consisting Subtree: tree consisting of a node and its of a node and its descendantsdescendants
2525
Tree ADTTree ADTWe use positions to We use positions to abstract nodesabstract nodes
Generic methods:Generic methods:– integer integer sizesize()()– boolean boolean isEmptyisEmpty()()– objectIterator objectIterator elementselements()()– positionIterator positionIterator positionspositions()()
Accessor methods:Accessor methods:– position position rootroot()()– position position parentparent(p)(p)– positionIterator positionIterator childrenchildren(p)(p)
Query methods:Query methods:– boolean boolean isInternalisInternal(p)(p)– boolean boolean isExternalisExternal(p)(p)– boolean boolean isRootisRoot(p)(p)
Update methods:Update methods:– swapElementsswapElements(p, q)(p, q)– object object replaceElementreplaceElement(p, o)(p, o)
Additional update methods Additional update methods may be defined by data may be defined by data structures implementing the structures implementing the Tree ADTTree ADT