Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistant: Yahav Nussbaum Website: Exam: 80% Theoretical Assingnments:

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Queues and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Data Structures Haim Kaplan and Uri Zwick October 2013 Lecture 2 Amortized Analysis “amortized analysis bounds the worst case running time of a sequence.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
DATA STRUCTURE & ALGORITHMS
Data Structures: A Pseudocode Approach with C
CS Data Structures II Review COSC 2006 April 14, 2017
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Data Structures Haim Kaplan and Uri Zwick February 2010 Lecture 2 Amortized Analysis “amortized analysis finds the average running time per operation over.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Chapter 3: Arrays, Linked Lists, and Recursion
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
Data Structures Using C++ 2E
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Design and Analysis of Algorithms CSC201 Shahid Hussain 1.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistants: Yaron Orenstein and Yahav Nussbaum view.php?id=
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Dan Grossman Fall 2013.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Data Structures Hanoch Levi and Daniel Cohen-Or Nov 2011 Lecture 1 Abstract Data Types Lists, Stacks, Queues, Deques Arrays, Linked Lists Amortized Analysis.
Hashing TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AA Course: Data Structures Lecturer: Haim Kaplan and Uri Zwick.
Linear Data Structures
C++ Review STL CONTAINERS.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
LINKED LISTS.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Elementary data structures
Linked List Stacks, Linked List Queues, Dequeues
Stacks and Queues.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
CMSC 341 Lecture 5 Stacks, Queues
Arrays and Linked Lists
Data Structures Lectures: Haim Kaplan, Uri Zwick Exam: 80%
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Data Structures Website: Lectures: Haim Kaplan and Uri Zwick
Haim Kaplan, Uri Zwick March 2018
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistant: Yahav Nussbaum Website: Exam: 80% Theoretical Assingnments: 10% Practical Assignments: 10% Cormen, Leiserson, Rivest and Stein Introduction to Algorithms (Second Edition)

Data Structures Lists Search Trees Heaps/Priority Queues Sorting and Selection Hashing Union-Find Tries and Suffix Trees “In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.”computer sciencedatacomputerefficiently

Data Structures Haim Kaplan and Uri Zwick February 2010 Lecture 1 Abstract Data Types Lists, Stacks, Queues, Deques Arrays, Linked Lists Amortized Analysis

Lists (Sequences) Insert b at position i : Delete the item at position i: [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] b Retrieve the item at position i:

5 Abstract Data Type (ADT) Lists (Sequences) List() – Create an empty list Length(L) – Return the length of list L Insert(L,i,b) – Insert b as the i-th item of L Retrieve(L,i) – Return the i-th item of L Delete(L,i) – Delete and return the i-th item of L ( Search(L,b) – Return the position of b in L, or −1 ) Interesting special cases: Insert-First(L,b), Retrieve-First(L), Delete-First(L) Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L)

Abstract Data Types (ADT) Stacks, Queues, Dequeues Stacks – Implement only: Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L) Also known as: Push(L,b), Top(L), Pop(L) Last In First Out (LIFO) Queues – Implement only: Insert-Last(L,b), Retrieve-First(L), Delete-First(L) First In First Out (FIFO) Deques (double ended queues) – Implement only: Insert-First(L,b), Retrieve-First(L), Delete-First(L) Insert-Last(L,b), Retrieve-Last(L), Delete-Last(L)

Implementing lists using arrays L array maxlen length a0a0 a1a1 … a n−1 n M M n *** We need to know the maximal length M in advance. Retrieve(L,i) (of any element) takes O(1) time Insert-Last(L,b) and Delete-Last(L) take O(1) time Stack operations in O(1) time

Implementing lists using arrays L array maxlen length a0a0 a1a1 … a n−1 n M M n *** We need to know the maximal length M in advance. Retrieve(L,i) (of any element) takes O(1) time Insert-Last(L,b) and Delete-Last(L) take O(1) time Insert(L,i,b) and Delete(L,i) take O(n−i+1) time

Implementing lists using arrays L array maxlen length n M M n n−1 0 1 n+1 Delete-Last very similar

Implementing lists using arrays L array maxlen length n M M n n−1 0 1 n+1 i We need to move n−i items, then insert. O(n−i+1) time.

Implementing lists using circular arrays Implement queue and deque operations in constant time L array maxlen length n M M n start 2 New field: start 01 2

Implementing lists using circular arrays Implement queue and deque operations in constant time L array maxlen length M−4 M M start 7 Occupied region can wrap around! M−401M−1 2

Implementing lists using circular arrays Implement queue and deque operations in constant time L array maxlen length 0 M M n start n− n Code of other operations similar

Implementing lists using singly linked lists Lists of unbounded length Support some additional operations length n last L first a0a0 a1a1 a n-1 a2a2 … List object List-Node object itemnext

item Insert-First with singly linked lists L first last length … n a0a0 a1a1 a n-1 a2a2 … next Insert-First(L,b) Generate a new List-Node object B containing b Increment L.length b (Return B) B Add B to the list n+1 Adjust L.last, if necessary

item Insert-First with singly linked lists L first last length … n a0a0 a1a1 a n-1 a2a2 … next b B n+1 Insert-Last and Delete-First– very similar, also O(1) time Unfortunately, Delete-Last requires O(n+1) time

item Retrieve with singly linked lists L first last length n a0a0 a1a1 a n-1 a2a2 … next Retrieving the i-th item takes O(i+1) time

item Insert with singly linked lists L first last length n a0a0 a1a1 a n-1 a2a2 … next Inserting an item into position i takes O(i+1) time

Inserting a node (After a given node) Insert-After(A,B) – Insert B after A a i−1 aiai … A … b B

Deleting a node (After a given node, not the last one) a i−1 aiai … A … Delete-After(A) – Delete the node following A a i+1 What happens to the node removed?

21 Abstraction barriers List Insert, Retrieve, Delete Search User ListNode Retrieve-Node Insert-After, Delete-After

22 Circular arrays vs. Linked Lists Circular arrays Linked lists Insert/Delete-First Delete-First O(1) Delete-LastO(1)O(n) Insert/Delete(i)O(min{i+1,n−i+1})O(i+1) Retrieve(i)O(1)O(i+1) Arrays have a fixed size. (For the time being.) Linked lists can do a few more things. Arrays always better than lists?

Concatenating lists Concat(L 1,L 2 ) – Attach L 2 to the end of L 1 L1L1 n a0a0 a1a1 a n-1 a2a2 … m b0b0 b1b1 b m-1 b2b2 … L2L2 n+m (What happened to L 2 ?) O(1) time!

24 Circular arrays vs. Linked Lists Circular arrays Linked lists Insert/Delete-First Insert-Last O(1) Delete-LastO(1)O(n) Insert/Delete(i)O(min{i+1,n−i+1})O(i+1) Retrieve(i)O(1)O(i+1) ConcatO(min{n 1,n 2 }+1)O(1) Linked list can do still more things…

Modified ADT for lists The current specification does not allow us to utilize one of the main capabilities of linked lists: Insert-After, Delete-After, Next in O(1) time We can define a new ADT in which the user is allowed to call ListNode, Insert-After, Delete-After.

Lists with “handles” [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] A B Insert operations return a handle to the item inserted Items can be accessed and moved through their handles

Lists with “handles” [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] A List() – Create an empty list Length(L) – Return the length of list L Insert(L,i,b) – Insert b as the i-th item of L Retrieve(L,i) – Return the i-th item of L Delete(L,i) – Delete and return the i-th item of L Concat(L 1, L 2 ) – Concatenate L 1 and L 2 B

Lists with “handles” [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] A Handle(b) – Return a handle to item b Next(A) – Return a handle to item following A (assuming there is one) Insert-After(A,B) – Insert B after A (assuming B is not in any list) Delete-After(A) – Delete the item following A (assuming there is one) List() – Create an empty list Length(L) – Return the length of list L Insert(L,i,b) – Insert b as the i-th item of L Retrieve(L,i) – Return the i-th item of L Delete(L,i) – Delete and return the i-th item of L Concat(L 1, L 2 ) – Concatenate L 1 and L 2 B Note: L is not an argument of these operations!

Implementing Lists with “handles” using Linked Lists [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] A B Handles List-Nodes Operations involving handles take O(1) time How do we maintain the length of a list? How do we keep track of first and last elements of a list?

Delete vs. Delete-After [a 0 a 1 a 2 a 3 … a i-1 a i a i+1 … a n-2 a n-1 ] A We have Delete-After(A) – Delete the item following A B How about Delete(A) – Delete item with handle A We cannot delete A from a singly linked list without a handle to the item preceding it.

Implementing lists using doubly linked lists Insert and deleting nodes in O(1) time Concatenation also in O(1) time First and last element are special. Causes problems. Difficult to maintain length when nodes inserted and deleted a0a0 a n-1 … a1a1 a2a2 L last n length first nextitem prev Each node now has a prev field

Deleting a node from a Doubly Linked List Delete(A) – Delete node A from its list a i−1 … a i+1 Each node now has a prev field aiai A Note: A itself not changed! Is that good?

Inserting a node into a Doubly Linked List Insert-After(A,B) – Insert node B after node A a i−1 aiai A b B

Implementing lists with handles using circular doubly linked lists The list is now circular L first A sentinel is added … a3a3 a2a2 a1a1 a n-1 No special treatment of first and last element Each node has a successor and a predecessor Every node can be removed in O(1) time

Empty list L Implementing lists with handles using circular doubly linked lists

L … a3a3 a2a2 a1a1 a n-1

Implementing lists with handles using circular doubly linked lists L … a3a3 a2a2 a1a1 a n-1 Implement Insert(L,i)

Pitfalls of the new ADT L1L1 … a3a3 a2a2 a1a1 a n-1 L2L2 … b3b3 b2b2 b1b1 b m-1 A B Insert-After(A,B) L 2 is not a valid list now Should call Delete(B) before Insert-After(A,B)

Implementing lists using (circular) arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 M M M n What do we do when the array is full? We cannot extend the array. Allocate a larger array and copy What should be the size of the new array?

Implementing lists using (circular) arrays with resizing L array maxlen length a0a0 a1a1 … a M−1 M M M n If we start with an empty array and increase its length by 1, then the time to do n Insert-Last is about:

Implementing lists using (circular) arrays with doubling When the array is full, double its size and copy What is the cost of n Insert-Last operations? Assume, for simplicity that n=2 k cost of insertion cost of copying The average/amortized cost of each operation is O(1)

Worst-case bounds Suppose that a data structure supports k different types of operations T 1, T 2,…,T k Let worst(T i ), the maximal time that a single operation of type T i may take. Let op 1, op 2,… op n be a sequence of operations that includes n i operations of type T i. Sometimes, this bound is very loose.

Amortized bounds We say that amort(T i ) is an amortized bound on the cost of operation of type T i iff for every valid sequence of operations op 1, op 2,… op n be sequence of operations including n i operations of type T i we have Note: Amortized bounds are bounds, they are not unique