P1 Engineering Computation

Size: px
Start display at page:

Download "P1 Engineering Computation"

Transcription

1 1EC / 1 P1 Engineering Computation David Murray david.murray@eng.ox.ac.uk dwm/courses/1ec Hilary 2001

2 1EC / 1 Algorithms: Design, Constructs and Correctness

3 1EC / 1 Algorithm design A computation is an operation or set of operations that takes a set of input symbols I and from them produces some desired set of output symbols O. O G(I) For any high level computation to run on a machine, it must be broken down into a sequence of elementary computations which are accessible to the machine. You might write that G = E n...e 2 E 1 This breaking down is done in a number of stages. Fortunately the final stages are performed automatically by ever cleverer compilers, and often we need only be responsible for the design down to the level of a high-level programming language.

4 1EC / 1 Step 1: Specification of Inputs and Outputs Often the first step in the design of a computation G is to define it implicitly by defining its inputs I and outputs O For example At a high level: Input an ordered list of employees, their hourly pay rates, and their number of hours worked in a month; output a similarly ordered list of monthly pay. Or at a low level: Input a 32-bit integer; output its rightshift. You ll see that these statements say nothing explicitly about the nitty-gritty of how to achieve the transformation.

5 1EC / 1 Step 2: Algorithm Design The second (broad) stage is to design an Algorithm. An algorithm is simply a recipe which conforms to a particular computing genre. At the initial substages, the algorithm may include phrases in English and expressions in mathematics. At a later stage, those phrases might become comments, and be replaced by statements which embody the constructs common to all programming languages but specific to none. 1: Determine S the sum of all n of the θ i s 1: //Determine S the sum of all n of the θ i s 2: S n i=1 θ i 1: // Determine S the sum of all n of the theta i s 2: S = 0 3: for i = 1 to n step 1 do Notice that already in the example we have assumed that algorithm is following the imperative or procedural genre. In this the designer assumes control of what the machine does next, albeit in the light of data. There is a very sharp separation between control and data here, whereas in other genres the distinction is blurred.

6 1EC / 1 Step 3: Program D,B and T Only now does one enter the stage of program design. With good algorithm design, the transfer to code should be smooth, although it may be that the different data and control structures a particular level language affords make the transition more or less easy. At a particular level and usually this means the high level most programming languages share a set of common control constructs. More often the choice of language is made by looking at the way that data can be represented, and the way that data structures can be manipulated. Specify Inputs and Outputs (Natural Lang) Feedback Program Test against spec ( Algorithm Design Blackboard Lang) Program Build Program Design (Programming Lang)

7 1EC / 1 Control constructs If we think of how a imperative language runs on a machine, two constructs which are required are assignments to move data from one place to another conditional jumps so that the order of execution can be changed depending on the result of a computation. To these we add for convenience loops run the same code many times; subroutines run the same code from many places; We now spend more time looking at the four main constructs, and clarify the stylized blackboard language which we have used throughout these lectures. Loop Assignments Conditionals Subroutine

8 1EC / 1 Construct 1: Assignments We noted earlier most of the programming languages in common use are imperative languages. In these we specify what happens in terms of a sequence of changing values of program variables. An assignment consists of a variable name, followed by an equals or sign, followed by an expression. The expression is evaluated and the value given to the variable name. For example x k 355/a Assignments do not alter the flow of the program. Hence, thusfar we can only execute a sequence of statements in the order given, with no possibility of variation from that sequence.

9 1EC / 1 Construct 2: Conditionals One way of achieving variation is by testing a condition, and branching according to whether the condition is met or not. The conditional construct can take a number of forms depending on whether or not further tests are required. The simplest form is: IF if condition then statement; statement;... end if but when a block of statements is executed conditionally on the condition not being satisfied: IF with ELSE: if condition then statement; statement;... else statement; statement;... end if With further conditions, we reach the most general form: IF in most general form: if condition1 then statement1a; statement1b;... else if condition2 then statement2a; statement2b;... else if condition3 then statement3a; statement3b;... else statement4a; statement4b;... end if

10 1EC / 1 Construct 3: Loops The third key construct is the loop. Loops allow the same code to be re-run repeatedly, usually with different data, until some termination condition is satisfied. Because they are so useful, there are several flavours of loops that we will use, of which the most fundamental is the while loop. WHILE while condition do statement1; statement2;... end while Clearly, if the loop is to be entered at all, the condition must be satisfied initially. Once entered, if the loop is to terminate, one of the statements must affect the tested condition.

11 1EC / 1 Construct 3: Loops A very common condition is based on a loop counter, which is initialized and incremented, as in the example below, which computes the factorial of a variable n as f(n) = n WHILE i=0; fn=1 while i < n do i=i+1 fn = fn*i end while i=1 i Although other forms of loops are stricly unnecessary, they are useful. For example, the for loop: FOR fn=1 for i=0 to n step 1 do fn = fn*i end for

12 1EC / 1 Construct 4: Subroutines and Functions A subroutine allows a fragment of code to be run from different calling points with the flow returning to the calling point so same code from many places. Even if a piece of code is not called more than once, it might be highly desirable to write it as a subroutine in order that so that steps in an algorithm exhibit a similar level of detail. Some subroutines might be referred to a functions, because they have a single output value determined by a return statement. Others output multiple values placing them in a list of formal parameters. The inputs are always supplied in this list of parameters. As examples consider the use of a return statement: abs (x) if x < 0.0 then return x else return x end if abs (x; r) if x < 0.0 then r = x else r = x end if The general form can use a parameter list and a return: Name(input1, input2,...; output1, output2)

13 1EC / 1 Recursion in Subroutines Subroutines can call themselves, a technique called recursion. factorial(n) if n 1 then return 1 else return n factorial(n 1) end if The recursive code for the factorial will be to most far clearer than the loop code. The construct is powerful because it allows you to solve just one step of the problem. However some regard recursion as undesirable not least because in practice the way it uses the computer s stack memory can lead to unexpected failure at run time. Local storage Local storage Local storage Local storage Local storage Local storage Local storage Local storage Local storage Stack Memory Fills up

14 1EC / 1 Program Correctness Even in the shortest program it is easy to make a error. Not a syntactical error these are picked up by the compiler but a fundamental error, so that the code fails to compute what it is supposed to. For example, we might try to compute the factorial function the other way round, counting down from n to 1: This looks entirely plausible, but it isn t too difficult to values of n for which this fails. But checking an arbitrarily complicated program using specific values is really not feasible. // BAD!! fact = 1; i = n; while i > 1 do i = i - 1; fact = fact * i ; end while We now explore some techniques for performing such checking. Although the techniques are quite powerful, the examples will be rather simple so simple that some might be tempted to think what a waste of effort and why bother? You shouldn t.

15 1EC / 1 Assertions and Pre- and Post-conditions Before we can decide whether a program will do what we want it to do, we need a way of specifying what it should do. At every level, specification takes the form of some assertion about the values of the program variables at key points in the program s execution. Eg, we would like to be able to assert that after the code for factorial is run that f equals n!. English assertions are not generally precise enough, and one might wish to use a formal specification language (which we won t discuss here) or use mathematics. In computer programs, the standard way of asserting something about a program is the use of a program comment. Note that // We require n 0 f =factorial(n) // At this point f = n! the 1st comment warns us that the following makes sense only if n 0 it supplies a set of requirements or PRE-conditions. the 2nd comment asserts something about our expectation after the one line has executed. This supplies a set of POST-conditions.

16 1EC / 1 Backward Tracing from Post-conditions So far, assertions merely tell us the expectations of the programmer. What we wish to do now is to prove that the piece of code between a pre-condition and post-condition is valid. Rather than working forwards, work backward from post- to pre-conditions. There is a simple rule for generating what needs to be true before an assignment, given something which has to be true after the assignment. Namely, if we have an assignment and required post-condition as in x Expression // Post: P(x) is true here where P(x) is a logical expression (or predicate) depending on x then the necessary pre-condition is given by this rule: THE BACKWARD TRACING RULE // Pre: P(Expression) is true here x Expression // Post: P(x) is true here

17 1EC / 1 Example 1: Swapping Find statements S that satisfy the assertions // Pre: x = A AND y = B (So A,B are starting values of x,y) S 1 ; S 2 ;... // Post: x = B AND y = A The following is bad!! // Pre: x = A AND y = B x = y y = x // Post: x = B AND y = A Proof of badness: Working backwards inserting assertions A1 and A2 // Pre: x = A AND y = B // A2: y = B AND y = A x = y // A1: x = B AND x = A y = x // Post: x = B AND y = A

18 1EC / 1 Example 1: Swapping /ctd Of course, the trick to swapping is to introduce an extra variable. You are asked in 1P1K to prove that the following satisfies the assertions. // Good swapper // Pre: x = A AND y = B temp = x x = y y = temp // Post: x = B AND y = A

19 1EC / 1 Example 2: Fibonacci Series Step 1 Generate and verify a program to compute the F n from the Fibonacci series, defined by F 0 = F 1 = 1, F n = F n 2 +F n 1 for n 2 The first few terms are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,... We could proceed as follows: Noting that the definition of F n is split into two different cases, we might use a conditional statement to split our code // Pre: n is non-negative if n = 0 OR n = 1 then // Pre: n is 0 or 1 fn = 1 // Post: fn = F n else // Pre: n 2 C Harder!! // Post: fn = F n end if

20 1EC / 1 Fibonacci Step 2 The harder case in the definition of F n is defined by a recurrence relation F n = F n 2 + F n 1 and this suggests that a loop might be a suitable way of constructing code C, storing the values of the two preceding terms in the series to compute the current one. To speed understanding here it helps for you to see the final code without conditions F 0 = F 1 = 1, F n = F n 2 + F n 1 for n 2 So, when we go into this bit of code, m is 1, fm represents F 1 and fmm1 represents F 0. We add up fm and fp, but make sure that preserve the old value of fm to put into fmm1 for the next go round. It seems reasonable, but now let us prove it works // Pre: n 2 m = 1 fm = 1; fmm1 = 1 while m < n do temp = fm fm = fm + fmm1 fmm1 = temp m = m + 1 end while fn = fm // Post: fn = F n

21 1EC / 1 Fibonacci Step 2 m = 1 fm = 1; fmm1 = 1 while m<n do // [A2]: fmm1 is F m 1 AND fm is F m // [A6]: fm is F m AND fm+fmm1 is F m+1 temp = fm // [A5]: temp is F m AND fm+fmm1 is F m+1 fm = fm + fmm1 // [A4]: temp is F m AND fm is F m+1 fmm1 = temp // [A3]: fmm1 is F m AND fm is F m+1 m = m+1 // [A2] fmm1 is F m 1 AND fm is F m end while // [A1]: fm is F m AND m is n fn = fm // Post: fn = F n The final step is to show that [A6] and [A2] are equivalent. Work backwards

22 1EC / 1 Loop Invariants In program constructs other than loops we can always work our way through the code, working out exactly what the computer would do when executing the code. However problems arise in loop because there is no way of telling (in general) how many times the loop will execute. The way on is to use mathematical induction to deduce properties of a loop that will hold regardless of the number of times the loop body is run. // Pre: n 2 fmm1 = 1; fm = 1; m = 1 while m<n do // fmm1 is F m 1 AND fm is F m temp = fm fm = fm + fmm1 fmm1 = temp m = m+1 // fmm1 is F m 1 AND fm is F m end while // fm is F m AND m is n Recall the example of the previous section. The induction hypothesis here is that At the START of each loop iteration: fmm1 = F m 1 AND fm = F m Notice that if the hypothesis is true at the start of the loop, because no action takes place between the end of one run through the loop and the start of

23 1EC / 1 Watch out in for loops! NB While loops are obvious, but For loops hide some of the action. 1 that the m=m+1 is executed before the end of the loop; and 2 that the m=1 is executed before the loop starts. m=1 while (m<10) do // Start of loop... statements... m = m + 1 // End of loop end while for (m=1; m<10; m=m+1) do // Start of loop... statements... // End of loop? NO, m=m+1 is really here! end for Best to convert for loops into while loops beforehand.

24 1EC / 1 Example of loop invariants As another example, consider the code fragment to find the product of the first n positive integers: // n > 0 prod = 1; i = 1 // Loop Inv: prod is the prdct of first i positive integers while i<n do prod = prod*i i = i+1 end while

25 1EC / 1 Example ctd/ // n > 0 prod = 1; i = 1 // Invariant: prod is the product of the first i positive integers while i<n do // prod is i j=1 j [1] the loop inv // prod*i is i+1 j=1 j [4] prod = prod*i // prod is i+1 j=1 j [3] i = i + 1 // prod is i j=1 j [2] the loop inv end while The final step [1] [4] is to show that prod is i j=1 j prod*i is i+1 j=1 j You should contrast this with what happens if we transpose i = i+1; prod = prod * i;

26 1EC / 1 Checking that Loops Terminate It is easy to write down a loop that does not terminate, as in i = 1 while i>0 do i=i+1 end while That was easy to spot. But consider the code on the right that counts the number of 1 s in the binary representation of a positive integer number. DIV is the integer division operator. MOD is the modulo operator, ie. (evennumber MOD 2) = 0, and (oddnumber MOD 2 ) = 1 rest starts out positive // Necessary pre-condition: number 0 rest = number count = 0 // Loop Invariant: an exercise... while rest = 0 do if (rest MOD 2) == 1 then // the right-most bit of rest must be a 1 count = count + 1 rest = (rest DIV 2) end if end while // count is the total number of 1 bits in number

27 1EC / 1 Loop termination /ctd Can we be sure that this is the case? Fortunately yes. The statement rest = (rest DIV 2) is equivalent to a RightShift of the bits in rest. As examples, take the odd and even numbers Odd Number Right Shift Even Number Right Shift So each time around the loop the value of rest gets smaller, and the value of rest is always non-negative. Thus although it is not immediately clear exactly how many times the loop body will be executed, it is clear that the loop must terminate.

28 1EC / 1 Loop Variants In the previous example, the variable rest is called a LOOP VARIANT. LOOP VARIANT A loop variant is some function of the program variables that can take only a finite number of values in any particular run of a program; and that takes a different value each time around the loop. Example using a function not just // Invariant: one variable a 0 AND b 0 In the following code, each time while a>0 the loop is executed AND b>0 do (i) we can t be sure which of a or if a>b then b will get smaller, but a = a-b (ii) we do know that one will else (iii) we know that both remain b = b - a non-negative. end if Therefore a suitable simple end while expression for a Loop Variant is

29 1EC / 1 Summary We have discussed the design process in terms of input and output specification, of algorithm design and program DBT. We saw that there are four programming constructs. Aassignments and conditional are essential, and that loops and subroutines are highly desirable. Comments in programs are important for the purposes of documention, which is vital so that we can be sure both what the code is supposed to do and that it does it; furthermore, it makes it possible to alter code at a later date in a safe manner. (For code that is used in real applications, the biggest cost incurred is not when the code is originally written, but when the program is subsequently altered to meet new requirements.) Assertions about the expected state of the program variables are a vital part of code documentation. Assertions are easily added to programs in the form of comments, and assertions that are written in a mathematical style are likely to be more precise than assertions using just English. For the simplest forms of program statements (assignments and loops) we can then trace through the code, working out what we expect to happen. In particular, for assignments we can use the

30 1EC / 1 Summary /ctd For code involving loops we have a problem in that we do not know in the general case exactly how many times the loop body will be executed. However the declaration of invariants for loops gives us a straightforward method for inferring properties of programs containing loops. Invariants are normally simple for the original programmer to state and very hard for anybody else to determine! Therefore the assertion of invariants for all but trivial loops is again an important part of the documentation of a program. Although invariants tell us a lot about the state of a program when it terminates, we generally need some further argument to convince ourselves that all our loops do terminate. Informal arguments are sufficient for many cases, and for other cases the use of a variant expression will suffice.

6.001 Notes: Section 4.1

6.001 Notes: Section 4.1 6.001 Notes: Section 4.1 Slide 4.1.1 In this lecture, we are going to take a careful look at the kinds of procedures we can build. We will first go back to look very carefully at the substitution model,

More information

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 1 Contracts 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,

More information

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning Lecture 1 Contracts 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,

More information

15 212: Principles of Programming. Some Notes on Induction

15 212: Principles of Programming. Some Notes on Induction 5 22: Principles of Programming Some Notes on Induction Michael Erdmann Spring 20 These notes provide a brief introduction to induction for proving properties of ML programs. We assume that the reader

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17 01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are

More information

AXIOMS FOR THE INTEGERS

AXIOMS FOR THE INTEGERS AXIOMS FOR THE INTEGERS BRIAN OSSERMAN We describe the set of axioms for the integers which we will use in the class. The axioms are almost the same as what is presented in Appendix A of the textbook,

More information

Lecture 6: Arithmetic and Threshold Circuits

Lecture 6: Arithmetic and Threshold Circuits IAS/PCMI Summer Session 2000 Clay Mathematics Undergraduate Program Advanced Course on Computational Complexity Lecture 6: Arithmetic and Threshold Circuits David Mix Barrington and Alexis Maciel July

More information

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed

More information

The divide and conquer strategy has three basic parts. For a given problem of size n,

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Lecture Notes on Arrays

Lecture Notes on Arrays Lecture Notes on Arrays 15-122: Principles of Imperative Computation July 2, 2013 1 Introduction So far we have seen how to process primitive data like integers in imperative programs. That is useful,

More information

Recursively Enumerable Languages, Turing Machines, and Decidability

Recursively Enumerable Languages, Turing Machines, and Decidability Recursively Enumerable Languages, Turing Machines, and Decidability 1 Problem Reduction: Basic Concepts and Analogies The concept of problem reduction is simple at a high level. You simply take an algorithm

More information

Repetition Through Recursion

Repetition Through Recursion Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something

More information

14.1 Encoding for different models of computation

14.1 Encoding for different models of computation Lecture 14 Decidable languages In the previous lecture we discussed some examples of encoding schemes, through which various objects can be represented by strings over a given alphabet. We will begin this

More information

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1

(a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are rational they can be written as the ratio of integers a 1 CS 70 Discrete Mathematics for CS Fall 2000 Wagner MT1 Sol Solutions to Midterm 1 1. (16 pts.) Theorems and proofs (a) (4 pts) Prove that if a and b are rational, then ab is rational. Since a and b are

More information

Induction and Semantics in Dafny

Induction and Semantics in Dafny 15-414 Lecture 11 1 Instructor: Matt Fredrikson Induction and Semantics in Dafny TA: Ryan Wagner Encoding the syntax of Imp Recall the abstract syntax of Imp: a AExp ::= n Z x Var a 1 + a 2 b BExp ::=

More information

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122 I. Logic 101 In logic, a statement or proposition is a sentence that can either be true or false. A predicate is a sentence in

More information

Mathematical Induction

Mathematical Induction Mathematical Induction Victor Adamchik Fall of 2005 Lecture 3 (out of three) Plan 1. Recursive Definitions 2. Recursively Defined Sets 3. Program Correctness Recursive Definitions Sometimes it is easier

More information

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37 Tjark Weber Functional Programming 1 Based on notes by Sven-Olof Nyström Tjark Weber (UU) Recursion 1 / 37 Background FP I / Advanced FP FP I / Advanced FP This course (Functional Programming I) (5 hp,

More information

An Annotated Language

An Annotated Language Hoare Logic An Annotated Language State and Semantics Expressions are interpreted as functions from states to the corresponding domain of interpretation Operators have the obvious interpretation Free of

More information

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016 Complexity, Induction, and Recurrence Relations CSE 373 Help Session 4/7/2016 Big-O Definition Definition: g(n) is in O( f(n) ) if there exist positive constants c and n0 such that g(n) c f(n) for all

More information

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Module 02 Lecture - 45 Memoization Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Module 02 Lecture - 45 Memoization Let us continue our discussion of inductive definitions. (Refer Slide Time: 00:05)

More information

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

Lecture Notes on Contracts

Lecture Notes on Contracts Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,

More information

Figure 4.1: The evolution of a rooted tree.

Figure 4.1: The evolution of a rooted tree. 106 CHAPTER 4. INDUCTION, RECURSION AND RECURRENCES 4.6 Rooted Trees 4.6.1 The idea of a rooted tree We talked about how a tree diagram helps us visualize merge sort or other divide and conquer algorithms.

More information

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include Outline Computer Science 331 Correctness of Algorithms Mike Jacobson Department of Computer Science University of Calgary Lectures #2-4 1 What is a? Applications 2 Recursive Algorithms 3 Final Notes Additional

More information

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017 Recursion Recursion is a technique for solving problems in which the solution to the problem of size n is based on solutions to versions of the problem of size smaller than n. Many problems can be solved

More information

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02) Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 04 Lecture - 01 Merge Sort (Refer

More information

More Complicated Recursion CMPSC 122

More Complicated Recursion CMPSC 122 More Complicated Recursion CMPSC 122 Now that we've gotten a taste of recursion, we'll look at several more examples of recursion that are special in their own way. I. Example with More Involved Arithmetic

More information

1 Achieving IND-CPA security

1 Achieving IND-CPA security ISA 562: Information Security, Theory and Practice Lecture 2 1 Achieving IND-CPA security 1.1 Pseudorandom numbers, and stateful encryption As we saw last time, the OTP is perfectly secure, but it forces

More information

CS 161 Computer Security

CS 161 Computer Security Wagner Spring 2014 CS 161 Computer Security 1/27 Reasoning About Code Often functions make certain assumptions about their arguments, and it is the caller s responsibility to make sure those assumptions

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Approximation algorithms Date: 11/27/18 22.1 Introduction We spent the last two lectures proving that for certain problems, we can

More information

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF

ELEMENTARY NUMBER THEORY AND METHODS OF PROOF CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF Copyright Cengage Learning. All rights reserved. SECTION 4.2 Direct Proof and Counterexample II: Rational Numbers Copyright Cengage Learning. All

More information

Chapter 4. Number Theory. 4.1 Factors and multiples

Chapter 4. Number Theory. 4.1 Factors and multiples Chapter 4 Number Theory We ve now covered most of the basic techniques for writing proofs. So we re going to start applying them to specific topics in mathematics, starting with number theory. Number theory

More information

n! = 1 * 2 * 3 * 4 * * (n-1) * n

n! = 1 * 2 * 3 * 4 * * (n-1) * n The Beauty and Joy of Computing 1 Lab Exercise 9: Problem self-similarity and recursion Objectives By completing this lab exercise, you should learn to Recognize simple self-similar problems which are

More information

Fall Recursion and induction. Stephen Brookes. Lecture 4

Fall Recursion and induction. Stephen Brookes. Lecture 4 15-150 Fall 2018 Stephen Brookes Lecture 4 Recursion and induction Last time Specification format for a function F type assumption guarantee (REQUIRES) (ENSURES) For all (properly typed) x satisfying the

More information

Handout 9: Imperative Programs and State

Handout 9: Imperative Programs and State 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 9: Imperative Programs and State Imperative

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION Alessandro Artale UniBZ - http://www.inf.unibz.it/ artale/ SECTION 5.5 Application: Correctness of Algorithms Copyright Cengage Learning. All

More information

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall

1KOd17RMoURxjn2 CSE 20 DISCRETE MATH Fall CSE 20 https://goo.gl/forms/1o 1KOd17RMoURxjn2 DISCRETE MATH Fall 2017 http://cseweb.ucsd.edu/classes/fa17/cse20-ab/ Today's learning goals Explain the steps in a proof by mathematical and/or structural

More information

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009

Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009 Logic and Computation Lecture 20 CSU 290 Spring 2009 (Pucella) Thursday, Mar 12, 2009 Note that I change the name of the functions slightly in these notes from what I used in class, to be consistent with

More information

Chapter 3. Set Theory. 3.1 What is a Set?

Chapter 3. Set Theory. 3.1 What is a Set? Chapter 3 Set Theory 3.1 What is a Set? A set is a well-defined collection of objects called elements or members of the set. Here, well-defined means accurately and unambiguously stated or described. Any

More information

Introduction to Scientific Computing

Introduction to Scientific Computing Introduction to Scientific Computing Dr Hanno Rein Last updated: October 12, 2018 1 Computers A computer is a machine which can perform a set of calculations. The purpose of this course is to give you

More information

Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD

Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD Introduction to Programming in C Department of Computer Science and Engineering\ Lecture No. #02 Introduction: GCD In this session, we will write another algorithm to solve a mathematical problem. If you

More information

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4) Orders of Growth of Processes Today s topics Resources used by a program to solve a problem of size n Time Space Define order of growth Visualizing resources utilization using our model of evaluation Relating

More information

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré Hoare Logic COMP2600 Formal Methods for Software Engineering Rajeev Goré Australian National University Semester 2, 2016 (Slides courtesy of Ranald Clouston) COMP 2600 Hoare Logic 1 Australian Capital

More information

Lecture 15 : Review DRAFT

Lecture 15 : Review DRAFT CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.

More information

Ramsey s Theorem on Graphs

Ramsey s Theorem on Graphs Ramsey s Theorem on Graphs 1 Introduction Exposition by William Gasarch Imagine that you have 6 people at a party. We assume that, for every pair of them, either THEY KNOW EACH OTHER or NEITHER OF THEM

More information

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Semantics via Syntax. f (4) = if define f (x) =2 x + 55. 1 Semantics via Syntax The specification of a programming language starts with its syntax. As every programmer knows, the syntax of a language comes in the shape of a variant of a BNF (Backus-Naur Form)

More information

(Refer Slide Time: 01.26)

(Refer Slide Time: 01.26) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture # 22 Why Sorting? Today we are going to be looking at sorting.

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

Chapter 1 Programming: A General Overview

Chapter 1 Programming: A General Overview Chapter 1 Programming: A General Overview 2 Introduction This class is an introduction to the design, implementation, and analysis of algorithms. Examples: sorting large amounts of data organizing information

More information

Lecture Notes on Induction and Recursion

Lecture Notes on Induction and Recursion Lecture Notes on Induction and Recursion 15-317: Constructive Logic Frank Pfenning Lecture 7 September 19, 2017 1 Introduction At this point in the course we have developed a good formal understanding

More information

Programming and Data Structure

Programming and Data Structure Programming and Data Structure Dr. P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture # 09 Problem Decomposition by Recursion - II We will

More information

CITS5501 Software Testing and Quality Assurance Formal methods

CITS5501 Software Testing and Quality Assurance Formal methods CITS5501 Software Testing and Quality Assurance Formal methods Unit coordinator: Arran Stewart May 1, 2018 1 / 49 Sources Pressman, R., Software Engineering: A Practitioner s Approach, McGraw-Hill, 2005

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret

Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret Advanced Algorithms Class Notes for Monday, October 23, 2012 Min Ye, Mingfu Shao, and Bernard Moret Greedy Algorithms (continued) The best known application where the greedy algorithm is optimal is surely

More information

Solutions to Homework 10

Solutions to Homework 10 CS/Math 240: Intro to Discrete Math 5/3/20 Instructor: Dieter van Melkebeek Solutions to Homework 0 Problem There were five different languages in Problem 4 of Homework 9. The Language D 0 Recall that

More information

Lecture 25 Spanning Trees

Lecture 25 Spanning Trees Lecture 25 Spanning Trees 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning, Iliano Cervesato The following is a simple example of a connected, undirected graph with 5 vertices (A,

More information

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers

CIS 194: Homework 6. Due Monday, February 25. Fibonacci numbers CIS 194: Homework 6 Due Monday, February 25 Files you should submit: Fibonacci.hs This week we learned about Haskell s lazy evaluation. This homework assignment will focus on one particular consequence

More information

We would like guidance on how to write our programs beyond simply knowing correlations between inputs and outputs.

We would like guidance on how to write our programs beyond simply knowing correlations between inputs and outputs. Chapter 3 Correctness One of the most appealing aspects of computer programs is that they have a close connection to mathematics, in particular, logic. We use these connections implicitly every day when

More information

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström

Recursion. Lars-Henrik Eriksson. Functional Programming 1. Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström Lars-Henrik Eriksson Functional Programming 1 Based on a presentation by Tjark Weber and notes by Sven-Olof Nyström Tjark Weber (UU) Recursion 1 / 41 Comparison: Imperative/Functional Programming Comparison:

More information

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment Iteration 6.1. Multiple assignment As you may have discovered, it is legal to make more than one assignment to the same variable. A new assignment makes an existing variable refer to a new value (and stop

More information

142

142 Scope Rules Thus, storage duration does not affect the scope of an identifier. The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned

More information

Lecture 3: Recursion; Structural Induction

Lecture 3: Recursion; Structural Induction 15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion

More information

Proofwriting Checklist

Proofwriting Checklist CS103 Winter 2019 Proofwriting Checklist Cynthia Lee Keith Schwarz Over the years, we ve found many common proofwriting errors that can easily be spotted once you know how to look for them. In this handout,

More information

Lecture 4 Searching Arrays

Lecture 4 Searching Arrays Lecture 4 Searching Arrays 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning One of the fundamental and recurring problems in computer science is to find elements in collections,

More information

SOFTWARE ENGINEERING DESIGN I

SOFTWARE ENGINEERING DESIGN I 2 SOFTWARE ENGINEERING DESIGN I 3. Schemas and Theories The aim of this course is to learn how to write formal specifications of computer systems, using classical logic. The key descriptional technique

More information

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell) Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 13 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 16 October, 2012 1 / 21 1 Types 2 3 4 2 / 21 Thus far

More information

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop Announcements Lab Friday, 1-2:30 and 3-4:30 in 26-152 Boot your laptop and start Forte, if you brought your laptop Create an empty file called Lecture4 and create an empty main() method in a class: 1.00

More information

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets

Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets Lecture 4: examples of topological spaces, coarser and finer topologies, bases and closed sets Saul Glasman 14 September 2016 Let s give the definition of an open subset of R. Definition 1. Let U R. We

More information

Binary Search. Roland Backhouse February 5th, 2001

Binary Search. Roland Backhouse February 5th, 2001 1 Binary Search Roland Backhouse February 5th, 2001 Outline 2 An implementation in Java of the card-searching algorithm is presented. Issues concerning the correctness of the implementation are raised

More information

1 Linear programming relaxation

1 Linear programming relaxation Cornell University, Fall 2010 CS 6820: Algorithms Lecture notes: Primal-dual min-cost bipartite matching August 27 30 1 Linear programming relaxation Recall that in the bipartite minimum-cost perfect matching

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

Computational Geometry: Lecture 5

Computational Geometry: Lecture 5 Computational Geometry: Lecture 5 Don Sheehy January 29, 2010 1 Degeneracy In many of the algorithms that we have discussed so far, we have run into problems when that input is somehow troublesome. For

More information

Inference rule for Induction

Inference rule for Induction Inference rule for Induction Let P( ) be a predicate with domain the positive integers BASE CASE INDUCTIVE STEP INDUCTIVE Step: Usually a direct proof Assume P(x) for arbitrary x (Inductive Hypothesis),

More information

An Interesting Way to Combine Numbers

An Interesting Way to Combine Numbers An Interesting Way to Combine Numbers Joshua Zucker and Tom Davis October 12, 2016 Abstract This exercise can be used for middle school students and older. The original problem seems almost impossibly

More information

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1

Introduction to Automata Theory. BİL405 - Automata Theory and Formal Languages 1 Introduction to Automata Theory BİL405 - Automata Theory and Formal Languages 1 Automata, Computability and Complexity Automata, Computability and Complexity are linked by the question: What are the fundamental

More information

Lecture Transcript While and Do While Statements in C++

Lecture Transcript While and Do While Statements in C++ Lecture Transcript While and Do While Statements in C++ Hello and welcome back. In this lecture we are going to look at the while and do...while iteration statements in C++. Here is a quick recap of some

More information

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute Week 02 Module 06 Lecture - 14 Merge Sort: Analysis So, we have seen how to use a divide and conquer strategy, we

More information

Lecture 3 Notes Arrays

Lecture 3 Notes Arrays Lecture 3 Notes Arrays 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning, André Platzer 1 Introduction So far we have seen how to process primitive data like integers in imperative

More information

Graph Theory Questions from Past Papers

Graph Theory Questions from Past Papers Graph Theory Questions from Past Papers Bilkent University, Laurence Barker, 19 October 2017 Do not forget to justify your answers in terms which could be understood by people who know the background theory

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #06 Loops: Operators We have seen comparison operators, like less then, equal to, less than or equal. to and

More information

Symbolic Execution and Proof of Properties

Symbolic Execution and Proof of Properties Chapter 7 Symbolic Execution and Proof of Properties Symbolic execution builds predicates that characterize the conditions under which execution paths can be taken and the effect of the execution on program

More information

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting CS2 Algorithms and Data Structures Note 10 Depth-First Search and Topological Sorting In this lecture, we will analyse the running time of DFS and discuss a few applications. 10.1 A recursive implementation

More information

Recall our recursive multiply algorithm:

Recall our recursive multiply algorithm: Recall our recursive multiply algorithm: PRECONDITION: x and y are both binary bit arrays of length n, n a power of 2. POSTCONDITION: Returns a binary bit array equal to the product of x and y. REC MULTIPLY

More information

Lecture 1: Overview

Lecture 1: Overview 15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant

More information

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table

More information

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G.

γ(ɛ) (a, b) (a, d) (d, a) (a, b) (c, d) (d, d) (e, e) (e, a) (e, e) (a) Draw a picture of G. MAD 3105 Spring 2006 Solutions for Review for Test 2 1. Define a graph G with V (G) = {a, b, c, d, e}, E(G) = {r, s, t, u, v, w, x, y, z} and γ, the function defining the edges, is given by the table ɛ

More information

A3 Computer Architecture

A3 Computer Architecture A3 Computer Architecture Engineering Science 3rd year A3 Lectures Prof David Murray david.murray@eng.ox.ac.uk www.robots.ox.ac.uk/ dwm/courses/3co Michaelmas 2000 1 / 1 2: Introduction to the CPU 3A3 Michaelmas

More information

Program development plan

Program development plan Appendix A Program development plan If you are spending a lot of time debugging, it is probably because you do not have an effective program development plan. A typical, bad program development plan goes

More information

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning Lecture 7 Quicksort 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we consider two related algorithms for sorting that achieve a much better running time than

More information

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017

CMSC 451: Lecture 10 Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct 3, 2017 CMSC 45 CMSC 45: Lecture Dynamic Programming: Weighted Interval Scheduling Tuesday, Oct, Reading: Section. in KT. Dynamic Programming: In this lecture we begin our coverage of an important algorithm design

More information

Python for Informatics

Python for Informatics Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.

More information

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2012 Lab 3 Matrix Math Introduction Reading In this lab you will write a

More information

Unit #2: Recursion, Induction, and Loop Invariants

Unit #2: Recursion, Induction, and Loop Invariants Unit #2: Recursion, Induction, and Loop Invariants CPSC 221: Algorithms and Data Structures Will Evans 2012W1 Unit Outline Thinking Recursively Recursion Examples Analyzing Recursion: Induction and Recurrences

More information

Recursive Definitions Structural Induction Recursive Algorithms

Recursive Definitions Structural Induction Recursive Algorithms Chapter 4 1 4.3-4.4 Recursive Definitions Structural Induction Recursive Algorithms 2 Section 4.1 3 Principle of Mathematical Induction Principle of Mathematical Induction: To prove that P(n) is true for

More information

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop AXIOMS OF AN IMPERATIVE LANGUAGE We will use the same language, with the same abstract syntax that we used for operational semantics. However, we will only be concerned with the commands, since the language

More information

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently. The Science of Computing I Lesson 4: Introduction to Data Structures Living with Cyber Pillar: Data Structures The need for data structures The algorithms we design to solve problems rarely do so without

More information