Download presentation
Presentation is loading. Please wait.
Published byGrant Bruce Modified over 9 years ago
1
CS 221 Analysis of Algorithms Data Structures Vectors, Lists
2
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
3
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?
4
Data Structures Stacks and Queue are not so efficient When? We need to handle data somewhere other than the end of the structure
5
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
6
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
7
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
8
Vectors Auxiliary vector operations: Size(): how many elements are in S isEmpty(): return boolean for whether vector has no elements
9
Vectors - applications How would you use this type of structure?
10
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 012n r from: Goodrich and Tamassia, 2002
11
Vectors – array implementation elementAtRank(r) run-time? O(1) V 012n r from: Goodrich and Tamassia, 2002
12
Vectors – array implementation replaceAtRank(r) run-time? O(1) V 012n r from: Goodrich and Tamassia, 2002
13
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 V 012n r V 012n r V 012n o r
14
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 V 012n r V 012n o r V 012n r
15
Vectors - performance OperationT(n) size()O(1) isEmpty()O(1) elementAtRank()O(1) replaceAtRank()O(1) insertAtRank()O(n) removeAtRank()O(n)
16
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?
17
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
18
Lists As a single linked list we maintain sequence if we have an element we know next element ABCD …
19
Lists How about a Stack (LIFO) as a list Queue (FIFO)as a list ABCD …
20
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 prevnext elem trailer header nodes/positions elements node
21
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
22
List Abstract Data Type It establishes a before/after relation between positions Generic methods: size(), isEmpty() Query methods: isFirst(p), isLast(p)
23
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)
24
Element Insertion insertAfter(p, X), which returns position q ABXC ABC ABC p X q pq
25
Element Deletion remove(p), where p = last() ABCD p ABC D p ABC
26
List Performance Consider a double linked list Space? run-time insertAfter(p) remove(p) What about growth?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.