Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27.

Slides:



Advertisements
Similar presentations
Chessboard problems You don’t have to know chess to solve them.
Advertisements

More Recursion: Permutations and Towers of Hanoi COP 3502.
Today’s agenda 1. WASI test results 2.Quiz or HW? 3.Your stage 2 reports 4.Individual activity points.
Reasoning About Code; Hoare Logic, continued
Mathematical Induction II Lecture 14: Nov
Recursion Lecture 18: Nov 18.
Announcements We are done with homeworks Second coding exam this week, in recitation –Times will be posted later today –If in doubt, show up for your regular.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Fall 2008 Insertion Sort – review of loop invariants.
The Tower of Hanoi
Invariant Method Lecture 6: Sep
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Describing Syntax and Semantics
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
Invariant Method
Towers of Hanoi. Introduction This problem is discussed in many maths texts, And in computer science an AI as an illustration of recursion and problem.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
The Pigeonhole (Dirichlet’s box) Principle
Data Structures Using C++ 2E Chapter 6 Recursion.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Reading and Writing Mathematical Proofs
Activity Set 3.5.i PREP PPTX Visual Algebra for Teachers.
CSE 20 Lecture 12 Induction CK Cheng 1. Induction Outlines Introduction Theorem Examples: The complexity calculation – Tower of Hanoi – Merge Sort – Fibonacci.
Analyzing the Requirements with Formal Specifications Vienna Development Method Specification Language (VDM-SL) Book: Formal Software Development From.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 24.
Additional Problems.
Discrete Mathematics Tutorial 11 Chin
Reasoning about programs March CSE 403, Winter 2011, Brun.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Iteration vs. Recursion If we have time: live demo!!!
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Chapter 6 Questions Quick Quiz
Quiz highlights 1.Probability of the song coming up after one press: 1/N. Two times? Gets difficult. The first or second? Or both? USE THE MAIN HEURISTICS:
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 26.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 25.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
CS320n – Elements of Visual Programming Assignment Help Session.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
General Computer Science for Engineers CISC 106 Lecture 06 James Atlas Computer and Information Sciences 06/24/2009.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
CS100A, Fall Review of loops 1 CS100A, Fall 1998, Review of Loops and Loop Invariants Some students are still having trouble understanding loop invariants.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
COMP108 Algorithmic Foundations Divide and Conquer
COMP108 Algorithmic Foundations Divide and Conquer
Reasoning About Code.
Reasoning about code CSE 331 University of Washington.
Proving Loops Testing debugging and verification
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Mathematical Induction
Gray Code Can you find an ordering of all the n-bit strings in such a way that two consecutive n-bit strings differed by only one bit? This is called the.
Chapter 5 Induction and Recursion
Sets and Logic…. Chapters 5 and 6
Fundamentals of Programming
Notes 9.5 – Mathematical Induction
Induction and recursion
Fractions 1/2 1/8 1/3 6/8 3/4.
Program Verification with Hoare Logic
Presentation transcript:

Dr. Naveed Riaz Design and Analysis of Algorithms 1 1 Formal Methods in Software Engineering Lecture # 27

Dr. Naveed Riaz Design and Analysis of Algorithms 2 2 Classical Problem Can you completely cover the chessboard with these dominoes without partially using a domino? If so how. If not prove that you cannot.

Dr. Naveed Riaz Design and Analysis of Algorithms 3 3 Classical Problem Key of invariant condition i.e. Shape of the tiles which we are using to cover the chessboard ( some information store in color) Given piece will cover one light and one blue tile because on a cheeseboard we do not have two white or black tiles adjusnt to each other We have equal number of blue and white squares on chessboard If do not have equal number of squares then we will not cover the whole chessboard Condition: We have removed two squares of the same color i.e. We are left with more blue then white i.e. 32 blue and 30 whites

Dr. Naveed Riaz Design and Analysis of Algorithms 4 4 Conditional statements If ( i <= j ) then m : = i; else m :=j (m <= i and m <= j) and ( m = i or m =j) Possible: when “m” is smaller than i and j Current program assign smallest value to “m” Question: what is the wp? i.e. What is the condition to impose on input i and j such that we get our objective

Dr. Naveed Riaz Design and Analysis of Algorithms 5 5 Conditional statements Else Part (i >j) m :=j ; (m = i or m = j) and ( m <= i and m <=j) (i > j) and ( j =i or j = j ) and ( j <=i and j <= j) ( i>j ) and (true) and ( j<= i and true) ( i >j) and ( j <= i ) (i>j)

Dr. Naveed Riaz Design and Analysis of Algorithms 6 6 Conditional statements If ( i <= j ) then m : = i; (m = i or m =j) and ( m <= i and m <= j) ( i < = j ) and ( i= i or i = j) and ( i < = i and i <= j) ( i <=j ) and ( true) and ( true and i <=j) ( i < = j) and ( i < = j) ( i< = j)

Dr. Naveed Riaz Design and Analysis of Algorithms 7 7 Conditional statements ( i j) Universal set

Dr. Naveed Riaz Design and Analysis of Algorithms 8 8 Tower of Hanoi

Dr. Naveed Riaz Design and Analysis of Algorithms 9 9 Tower of Hanoi

Dr. Naveed Riaz Design and Analysis of Algorithms 10 Tower of Hanoi

Dr. Naveed Riaz Design and Analysis of Algorithms 11 Invariant condition in Iteration Recursive solution is given in every book But we need to find iterative solution Odd number moves involved smallest disk Smallest disk move in clock-wise or anti-clock wise depending on the number of disks If you started with even number of disks it would be clock wise, if started with odd number then anti-clock Your having only one move after moving small disk Finding the weakest pre-condition in loops are not simple as compared to conditional statements

Dr. Naveed Riaz Design and Analysis of Algorithms 12 Loop invariants s = 0; for i: = 1 to n do s = s + a [i]; What is the “loop invariant” ? Post condition: In “s” we want to have the sum of all the elements of an array. I got many answers in front of me: value of “i” between 1 and “n”. But that does not help us. Remember loop invariant definition.

Dr. Naveed Riaz Design and Analysis of Algorithms 13 Loop invariants How step in the loop should take us closer to achieve our objectives. How ? s = 0; for i: = 1 to n do s = s + a [i]; Values of “s” will be stated in terms of formula. What is the value of “s” before start of the loop : S=0 Value of “s” after first iteration : First element in it After two: “s” has the sum of first and second element. After three: Sum of first three elements After K iteration: S has the value which is the sum of 1 to k

Dr. Naveed Riaz Design and Analysis of Algorithms 14 Loop invariants “s” is the sum of elements from a[1] to a [i] immediately before i is incremented Think about While loop ( compare it with IF statement)

Dr. Naveed Riaz Design and Analysis of Algorithms 15 Weakest pre-condition for While statement { P} while B do S {Q} Let W be while B do S Condition for termination of the loop Po = ( not B) P1 = B and wp (S, Po) = wp ( S, not B) ( once true) Pk = B and wp ( S, P k-1)

Dr. Naveed Riaz Design and Analysis of Algorithms 16 Weakest pre-condition for While statement The invariant condition {I} while B do S { I and not B}

Dr. Naveed Riaz Design and Analysis of Algorithms 17 Weakest pre-condition for While statement a =0; i=0; while ( i<N) a = a + i++; Do we ever come out of this loop? Objective of this program is to add the first “n” numbers Loop invariant : Constraints on the input?

Dr. Naveed Riaz Design and Analysis of Algorithms 18 Weakest pre-condition for While statement What would happen when “N” is zero or negative number When “N” is -1 then we have zero in a.

Dr. Naveed Riaz Design and Analysis of Algorithms 19