CS 113: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
1 Chapter 17: Amortized Analysis III. 2 Dynamic Table Sometimes, we may not know in advance the #objects to be stored in a table We may allocate space.
Linked Structures, Project 1: Linked List Bryce Boe 2013/10/16 CS24, Fall 2013.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Preliminaries Attendance sheets –I remembered! HW1 due tonight –progress report Stage 1 due on Friday –progress report.
Tirgul 9 Amortized analysis Graph representation.
Rossella Lau Lecture 5, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 5: Deque Comparison of sequence containers  Deque.
Theory I Algorithm Design and Analysis (8 – Dynamic tables) Prof. Th. Ottmann.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
A vector space containing infinitely many vectors can be efficiently described by listing a set of vectors that SPAN the space. eg: describe the solutions.
Chapter 4 Memory Management.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Generic Programming Using the C++ Standard Template Library.
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
David Luebke 1 12/12/2015 CS 332: Algorithms Amortized Analysis.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Heap Sort Uses a heap, which is a tree-based data type Steps involved: Turn the array into a heap. Delete the root from the heap and insert into the array,
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
An Array-Based Implementation of the ADT List
Complexity Analysis (Part I)
Insertion sort Loop invariants Dynamic memory
CE 221 Data Structures and Algorithms
Lecture 10 Collections Richard Gesick.
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,
Data Structures I (CPCS-204)
Review Deleting an Element from a Linked List Deletion involves:
CE 221 Data Structures and Algorithms
Data Structure and Algorithms
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Chapter 8 Arrays, Strings and pointers
Introduction to Algorithms
Lecture 16 Amortized Analysis
CS 213: Data Structures and Algorithms
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Object Oriented Programming COP3330 / CGS5409
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,
Arrays and Linked Lists
Recursion Chapter 11.
Vectors 11/23/2018 1:03 PM Growing Arrays Vectors.
Linked List Intro CSCE 121 J. Michael Moore.
" A list is only as strong as its weakest link. " - Donald Knuth
CS222: Principles of Data Management Notes #8 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Array-Based Sequences
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
CS222P: Principles of Data Management Notes #8 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Tim Ehrlich Growing Arrays in C.
Chapter 4.
CS 332: Algorithms Amortized Analysis Continued
CSC 427: Data Structures and Algorithm Analysis
Data Structures & Algorithms
Amortized Analysis and Heaps Intro
Priority Queues & Heaps
Chapter 11 Instructor: Xin Zhang
the Standard Template Library
B-Trees.
Linked List Intro CSCE 121.
Lecture 20 Hashing Amortized Analysis
Lecture 21 Amortized Analysis
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Complexity Analysis (Part I)
An Introduction to Programming though C++
Complexity Analysis (Part I)
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

CS 113: Data Structures and Algorithms Abhiram G. Ranade Some remarks on vectors

How much time do different vector operations take? Indexing: The vector class stores elements in an array on the heap. So indexing takes time O(1) Appending an element (push_back) When allocating the array, some room is kept for pushbacks. If size of vector < allocated space, then new element is placed beyond the current size. So time taken = O(1) If size of vector = size of allocated array, new space must be allocated, and the vector must be copied to new space. So time taken = O(vector size) Typical strategy: If allocated size n is exceeded because of pushback, allocate size 2n (and use only n+1 elements in it).

Example of doubling protocol Statement executed vector v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); v.push_back(6); v.push_back(7); Content of array _ allocation 1 1 2 allocation + copy 1 1 2 3 _ a + copy 2 1 2 3 4 1 2 3 4 5 _ _ _ a + copy 4 1 2 3 4 5 6 _ _ 1 2 3 4 5 6 7 _

So what is the cost of a pushback? Theorem: If the size of a vector grows to n as a result of pushback operations, the total time spent on pushback operations = O(n) Proof: Total time = time on allocating memory + time spent copying Since the allocated size always doubles, after k allocations the allocated size will be 2k-1 Because the last of these allocations was necessary, n > 2k-2 Thus k-2 < log n So k < 2+log n Time spent on copying: 1+2+4+...2k-2 = 2*2k-2 – 1 < 2n -1 So total is at most 2n + log n = O(n) Average per pushback is O(1), so pushbacks can be considered to be as fast as indexing!

Remarks If n push_back operations happen, some push_back will take time O(n), but total time is also O(n), i.e. most push_backs take time O(1), and average per operation is O(1). This called “amortization”. Amortization is distributing the cost over many items for the purpose of accounting. Inserting in the middle of a vector: requires copying after every operation so time for each operation is O(n) thus this is an expensive operation always.

Homework Suppose like push_back, we have pop_back which just drops off the last element. We would also like that the size of the allocated array be at most k times the size of the used portion at all times, so that memory will be used at least somewhat efficiently. So when the number of used elements fall below 1/k you should deallocate memory. What k will you use so that push_back and pop_back work in O(1) time in an amortized sense? (if there have been n push_backs and m pop_backs, then we want total time to be O(m+n), and memory to always be k times the number of elements in the vector at that time)