CSE 326: Data Structures Lists

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
CSE 326: Data Structures Lecture #3 Analysis of Recursive Algorithms Alon Halevy Fall Quarter 2000.
1 CSE 326: Data Structures Program Analysis Lecture 3: Friday, Jan 8, 2003.
Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue.
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Stack Queue.
CSE 326: Data Structures Part Two: Lists Henry Kautz Autumn Quarter 2002.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Information and Computer Sciences University of Hawaii, Manoa
Week 3 – Wednesday.  What did we talk about last time?  ADTs  List implementation with a dynamic array.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
CSE 326: Data Structures Class #4 Analysis of Algorithms III Analysis of Recursive Algorithms Henry Kautz Winter 2002.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
STACKS & QUEUES for CLASS XII ( C++).
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
Week 4 - Monday CS221.
G64ADS Advanced Data Structures
Chapter 4 The easy stuff.
Week 4 - Friday CS221.
Practicum 1: - Persistent vs. destructive lists - Java interfaces
CE 221 Data Structures and Algorithms
Data Structure and Algorithms
Chapter 15 Lists Objectives
Abstract Data Types and Stacks
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Week 3 - Friday CS221.
Stacks.
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Programming Abstractions
Hashing Exercises.
Introduction to Data Structure
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.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
Object Oriented Programming COP3330 / CGS5409
Stacks, Queues, and Deques
Data Structures ADT List
Linked List (Part I) Data structure.
CSE 326: Data Structures Lists
ITEC 2620M Introduction to Data Structures
Hassan Khosravi / Geoffrey Tien
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
ADT list.
Elementary Data Structures
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
The List, Stack, and Queue ADTs
Data Structures and Analysis (COMP 410)
Lecture 16 Stacks and Queues CSE /26/2018.
Lecture No.02 Data Structures Dr. Sohail Aslam
CS210- Lecture 3 Jun 6, 2005 Announcements
Lecture 16 Stacks and Queues CSE /26/2018.
Dynamic Array: Implementation of Stack
structures and their relationships." - Linus Torvalds
Abstract Data Types Stacks CSCI 240
LINEAR DATA STRUCTURES
Abstract Data Types and Stacks
Presentation transcript:

CSE 326: Data Structures Lists Lecture 4: Monday, Jan 13, 2003

Today Finish amortized analysis of stretchy arrays The List ADT Reading assignment for this week: Weiss, Chapter 3

Amortized Analysis Stack Stack operations B C D E F E D C B A F Stack Stack operations push pop is_empty Stack property: if x is on the stack before y is pushed, then x will be popped after y is popped What is biggest problem with an array implementation?

Stretchy Stack Implementation int[] data; int maxsize; int top; Push(e){ if (top == maxsize){ temp = new int[2*maxsize]; for (i=0;i<maxsize;i++) temp[i]=data[i]; data = temp; maxsize = 2*maxsize; } data[++top] = e; } int pop() { return data[--top]; } Best case Push = O( ) Worst case Push = O( )

Stretchy Stack Amortized Analysis Consider sequence of n push/pop operations Amortized time = (T1 + T2 + . . . + Tn) / n We compute this next push(e1) push(e2) pop() push(e3) push(e4) . . . push(ek)  time = T1 n  time = Tn

Stretchy Stack Amortized Analysis The length of the array increases like this: 1, 2, 4, 8, . . . , 2k, . . ., n For each Ti we have one of the following Ti = O(1) for pop( ), and for some push(ei) Ti = O(2k) for some push(ei) Hence

Stretchy Stack Amortized Analysis Let’s compute this sum: And therefore: In an asymptotic sense, there is no overhead in using stretchy arrays rather than regular arrays!

Geometric Series

Stretchy Stack Amortized Analysis Careful ! We must be clever to get good amortized performance ! Consider “smart pop”: int pop(){ int e = data[--top]; if (top <= maxsize/2){ maxsize = maxsize/2; temp = new int[maxsize]; for (i=0;i<maxsize;i++) temp[i]=data[i]; data = temp;} return e; }

Stretchy Stack Amortized Analysis Take the sequence of 3n push/pop operations: push(e1) push(e2) ... push(en) pop() n Suppose n = 2k+1 Hence amortized time is: T = ((1) + . . . + (1) + (n) + . . .+ (n))/3n = (n (1) + 2n (n))/3n = 2/3 (n) Hence T = (n) !!! 2n

Stretchy Stack Amortized Analysis A more clever pop: int pop(){ int e = data[--top]; if (top <= maxsize/3){ maxsize = maxsize/2; temp = new int[maxsize]; for (i=0;i<top;i++) temp[i]=data[i]; data = temp;} return e; }

Stretchy Stack Amortized Analysis Some op’s take time=1, some take time > 1. Let’s look at consecutive op’s with time > 1. Four cases: op1 op2 . . . opi push() opj push( ) opk Case 1 op1 op2 . . . opi pop() opj push( ) opk Case 2  time > 1  time > 1 Case 1: push/push. The second push has to copy k data items. It means that right after the previous expensive push the array had size k and had k/2 data elements. Hence there were at least k/2 push’s with time=1 before we had to stretch the array again. Case 2: pop/push. Right before the push, the array has size k and k data elements (it is full). Hence the expensive pop must have reduced its size from 2k to k, and left only 2k/3 data items in the array: at least k-2k/3 = k/6 push’s are necessary before the array becomes full. time = 1 at least k/2 push’s (why ?) time = 1 at least k/6 push’s (why ?)  time = k > 1  time = k > 1

Stretchy Stack Amortized Analysis Some op’s take time=1, some take time > 1. Let’s look at consecutive op’s with time > 1. Four cases: Case 4 op1 op2 . . . opi push() opj pop( ) opm Case 3 op1 op2 . . . opi pop() opj pop( ) opm  time > 1  time > 1 Case 3: push/pop. The array has size 3k when the expensive pop copies k elements. The expensive push left it half full. Hence 3k/2 – k = k/2 pop’s are needed between the push and the pop. Case 4: pop/pop. As before, the array has k data elements and size 3k at the time of the second pop. Hence it had 2/3 * 3k = 2k data elements at the time of the first pop. 2k-k = k pop’s are needed between them. time = 1 at least k/2 pop’s (why ?) time = 1 at least k pop’s (why ?)  time = k > 1  time = k > 1

Stretchy Stack Amortized Analysis Now compute the average time: n op1 op2 . . . opi opj opm opn  time = 1 at least k1/6  time = 1  time = 1  time = k1 > 1 Total time: T  6n Amortized time: T/n = O(1)  time = 1 at least k2/6  time = 1  time = 1  time = k2 > 1  time = 1 at least k3/6  time = 1  time = 1  time = k3 > 1  time = 1 . . . . . .  time = 1 . . . . . .

Lists We will describe them as ADTs = Abstract Data Types

Abstract vs. Concrete Data Types Abstract Data Type (ADT) Mathematical description of an object and the set of operations on the object List, Stack, Tree, Heap, Graph, … One ADT may specialize another ADT One ADT may implement another ADT Concrete Data Type Implementation of an ADT using some set of primitive data types and operations of known complexity Primitives: integers, arrays, pointers or references Object-oriented programming languages (Java, C++) let you explicitly define new concrete data types that correspond to ADT’s.

List ADT ( A1 A2 … An-1 An ) length = n Mathematical description: a sequence of items Ai precedes Ai+1 for 1  i < n Operations First() = position Value(position) = item Next(position) = position Length() = integer Insert(item,position) Delete(position) What other operations might be useful? Kth(integer)=item SetKth(item,integer) Find(item)=position

Specialization Hierarchy List Property: Sequence First()=pos Value(pos)=item Kth(integer)=item Next(pos)=pos Length()=integer SetKth(item,integer) Insert(item,pos) Delete(pos) Find(item)=position Stack Property: LIFO Push(item) Pop()=item IsEmpty()=true/false Queue Property: FIFO Enqueue(item) Dequeue()=item IsEmpty()=true/false Vector Property: random access Kth(int) = item SetKth(item,integer)

Implementation Hierarchy List Complexity: Unspecified First()=pos Value(pos)=item Kth(integer)=item Next(pos)=pos Length()=integer SetKth(item,integer) Insert(item,pos) Delete(pos) Find(item)=position Linked List (1) for: (n) for: Array (1) for: (n) for:

Specialization and Implementation Hierarchies List Stack Queue Vector Sorted Vector Linked List

What’s an alternative implementation? Concrete Data Types List b c  Linked List What’s an alternative implementation? Linked List using References nodeB.value = “b”; nodeC.value = “c”; list = nodeB; nodeB.next = nodeC

Concrete Data Types List Linked List Linked List using References b c  Linked List Linked List using References Linked List using Arrays list = 4; nodeB.value = “b”; nodeC.value = “c”; list = nodeB; nodeB.next = nodeC “c” “b” 2 1 3 4 5

Linked Lists in C struct node{ Object element; struct node * next; } a b c  L struct node{ Object element; struct node * next; } Everything else is a pointer to a node! typedef stuct node * List; typedef struct node * Position;

Linked Lists in Java – version 1 References to objects are implicit pointers class ListNode{ Object element; ListNode next; } class List{ Listnode head; Listnode find(Object item) { Listnode n = head; while (n != null) { if (n.element == item) return n; } return null; }

Data Hiding Good programming style hides internal details of an object from the rest of the program Guarantees that data structure always works as expected – cannot easily be corrupted Here, must make details of ListNode and List public Type returned by find For iterating through a list: ListNode n; for (n = mylist.head; n!= null; n = n.next){ v = n.element; do something on each v }

Iterators Introduce a new public class to explicitly represent a position in a list Then: public class LinkedListItr { ListNode current; public Object retrieve() { return current.element; } public void advance() { current = current.next; } public boolean pastEnd() { return current == NULL; } LinkedListItr i; for (i = mylist.first(); !i.pastEnd(); i.advance){ do something on each v.retrieve() }

Abstract Iterators Iterators can also be defined for an array implementation of lists: We can create an abstract iterator that works for both linked list and array implements of List public class ArrayListItr { Object [] data; integer current; public Object retrieve() { return data[current]; } public void advance() { current = current+1; }

Abstract Iterator Why do this? abstract class ListItr { abstract Object retrieve(); abstract void advance(); … } class LinkedListItr extends ListItr { … } class ArrayListItr extends ListItr { … } Why do this?

Array Implementation of Linked Lists 1 2 3 4 5 6 7 8 9 10 Data F O A R N R T Next 3 8 6 4 -1 10 5 First = 2 How do we implement Delete(position) ? Insert(element, position)?

Free Cell Management Data Next 7 9 First = 2 Free = 1 3 4 5 6 7 8 9 10 Data F O A R N R T Next 7 3 8 6 4 -1 9 10 5 First = 2 Free = 1 When an item is removed from the list, must “reclaim” the unused cell for later use Can use same array to manage a second list of unused cells

Memory Management Keeping a free cell list is an example of a memory management strategy How is memory managed in C? C++? Java?

Summary: Complexity Linked list Array Sorted array Kth(int) Find(e) Insert(e,pos) Next(pos) InsertAnywhere(e)