elementary data structures
play

Elementary Data Structures Stacks, Queues, & Lists Amortized - PowerPoint PPT Presentation

Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees The Stack ADT (4.2.1) The Stack ADT stores arbitrary objects Auxiliary stack Insertions and deletions operations: follow the last-in first-out top():


  1. Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees

  2. The Stack ADT (§4.2.1) The Stack ADT stores arbitrary objects Auxiliary stack Insertions and deletions operations: follow the last-in first-out � top(): returns the last scheme inserted element without Think of a spring-loaded removing it plate dispenser � size(): returns the number of elements Main stack operations: stored � push(Object o): inserts � isEmpty(): a Boolean element o value indicating whether � pop(): removes and returns no elements are stored the last inserted element Elementary Data Structures 2

  3. Applications of Stacks Direct applications � Page-visited history in a Web browser � Undo sequence in a text editor � Chain of method calls in the Java Virtual Machine or C++ runtime environment Indirect applications � Auxiliary data structure for algorithms � Component of other data structures Elementary Data Structures 3

  4. Array-based Stack (§4.2.2) Algorithm pop (): if isEmpty () then A simple way of throw EmptyStackException implementing the else Stack ADT uses an t ← t − 1 array return S [ t + 1] We add elements Algorithm push ( o ) from left to right if t = S.length − 1 then A variable t keeps throw FullStackException track of the index of else the top element t ← t + 1 (size is t+1) S [ t ] ← o … S 0 1 2 t Elementary Data Structures 4

  5. Growable Array-based Stack In a push operation, when the array is full, instead of throwing an exception, we Algorithm push ( o ) if t = S.length − 1 then can replace the array with A ← new array of a larger one size … How large should the new for i ← 0 to t do array be? A [ i ] ← S [ i ] � incremental strategy: S ← A increase the size by a t ← t + 1 constant c S [ t ] ← o � doubling strategy: double the size Elementary Data Structures 5

  6. Comparison of the Strategies We compare the incremental strategy and the doubling strategy by analyzing the total time T ( n ) needed to perform a series of n push operations We assume that we start with an empty stack represented by an array of size 1 We call amortized time of a push operation the average time taken by a push over the series of operations, i.e., T ( n )/ n Elementary Data Structures 6

  7. Analysis of the Incremental Strategy We replace the array k = n / c times The total time T ( n ) of a series of n push operations is proportional to n + c + 2 c + 3 c + 4 c + … + kc = n + c (1 + 2 + 3 + … + k ) = n + ck ( k + 1)/2 Since c is a constant, T ( n ) is O ( n + k 2 ) , i.e., O ( n 2 ) The amortized time of a push operation is O ( n ) Elementary Data Structures 7

  8. Direct Analysis of the Doubling Strategy We replace the array k = log 2 n times geometric series The total time T ( n ) of a series of n push operations is 2 proportional to 4 n + 1 + 2 + 4 + 8 + …+ 2 k = 1 1 n + 2 k + 1 − 1 = 2 n − 1 8 T ( n ) is O ( n ) The amortized time of a push operation is O (1) Elementary Data Structures 8

  9. Accounting Method Analysis of the Doubling Strategy The accounting method determines the amortized running time with a system of credits and debits We view a computer as a coin-operated device requiring 1 cyber-dollar for a constant amount of computing. � We set up a scheme for charging operations. This is known as an amortization scheme . � The scheme must give us always enough money to pay for the actual cost of the operation. � The total cost of the series of operations is no more than the total amount charged. (amortized time) ≤ (total $ charged) / (# operations) Elementary Data Structures 9

  10. Amortization Scheme for the Doubling Strategy Consider again the k phases, where each phase consisting of twice as many pushes as the one before. At the end of a phase we must have saved enough to pay for the array-growing push of the next phase. At the end of phase i we want to have saved i cyber-dollars, to pay for the array growth for the beginning of the next phase. $ $ $ $ $ $ $ $ $ $ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7 • We charge $3 for a push. The $2 saved for a regular push are “stored” in the second half of the array. Thus, we will have 2( i /2)= i cyber-dollars saved at then end of phase i. • Therefore, each push runs in O (1) amortized time; n pushes run in O ( n ) time. Elementary Data Structures 10

  11. The Queue ADT (§4.3.1) Auxiliary queue operations: The Queue ADT stores arbitrary objects � front(): returns the element at the front without removing Insertions and deletions follow it the first-in first-out scheme � size(): returns the number of Insertions are at the rear of elements stored the queue and removals are � isEmpty(): returns a Boolean at the front of the queue value indicating whether no Main queue operations: elements are stored � enqueue(object o): inserts Exceptions element o at the end of the � Attempting the execution of queue dequeue or front on an � dequeue(): removes and empty queue throws an returns the element at the EmptyQueueException front of the queue Elementary Data Structures 11

  12. Applications of Queues Direct applications � Waiting lines � Access to shared resources (e.g., printer) � Multiprogramming Indirect applications � Auxiliary data structure for algorithms � Component of other data structures Elementary Data Structures 12

  13. Singly Linked List A singly linked list is a concrete data structure next consisting of a sequence of nodes Each node stores node � element elem � link to the next node ∅ A B C D Elementary Data Structures 13

  14. Queue with a Singly Linked List We can implement a queue with a singly linked list � The front element is stored at the first node � The rear element is stored at the last node The space used is O ( n ) and each operation of the Queue ADT takes O (1) time r nodes f ∅ elements Elementary Data Structures 14

  15. List ADT (§5.2.2) The List ADT models a Accessor methods: sequence of positions � first(), last() storing arbitrary objects � before(p), after(p) It allows for insertion Update methods: and removal in the � replaceElement(p, o), “middle” swapElements(p, q) � insertBefore(p, o), Query methods: insertAfter(p, o), � isFirst(p), isLast(p) � insertFirst(o), insertLast(o) � remove(p) Elementary Data Structures 15

  16. Doubly Linked List A doubly linked list provides a natural prev next implementation of the List ADT Nodes implement Position and store: � element � link to the previous node elem node � link to the next node Special trailer and header nodes trailer nodes/positions header elements Elementary Data Structures 16

  17. Trees (§6.1) In computer science, a tree is an abstract model Computers”R”Us of a hierarchical structure Sales Manufacturing R&D A tree consists of nodes with a parent-child relation US International Laptops Desktops Applications: � Organization charts � File systems Europe Asia Canada � Programming environments Elementary Data Structures 17

  18. Tree ADT (§6.1.2) We use positions to abstract Query methods: nodes � boolean isInternal(p) Generic methods: � boolean isExternal(p) � boolean isRoot(p) � integer size() � boolean isEmpty() Update methods: � objectIterator elements() � swapElements(p, q) � positionIterator positions() � object replaceElement(p, o) Accessor methods: Additional update methods may be defined by data � position root() structures implementing the � position parent(p) Tree ADT � positionIterator children(p) Elementary Data Structures 18

  19. Preorder Traversal (§6.2.3) A traversal visits the nodes of a Algorithm preOrder ( v ) tree in a systematic manner visit ( v ) In a preorder traversal, a node is for each child w of v visited before its descendants preorder ( w ) Application: print a structured document 1 Make Money Fast! 2 5 9 1. Motivations 2. Methods References 6 7 8 3 4 2.1 Stock 2.2 Ponzi 2.3 Bank 1.1 Greed 1.2 Avidity Fraud Scheme Robbery Elementary Data Structures 19

  20. Postorder Traversal (§6.2.4) In a postorder traversal, a Algorithm postOrder ( v ) node is visited after its for each child w of v descendants postOrder ( w ) Application: compute space used by files in a directory and visit ( v ) its subdirectories 9 cs16/ 8 3 7 todo.txt homeworks/ programs/ 1K 4 5 6 1 2 h1c.doc h1nc.doc DDR.java Stocks.java Robot.java 3K 2K 10K 25K 20K Elementary Data Structures 20

  21. Amortized Analysis of Tree Traversal Time taken in preorder or postorder traversal of an n-node tree is proportional to the sum, taken over each node v in the tree, of the time needed for the recursive call for v. � The call for v costs $(c v + 1), where c v is the number of children of v � For the call for v, charge one cyber-dollar to v and charge one cyber-dollar to each child of v. � Each node (except the root) gets charged twice: once for its own call and once for its parent’s call. � Therefore, traversal time is O(n). Elementary Data Structures 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend