1 / 20

Chapter 6 List and Iterators

Chapter 6 List and Iterators. 6.1 Array Lists (Vectors). Definitions:. List (Sequence): - It’s a collection S of n elements stored in a certain linear order, so that we can refer to the elements in S as first , second , third , and so on.

fruth
Download Presentation

Chapter 6 List and Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6List and Iterators Lists and Iterators

  2. 6.1 Array Lists (Vectors) Lists and Iterators

  3. Definitions: • List (Sequence): - It’s a collection S of n elements stored in a certain linear order, so that we can refer to the elements in S as first, second, third, and so on. - Each element e in S can be uniquely referred to by an integer in the range 0 to n-1. • Index of Element: - The index of an element e in S is the number of elements that precedee in S. - First element in S has index 0, last element has index n-1. - Also, if an element in S has index i, its previous element (if it exists) has index (i-1), and its next element (if it exists) has index (i+1). • Rank of Element: - The rank of an element in a list is its order in this list. It’s defined to be one more the index of that element. So, the first element is at rank 1, the second is at rank 2, and so on. • Array List (Vector): - A list (or Sequence) that supports access to its elements by their indices is called an array-list (or vector). This index concept is used to specify where to insert a new element into a list, or where to remove and old element. Lists and Iterators

  4. 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 index (number of elements preceding it) An exception is thrown if an incorrect index is specified (e.g. negative index, or larger than current size) Main vector operations: object get (integer i): returns the element of S at index i, without removing it, 0 ≤ i ≤ n-1, otherwise, an error condition occurs. object set (integer i, object o): replace the element at index i with o and return the old element, 0 ≤ i ≤ n-1 object add (integer i, object o): insert a new element o to have index i, 0 ≤ i ≤ n object remove (integer i): removes and returns the element at index i, 0 ≤ i ≤ n-1 Additional operations size() and isEmpty() 6.1.1 The Array List (Vector) ADT Lists and Iterators

  5. We do not insist that an array should be used to implement an array list, so that the element at index 0 is stored at index 0 in the array. • The index definition offers us a way to refer to the “place” where an element is stored in a sequence, without having to worry about the exact implementation of that sequence. • The index of an element may change whenever the sequence (array list) is updated, however, as we illustrate in the following example. Lists and Iterators

  6. Example of an Array ListFollowingis an example of some operations on an initially empty list S: Lists and Iterators

  7. Applications of Vectors • Direct applications • Sorted collection of objects (elementary database) • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures Lists and Iterators

  8. An obvious choice for implementing the array-list (vector) ADT is to use an array V , where V [i] stores (a reference to) the element with index i. Use an array V of size N, that is sufficiently large. A variable n keeps track of the size of the vector (number of elements stored). Operation get (i) is implemented in O(1) time by returning V [i] 6.1.3 A Simple Array-based Implementation of Vector ArrayV 0 1 2 n i Lists and Iterators

  9. Insertion • In operation add(i, o), we need to make room for the new element by shifting forward the n - i elements V [i], …, V [n -1] • In the worst case (i =0), this takes O(n) time V 0 1 2 n i V 0 1 2 n i V o 0 1 2 n i Lists and Iterators

  10. Insertion Algorithm Algorithmadd(i,o) for j = n-1, n-2, … , i do V [j+1] ← V [j] {make room for the new element} V [i] ← o n ← n+1 Lists and Iterators

  11. V 0 1 2 n i V 0 1 2 n i V o 0 1 2 n i Deletion • In operation remove (i), we need to fill the hole left by the removed element by shifting backward the n - i -1 elements V [i +1], …, V [n -1] • In the worst case (i =0), this takes O(n) time Lists and Iterators

  12. Deletion Algorithm Algorithmremove(i) e← V [i] {e is a temporary variable} for j = i, i+1, …, n-2 do V [j]← V [j+1] {fill in the room of the removed element} n ← n-1 return e Lists and Iterators

  13. Performance of Array Implementation • In the array-based implementation of a Vector ADT • The space used by the data structure is O(n) • size, isEmpty, getand setrun in O(1) time • addand remove run in O(n) time (worst case, i = 0) • add(n,e)and remove(n-1)run in O(1) time (add or remove at the end of list) • If we use the array in a circular fashion, add(0,e)and remove (0)run in O(1) time. In this implementation, we give up our rule that an element at index i is stored in the array at index i. Lists and Iterators

  14. 6.1.5 Extendable Array-based Vector • In an addoperation, when the array is full, instead of throwing an exception, we can replace the array with a larger one • An illustration of the three steps for "growing" an extendable array: • (a) create new array B; • (b) copy elements from A to B; • (c) reassign reference A to the new array Lists and Iterators

  15. 6.1.5 Extendable Array-based Vector Algorithmpush(o)ift=A. length 1then B new array of size … fori0tot do B[i]  A[i] A B tt +1 A[t] o Lists and Iterators

  16. Comparison of the Strategies • How large should the new array be? • incremental strategy: increase the size by a constant c • doubling strategy: double the size • 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 Lists and Iterators

  17. Comparison of the Strategies • 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 Lists and Iterators

  18. 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) Lists and Iterators

  19. Incremental Strategy Analysis • 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) Lists and Iterators

  20. geometric series 2 4 1 1 8 Doubling Strategy Analysis • We replace the array k = log2n 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) Lists and Iterators

More Related