Vectors and Array Lists

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
Advertisements

© 2004 Goodrich, Tamassia Sequences and Iterators1.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
CS 307 Fundamentals of Computer Science 1 Lists and ArrayList many slides taken from Mike Scott, UT Austin.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
Vectors, Lists, Sequences. Vectors Linear sequence s of n elements e rank – number of elements before e in s Vector supports access to elements via their.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
Elementary Data Structures
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CSCI 3333 Data Structures Stacks.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Stacks.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Sequences and Iterators
Queues 11/9/2018 6:32 PM Queues.
Array Lists, Node Lists & Sequences
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Elementary Data Structures
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Stacks.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
" A list is only as strong as its weakest link. " - Donald Knuth
Array-Based Sequences
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
Copyright © Aiman Hanna All rights reserved
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Stacks and Linked Lists
Presentation transcript:

Vectors and Array Lists 7/6/2019 3:28 AM Vectors and Array Lists Vectors

The Vector ADT (§5.1) The Vector ADT extends the notion of array by storing a sequence of arbitrary objects 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. negative rank, or larger than current size) Main vector operations: object elemAtRank(integer r): returns the element at rank r without removing it, 0≤r≤s-1 object replaceAtRank(integer r, object o): replace the element at rank with o and return the old element, 0≤r≤s-1 insertAtRank(integer r, object o): insert a new element o to have rank r, 0≤r≤s object removeAtRank(integer r): removes and returns the element at rank r, 0≤r≤s-1 Additional operations size() and isEmpty() Vectors

Applications of Vectors Direct applications Sorted collection of objects (elementary database) Indirect applications Auxiliary data structure for algorithms Component of other data structures Vectors

Array-based Vector Use an array V of size N A variable n keeps track of the size of the vector (number of elements stored) Operation elemAtRank(r) is implemented in O(1) time by returning V[r] V 1 2 r n Vectors

Insertion In operation insertAtRank(r, o), we need to make room for the new element by shifting forward the n - r elements V[r], …, V[n - 1] In the worst case (r = 0), this takes O(n) time V 1 2 r n V 1 2 r n V o 1 2 r n Vectors

Deletion In operation removeAtRank(r), we need to fill the hole left by the removed element by shifting backward the n - r - 1 elements V[r + 1], …, V[n - 1] In the worst case (r = 0), this takes O(n) time V 1 2 n o r V 1 2 n r V 1 2 n r Vectors

Performance In the array based implementation of a Vector The space used by the data structure is O(n) size, isEmpty, elemAtRank and replaceAtRank run in O(1) time insertAtRank and removeAtRank run in O(n) time If we use the array in a circular fashion, insertAtRank(0) and removeAtRank(0) run in O(1) time In an insertAtRank operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one Vectors

Growable Array-based Vector In a push operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one How large should the new array be? incremental strategy: increase the size by a constant c doubling strategy: double the size Algorithm push(o) if t = S.length  1 then A  new array of size … for i  0 to t do A[i]  S[i] S  A t  t + 1 S[t]  o Vectors

Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n push operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of a push operation the average time taken by a push over the series of operations, i.e., T(n)/n Vectors

Incremental Strategy Analysis We replace the array k = n/c times The total cost T(n) of a series of n push operations is proportional to n + 2(c + 2c + 3c + 4c + … + kc) = n + 2c(1 + 2 + 3 + … + k) = n + 2c × k(k + 1)/2 = O(n+k2) Since c is a constant, T(n) = O(n+k2) = O(n+(n/c)2) = O(n2) The amortized time of 1 push is O(n2)/n = O(n) Vectors

Doubling Strategy Analysis We replace the array k = log2 n times The total time T(n) of a series of n push operations is proportional to n + 2×(1 + 2 + 4 + 8 + …+ 2k) = n + 2×(2k + 1 -1) = 3n -2 = O(n) The amortized time of 1 push is O(n)/n = O(1) geometric series 1 2 4 8 Vectors