Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CSC130 Data Structures and Algorithm Analysis Spring 2017, Lecture 4 Instructor: Dr. Yuan Cheng Session Plan Review: Theoretical Analysis of Algorithm Arrays and Lists Abstract Data Type Data Types vs. Data Structures A data type is a well-defined collection of data with a well-defined set of operations on it. A data structure is an actual implementation of a particular abstract data type. Abstract data type (ADT) specifies: Types of data stored Operations and their parameters supported on the data ADT specifies what each operation does, not how it does. Array An array stores a sequence of values that are all of the same type. Contiguous area of memory consisting of equal-size elements indexed by contiguous integers. 0 1 2 3 4 5 6 7 8 9 What’s Special About Arrays? Constant-time access array_addr + elem_size × (i – first_index) 0 1 2 3 4 5 6 7 8 9 Quiz Given an array whose: • Address is 1000, • Element size is 8, • First index is 0 What is the address of the element at index 6? 1) 40 2) 48 3) 1005 4) 1048 5) 1006 6) 1040 Quiz Given an array whose: • Address is 1000, • Element size is 8, • First index is 0 What is the address of the element at index 6? 1) 40 2) 48 3) 1005 4) 1048 5) 1006 6) 1040 Multi-Dimensional Arrays Multi-Dimensional Arrays (1,1) (3, 4) Multi-Dimensional Arrays (1,1) (3, 4) array_addr + elem_size × (( i – firstrow_index) × row_size + (j – firstcol_index)) Time for Common Operations Add Beginning End Middle Remove Access Time for Common Operations Add Remove Access Beginning O(n) O(n) O(1) End O(1) O(1) O(1) Middle O(n) O(n) O(1) List An ordered series of objects We can do: Insert an object to a list (at location k) Remove an object from a list Access an object from a list (at location k) Applications for List To-Do List: insert tasks, remove when done. Word Processor: Typing text inserts to list, Deleting text removes (simple array won’t work) Shopping: insert needed items, remove when bought Array Implementation of List 1st hurdle: inserting object anywhere but the end Shift all entries forward one. O(n) 2nd hurdle: arrays have fixed sizes Create bigger array when we run out of space, copy old array to big array Linked List Implementation of List Store list objects anywhere in memory A linked list is made up of nodes, each of which contains: An item A reference to its next node APIs for Singly-Linked List Insert at the beginning Remove from the beginning Insert at the end Remove from the end Insert/Remove at other positions Remove a given node Insert a new node before a given node Time for Common Operations Singly-Linked List No tail PushFront(Key) O(1) PopFront() O(1) PushBack(Key) O(n) PopBack() O(n) Find(Key) O(n) IsEmpty() O(1) AddBefore(Node, Key) O(n) AddAfter(Node, Key) O(1) With tail O(1) Linked List vs. Array List Linked List Array List No additional penalty on size Need to estimate size/grow array Insert/remove at front O(1) Insert/remove at front O(n) Get kth costs O(n) Get kth costs O(1) Need some extra memory for links Arrays are compact in memory Doubly-linked List Time for Common Operations Doubly-Linked List No tail With tail PushFront(Key) O(1) PopFront() O(1) PushBack(Key) O(n) O(1) PopBack() O(n) O(1) Find(Key) O(n) IsEmpty() O(1) AddBefore(Node, Key) O(n) AddAfter(Node, Key) O(1) O(1) List in Java Collection Interface extends Iterable A Collection stores a group of objects We can add and remove from a Collection Iterator objects let us iterate over objects in a Collection Built in LinkedList and ArrayList implementations of Collection List: Summary Constant time to insert at or remove from the front. With tail and doubly-linked list, constant time to insert at or remove from the back. O(n) time to find arbitrary element. List elements need not be contiguous. With doubly-linked list, constant time to insert between nodes or remove a node. Next class Topics covered: Stacks Queues Reading assignment: Sedgewick: Chapter 1.3 Lafore: Chapter 4 Weiss: Chapter 3