Download Lecture 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
CS 221
Analysis of Algorithms
Data Structures
Vectors, Lists
Portions of the following slides are from
Goodrich and Tamassia, Algorithm
Design: Foundations, Analysis and
Internet Examples, 2002
and
Material provided by the publisher’s
John Wiley & Son, Inc.) companion
website for this book
Data Structures
 We have seen data structures
 Stacks – LIFO
 Queues – FIFO
 These were very efficient where
 we need back most recently stored data –
Stack
 we need to retrieve data in the order that is
was stored
 How efficient?
Data Structures
 Stacks and Queue are not so efficient
 When?
 We need to handle data somewhere
other than the end of the structure
Vectors
 Suppose we have a linear sequence
of data
What does linear sequence means?
Call sequence S
S contains n elements
Rank = number of elements before a
given element in the sequence
 First element in S has rank = 0
 Last element in S has rank = n-1




Vectors
 An element can be accessed, inserted
or removed by specifying its rank
(number of elements preceding it)
 An exception is thrown if an incorrect
rank is specified (e.g., a negative
rank, rank > n)
 Can be implemented using array
construct
Vectors
 Main vector operations:
 elemAtRank(integer r): returns the
element at rank r without removing it
 replaceAtRank(integer r, object o):
replace the element at rank with o and return
the old element
 insertAtRank(integer r, object o):
insert a new element o to have rank r
 removeAtRank(integer r): removes and
returns the element
Vectors
 Auxiliary vector operations:
 Size(): how many elements are in S
 isEmpty(): return boolean for whether vector
has no elements
Vectors - applications
 How would you use this type of structure?
Vectors – array implementation
 Use an array V of size N
 A variable n keeps track of the size of
the vector (number of elements
stored)
V
0 1 2
r
n
from: Goodrich and Tamassia, 2002
Vectors – array implementation
 elementAtRank(r)
 run-time?
 O(1)
V
0 1 2
r
n
from: Goodrich and Tamassia, 2002
Vectors – array implementation
 replaceAtRank(r)
 run-time?
 O(1)
V
0 1 2
r
n
from: Goodrich and Tamassia, 2002
Vectors – array implementation

insertAtRank(r,o)
 keep in mind-we need to make room for the new
element by shifting forward the n - r elements V[r], …,
V[n - 1]



Worst case?
run-time?
O(n)
V
0 1 2
r
n
0 1 2
r
n
0 1 2
o
r
V
V
from: Goodrich and Tamassia, 2002
n
Vectors – array implementation
 removeAtRank(r,o)
 We need to shift every element after removed
element to fill hole .. V[r]=V[r+1], V[r+1]=V[r+2],…
 Worst case?
 run-time?
 O(n)
V
0 1 2
o
r
n
0 1 2
r
n
0 1 2
r
V
V
from: Goodrich and Tamassia, 2002
n
Vectors - performance
Operation
size()
isEmpty()
elementAtRank()
replaceAtRank()
insertAtRank()
removeAtRank()
T(n)
O(1)
O(1)
O(1)
O(1)
O(n)
O(n)
Lists
 Vectors (or the algorithms to use them)are
pretty efficient data structures
 …but not so efficient if we have to
reorganize them (insertAtRank,
removeAtRank)
 What if we want to access data with respect
to its position in structure, particularly with
respect to other elements?
Lists
 Consider the concept of an element in a
structure as a node
 Node contains
 value of element
 location of its neighbor node - link
next
elem
node
Lists
 As a single linked list
 we maintain sequence
 if we have an element we know next element
…
A
B
C
D
Lists
 How about a Stack (LIFO) as a list
 Queue (FIFO)as a list
…
A
B
C
D
Double LinkedLists


A doubly linked list provides a
natural implementation of the List
ADT
Nodes implement Position and
store:




element
link to the previous node
link to the next node
prev
next
elem
node
Special trailer and header nodes
header
nodes/positions
elements
trailer
Position Abstract Data Type
 The Position ADT models the notion
of place within a data structure
where a single object is stored
 It gives a unified view of diverse
ways of storing data, such as
 a cell of an array
 a node of a linked list
 Just one method:
 object element(): returns the element
stored at the position
List Abstract Data Type
It establishes a before/after
relation between positions
Generic methods:
size(), isEmpty()
Query methods:
isFirst(p), isLast(p)
List Abstract Data Type
Accessor methods:
 first(), last()
 before(p), after(p)
 Update methods:




replaceElement(p, o), swapElements(p, q)
insertBefore(p, o), insertAfter(p, o),
insertFirst(o), insertLast(o)
remove(p)
Element Insertion
 insertAfter(p, X), which returns position q
A
B
C
p
A
q
B
C
X
p
A
q
B
X
C
Element Deletion
 remove(p), where p = last()
p
A
B
C
A
B
C
D
p
D
A
B
C
List Performance
 Consider a double linked list
 Space?
 run-time
 insertAfter(p)
 remove(p)
 What about growth?