cs 221 analysis of algorithms data structures vectors, lists

27
CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Upload: grant-bruce

Post on 13-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

CS 221

Analysis of AlgorithmsData StructuresVectors, Lists

Page 2: 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

Page 3: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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?

Page 4: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Data Structures

Stacks and Queue are not so efficient When?

We need to handle data somewhere other than the end of the structure

Page 5: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 6: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 7: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 8: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Vectors Auxiliary vector operations:

Size(): how many elements are in S isEmpty(): return boolean for whether vector has

no elements

Page 9: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Vectors - applications How would you use this type of structure?

Page 10: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 11: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Vectors – array implementation elementAtRank(r)

run-time? O(1)

V0 1 2 nr

from: Goodrich and Tamassia, 2002

Page 12: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Vectors – array implementation replaceAtRank(r)

run-time? O(1)

V0 1 2 nr

from: Goodrich and Tamassia, 2002

Page 13: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 14: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 15: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Vectors - performance

Operation T(n)

size() O(1)

isEmpty() O(1)

elementAtRank() O(1)

replaceAtRank() O(1)

insertAtRank() O(n)

removeAtRank() O(n)

Page 16: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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?

Page 17: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 18: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Lists As a single linked list

we maintain sequence if we have an element we know next element

A B C D

Page 19: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Lists How about a Stack (LIFO) as a list Queue (FIFO)as a list

A B C D

Page 20: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 21: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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

Page 22: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

List Abstract Data TypeIt establishes a before/after relation between positionsGeneric methods:

size(), isEmpty()Query methods:

isFirst(p), isLast(p)

Page 23: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

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)

Page 24: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Element Insertion insertAfter(p, X), which returns position q

A B X C

A B C

A B C

p

X

q

p q

Page 25: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

Element Deletion remove(p), where p = last()

A B C D

p

A B C

D

p

A B C

Page 26: CS 221 Analysis of Algorithms Data Structures Vectors, Lists

List Performance

Consider a double linked list Space? run-time

insertAfter(p) remove(p)

What about growth?

Page 27: CS 221 Analysis of Algorithms Data Structures Vectors, Lists