Array-Based Sequences

Slides:



Advertisements
Similar presentations
ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
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 Hash Tables1  
© 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.
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
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.
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.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
© 2004 Goodrich, Tamassia Hash Tables1  
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
© 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.
Arrays1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Parasol Lab, Dept. CSE, Texas A&M University
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
An Array-Based Implementation of the ADT List
Hash Tables 1/28/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Sorting With Priority Queue In-place Extra O(N) space
CSE373: Data Structures & Algorithms Priority Queues
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,
Lecture 5 of Computer Science II
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Bucket-Sort and Radix-Sort
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
More Stacks Growable stacks Amortized analysis
© 2013 Goodrich, Tamassia, Goldwasser
Hash Tables 3/25/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Skip Lists S3 + - S2 + - S1 + - S0 + -
05 Array-Based Sequences
Array Lists, Node Lists & Sequences
Ch7. List and Iterator ADTs
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,
Python Primer 2: Functions and Control Flow
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
Ch. 8 Priority Queues And Heaps
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Dictionaries 1/17/2019 7:55 AM Hash Tables   4
Problem Understanding
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Recall What is a Data Structure Very Fundamental Data Structures
CSE 2010: Algorithms and Data Structures Algorithms
Python Primer 1: Types and Operators
Programming with Recursion
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
Computing Spans Given an an array X, the span S[i] of X[i] is
Amortized Analysis and Heaps Intro
Vectors and Array Lists
Arrays © 2014 Goodrich, Tamassia, Goldwasser Arrays Vectors
Divide and Conquer Merge sort and quick sort Binary search
Stacks and Linked Lists
Presentation transcript:

Array-Based Sequences Vectors 12/2/2018 1:36 PM Array-Based Sequences Hongfei Yan April 1, 2015 Array-Based Sequences

Recap: The Three Laws of Recursion Vectors 12/2/2018 1:36 PM Recap: The Three Laws of Recursion A recursive algorithm must have a base case. A recursive algorithm must change its state and move toward the base case. A recursive algorithm must call itself, recursively. http://interactivepython.org/runestone/static/pythonds/Recursion/TheThreeLawsofRecursion.html Array-Based Sequences

Q: How many recursive calls are made when computing the sum of the list [2,4,6,8,10]? a)6 b)5 c)4 d)3 c) Correct!! the first recursive call passes the list [4,6,8,10], the second [6,8,10] and so on until [10].

fact(n) returns n * n-1 * n-2 * ... Q: Suppose you are going to write a recursive function to calculate the factorial of a number. fact(n) returns n * n-1 * n-2 * ... Where the factorial of zero is definded to be 1. What would be the most appropriate base case? a)n==0 b)n==1 c)n>=0 d)n<=1 d) Correct!! Good, this is the most efficient, and even keeps your program from crashing if you try to compute the factorial of a negative number.

Assignment #3: Recursion C-4.16 Write a short recursive Python function that takes a character string s and outputs its reverse. For example, the reverse of 'pots&pans' would be 'snap&stop' . C-4.19 Write a short recursive Python function that rearranges a sequence of integer values so that all the even values appear before all the odd values. C-4.21 Suppose you are given an n-element sequence, S, containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in S that sum to k, if such a pair exists. What is the running time of your algorithm? Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences Caesar: An interesting application of strings and lists is cryptography Dynamic arrays and amortization Sorting high scores for a game Insertion sort Tic tac toe Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

5.5 Using Array-Based Sequences 5.5.1 Storing High Scores for a Game The first application we study is storing a sequence of high score entries for a video game. Array-Based Sequences

Array-Based Sequences Vectors 12/2/2018 1:36 PM Python: How to print a class or objects of class using print()? http://stackoverflow.com/questions/1535327/python-how-to-print-a-class-or-objects-of-class-using-print >>> class Test: ... def __repr__(self): ... return "Test()" ... def __str__(self): ... return "member of Test" ... >>> t = Test() >>> t Test() >>> print t member of TestThe __str__ method is what happens when you print it, and the __repr__ method is what happens when you use the repr() function (or when you look at it with the interactive prompt). If this isn't the most Pythonic method, I apologize, because I'm still learning too - but it works. If no __str__ method is given, Python will print the result of __repr__ instead. If you define __str__ but not __repr__, Python will use what you see above as the __repr__, but still use __str__ for printing. Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Array-Based Sequences

Python Sequence Classes Python has built-in types, list, tuple, and str. Each of these sequence types supports indexing to access an individual element of a sequence, using a syntax such as A[i] Each of these types uses an array to represent the sequence. An array is a set of memory locations that can be addressed using consecutive indices, which, in Python, start with index 0. A 1 2 i n © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Arrays of Characters or Object References An array can store primitive elements, such as characters, giving us a compact array. An array can also store references to objects. © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Array-Based Sequences Vectors 12/2/2018 1:36 PM Compact Arrays Primary support for compact arrays is in a module named array. That module defines a class, also named array, providing compact storage for arrays of primitive data types. The constructor for the array class requires a type code as a first parameter, which is a character that designates the type of data that will be stored in the array. import array Primes = array.array(‘I’, [2,3,5,7,11,13,17,19]) © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Type Codes in the array Class Python’s array class has the following type codes: © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Array-Based Sequences Insertion In an operation add(i, o), we need to make room for the new element by shifting forward the n - i elements A[i], …, A[n - 1] In the worst case (i = 0), this takes O(n) time A 1 2 i n A 1 2 i n A o 1 2 i n © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Array-Based Sequences Element Removal In an operation remove(i), we need to fill the hole left by the removed element by shifting backward the n - i - 1 elements A[i + 1], …, A[n - 1] In the worst case (i = 0), this takes O(n) time A 1 2 n o i A 1 2 n i A 1 2 n i © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Array-Based Sequences Performance In an array based implementation of a dynamic list: The space used by the data structure is O(n) Indexing the element at I takes O(1) time add and remove run in O(n) time in worst case In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one… © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Growable Array-based Array List In an add(o) operation (without an index), we could always add at the end When the array is full, we 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 add(o) if t = S.length  1 then A  new array of size … for i  0 to n-1 do A[i]  S[i] S  A n  n + 1 S[n-1]  o © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Comparison of the Strategies compare the incremental strategy and the doubling strategy by analyzing the total time T(n) needed to perform a series of n add(o) operations assume that we start with an empty stack represented by an array of size 1 call amortized time of an add operation the average time taken by an add over the series of operations, i.e., T(n)/n © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Incremental Strategy Analysis We replace the array k = n/c times The total time T(n) of a series of n add operations is proportional to n + c + 2c + 3c + 4c + … + kc = n + c(1 + 2 + 3 + … + k) = n + ck(k + 1)/2 Since c is a constant, T(n) is O(n + k2), i.e., O(n2) The amortized time of an add operation is O(n) © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Doubling Strategy Analysis We replace the array k = log2 n times The total time T(n) of a series of n add operations is proportional to n + 1 + 2 + 4 + 8 + …+ 2k = n + 2k + 1 - 1 = 3n - 1 T(n) is O(n) The amortized time of an add operation is O(1) geometric series 1 2 4 8 © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

Python Implementation © 2013 Goodrich, Tamassia, Goldwasser Array-Based Sequences

5.6 Multidimensional Data Sets For example, the two-dimensional data might be stored in Python as follows. data = [ [22, 18, 709, 5, 33], [45, 32, 830, 120, 750], [4, 880, 45, 66, 61] ] Array-Based Sequences

Constructing a Multidimensional List initialize a one-dimensional list, rely on a syntax such as data = [0] * n to create a list of n zeros. list comprehension syntax for a 2D list of integers data = [ [0] c for j in range(r) ] Array-Based Sequences

Array-Based Sequences Summary Array-Based Sequences