Graduate Analysis of Algorithms Dr. Haim Levkowitz

Size: px
Start display at page:

Download "Graduate Analysis of Algorithms Dr. Haim Levkowitz"

Transcription

1 UMass Lowell Computer Science 9.53 Graduate Analysis of Algorithms Dr. Haim Levkowitz Fall 27 Lecture 5 Tuesday, 2 Oct 27 Amortized Analysis

2 Overview Amortize: To pay off a debt, usually by periodic payments [Websters] 2

3 Amortized Analysis: creative accounting for operations Average operation cost over sequence of op s can show average cost of operation small even though single op in sequence expensive result must hold for any sequence of these op s no probability involved (unlike average-case analysis) guarantee holds in worst-case analysis method only no effect on code operation 3

4 Overview (continued) determine amortized cost of one operation part of sequence of operations 4

5 3 methods Aggregate method Accounting method Potential method 5

6 3 examples stack with multipop operation binary counter dynamic tables (later on) 6

7 Aggregate Method find upper bound T(n) on total cost of sequence of n operations amortized cost = average cost per operation = T(n)/n same for all operations in sequence 7

8 Accounting Method amortized cost can differ across operations overcharge some operations early in sequence store overcharge as prepaid credit on specific data structure objects 8

9 Potential Method amortized cost can differ across operations (as in accounting method) overcharge some operations early in sequence (as in accounting method) store overcharge as potential energy of data structure as whole (unlike accounting method) 9

10 Aggregate Method: Stack Operations

11 Traditional Stack Operations PUSH(S, x) pushes object x onto stack S POP(S) pops top of stack S, returns popped object O() time: consider cost as for our discussion Total actual cost of sequence of n PUSH/POP operations = n

12 Aggregate Method: Stack Operations (continued) New Stack Operation MULTIPOP(S, k) pops top k elements off stack S entire stack if stack has < k items MULTIPOP(S, k) while not STACK-EMPTY(S) and k > 2 do POP(S) 3 k k - 2 source: 9.53 textbook Cormen et al.

13 MULTIPOP actual cost for stack containing s items: Use cost = for each PUSH/POP Cost = min(s, k) [# iterations of while loop] Worst-case cost = O(n) 3

14 Sequence of n PUSH, POP, MULTIPOP operations: initially empty stack Worst-case cost of MULTIPOP O(n) Have n operations worst-case cost of sequence is O(n 2 ) 4

15 Observation Each object can be popped only once per time that it s pushed Have n PUSHes n POPs including those in MULTIPOP total cost = O(n) Average over n operations O() per operation on average 5

16 Again, notice no probability Showed worst-case O(n) cost for sequence O() per operation on average Aggregate analysis 6

17 Fig. 7.: Multipop demo d 7

18 Binary counter k-bit binary counter A[.. k ] bits A[] = LSB A[k ] = MSB Counts upward from Value of counter is Initial counter value = A[.. k ] = k i= A[ i] 2 i 8

19 To increment, add (mod 2 k ): INCREMENT(A, k) i while i < k and A[i ] = do A[i ] i i + if i < k then A[i ] Example k = 3 9

20 Example: k = 3, counter : Counter value A2 A A Cost Underlined bits flip 2 3 Cost: # flips to get here

21 Example: k = 3, counter : 3 4 Counter value A2 A A Cost Underlined bits flip 2 3 Cost: # flips to get here

22 Cost: # flips to get here Cost of INCREMENT = (# of bits flipped) Analysis: Each call could flip k bits n INCREMENTs take O(nk)time 22

23 Observation Not every bit flips every time See table 23

24 bit 2 i i k flips how often every time /2 the time /4 the time /2 i the time never times in n INCREMENTs n n/2 n/4 n/2 i 24

25 25 total # of flips = n INCREMENTs costs O(n) Average cost per operation = O() n n n n i i k i i 2 2 / 2 / 2 / = = < = = =

26 Accounting Method Diff charges to diff op s Some charged more than actual cost Some charged less 26

27 Amortized cost = amount we charge When amortized cost > actual cost store difference on specific objects in data structure prepaid credit Use credit later to pay for op s whose actual cost > amortized cost 27

28 Differs from aggregate analysis: Accounting method: diff op s can have diff costs Aggregate analysis: all op s have same cost Credit should never go negative Otherwise sequence of op s for which amortized cost is not upper bound on actual cost Amortized cost would tell us nothing 28

29 Let c i be actual cost of ith operation Let be amortized cost of ith operation Require ĉ i n i= cˆ i c Total amortized cost of sequence of n operations must be upper bound on total actual cost of sequence Total credit stored in data structure = i must be nonnegative for all n n i= i n = n cˆ i c i= i 29

30 Accounting Method: Stack Operations Operation Actual Cost Assigned Amortized Cost PUSH 2 POP MULTIPOP min(k,s) 3 source: 9.53 textbook Cormen et al.

31 Intuition Think trays in cafeteria : push/pop costs $ each When pushing object, pay $2 $ for PUSH $ = prepayment for it being popped by either POP MULTIPOP Each object has $ credit credit can never go negative total amortized cost = O(n) upper bound on total actual cost 3

32 Binary counter Charge $2 to set bit to $ pays for setting bit to $ prepayment for flipping it back to Have $ of credit for every in counter credit 32

33 Amortized cost of INCREMENT: Cost of resetting bits to paid by credit At most bit set to amortized cost $2 For n op s, amortized cost = O(n) 33

34 Potential Method Amortized cost can differ across operations (as in accounting method) Overcharge some operations early in sequence (as in accounting method) Store overcharge as potential energy of data structure as whole (unlike accounting method) Where credit stored in specific object Credit can be used for whichever most flexible of amortized analysis methods 34

35 Potential Method (cont.) Start w/initial data structure D Perform n ops on it (i =,..., n) Let c i be actual cost of ith op Let D i be data structure after applying ith op Let Φ(D i ) be potential associated w/ D i Amortized cost of ith operation: cˆ i = ci + Φ( Di ) Φ( Di ) Total amortized cost of n operations: n i= n cˆ i = ( ci + Φ( Di ) Φ( Di )) = ci + Φ( Dn ) Φ( D ) i= Must have: Φ( D n ) Φ( D ) to pay in advance To guarantee (n unknown), must have Φ(D ) =, Φ(D i ) for all i n i= terms telescope 35

36 Potential Method: Stack Operations Potential function value Φ = # items in stack (# $ bills in accounting method) D = empty stack Φ(D )= Since # objects in stack always Φ(D i ) = Φ(D ) for all i 36

37 Amortized operation costs (assuming stack has s items) Op Actual cost ΔΦ Φ(D i ) - Φ(D i - ) Amortized cost c i + ΔΦ PUSH (s + ) s = + = 2 POP (s ) s = = MULTIPOP k = min(k, s) (s k ) s = k k k = amortized cost of sequence of n operations = O(n) 37

38 Binary counter Φ = b i = # of s after ith INCREMENT Suppose ith op reset t i bits to 38

39 c i t i + (resets t i bits, sets bit to ) If b i =, ith op reset all k bits & didn t set one b i = t i = k b i = b i t i If b i >, ith op reset t i bits, set one b i = b i t i + Either way, b i b i t i + ΔΦ(D i ) (b i t i + ) b i = t i c^i = c i + ΔΦ(D i ) (t i + ) + ( t i ) = 2. If counter starts at, Φ(D )= amortized cost of n operations = O(n) 39

40 Dynamic Tables Scenario: Have table maybe hash table Don t know in advance how many objects will be stored When it fills, must reallocate with larger size copy all objects into new, larger table When it gets sufficiently small might want to reallocate with smaller size Details of table organization not important 4

41 Goals. O() amortized time per operation 2. Unused space always constant fraction of allocated space 4

42 Load factor α α(t) = num(t)/size(t), where num(t) = # items stored size(t) = allocated size If size(t) = then num(t) = Call α(t) = Never allow α > Keep α > constant fraction goal #2 42

43 Table expansion Consider only insertion: When table becomes full double its size reinsert all existing items Guarantees that α /2 Each time we actually insert item into table it s elementary insertion 43

44 Table-Insert(T, x) Linear in individual insertion time Constant overhead 5-9 then: Expansion 5-7 overhead dominated by transfer cost in 6 Elementary insertion 44

45 Running time: Charge per elementary insertion Count only elementary insertions since all other costs together are constant per call c i = actual cost of ith operation If not full, c i = If full have i items in table at start of ith op have to copy all i existing items then insert ith item c i = i n operations c i = O(n) O(n 2 ) time for n operations 45

46 We don t always expand: c i = p i i = 2 Exact power of 2 ow 46

47 Total cost Total cost aggregate analysis says amortized cost per operation = 3 47

48 Accounting Method: Charge $3 per insertion of x $ pays for x s insertion $ pays for x to be moved in future $ pays for some other item to be moved 48

49 Suppose just expanded size = m before next expansion size = 2m after Assume expansion used up all credit no credit stored after expansion Will expand again after another m insertions 49

50 Each insertion will put $ on one of m items that were in table just after expansion, and will put $ on item inserted Have $2m of credit by next expansion when there are 2m items to move Just enough to pay for expansion with no credit left over! 5

51 Potential method Φ(T) = 2 num[t ] size[t ] Initially num = size = Φ = Just after expansion size = 2 num Φ = Just before expansion size = num Φ = num have enough potential to pay for moving all items Need Φ, always 5

52 Always have size num /2 size 2 num size Φ 52

53 Amortized cost of ith operation: num i = num after ith op size i = size after ith op Φ i = Φ after ith op If no expansion: size i = size i num i = num i + c i = 53

54 Then we have c^i = c i + Φ i Φ i- = + (2 num i size i ) (2 num i size i ) = + (2 num i size i ) (2(num i ) size i ) // no expansion = + 2 = 3 54

55 If expansion: size i = 2 size i size i = num i = num i c i = num i + = num i 55

56 Then we have c^i = c i + Φ i Φ i- = num i + (2 num i size i ) (2 num i size i ) = num i + (2 num i 2(num i )) (2(num i ) (num i )) = num i + 2 (num i ) = 3 56

57 Expansion and contraction When α drops too low, contract table Allocate new, smaller one Copy all items Still want α bounded from below by constant amortized cost per operation = O() Measure cost in terms of elementary insertions and deletions 57

58 58

59 Obvious strategy : Double size when inserting into full table (when α =, after insertion α would become > ) Halve size when deletion would make table < half full (when α = /2, after deletion α would become < /2) Then always have /2 α 59

60 Example (p. 42) Perform n ops on table T n = exact power of 2 First n/2 : insertions Cost: Θ(n) At end: num[t] = size[t] = n/2 Second n/2 : I, D, D, I, I, D, D, I, I,. I = insert; D = delete 6

61 Example (p. 42) cont. First I expand to size n Following DD contract to n/2 II expand Etc. Cost of ea. expansion & contraction: Θ(n) There are Θ(n) of them total Θ(n 2 ) amortized cost per op = Θ(n) 6

62 What s the problem? Not enough Deletions after expansion to pay for contraction Insertions after contraction to pay for expansion Improve: allow load factor drop below /2 Specifically, down to /4 62

63 63

64 Simple solution: Double as before: when inserting with α = after doubling, α = /2 Halve size when deleting with α = /4 after halving, α = /2 immediately after either expansion or contraction, have α = /2 Always have /4 α 64

65 Intuition: Want to make sure that perform enough op s between consecutive expansions/contractions to pay for change in table size Need to delete half items before contraction double number of items before expansion Either way, # ops between expansions/contractions is at least constant fraction of # items copied 65

66 66

67 Intuition: Φ measures how far from α = /2 we are α = /2 Φ = 2 num 2 num = α = Φ = 2 num num = num α = /4 Φ = size /2 num = 4 num /2 num = num when double or halve, have enough potential to pay for moving all num items 67

68 Potential increases linearly between α = /2 and α = α = /2 and α = /4 68

69 α has diff distances to go to get to or /4, starting from /2 rate of increase of Φ differs For α to go /2 For α to go /2 /4 69

70 For α to go /2 num increases size/2 size for a total increase of size/2 Φ increases size Φ needs to increase by 2 for each item inserted That s why there s coeff of 2 on num[t] term in formula for Φ when α /2 7

71 For α to go /2 /4 num decreases size/2 size/4 for total decrease of size/4 Φ increases size/4 needs to increase by for each item deleted That s why there s a coefficient of on the num[t ] term in the formula for when α < /2 7

72 Amortized costs: more cases insert, delete α /2, α < /2 use αi, since α can vary a lot size does/doesn t change 72

73 Insert: α i /2 same analysis as before c^i = 3 α i < /2 no expansion only occurs when α i = If α i < /2 and α i < /2: 73

74 74

75 Therefore, amortized cost of insert is < 3 75

76 Delete 76

77 77

78 78

79 79

80 Therefore, amortized cost of delete is 2 8

Lecture Notes for Chapter 17: Amortized Analysis

Lecture Notes for Chapter 17: Amortized Analysis Lecture Notes for Chapter 17: Amortized Analysis Chapter 17 overview Amortized analysis Analyze a sequence of operations on a data structure. Goal: Show that although some individual operations may be

More information

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d

CS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d CS 5321: Advanced Algorithms Amortized Analysis of Data Structures Ali Ebnenasir Department of Computer Science Michigan Technological University Motivations Why amortized analysis and when? Suppose you

More information

Lecture 7: Amortized analysis

Lecture 7: Amortized analysis Lecture 7: Amortized analysis In many applications we want to minimize the time for a sequence of operations. To sum worst-case times for single operations can be overly pessimistic, since it ignores correlation

More information

Today: Amortized Analysis

Today: Amortized Analysis Today: Amortized Analysis COSC 581, Algorithms March 6, 2014 Many of these slides are adapted from several online sources Today s class: Chapter 17 Reading Assignments Reading assignment for next class:

More information

Amortized Analysis (chap. 17)

Amortized Analysis (chap. 17) Amortized Analysis (chap. 17) Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations. Probabilistic analysis: Average case running

More information

Data Structures and Algorithm. Xiaoqing Zheng

Data Structures and Algorithm. Xiaoqing Zheng Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn MULTIPOP top[s] = 6 top[s] = 2 3 2 8 5 6 5 S MULTIPOP(S, x). while not STACK-EMPTY(S) and k 0 2. do POP(S) 3. k k MULTIPOP(S, 4) Analysis

More information

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case. Amortized Analysis: CLRS Chapter 17 Last revised: August 30, 2006 1 In amortized analysis we try to analyze the time required by a sequence of operations. There are many situations in which, even though

More information

Amortized Analysis. DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person

Amortized Analysis. DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person Amortized Analysis DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person What is the maximum amount of money I can receive? Amortized Analysis DistributeMoney(n,

More information

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM AMORTIZED ANALYSIS binary counter multipop stack dynamic table Lecture slides by Kevin Wayne http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on 1/24/17 11:31 AM Amortized analysis Worst-case

More information

Tutorial 4. Dynamic Set: Amortized Analysis

Tutorial 4. Dynamic Set: Amortized Analysis Tutorial 4 Dynamic Set: Amortized Analysis Review Binary tree Complete binary tree Full binary tree 2-tree P115 Unlike common binary tree, the base case is not an empty tree, but a external node Heap Binary

More information

Amortized analysis. Amortized analysis

Amortized analysis. Amortized analysis In amortized analysis the goal is to bound the worst case time of a sequence of operations on a data-structure. If n operations take T (n) time (worst case), the amortized cost of an operation is T (n)/n.

More information

CSE 548: Analysis of Algorithms. Lecture 8 ( Amortized Analysis )

CSE 548: Analysis of Algorithms. Lecture 8 ( Amortized Analysis ) CSE 548: Analysis of Algorithms Lecture 8 ( Amortized Analysis ) Rezaul A. Chowdhury Department of Computer Science SUNY Stony Brook Fall 2017 A Binary Counter value #bit flips #bit resets ( 1 0 ) #bit

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Amortized Analysis Ulf Leser Two Examples Two Analysis Methods Dynamic Tables SOL - Analysis This lecture is not covered in [OW93] but, for instance, in [Cor03] Ulf Leser:

More information

CHAPTER 17 Amortized Analysis

CHAPTER 17 Amortized Analysis CHAPTER 7 Amortzed Analyss In an amortzed analyss, the tme requred to perform a sequence of data structure operatons s averaged over all the operatons performed. It can be used to show that the average

More information

N/4 + N/2 + N = 2N 2.

N/4 + N/2 + N = 2N 2. CS61B Summer 2006 Instructor: Erin Korber Lecture 24, 7 Aug. 1 Amortized Analysis For some of the data structures we ve discussed (namely hash tables and splay trees), it was claimed that the average time

More information

DATA STRUCTURES I, II, III, AND IV

DATA STRUCTURES I, II, III, AND IV Data structures DATA STRUCTURES I, II, III, AND IV I. Amortized Aalysis II. Biary ad Biomial Heaps III. Fiboacci Heaps IV. Uio Fid Static problems. Give a iput, produce a output. Ex. Sortig, FFT, edit

More information

Amortized Complexity Main Idea

Amortized Complexity Main Idea omp2711 S1 2006 Amortized Complexity Example 1 Amortized Complexity Main Idea Worst case analysis of run time complexity is often too pessimistic. Average case analysis may be difficult because (i) it

More information

CSCB63 Winter Week10 - Lecture 2 - Hashing. Anna Bretscher. March 21, / 30

CSCB63 Winter Week10 - Lecture 2 - Hashing. Anna Bretscher. March 21, / 30 CSCB63 Winter 2019 Week10 - Lecture 2 - Hashing Anna Bretscher March 21, 2019 1 / 30 Today Hashing Open Addressing Hash functions Universal Hashing 2 / 30 Open Addressing Open Addressing. Each entry in

More information

Analysis of Algorithms I: Perfect Hashing

Analysis of Algorithms I: Perfect Hashing Analysis of Algorithms I: Perfect Hashing Xi Chen Columbia University Goal: Let U = {0, 1,..., p 1} be a huge universe set. Given a static subset V U of n keys (here static means we will never change the

More information

Average Case Analysis of Marking Algorithms

Average Case Analysis of Marking Algorithms Average Case Analysis of Marking Algorithms D.S. Hirschberg and L.L. Larmore University of California, Irvine California State University, Dominguez Hills Abstract. The Lindstrom marking algorithm uses

More information

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February)

Algorithms and Data Structures 2016 Week 5 solutions (Tues 9th - Fri 12th February) Algorithms and Data Structures 016 Week 5 solutions (Tues 9th - Fri 1th February) 1. Draw the decision tree (under the assumption of all-distinct inputs) Quicksort for n = 3. answer: (of course you should

More information

CS361 Homework #3 Solutions

CS361 Homework #3 Solutions CS6 Homework # Solutions. Suppose I have a hash table with 5 locations. I would like to know how many items I can store in it before it becomes fairly likely that I have a collision, i.e., that two items

More information

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park

Insert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park 1617 Preview Data Structure Review COSC COSC Data Structure Review Linked Lists Stacks Queues Linked Lists Singly Linked List Doubly Linked List Typical Functions s Hash Functions Collision Resolution

More information

Fundamental Algorithms

Fundamental Algorithms Chapter 5: Hash Tables, Winter 2018/19 1 Fundamental Algorithms Chapter 5: Hash Tables Jan Křetínský Winter 2018/19 Chapter 5: Hash Tables, Winter 2018/19 2 Generalised Search Problem Definition (Search

More information

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Priority Queue / Heap Stores (key,data) pairs (like dictionary) But, different set of operations: Initialize-Heap: creates new empty heap

More information

Hash Tables. Direct-Address Tables Hash Functions Universal Hashing Chaining Open Addressing. CS 5633 Analysis of Algorithms Chapter 11: Slide 1

Hash Tables. Direct-Address Tables Hash Functions Universal Hashing Chaining Open Addressing. CS 5633 Analysis of Algorithms Chapter 11: Slide 1 Hash Tables Direct-Address Tables Hash Functions Universal Hashing Chaining Open Addressing CS 5633 Analysis of Algorithms Chapter 11: Slide 1 Direct-Address Tables 2 2 Let U = {0,...,m 1}, the set of

More information

Data Structures and Algorithm. Xiaoqing Zheng

Data Structures and Algorithm. Xiaoqing Zheng Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn Dictionary problem Dictionary T holding n records: x records key[x] Other fields containing satellite data Operations on T: INSERT(T, x)

More information

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 ) CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26 Homework #2 ( Due: Nov 8 ) Task 1. [ 80 Points ] Average Case Analysis of Median-of-3 Quicksort Consider the median-of-3 quicksort algorithm

More information

1 Maintaining a Dictionary

1 Maintaining a Dictionary 15-451/651: Design & Analysis of Algorithms February 1, 2016 Lecture #7: Hashing last changed: January 29, 2016 Hashing is a great practical tool, with an interesting and subtle theory too. In addition

More information

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2 Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2 Outline Introduction to algorithm analysis: fibonacci seq calculation counting number of computer steps recursive formula

More information

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2 Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2 Outline Introduction to algorithm analysis: fibonacci seq calculation counting number of computer steps recursive formula

More information

Module 1: Analyzing the Efficiency of Algorithms

Module 1: Analyzing the Efficiency of Algorithms Module 1: Analyzing the Efficiency of Algorithms Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu What is an Algorithm?

More information

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is: CS 24 Section #8 Hashing, Skip Lists 3/20/7 Probability Review Expectation (weighted average): the expectation of a random quantity X is: x= x P (X = x) For each value x that X can take on, we look at

More information

COEN 312 DIGITAL SYSTEMS DESIGN - LECTURE NOTES Concordia University

COEN 312 DIGITAL SYSTEMS DESIGN - LECTURE NOTES Concordia University 1 OEN 312 DIGIAL SYSEMS DESIGN - LEURE NOES oncordia University hapter 6: Registers and ounters NOE: For more examples and detailed description of the material in the lecture notes, please refer to the

More information

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan Lecture 2 More Algorithm Analysis, Math and MCSS By: Sarah Buchanan Announcements Assignment #1 is posted online It is directly related to MCSS which we will be talking about today or Monday. There are

More information

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM

Sorting. Chapter 11. CSE 2011 Prof. J. Elder Last Updated: :11 AM Sorting Chapter 11-1 - Sorting Ø We have seen the advantage of sorted data representations for a number of applications q Sparse vectors q Maps q Dictionaries Ø Here we consider the problem of how to efficiently

More information

Problem Set 4 Solutions

Problem Set 4 Solutions Introduction to Algorithms October 8, 2001 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 18

More information

Counters. We ll look at different kinds of counters and discuss how to build them

Counters. We ll look at different kinds of counters and discuss how to build them Counters We ll look at different kinds of counters and discuss how to build them These are not only examples of sequential analysis and design, but also real devices used in larger circuits 1 Introducing

More information

Ch 01. Analysis of Algorithms

Ch 01. Analysis of Algorithms Ch 01. Analysis of Algorithms Input Algorithm Output Acknowledgement: Parts of slides in this presentation come from the materials accompanying the textbook Algorithm Design and Applications, by M. T.

More information

data structures and algorithms lecture 2

data structures and algorithms lecture 2 data structures and algorithms 2018 09 06 lecture 2 recall: insertion sort Algorithm insertionsort(a, n): for j := 2 to n do key := A[j] i := j 1 while i 1 and A[i] > key do A[i + 1] := A[i] i := i 1 A[i

More information

Ch01. Analysis of Algorithms

Ch01. Analysis of Algorithms Ch01. Analysis of Algorithms Input Algorithm Output Acknowledgement: Parts of slides in this presentation come from the materials accompanying the textbook Algorithm Design and Applications, by M. T. Goodrich

More information

Databases. DBMS Architecture: Hashing Techniques (RDBMS) and Inverted Indexes (IR)

Databases. DBMS Architecture: Hashing Techniques (RDBMS) and Inverted Indexes (IR) Databases DBMS Architecture: Hashing Techniques (RDBMS) and Inverted Indexes (IR) References Hashing Techniques: Elmasri, 7th Ed. Chapter 16, section 8. Cormen, 3rd Ed. Chapter 11. Inverted indexing: Elmasri,

More information

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26 Binary Search Introduction Problem Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26 Strategy 1: Random Search Randomly select a page until the page containing

More information

Splay Trees. CMSC 420: Lecture 8

Splay Trees. CMSC 420: Lecture 8 Splay Trees CMSC 420: Lecture 8 AVL Trees Nice Features: - Worst case O(log n) performance guarantee - Fairly simple to implement Problem though: - Have to maintain etra balance factor storage at each

More information

CSCB63 Winter Week 11 Bloom Filters. Anna Bretscher. March 30, / 13

CSCB63 Winter Week 11 Bloom Filters. Anna Bretscher. March 30, / 13 CSCB63 Winter 2019 Week 11 Bloom Filters Anna Bretscher March 30, 2019 1 / 13 Today Bloom Filters Definition Expected Complexity Applications 2 / 13 Bloom Filters (Specification) A bloom filter is a probabilistic

More information

An Optimal Algorithm for l 1 -Heavy Hitters in Insertion Streams and Related Problems

An Optimal Algorithm for l 1 -Heavy Hitters in Insertion Streams and Related Problems An Optimal Algorithm for l 1 -Heavy Hitters in Insertion Streams and Related Problems Arnab Bhattacharyya, Palash Dey, and David P. Woodruff Indian Institute of Science, Bangalore {arnabb,palash}@csa.iisc.ernet.in

More information

Models of Computation, Recall Register Machines. A register machine (sometimes abbreviated to RM) is specified by:

Models of Computation, Recall Register Machines. A register machine (sometimes abbreviated to RM) is specified by: Models of Computation, 2010 1 Definition Recall Register Machines A register machine (sometimes abbreviated M) is specified by: Slide 1 finitely many registers R 0, R 1,..., R n, each capable of storing

More information

CSE 21 Practice Exam for Midterm 2 Fall 2017

CSE 21 Practice Exam for Midterm 2 Fall 2017 CSE 1 Practice Exam for Midterm Fall 017 These practice problems should help prepare you for the second midterm, which is on monday, November 11 This is longer than the actual exam will be, but good practice

More information

A Lecture on Hashing. Aram-Alexandre Pooladian, Alexander Iannantuono March 22, Hashing. Direct Addressing. Operations - Simple

A Lecture on Hashing. Aram-Alexandre Pooladian, Alexander Iannantuono March 22, Hashing. Direct Addressing. Operations - Simple A Lecture on Hashing Aram-Alexandre Pooladian, Alexander Iannantuono March 22, 217 This is the scribing of a lecture given by Luc Devroye on the 17th of March 217 for Honours Algorithms and Data Structures

More information

arxiv: v2 [cs.dc] 28 Apr 2013

arxiv: v2 [cs.dc] 28 Apr 2013 A simpler load-balancing algorithm for range-partitioned data in Peer-to-Peer systems Jakarin Chawachat and Jittat Fakcharoenphol Department of Computer Engineering Kasetsart University Bangkok Thailand

More information

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017 CMSC CMSC : Lecture Greedy Algorithms for Scheduling Tuesday, Sep 9, 0 Reading: Sects.. and. of KT. (Not covered in DPV.) Interval Scheduling: We continue our discussion of greedy algorithms with a number

More information

CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14. For random numbers X which only take on nonnegative integer values, E(X) =

CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14. For random numbers X which only take on nonnegative integer values, E(X) = CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14 1 Probability First, recall a couple useful facts from last time about probability: Linearity of expectation: E(aX + by ) = ae(x)

More information

Hashing, Hash Functions. Lecture 7

Hashing, Hash Functions. Lecture 7 Hashing, Hash Functions Lecture 7 Symbol-table problem Symbol table T holding n records: x record key[x] Other fields containing satellite data Operations on T: INSERT(T, x) DELETE(T, x) SEARCH(T, k) How

More information

CS3719 Theory of Computation and Algorithms

CS3719 Theory of Computation and Algorithms CS3719 Theory of Computation and Algorithms Any mechanically (automatically) discretely computation of problem solving contains at least three components: - problem description - computational tool - analysis

More information

LA lecture 4: linear eq. systems, (inverses,) determinants

LA lecture 4: linear eq. systems, (inverses,) determinants LA lecture 4: linear eq. systems, (inverses,) determinants Yesterday: ˆ Linear equation systems Theory Gaussian elimination To follow today: ˆ Gaussian elimination leftovers ˆ A bit about the inverse:

More information

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2006 University of New Mexico Department of Computer Science Final Examination CS 561 Data Structures and Algorithms Fall, 2006 Name: Email: Print your name and email, neatly in the space provided above; print

More information

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations Abstract Data Type Part III Data Structures An abstract data type (ADT) is defined by an interface of operations or methods that can be performed and that have a defined behavior. The data types in this

More information

Lecture 6 September 21, 2016

Lecture 6 September 21, 2016 ICS 643: Advanced Parallel Algorithms Fall 2016 Lecture 6 September 21, 2016 Prof. Nodari Sitchinava Scribe: Tiffany Eulalio 1 Overview In the last lecture, we wrote a non-recursive summation program and

More information

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions

Assignment 4. CSci 3110: Introduction to Algorithms. Sample Solutions Assignment 4 CSci 3110: Introduction to Algorithms Sample Solutions Question 1 Denote the points by p 1, p 2,..., p n, ordered by increasing x-coordinates. We start with a few observations about the structure

More information

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort

Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort Quicksort (CLRS 7) We previously saw how the divide-and-conquer technique can be used to design sorting algorithm Merge-sort Partition n elements array A into two subarrays of n/2 elements each Sort the

More information

CS6901: review of Theory of Computation and Algorithms

CS6901: review of Theory of Computation and Algorithms CS6901: review of Theory of Computation and Algorithms Any mechanically (automatically) discretely computation of problem solving contains at least three components: - problem description - computational

More information

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review: Mathematical Background Mathematical Background CSE 373 Data Structures Today, we will review: Logs and eponents Series Recursion Motivation for Algorithm Analysis 5 January 007 CSE 373 - Math Background

More information

Lecture 2: Divide and conquer and Dynamic programming

Lecture 2: Divide and conquer and Dynamic programming Chapter 2 Lecture 2: Divide and conquer and Dynamic programming 2.1 Divide and Conquer Idea: - divide the problem into subproblems in linear time - solve subproblems recursively - combine the results in

More information

6.1 Occupancy Problem

6.1 Occupancy Problem 15-859(M): Randomized Algorithms Lecturer: Anupam Gupta Topic: Occupancy Problems and Hashing Date: Sep 9 Scribe: Runting Shi 6.1 Occupancy Problem Bins and Balls Throw n balls into n bins at random. 1.

More information

The Design Procedure. Output Equation Determination - Derive output equations from the state table

The Design Procedure. Output Equation Determination - Derive output equations from the state table The Design Procedure Specification Formulation - Obtain a state diagram or state table State Assignment - Assign binary codes to the states Flip-Flop Input Equation Determination - Select flipflop types

More information

So far we have implemented the search for a key by carefully choosing split-elements.

So far we have implemented the search for a key by carefully choosing split-elements. 7.7 Hashing Dictionary: S. insert(x): Insert an element x. S. delete(x): Delete the element pointed to by x. S. search(k): Return a pointer to an element e with key[e] = k in S if it exists; otherwise

More information

ELCT201: DIGITAL LOGIC DESIGN

ELCT201: DIGITAL LOGIC DESIGN ELCT201: DIGITAL LOGIC DESIGN Dr. Eng. Haitham Omran, haitham.omran@guc.edu.eg Dr. Eng. Wassim Alexan, wassim.joseph@guc.edu.eg Following the slides of Dr. Ahmed H. Madian Lecture 10 محرم 1439 ه Winter

More information

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community

CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Linked Lists 1. You are given the linked list: 1 2 3. You may assume that each node has one field

More information

Hashing. Algorithm : Design & Analysis [09]

Hashing. Algorithm : Design & Analysis [09] Hashig Algorithm : Desig & Aalysis [09] I the last class Implemetig Dictioary ADT Defiitio of red-black tree Black height Isertio ito a red-black tree Deletio from a red-black tree Hashig Hashig Collisio

More information

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17 Fibonacci (Min-)Heap A forest of heap-order trees (parent priority child priority). Roots in circular doubly-linked list. Pointer to minimum-priority root. Siblings in circular doubly-linked list; parent

More information

Analysis of clocked sequential networks

Analysis of clocked sequential networks Analysis of clocked sequential networks keywords: Mealy, Moore Consider : a sequential parity checker an 8th bit is added to each group of 7 bits such that the total # of 1 bits is odd for odd parity if

More information

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing 1 Splay Trees Sleator and Tarjan, Self Adjusting Binary Search Trees JACM 32(3) 1985 The claim planning ahead. But matches previous idea of being lazy, letting potential build up, using it to pay for expensive

More information

CS246: Mining Massive Datasets Jure Leskovec, Stanford University

CS246: Mining Massive Datasets Jure Leskovec, Stanford University CS246: Mining Massive Datasets Jure Leskovec, Stanford University http://cs246.stanford.edu 2/26/2013 Jure Leskovec, Stanford CS246: Mining Massive Datasets, http://cs246.stanford.edu 2 More algorithms

More information

Algorithm Efficiency. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University

Algorithm Efficiency. Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University Algorithm Efficiency Algorithmic Thinking Luay Nakhleh Department of Computer Science Rice University 1 All Correct Algorithms Are Not Created Equal When presented with a set of correct algorithms for

More information

Turing Machine Variants

Turing Machine Variants CS311 Computational Structures Turing Machine Variants Lecture 12 Andrew Black Andrew Tolmach 1 The Church-Turing Thesis The problems that can be decided by an algorithm are exactly those that can be decided

More information

Lecture VI AMORTIZATION

Lecture VI AMORTIZATION 1. Potential Framework Lecture VI Page 1 Lecture VI AMORTIZATION Many algorithms amount to a sequence of operations on a data structure. For instance, the well-known heapsort algorithm is a sequence of

More information

Logic Design II (17.342) Spring Lecture Outline

Logic Design II (17.342) Spring Lecture Outline Logic Design II (17.342) Spring 2012 Lecture Outline Class # 10 April 12, 2012 Dohn Bowden 1 Today s Lecture First half of the class Circuits for Arithmetic Operations Chapter 18 Should finish at least

More information

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz.

Submit Growable Array exercise Answer Q1-3 from today's in-class quiz. Q1-3 Growable Arrays Continued Big-Oh and its cousins Submit Growable Array exercise Answer Q1-3 from today's in-class quiz. } Finish course intro } Growable Array recap } Big-Oh and cousins } After today,

More information

Hash Tables. Given a set of possible keys U, such that U = u and a table of m entries, a Hash function h is a

Hash Tables. Given a set of possible keys U, such that U = u and a table of m entries, a Hash function h is a Hash Tables Given a set of possible keys U, such that U = u and a table of m entries, a Hash function h is a mapping from U to M = {1,..., m}. A collision occurs when two hashed elements have h(x) =h(y).

More information

Hashing. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing. Philip Bille

Hashing. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing. Philip Bille Hashing Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing Philip Bille Hashing Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing

More information

Lecture 2. Frequency problems

Lecture 2. Frequency problems 1 / 43 Lecture 2. Frequency problems Ricard Gavaldà MIRI Seminar on Data Streams, Spring 2015 Contents 2 / 43 1 Frequency problems in data streams 2 Approximating inner product 3 Computing frequency moments

More information

Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, === Homework submission instructions ===

Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, === Homework submission instructions === Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, 2011 === Homework submission instructions === Submit the answers for writing problems (including your programming report) through

More information

Hashing. Hashing. Dictionaries. Dictionaries. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing

Hashing. Hashing. Dictionaries. Dictionaries. Dictionaries Chained Hashing Universal Hashing Static Dictionaries and Perfect Hashing Philip Bille Dictionaries Dictionary problem. Maintain a set S U = {,..., u-} supporting lookup(x): return true if x S and false otherwise. insert(x): set S = S {x} delete(x): set S = S - {x} Dictionaries

More information

Review Of Topics. Review: Induction

Review Of Topics. Review: Induction Review Of Topics Asymptotic notation Solving recurrences Sorting algorithms Insertion sort Merge sort Heap sort Quick sort Counting sort Radix sort Medians/order statistics Randomized algorithm Worst-case

More information

7 Dictionary. Ernst Mayr, Harald Räcke 126

7 Dictionary. Ernst Mayr, Harald Räcke 126 7 Dictionary Dictionary: S. insert(x): Insert an element x. S. delete(x): Delete the element pointed to by x. S. search(k): Return a pointer to an element e with key[e] = k in S if it exists; otherwise

More information

Searching. Constant time access. Hash function. Use an array? Better hash function? Hash function 4/18/2013. Chapter 9

Searching. Constant time access. Hash function. Use an array? Better hash function? Hash function 4/18/2013. Chapter 9 Constant time access Searching Chapter 9 Linear search Θ(n) OK Binary search Θ(log n) Better Can we achieve Θ(1) search time? CPTR 318 1 2 Use an array? Use random access on a key such as a string? Hash

More information

Splay Trees. Splay Trees. Splay Trees. Splay Trees

Splay Trees. Splay Trees. Splay Trees. Splay Trees Slay Trees Slay Trees isadvantae of balanced search trees: worst case; no advantae for easy inuts additional memory required comlicated imlementation Slay Trees: + after access, an element is moved to

More information

Lecture 4. Quicksort

Lecture 4. Quicksort Lecture 4. Quicksort T. H. Cormen, C. E. Leiserson and R. L. Rivest Introduction to Algorithms, 3rd Edition, MIT Press, 2009 Sungkyunkwan University Hyunseung Choo choo@skku.edu Copyright 2000-2018 Networking

More information

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3

Algorithms. Algorithms 2.4 PRIORITY QUEUES. Pro tip: Sit somewhere where you can work in a group of 2 or 3 Algorithms ROBRT SDGWICK KVIN WAYN 2.4 PRIORITY QUUS Algorithms F O U R T H D I T I O N Fundamentals and flipped lectures Priority queues and heaps Heapsort Deeper thinking ROBRT SDGWICK KVIN WAYN http://algs4.cs.princeton.edu

More information

King Fahd University of Petroleum and Minerals College of Computer Science and Engineering Computer Engineering Department

King Fahd University of Petroleum and Minerals College of Computer Science and Engineering Computer Engineering Department King Fahd University of Petroleum and Minerals College of Computer Science and Engineering Computer Engineering Department Page 1 of 13 COE 202: Digital Logic Design (3-0-3) Term 112 (Spring 2012) Final

More information

Algorithm Design and Analysis

Algorithm Design and Analysis Algorithm Design and Analysis LECTURE 7 Greedy Graph Algorithms Shortest paths Minimum Spanning Tree Sofya Raskhodnikova 9/14/016 S. Raskhodnikova; based on slides by E. Demaine, C. Leiserson, A. Smith,

More information

CSE 190, Great ideas in algorithms: Pairwise independent hash functions

CSE 190, Great ideas in algorithms: Pairwise independent hash functions CSE 190, Great ideas in algorithms: Pairwise independent hash functions 1 Hash functions The goal of hash functions is to map elements from a large domain to a small one. Typically, to obtain the required

More information

Amortized Complexity Verified

Amortized Complexity Verified Amortized Complexity Verified Tobias Nipkow Technische Universität München Abstract A framework for the analysis of the amortized complexity of (functional) data structures is formalized in Isabelle/HOL

More information

4/26/2017. More algorithms for streams: Each element of data stream is a tuple Given a list of keys S Determine which tuples of stream are in S

4/26/2017. More algorithms for streams: Each element of data stream is a tuple Given a list of keys S Determine which tuples of stream are in S Note to other teachers and users of these slides: We would be delighted if you found this our material useful in giving your own lectures. Feel free to use these slides verbatim, or to modify them to fit

More information

Design of Sequential Circuits

Design of Sequential Circuits Design of Sequential Circuits Seven Steps: Construct a state diagram (showing contents of flip flop and inputs with next state) Assign letter variables to each flip flop and each input and output variable

More information

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n

i=1 i B[i] B[i] + A[i, j]; c n for j n downto i + 1 do c n i=1 (n i) C[i] C[i] + A[i, j]; c n Fundamental Algorithms Homework #1 Set on June 25, 2009 Due on July 2, 2009 Problem 1. [15 pts] Analyze the worst-case time complexity of the following algorithms,and give tight bounds using the Theta

More information

Hash Table Analysis. e(k) = bp(k). kp(k) = n b.

Hash Table Analysis. e(k) = bp(k). kp(k) = n b. Septemer 1, 2016 Hash Tale Analysis Assume that we have a hash tale structured as a vector of lists, resolving collisions y sequential search of uckets, as in the generic hash tale template HashTale

More information

Searching, mainly via Hash tables

Searching, mainly via Hash tables Data structures and algorithms Part 11 Searching, mainly via Hash tables Petr Felkel 26.1.2007 Topics Searching Hashing Hash function Resolving collisions Hashing with chaining Open addressing Linear Probing

More information

Chapter 6. Self-Adjusting Data Structures

Chapter 6. Self-Adjusting Data Structures Chapter 6 Self-Adjusting Data Structures Chapter 5 describes a data structure that is able to achieve an epected quer time that is proportional to the entrop of the quer distribution. The most interesting

More information

CS1210 Lecture 23 March 8, 2019

CS1210 Lecture 23 March 8, 2019 CS1210 Lecture 23 March 8, 2019 HW5 due today In-discussion exams next week Optional homework assignment next week can be used to replace a score from among HW 1 3. Will be posted some time before Monday

More information