1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Algebra Problems… Solutions Algebra Problems… Solutions © 2007 Herbert I. Gross Set 7 part 2 By Herb I. Gross and Richard A. Medeiros next.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
Program Proving Notes Ellen L. Walker.
 Instructor: Rob Nash  Readings: Berger, Chapter 1; Rosen, Chapter 3.
INFINITE SEQUENCES AND SERIES
1 CS100J 27 March 2007 Algorithms on arrays Reading: 8.3–8.5 Please punctuate this: Dear John, I want a man who knows what love is all about you are generous.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
© Love Ekenberg The Algorithm Concept, Big O Notation, and Program Verification Love Ekenberg.
CS 61C L02 Number Representation (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
INFINITE SEQUENCES AND SERIES
1 Recitation 10. Induction Introduction. Induction is useful in proving properties of recursive algorithms, like their execution times. It is also the.
1 Assignment 6. Developing Loops Due on Tuesday, 30 October, by midnight (submitted electronically). Note that each question will be graded on the following.
1 Sequences and Mathematical Induction An important task of mathematics is to discover and characterize regular patterns, such as those associated with.
1 Assignment 8. Implementing type integer Due on Tuesday, 4 December, by midnight (submitted electronically). Download class List and a skeleton of class.
Data Representation Number Systems.
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.
CS105 INTRODUCTION TO COMPUTER CONCEPTS BINARY VALUES & NUMBER SYSTEMS Instructor: Cuong (Charlie) Pham.
MATH 224 – Discrete Mathematics
Numbering Systems CS208.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
Reading and Writing Mathematical Proofs
Computer Science 111 Fundamentals of Programming I Number Systems.
The importance of sequences and infinite series in calculus stems from Newton’s idea of representing functions as sums of infinite series.  For instance,
1 Problem Solving using computers Data.. Representation & storage Representation of Numeric data The Binary System.
1 Inference Rules and Proofs (Z); Program Specification and Verification Inference Rules and Proofs (Z); Program Specification and Verification.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Round-off Errors.
Chapter 2 Binary Values and Number Systems. 2 2 Natural Numbers Zero and any number obtained by repeatedly adding one to it. Examples: 100, 0, 45645,
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
The Integers. The Division Algorithms A high-school question: Compute 58/17. We can write 58 as 58 = 3 (17) + 7 This forms illustrates the answer: “3.
Cosc 2150: Computer Organization Chapter 2 Part 1 Integers addition and subtraction.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Sequences and Summations
CMSC 104, Lecture 051 Binary / Hex Binary and Hex The number systems of Computer Science.
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Mar 3, 2006.
In section 11.9, we were able to find power series representations for a certain restricted class of functions. Here, we investigate more general problems.
Sequences and Summations Section 2.4. Section Summary Sequences. – Examples: Geometric Progression, Arithmetic Progression Recurrence Relations – Example:
Application: Correctness of Algorithms Lecture 22 Section 4.5 Fri, Feb 18, 2005.
College Algebra Sixth Edition James Stewart Lothar Redlin Saleem Watson.
Chinese Remainder Theorem Dec 29 Picture from ………………………
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Module #9 – Number Theory 1/5/ Algorithms, The Integers and Matrices.
Introduction To Number Systems Binary System M. AL-Towaileb1.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
November 12, 2009Theory of Computation Lecture 17: Calculations on Strings II 1 Numerical Representation of Strings First, we define two primitive recursive.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 1 Integers Department of Computer Science and Information Engineering,
Cosc 2150: Computer Organization
Correctness issues and Loop invariants
CS100J 30 October 2007 Algorithms on arrays Reading: 8.3–8.5
Arithmetic Operator Operation Example + addition x + y
Copyright © Cengage Learning. All rights reserved.
CS October 2008 The while loop and assertions
Copyright © Cengage Learning. All rights reserved.
Digital Logic & Design Lecture 02.
Asymptotic complexity Searching/sorting
Output Variables {true} S {i = j} i := j; or j := i;
Binary Search and Loop invariants
Asymptotic complexity
Copyright © Cengage Learning. All rights reserved.
(We’ll spend a few lectures on iteration)
Presentation transcript:

1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor will develop the loops as presented in this handout. During the recitation, it’s best that you concentrate on what the instructor is saying and participate in the loop developments without simply reading from this document. This document is meant for you to study at a later time. Remember this about ranges! The number of values in a segment b[h..k-1] is k-h: So, if you want the segment to have 0 values, set k to h (or h to k). You can discover the number of values in other segments, e.g. b[h..k], based on the above fact. Memorize this checklist! To prove // {Q} init; // {invariant: P; bound function: t} while (B) S // {R} do the following. 1. Prove that P is true initially: {Q} init {P} 2. Upon termination R holds: P and !B imply R 3. Each iteration decreases t. 4. t is really a bound function: P and B imply t > P is invariant: {P and B} S {P} In the examples below, we use the checklist to develop loops. Four loops will be developed for the same problem, using different invariants, so that you begin to see how invariants are used. Problem 1. Write a loop (with initialization) to set all elements of array segment b[0..n-1] to 0. Precondition Q: n >= 0 Postcondition R: b[0..n-1] is all 0 Invariant P: b[0..k-1] is all 0, and 0 <= k <= n, i.e. Bound function t: n-k Initialization: To make invariant P true, use k= 0;. (Why does this work?) Condition B: The loop can stop only when the ? segment of the invariant is empty, which happens when k=n. Therefore, the loop condition is k != n. Another way to see this is to look at P and see what extra information is needed to imply R. This extra information is k = n, so complement to get B. Progress toward termination: To reduce t, do k= k+1; Keep invariant true: Before the increment of k, introduce the statement b[k]= 0; The loop is: k= 0; while (k != n) {b[k]= 0; k= k+1;} Problem 2. Write a loop (with initialization) to set the elements of array segement b[0..n-1] to 0. Precondition Q: n >= 0 Postcondition R: b[0..n-1] is all 0 Invariant P: b[0..k] is all 0, and -1 <= k < n, i.e. Bound function t: n-1-k The only difference between this and the previous problem is that k is used differently. Compare the two invariants (in their picture form)! Initialization: To make invariant P true, use k= -1; Condition B: The loop can stop only when the ? segment of the invariant is empty, which happens when k=n-1. Therefore, the loop condition is k != n- 1. Another way to see this is to look at P and see what extra information is needed to imply R. This extra information is k = n-1, so complement to get B. Progress toward termination: To reduce t, do k= k+1; Keep invariant true: Before the increment of k, introduce the statement b[k+1]= 0;. However, it is better to exchange the two statements in the body and use The loop is: k= -1; while (k != n-1) {k= k+1; b[k]= 0;} b all 0’s ? 0 k n b all 0’s ? 0 k n b this segment has k-h values h k

2 Problem 3. Write a loop (with initialization) to set the elements of array segment b[0..n-1] to 0. Precondition Q: n >= 0 Postcondition R: b[0..n-1] is all 0 Invariant P: b[k..n-1] is all 0, and 0 <= k <= n, i.e. Bound function t: k The only difference between this and the previous problem is that k is used differently. Compare the two invariants! Here, the invariant was obtained from postcondition R by replacing expression 0 by k. Initialization: To make invariant P true, use k= n; Condition B: The loop can stop only when the ? segment of the invariant is empty, which happens when k=0. Therefore, the loop condition is k != 0. Progress toward termination: To reduce t, do k= k-1; Keep invariant true: Before the decrement of k, introduce the statement b[k-1]= 0;. However, it is better to exchange the two statements in the body and use The loop is: k= n; while (k != 0) {k= k-1; b[k]= 0;} Problem 4. Write a loop (with initialization) to set array b[0..n-1] Precondition Q: n >= 0 Postcondition R: b[0..n-1] is all 0 Invariant P: b[k-1..n-1] is all 0, and -1 <= k < n, i.e. Bound function t: k+1 The only difference between this and the previous problem is that k is used differently. Initialization: To make invariant P true, use k= n-1; Condition B: The loop can stop only when the ? segment of the invariant is empty, which happens when k=-1. Therefore, the loop condition is k != -1. Another way to see this is to look at P and see what extra information is needed to imply R. This extra information is k = -1, so complement to get B. b ? all 0’s 0 k n b ? all 0’s 0 k n Progress toward termination: To reduce t, do k= k-1; Keep invariant true: Before the decrement of k, introduce the statement b[k]= 0;. The loop is: k= n-1; while (k != -1) {b[k]= 0; k= k-1;} Summary. We developed four loops (with initialization) for the same problem. The solutions differed only in the meaning of variable k: Problem 1. b[0..k-1] is all zeros Problem 2. b[0..k] is all zeros Problem 3. b[k..n-1] is all zeros Problem 4. b[k-1..n-1] is all zeros This exercise should alert you to the need to define variables precisely and to refer to those definitions repeatedly when reading or developing a program. Problem 5. Produce a String that contains the decimal representation of nonnegative int n. Consider an integer in decimal notation, like This number has the value 5* * * *10 0. A nonnegative integer n is written in decimal as a sequence of digits d k d k-1 …d 1 d 0 for some k, where: (1) Each d i satisfies 0 <= d i < 10 (2) d k, the most significant digit, is > 0. (3) n = d k *10 k + …+ d 1 * d 0 *10 0 Note that with this definition, the deimal representation for 0 is the empty String, “”. How do we calculate d k …d 1 d 0 as a sequence of characters? Consider n = We have: d 0 = 5647 % 10 = 7. so we can get the last digit easily. Suppose we compute m = 5647 / 10 = 564. Now we can repeat the process using m, picking off first the 4, then the 6, then the 5. The above expresses the idea. Lets now formalize. Precondition: n >= 0, and in decimal, n is d k d k-1 …d 1 d 0 for some k Postcondition: s = “d k d k-1 …d 1 d 0 ” is the decimal representation of n.

3 Invariant P: n = m*10 k + (integer represented by s) (for some k) Here’s another view of the invariant, which may be easier for you to understand. Suppose n, in decimal, is a series of digits, n = d d d d d d d d d d d. Here’s the invariant: Invariant P: n = d d... d d … d d Note that k is not needed in this little algorithm. It’s a “thought” variable --in our heads but not in the program. The “for some k” alerts us to this fact. Bound function: m (it will decrease with each iteration). 1. Initialization. We make the invariant true by setting s to “” and m to n. 2. Loop Condition. From the invariant, we see that when m = 0, n is the integer represented by s. that’s the termination condition. So, the loop condition is m != Progress and maintaining the invariant. Progress is made by reducing m, but the invariant has to be maintained using the idea presented earlier. We calculate with m*10 k m*10 k (for some k) = ((m/10)*10 + m%10)*10 k = (m/10)* 10 k+1 + (m%10)*10 k Therefore we can manipulate P as follows: n = m*10 k + (integer represented by s) = n = (m/10)* 10 k+1 + (m%10)*10 k + (integer represented by s) Thus, the digit m%10 has to be prepended to s and m has to be set to m/10. Here’s the loop (with initialization): int m= n; String s= “”; // inv: n = m*10**k + (int represented by s) // (for some k) while (m != 0) { // Prepend next digit to s int d= m%10; // Note: 0 <= d <= 9 s= (char)(d + ‘0’) + s; m= m/10; } Problem 6. Find the quotient and remainder when x is divided by y (for y > 0 and x >= 0). We can’t use division or multiplication. Discussion. Here is the formula: x = q*y + r where 0 <= r < yWe call q the quotient and y the remainder. For example, we know that dividing 14 by 4 yields 3 with a remainder of 2: 14 = 3*4 + 2, i.e. 14 = q*4 + r, where q = 3 and r = 2, 0 <= r < 4. Precondition Q: y > 0 and x >= 0 Store in q and r to truthify R: Postcondition R: x = q*y + r and 0 <= r < y Invariant P: We can truthify x = q*y + r by setting q to 0 and r to x. This also truthifies 0 <= y. But it does not necessarily truthify r < y. So let’s take as P the following: P: x = q*y + r and 0 <= r Thus, the only part of the postcondition that is not always true is r < y, so we take its negation as the condition. Thus far we have: q= 0; r= x; while (y <= r) { …} To make progress, we have to reduce r. Subtracting 1 from it is a possibility, but it wuld be hard to maintain P. Instead, let’s investigate P and loop condition y <= r: x = q*y + r and 0 <= r and y <= r = x = (q+1)*y + r-y and 0 <= r and 0 <= r-y So adding 1 to q and subtracting y from r keeps the invariant true. The loop is then: q= 0; r= x; // inv P: x = q*y + r and 0 <= r while (y <= r) { q= q+1; r= r-y; } this integer is in mthese digits are in s