cs 221 analysis of algorithms data structures vectors, lists
TRANSCRIPT
CS 221
Analysis of AlgorithmsData StructuresVectors, 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)
V0 1 2 nr
from: Goodrich and Tamassia, 2002
Vectors – array implementation elementAtRank(r)
run-time? O(1)
V0 1 2 nr
from: Goodrich and Tamassia, 2002
Vectors – array implementation replaceAtRank(r)
run-time? O(1)
V0 1 2 nr
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)
from: Goodrich and Tamassia, 2002
V0 1 2 nr
V0 1 2 nr
V0 1 2 n
or
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)
from: Goodrich and Tamassia, 2002
V0 1 2 nr
V0 1 2 n
or
V0 1 2 nr
Vectors - performance
Operation T(n)
size() O(1)
isEmpty() O(1)
elementAtRank() O(1)
replaceAtRank() O(1)
insertAtRank() O(n)
removeAtRank() 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
Special trailer and header nodes
prev next
elem
trailerheader nodes/positions
elements
node
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 TypeIt establishes a before/after relation between positionsGeneric 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 X C
A B C
A B C
p
X
q
p q
Element Deletion remove(p), where p = last()
A B C D
p
A B C
D
p
A B C
List Performance
Consider a double linked list Space? run-time
insertAfter(p) remove(p)
What about growth?