Array Lists, Node Lists & Sequences

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Advertisements

Iterators and Sequences1 © 2010 Goodrich, Tamassia.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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.
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.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
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 and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
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.
Parasol Lab, Dept. CSE, Texas A&M University
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)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Linked List, Stacks 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,
Iterators and Sequences
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Double-Ended Queues Chapter 5.
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
Sequences and Iterators
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
COMP9024: Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Sequences and Iterators
Bucket-Sort and Radix-Sort
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
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,
Copyright © Aiman Hanna All rights reserved
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
Ordered Maps & Dictionaries
Copyright © Aiman Hanna All rights reserved
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Copyright © Aiman Hanna All rights reserved
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.
Copyright © Aiman Hanna All rights reserved
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

Array Lists, Node Lists & Sequences Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, 2010. ISBN 978-0-470-38326-1. Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN 978-0-470-48267-4. Both books are published by Wiley. Copyright © 2010-2011 Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © 2011-2016 Aiman Hanna All rights reserved

Array Lists & Sequences Coverage Array Lists Node Lists Sequences Array Lists & Sequences

Array Lists & Sequences The Array List ADT The Array List 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 given (e.g., a negative index) Array Lists & Sequences

Array Lists & Sequences The Array List ADT Main methods: get(integer i): returns the element at index i without removing it set(integer i, object o): replace the element at index i with o and return the old element add(integer i, object o): insert a new element o to have index i remove(integer i): removes and returns the element at index i Additional methods: size() isEmpty()  See ArrayList1.java, ArrayList2.java & ArrayList3.java Array Lists & Sequences

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

Array-based Implementation Use an array A of size N A variable n keeps track of the size of the array list (number of elements stored) Operation get(i) is implemented in O(1) time by returning A[i] Operation set(i,o) is implemented in O(1) time by performing t = A[i], A[i] = o, and returning t. A 1 2 i n Array Lists & Sequences

Array Lists & Sequences Insertion In 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 Array Lists & Sequences

Array Lists & Sequences Element Removal In 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 Array Lists & Sequences

Array Lists & Sequences Performance In the array based implementation of an array list: The space used by the data structure is O(n) size, isEmpty, get and set run in O(1) time add and remove run in O(n) time in worst case If we use the array in a circular fashion, operations add(0, x) and remove(0, x) run in O(1) time In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one Array Lists & Sequences

Growable Array-based Array List In an add(o) operation (without an index), we 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 Array Lists & Sequences

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 add(o) operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of an add operation the average time taken by an add over the series of operations, i.e., T(n)/n Array Lists & 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) Array Lists & 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 Array Lists & Sequences

Realization with the Array List Methods Array List ADT That array list ADT can then provide an adapter class for the D.Q ADT, as shown below: D.Q. Methods Realization with the Array List Methods size(), isEmpty() getFirst() get(0) getLast() get(size() - 1) addFirst(e) add(0, e) addLast(e) add(size(), e) removeFirst() remove(0) removeLast() remove(size() – 1) Array Lists & Sequences

Node List ADT Using an index is sometimes very suitable, such as in the case arrays, for referencing an element in a collection. However, in the case of linked lists, it could be more efficient to use a node (actually pointer to a node) instead of an index to reference and update an element. However, returning a pointer to a node to reference it may very well lead to privacy leak and compromises the list. Array Lists & Sequences

Node List ADT - Positions The node list ADT abstracts the concrete linked list data structure to allow elements of the list to be accessed by their relative position in the list. That is, a list is viewed as a collection of nodes that are referenced/located by their positions. The positions are arranged in a linear order. That is, instead of returning pointers to the nodes, which may compromise privacy, the node list ADT utilizes another ADT, referred to as Position ADT, to locate the elements of the list. Array Lists & Sequences

Node List ADT - Positions The Position ADT support the following method: element(), which returns the element stored at this position A position is always defined relatively; that is in relation to its neighbors. That is, in a list, each position b will always be after a position, a, and before a position c, except when b is the first or last position in the list. A position in the list does not change even if the element it points to changes or swapped. It is only destroyed if the element it points to is explicitly removed. Array Lists & Sequences

Node List ADT - Positions 25 62 12 34 k l m n Array Lists & Sequences

Node List ADT Using Positions, the node list ADT can define the following methods: first(): returns the position of the first element; error if list is empty. last(): returns the position of the last element; error if list is empty. prev(p): returns the position preceding position p in the list; error if p is first position. next(p): returns the position following position p in the list; error if p is last position. The above methods allows us to refer to relative positions in the list, starting at the beginning or end and to move inclemently up or down the list. Array Lists & Sequences

Node List ADT Additionally, the following methods are defined: set(p, e): Replace the element at position p with e , and return that old element at position p. addFirst(e): Insert a new element e as the first element and returns the position object. addLast(e): Insert a new element e as the last element and returns the position object. addBefore(p, e): Insert a new element e before position p and returns the position object. addAfter(p, e): Insert a new element e after position p and returns the position object. remove(p): Remove and return the element at position p. This also invalidates that position p in the list. size() and isEmpty() methods can also be defined. Array Lists & Sequences

Node List ADT Is there a redundancy between the methods of the ADT? For instance, addFirst(e) can be replaced by addBefore(first(), e) addLast(e) can be replaced by addAfter(last(), e)  No; these substitutions will only work if the list is not empty. Array Lists & Sequences

Node List ADT Example of Node List: Operation Output List addFirst(8) For clarity, object stored at the returned position is also shown after “/” List addFirst(8) -- [8] first() p1 / 8 addAfter(p1, 5) [8, 5] next(p1) p2 / 5 addBefore(p2, 3) [8, 3, 5] prev(p2) p3 / 3 addFirst(9) [9, 8, 3, 5] last() remove(first()) 9 set(p3, 7) 3 [8, 7, 5] addAfter(first(), 2) [8, 2, 7, 5] Array Lists & Sequences

Realization with the Node List Methods Node List ADT That node list ADT can then provide an adapter class for the D.Q ADT, as shown below: D.Q. Methods Realization with the Node List Methods size(), isEmpty() getFirst() first().element() getLast() last().element() addFirst(e) addLast(e) removeFirst() remove(first()) removeLast() remove(last()) Array Lists & Sequences

Sequence ADT Sequence is an ADT that supports all the methods of the D.Q., the Array List, and the Node List ADTs Elements accessed by Index, or Position Generic methods: size(), isEmpty() ArrayList-based methods: get(i), set(i, e), add(i, e), remove(i) Array Lists & Sequences

Sequence ADT NodeList-based methods: first(), last(), prev(p), next(p), set(p, e), addBefore(p, e), addAfter(p, e), addFirst(e), addLast(e), remove(p) Additionally, the ADT provide two “bridging” methods, which provide connection between indices and positions: atIndex(i): Returns the position of the element with index i; an error if i < 0 or i > size – 1 indexOf(p): Returns the index of the element at position p Array Lists & Sequences

Applications of Sequences The Sequence ADT is a basic, general-purpose, data structure for storing an ordered collection of elements Direct applications: Generic replacement for stack, queue, or list (since all needed methods are supported) small database (e.g., address book) Indirect applications: Building block of more complex data structures Array Lists & Sequences

Linked List Implementation A doubly linked list provides a reasonable implementation of the Sequence ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes Position-based methods run in constant time Index-based methods require searching from header or trailer while keeping track of indices; hence, run in linear time trailer header nodes/positions elements Array Lists & Sequences

Linked List Implementation Efficiency of linked list implementation of Sequence ADT: All position-based operations (such as first(), last(), prev(p), next(p), set(p, e), addFirst(e), addLast(e), addBefore(p, e), addAfter(p, e), remove(p) ), run in All D.Q. operations run in , since they only involve the two ends of the list. O(1). also O(1) Array Lists & Sequences

Linked List Implementation Efficiency of linked list implementation of Sequence ADT: However, methods of the ArrayList (such as get(i), set(i, e), add(i, e), remove(i)) would run in with such implementation since these operations would require O(n) hopping from one end of the list towards the other until locating index i. Can this be optimized? Array Lists & Sequences

Linked List Implementation Efficiency of linked list implementation of Sequence ADT: A slight optimization for methods such as get(i), set(i, e) can be achieved by starting from the closer end to the index. Locating index i would result in a running time of: O(min(i+1, n - i)), where n is the number of elements in the list. Worst case occurs when i = floor(n/2)  Still O(n) Array Lists & Sequences

Linked List Implementation Efficiency of linked list implementation of Sequence ADT: Similarly, running time of add(i, e) and remove(i) would be: O(min(i+1, n – i+ 1)), which still O(n). One advantage of this approach however is that: If i = 0 or i = n – 1, as is the case of the adaption of AttayList ADT to the D.Q ADT, then add and remove would run in O(1). Nonetheless, as a general conclusion, linked list implementation for Sequence ADT is inefficient for ArrayList methods. Array Lists & Sequences

Array-based Implementation We can use an array storing each elements in a cell A[i]. A position object can the be defined to hold: An index A reference to the array A 1 2 3 4 … 1 2 3 Positions – Each element has an index and a reference to an array Array Lists & Sequences

Array-based Implementation We then implement method element(p), which returns A[i]. This approach however has a major drawback. The cells in the array have no way to reference their corresponding positions. A 1 2 3 4 … 1 2 3 Positions – Each element has an index and a reference to an array Array Lists & Sequences

Array-based Implementation For instance, after performing add(i, e), there is no way to inform the positions of S that their indices went up. Recall that positions are defined relatively to their neighbor positions and not to their indices. A 1 2 3 4 … 1 2 3 Positions – Each element has an index and a reference to an array Note that these positions are no longer correct Array Lists & Sequences

Array-based Implementation elements Alternatively, we can use a circular array storing positions A new position object is defined to store: Index Element associated with the position 1 2 3 positions S f l Array Lists & Sequences

Array-based Implementation elements With this data structure, we can easily scan through the array to update the index variable for each position whose index changed because of an insertion or deletion. 1 2 3 4 positions S f l Array Lists & Sequences

Array-based Implementation Efficiency of array-based implementation of Sequence ADT: add(i), addFirst(e), addBefore(p, e), addAfter(p, e), remove(p) , remove(i), run in since we need to shift position objects to make room for insertion or adjust positions after removal. All other methods run in O(n) O(1). Array Lists & Sequences

Comparing Sequence Implementations Operation Array List size, isEmpty 1 atIndex, indexOf, get n first, last, prev, next set(p,e) set(i,e) add, remove(i) addFirst addLast addAfter, addBefore remove(p) Array Lists & Sequences

Favorites List ADT The Favorites List ADT models a collection of elements while keeping track of the number of times each element is accessed. The access counts allow us to know which elements are most frequently accessed. Additional Methods: access(e): accessed the element e while incrementing its access count. remove(e): removes the element e from list. top(k): returns list of k most accessed elements. Array Lists & Sequences

Applications of Favorites List Keeping track of most popular Web addresses for a Web browser. For a GUI interface: keeping track of most popular buttons for a pull-down menu. Array Lists & Sequences