Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistants: Yaron Orenstein and Yahav Nussbaum view.php?id=368215899.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
Queues and Linked Lists
Linear Lists – Linked List Representation
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.
Review Learn about linked lists
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Data Structures: A Pseudocode Approach with C
CS Data Structures II Review COSC 2006 April 14, 2017
Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistant: Yahav Nussbaum Website: Exam: 80% Theoretical Assingnments:
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.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
COMP 110 Introduction to Programming Mr. Joshua Stough.
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.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Chapter 3: Arrays, Linked Lists, and Recursion
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.
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.
Computer Science Department Data Structures and Algorithms Lecture 1.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Subject Name : Data Structure Using C Title : Linked Lists
CS2006- Data Structures I Chapter 5 Linked Lists III.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
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)
2 Obaid Ullah HOD Computer Science Dept. Superior University Sialkot Campus.
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.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure & Algorithms
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.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Cpt S 122 – Data Structures Abstract Data Types
Elementary data structures
Program based on queue & their operations for an application
Linked List Stacks, Linked List Queues, Dequeues
Stacks and Queues.
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
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistants: Yaron Orenstein and Yahav Nussbaum view.php?id= Website: Exam: 80% Theoretical Assingnments: 10% Practical Assignments: 10% Cormen, Leiserson, Rivest and Stein Introduction to Algorithms (Second/Third Editions)

Data Structures Lists Search Trees Heaps/Priority Queues 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 Sorting and Selection

Data Structures Haim Kaplan and Uri Zwick October 2012 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 When items are inserted or deleted, the indices of some other items change

5 Abstract Data Type (ADT) Lists (Sequences) List() – Create an empty list Length(L) – Return the length of list L Retrieve(L,i) – Return the i-th item of L Insert(L,i,b) – Insert b as 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: Retrieve-First(L), Insert-First(L,b), Delete-First(L) Retrieve-Last(L), Insert-Last(L,b), Delete-Last(L) Concat(L 1, L 2 ) – Concatenate L 1 and L 2

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 O(1) 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 O(1) 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 O(1) time L array maxlen length 0 M M n start n− n Code of other operations similar

14 Arrays vs. Circular Arrays Arrays Circular Arrays Insert/Delete Last O(1) Insert/Delete First O(n+1)O(1) Insert/Delete(i)O(n−i+1)O(min{i+1,n−i+1}) Retrieve(i)O(1) Main advantage: Constant access time Main disadvantage: Inserting or deleting elements ‘in the middle’ is expensive

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 Assignments’ order is important

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?

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!

23 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) In linked lists we can insert or delete elements ‘in the middle’ is O(1) time

24 More fun with linked lists Circular singly linked lists (Circular) Doubly linked lists Doubly linked lists with a sentinel

item Circular singly linked lists L first last length n a0a0 a1a1 a n-1 a2a2 … next If we make the list circular, we don’t need first All capabilities remain the same

26 How do we implement Delete-Last(L) in O(1) time? Doubly linked lists!

(Circular) Doubly linked lists All previous benefits + Delete-Last(L) in O(1) time a0a0 a n-1 … a1a1 a2a2 L first nextitem prev Each List-Node now has a prev field length n

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

Deleting a node from a Doubly Linked List Delete-Node(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?

Circular Doubly linked lists with a sentinel L sentinel … a2a2 a1a1 a0a0 a n-1 No special treatment of first and last elements Each node has a successor and a predecessor No special treatment of empty lists In some case we can identify a list with its sentinel

Empty list L Circular Doubly linked lists with a sentinel

L … a3a3 a2a2 a1a1 a n-1 Circular Doubly linked lists with a sentinel

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

34 With the current interface we need to do: Can we do? O(n) O(1) Suppose we inserted a into L. After sometime, we want to delete a from L

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

Lists – A modified abstraction [ a 0 a 1 … a i … a n-1 ] A List-Node(b) – Create a List-Node containing item b Item(B) – Return the item contained in List-Node B Insert(L,i,B) – Insert B as the i-th List-Node of L Retrieve(L,i) – Return the i-th List-Node of L Delete(L,i) – Delete and return the i-th List-Node of L Concat(L 1, L 2 ) – Concatenate L 1 and L 2 B Lists are now composed of List-Nodes List() – Create an empty list Length(L) – Return the length of list L

Next(A) – Return the List-Node following A (assuming there is one) Insert-After(A,B) – Insert B after A Delete-Node(A) – Delete A from its current list Note: L is not an argument of these operations! Lists – A modified abstraction [ a 0 a 1 … a i … a n-1 ] A B We now allow the following additional operations: These operations assume that A is contained in some list, while B is not contained in any list

The actual implementation using linked lists remains essentially the same The user explicitly manipulates List-Nodes Lists – A modified abstraction The length field is removed from the implementation as it is hard to keep it up to date Due to concatenations, hard to keep track of which list contains a given List-Node

Pitfalls of the modified ADT L1L1 … a2a2 a1a1 a0a0 a n-1 L2L2 … b2b2 b1b1 b0b0 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)

Which-List? Concatenations move List-Nodes for lists to lists Which-List(A) – return the list currently containing A Naïve implementation: scan the list from A until getting to the sentinel Much more efficient implementation possible using a Union-Find data structure.

41 Implementation of lists Circular arrays Doubly Linked lists Balanced Trees Insert/Delete-First Insert/Delete-Last O(1) O(log n) Insert/Delete(i)O(i+1) O(log n) Retrieve(i)O(1)O(i+1)O(log n) ConcatO(n+1)O(1)O(log n)