Counting in Binary Amortized and Worst-Case Efficiency © Robert E. Tarjan, 2013.

Slides:



Advertisements
Similar presentations
Intro to CS – Honors I Representing Numbers GEORGIOS PORTOKALIDIS
Advertisements

©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Rank-Pairing Heaps Robert Tarjan, Princeton University & HP Labs Joint work with Bernhard Haeupler and Siddhartha Sen, ESA
Addition and Subtraction. Outline Arithmetic Operations (Section 1.2) – Addition – Subtraction – Multiplication Complements (Section 1.5) – 1’s complement.
CPSC 411, Fall 2008: Set 6 1 CPSC 411 Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Jennifer Welch Fall 2008.
King Fahd University of Petroleum and Minerals
Fixed-Point Arithmetics: Part I
Kavita Hatwal Fall The decimal system, also called the base 10 number system is based on ten numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. All the numbers.
Rank-Pairing Heaps Bernhard Haeupler, Siddhartha Sen, and Robert Tarjan, ESA
James Tam Non decimal math: doing math with non-base 10 number systems Addition, subtraction and multiplication with binary, octal and hexadecimal.
Andreas Klappenecker [based on the slides of Prof. Welch]
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
Binary Addition Addition Rules: = = = = = carry 1 1 carry 1 Example 1: Example 2:
Data Representation Number Systems.
Arithmetic for Computers
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
February 24, 2015Applied Discrete Mathematics Week 4: Number Theory 1 Modular Arithmetic Let a be an integer and m be a positive integer. We denote by.
Copyright © Cengage Learning. All rights reserved. CHAPTER 2 THE LOGIC OF COMPOUND STATEMENTS THE LOGIC OF COMPOUND STATEMENTS.
General Fixed Radix Number Systems Nonredundant Positive radix, ß n digits in digit set Vector:
CS 473Lecture 121 CS473-Algorithms I Lecture 12 Amortized Analysis.
1 Problem Solving using computers Data.. Representation & storage Representation of Numeric data The Binary System.
46 Number Systems Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs Display: Seven segment displays Inputs: Switches.
Integer Representation for People Computer Organization and Assembly Language: Module 3.
New Mexico Computer Science for All Author: Ed Angel Title: Bits and Bytes.
Chapter 2: The Logic of Compound Statements 2.5 Application: Number Systems and Circuits for Addition 1 Counting in binary is just like counting in decimal.
Positional Number Systems
Integer Representations and Counting in the Bit Probe Model M. Zaiur Rahman and J. Ian Munro Cheriton School of Computer Science University of Waterloo.
Operations on Bits Arithmetic Operations Logic Operations
Fast Exponentiation (3/31) What is the most efficient way to compute (mod 32591)? We will need an efficient algorithm in order to do “RSA cryptography”,
1 Chapter 17: Amortized Analysis I. 2 About this lecture Given a data structure, amortized analysis studies in a sequence of operations, the average time.
Computer Architecture Lecture 32 Fasih ur Rehman.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
Data Representation in Computer Systems. 2 Signed Integer Representation The conversions we have so far presented have involved only positive numbers.
CSCE 411H Design and Analysis of Algorithms Set 6: Amortized Analysis Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 6 1 * Slides adapted.
1 Fat heaps (K & Tarjan 96). 2 Goal Want to achieve the performance of Fibonnaci heaps but on the worst case. Why ? Theoretical curiosity and some applications.
Balanced Binary Search Trees
EEE342 Digital Electronics Ian McCrumRoom 5B18, Lecture 2: Codes & Arithmetic.
Introduction to Algorithms Amortized Analysis My T. UF.
ECE 101 An Introduction to Information Technology Information Coding.
1 CS 151 : Digital Design Chapter 4: Arithmetic Functions and Circuits 4-3 : Binary Subtraction.
Chapter 1 Representing Data in a Computer. 1.1 Binary and Hexadecimal Numbers.
Integer Operations Computer Organization and Assembly Language: Module 5.
Amortized Analysis In amortized analysis, the time required to perform a sequence of operations is averaged over all the operations performed Aggregate.
PHY 201 (Blum)1 Shift registers and Floating Point Numbers Chapter 11 in Tokheim.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Lecture 4: Digital Systems & Binary Numbers (4)
Introduction to Algorithms: Amortized Analysis. Introduction to Algorithms Amortized Analysis Dynamic tables Aggregate method Accounting method Potential.
Floating Point Representations
Data Representation COE 308 Computer Architecture
Data Representation ICS 233
Andreas Klappenecker [partially based on the slides of Prof. Welch]
Data Representation.
Negative Numbers and Subtraction
COMPUTING FUNDAMENTALS
Dr. Clincy Professor of CS
Dr. Clincy Professor of CS
Data Representation COE 301 Computer Organization
King Fahd University of Petroleum and Minerals
King Fahd University of Petroleum and Minerals
Unconventional Fixed-Radix Number Systems
UNIVERSITY OF MASSACHUSETTS Dept
EEL 3705 / 3705L Digital Logic Design
Overview Part 1 – Design Procedure Part 2 – Combinational Logic
Dr. Clincy Professor of CS
CPS120: Introduction to Computer Science
Data Representation ICS 233
Copyright © Cengage Learning. All rights reserved.
Data Representation COE 308 Computer Architecture
Today Binary addition Representing negative numbers 2.
Presentation transcript:

Counting in Binary Amortized and Worst-Case Efficiency © Robert E. Tarjan, 2013

Number Binary Total cost Cost to add

Total cost to count to n Cost to add 1 = number of trailing 1’s + 1 = decrease in number of 1’s + 2 Worst-case cost to add 1: lg(n + 1) + 1 Total cost to count to n: nlgn ?

Amortize: to liquidate a debt by installment payments. From Medieval Latin: to reduce to the point of death. In analysis of algorithms: to pay for the total cost of a sequence of operations by charging each operation an equal (or appropriate) amount. (A little stretch, but there you have it.)

Amortized Efficiency Coin-operated computer bit flip costs $1 $2 per addition suffices: unspent $ = #1’s ≥ 0 total cost = 2n - #1’s ≤ 2n

Amortization (banker) Assign $ to each operation (amortized cost) Keep track of savings (or borrowings) in state of data structure If no debt after all operations, total cost ≤ sum of amortized costs

Amortization (physicist) Switch perspective: start with savings, derive cost per operation Assign “potential” Φ to each state of data structure Define “amortized cost” of an operation to be actual cost plus net change in potential: a i = t i + Φ i – Φ i – 1 Thus t i = a i + Φ i – 1 – Φ i

total actual cost = total amortized cost + initial Φ – final Φ ≤ total amortized cost if initial Φ = 0, final Φ ≥ 0 (no net borrowing) Binary counting: Φ = number of 1’s Φ 0 = 0, Φ n ≥ 0 Amortized cost to add one = 2 → total actual cost ≤ 2n

Frequency of multiple carries Observation: a cost of k + 1 or more occurs at most n/2 k times (out of n) Proof of observation via a potential function: Fix k. Let Φ = n mod 2 k. Each add increases Φ by one, unless cost is k + 1 or more. (We call the add expensive.) In this case n mod 2 k = 2 k – 1, so Φ decreases by 2 k – 1. This can happen at most n/2 k times out of n: Φ = n - e2 k ≥ 0, where e = #expensive adds.

Intermixed additions and subtractions Start at zero, intermix arbitrary additions and subtractions of 1. How many carries and borrows? Bad example: : alternate +1, –1 If k bits, k carries or borrows each operation. To avoid: more flexibility in the number representation!

Redundant Binary Numbers Allow 2 as a digit as well as 0, 1 Number representations are no longer unique: 210 = 1010 = 1002 How does this help? Carries convert 2’s to 1’s (instead of 1’s to 0’s). Borrows convert 0’s to 1’s.

Φ = #2’s + #0’s Amortized cost (#digit changes) of +1 or –1 is ≤2. [Exponential potential to amortize ruler function]

Carry-free binary addition and borrow-free subtraction: from amortized to worst-case

Need to eliminate adjacent 2’s Regularity: At least one 0 between each pair of 2’s (adjacent or not) Regular: , 211 Not regular:

Fix for addition: 02 → → 20 To add one: Fix rightmost 2. Add 1 to rightmost digit = = = = Correct, maintains regularity, and carry-free: changes at most three digits

Implementation Stack of positions of 2’s, rightmost on top: , 10, 4 (top) Can update stack in O(1) time

What about borrow-free subtraction? Need to avoid adjacent 0’s, which force borrowing Strict regularity: 0’s and 2’s alternate, ignoring 1’s Problem: fix for addition can violate strict regularity Strictly regular addition: If rightmost non-1 is a 2, fix it. Add 1 to rightmost digit = (no fix)

Fix for subtraction: 10 → → 12 (look familiar?) To subtract one: If rightmost non-1 is a 0, fix it. Subtract 1 from rightmost digit – 1 = (no fix) – 1 = – 1 =

Implementation: Stack of non-1’s, rightmost on top , 11, 7, 6, 3, 1 (top) Addition and subtraction are correct, maintain strict regularity, and are carry-free and borrow-free, respectively.

Proof: A fix does not change the value of the number. A fix preserves regularity: …21*021* ↔ …21*101* …01*121* ↔ …01*201* (1* = zero or more 1’s.) After an add-fix, the rightmost digit is not a 2, so no carry. After a subtract-fix, the rightmost digit is not a 0, so no borrow. Adding or subtracting 1 changes at most three digits.

Extensions Addition or subtraction of two numbers in worst- case time proportional to the length of the smaller one Addition or subtraction of an arbitrary power of 2 (a 1 in position k) Use of any three consecutive digits, e. g. {–1, 0, 1}, {2,3,4}

Q: If strict regularity works, why bother with regularity? A1: If no subtractions, add is simpler and extra stack is smaller. A2: Design space is worth exploring. Options may be incomparable.