Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Structures Lectures: Haim Kaplan and Uri Zwick Teaching Assistants: Yaron Orenstein and Yahav Nussbaum view.php?id=368215899."— Presentation transcript:

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

2 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

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

4 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 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

6 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)

7 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

8 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

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

10 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.

11 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

12 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

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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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?

22 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 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 24 More fun with linked lists Circular singly linked lists (Circular) Doubly linked lists Doubly linked lists with a sentinel

25 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 26 How do we implement Delete-Last(L) in O(1) time? Doubly linked lists!

27 (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

28 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

29 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?

30 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

31 Empty list L Circular Doubly linked lists with a sentinel

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

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

34 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

35 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

36 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

37 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

38 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

39 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)

40 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 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)


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

Similar presentations


Ads by Google