Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Advertisements

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.
Elementary Data Structures
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
CS333 / Cutler Amortized Analysis 1 Amortized Analysis The average cost of a sequence of n operations on a given Data Structure. Aggregate Analysis Accounting.
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.
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.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
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.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Lists Last Update: Oct 29, 2014 EECS2011: Lists1.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
© 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.
Heaps and basic data structures David Kauchak cs161 Summer 2009.
Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
Amortized Analysis. Problem What is the time complexity of n insert operations into an dynamic array, which doubles its size each time it is completely.
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 
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Amortized Analysis.
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,
Andreas Klappenecker [partially based on the slides of Prof. Welch]
CE 221 Data Structures and Algorithms
Introduction to Analysis of Algorithms
Introduction to Analysis of Algorithms
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
CSCE 411 Design and Analysis of Algorithms
Presentation by Marty Krogel
Amortized Analysis The problem domains vary widely, so this approach is not tied to any single data structure The goal is to guarantee the average performance.
More Stacks Growable stacks Amortized analysis
Arrays, Stacks, and Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
05 Array-Based Sequences
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,
Quick Sort (11.2) CSE 2011 Winter November 2018.
Stacks.
" A list is only as strong as its weakest link. " - Donald Knuth
אחסון (אירגון) מידע DATA DATA DATA Link Link Link … …
Array-Based Sequences
Copyright © Aiman Hanna All rights reserved
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
Figure 7.1 Some queue operations. Figure 7.1 Some queue operations.
CSCE 411 Design and Analysis of Algorithms
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
More Stacks Growable stacks Amortized analysis
Vectors and Array Lists
CSCE 411 Design and Analysis of Algorithms
Stacks and Linked Lists
Presentation transcript:

Vectors 11/23/2018 1:03 PM Growing Arrays Vectors

Problem with fixed-size Arrays In the implementation of stack or queue with an array, we had to set the array size to some initial capacity If the number of pushes (for a stack) or enqueues (for a queue) passes this capacity, we had to throw an exception Linked List implementation does not have this problem, but sometimes Linked List is not the best implementation Can we avoid the capacity limit in the array-based implementations? Vectors

Growable Array Implementations (of stack, queue, array list, etc…) In a push (enqueue, etc) 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