how large should a hash
play

How large should a hash CS 5633 -- Spring 2005 table be? Goal: Make - PowerPoint PPT Presentation

How large should a hash CS 5633 -- Spring 2005 table be? Goal: Make the table as small as possible, but large enough so that it wont overflow (or otherwise become inefficient). Problem: What if we dont know the proper size in advance?


  1. How large should a hash CS 5633 -- Spring 2005 table be? Goal: Make the table as small as possible, but large enough so that it won’t overflow (or otherwise become inefficient). Problem: What if we don’t know the proper size in advance? Solution: Dynamic tables. Dynamic Tables I DEA : Whenever the table overflows, “grow” it Carola Wenk by allocating (via malloc or new ) a new, larger table. Move all items from the old table into the Slides courtesy of Charles Leiserson with small new one, and free the storage for the old table. changes by Carola Wenk 3/1/05 CS 5633 Analysis of Algorithms 1 3/1/05 CS 5633 Analysis of Algorithms 2 Example of a dynamic table Example of a dynamic table 1. I NSERT 1. I NSERT 1 1 1 2. I NSERT 2. I NSERT overflow overflow

  2. Example of a dynamic table Example of a dynamic table 1. I NSERT 1. I NSERT 1 1 1 1 2. I NSERT 2. I NSERT 2 2 2 3. I NSERT overflow 3/1/05 CS 5633 Analysis of Algorithms 5 3/1/05 CS 5633 Analysis of Algorithms 6 Example of a dynamic table Example of a dynamic table 1. I NSERT 1. I NSERT 1 1 2. I NSERT 2. I NSERT 2 2 3. I NSERT 3. I NSERT overflow

  3. Example of a dynamic table Example of a dynamic table 1. I NSERT 1. I NSERT 1 1 2. I NSERT 2. I NSERT 2 2 3. I NSERT 3. I NSERT 3 3 4. I NSERT 4. I NSERT 4 4 5. I NSERT overflow 3/1/05 CS 5633 Analysis of Algorithms 9 3/1/05 CS 5633 Analysis of Algorithms 10 Example of a dynamic table Example of a dynamic table 1. I NSERT 1. I NSERT 1 1 2. I NSERT 2. I NSERT 2 2 3. I NSERT 3. I NSERT 3 3 4. I NSERT 4 4. I NSERT 4 5. I NSERT 5. I NSERT overflow

  4. Example of a dynamic table Worst-case analysis Consider a sequence of n insertions. The 1. I NSERT 1 worst-case time to execute one insertion is 2. I NSERT 2 Ο ( n ). Therefore, the worst-case time for n 3. I NSERT 3 insertions is n · Ο ( n ) = Ο ( n 2 ). 4. I NSERT 4 WRONG! In fact, the worst-case cost for 5. I NSERT 5 n insertions is only Θ ( n ) ≪ Ο ( n 2 ). 6 6. I NSERT 7 7. I NSERT Let’s see why. 3/1/05 CS 5633 Analysis of Algorithms 13 3/1/05 CS 5633 Analysis of Algorithms 14 Tighter analysis Tighter analysis Let c i = the cost of the i th insertion Let c i = the cost of the i th insertion = 1 + cost to double array size i 1 2 3 4 5 6 7 8 9 10 i 1 2 3 4 5 6 7 8 9 10 size i 1 2 4 4 8 8 8 8 16 16 size i 1 2 4 4 8 8 8 8 16 16 1 1 1 1 1 1 1 1 1 1 c i c i ? ? ? ? ? ? ? ? ? ?

  5. Tighter analysis Tighter analysis Let c i = the cost of the i th insertion Let c i = the cost of the i th insertion 1 + cost to double array size 1 + cost to double array size = = i 1 2 3 4 5 6 7 8 9 10 i 1 2 3 4 5 6 7 8 9 10 size i 1 2 4 4 8 8 8 8 16 16 size i 1 2 4 4 8 8 8 8 16 16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 c i c i 1 2 3 1 5 1 1 1 9 1 0 1 2 0 4 0 0 0 8 0 0 1 2 0 4 0 0 0 8 0 3/1/05 CS 5633 Analysis of Algorithms 17 3/1/05 CS 5633 Analysis of Algorithms 18 Tighter analysis (continued) Amortized analysis An amortized analysis is any strategy for n ∑ = Cost of n insertions c analyzing a sequence of operations: i = i 1 • compute the total cost of the sequence, OR  −  lg( n 1 ) ∑ j ≤ + n 2 • amortized cost of an operation = average = j 0 cost per operation, averaged over the number ≤ 3 n of operations in the sequence = Θ ( n ) . • amortized cost can be small, even though a Thus, the average cost of each dynamic-table single operation within the sequence might be operation is Θ ( n )/ n = Θ (1). expensive

  6. Amortized analysis Types of amortized analyses Three common amortization arguments: Even though we’re taking averages, however, • the aggregate method, probability is not involved! • the accounting method, Won’t cover in class • the potential method. • An amortized analysis guarantees the We’ve just seen an aggregate analysis. average performance of each operation in the worst case . The aggregate method, though simple, lacks the precision of the other two methods. In particular, the accounting and potential methods allow a specific amortized cost to be allocated to each operation. 3/1/05 CS 5633 Analysis of Algorithms 21 3/1/05 CS 5633 Analysis of Algorithms 22 Accounting analysis of Accounting method dynamic tables • Charge i th operation a fictitious amortized cost ĉ i , Charge an amortized cost of ĉ i = $3 for the i th where $1 pays for 1 unit of work ( i.e. , time). insertion. • This fee is consumed to perform the operation, and • $1 pays for the immediate insertion. • any amount not immediately consumed is stored in • $2 is stored for later table doubling. the bank for use by subsequent operations. When the table doubles, $1 pays to move a • The bank balance must not go negative! We must recent item, and $1 pays to move an old item. ensure that n n Example: ∑ ∑ ≤ c c ˆ i i $0 $0 $0 $0 $0 $0 $0 $2 $2 $2 $2 $2 $2 = = overflow i 1 i 1 $0 for all n . • Thus, the total amortized costs provide an upper bound on the total true costs.

  7. Accounting analysis of Accounting analysis of dynamic tables dynamic tables Charge an amortized cost of ĉ i = $3 for the i th Charge an amortized cost of ĉ i = $3 for the i th insertion. insertion. • $1 pays for the immediate insertion. • $1 pays for the immediate insertion. • $2 is stored for later table doubling. • $2 is stored for later table doubling. When the table doubles, $1 pays to move a When the table doubles, $1 pays to move a recent item, and $1 pays to move an old item. recent item, and $1 pays to move an old item. Example: Example: overflow $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $0 $2 $2 $2 $0 $0 3/1/05 CS 5633 Analysis of Algorithms 25 3/1/05 CS 5633 Analysis of Algorithms 26 Accounting analysis Conclusions (continued) • Amortized costs can provide a clean abstraction Key invariant: Bank balance never drops below 0. of data-structure performance. Thus, the sum of the amortized costs provides an upper bound on the sum of the true costs. • Any of the analysis methods can be used when an amortized analysis is called for, but each i 1 2 3 4 5 6 7 8 9 10 method has some situations where it is arguably size i 1 2 4 4 8 8 8 8 16 16 the simplest. c i 1 2 3 1 5 1 1 1 9 1 • Different schemes may work for assigning amortized costs in the accounting method, * ĉ i 2 3 3 3 3 3 3 3 3 3 sometimes yielding radically different bounds. bank i 1 2 2 4 2 4 6 8 2 4 *Okay, so I lied. The first operation costs only $2, not $3.

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