Lecture 12.2 Loop Invariants for Designing Array Algorithms.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Lecture 10.2 Different Types of Loops, including for and do.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Loops.
© 2010 Pearson Education, Inc. All rights reserved.
Chapter 2 Application Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Chapter 13 Income Inequality. Copyright © 2005 Pearson Addison-Wesley. All rights reserved
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Chapter 1 The Facts to Be Explained. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 1-2.
Programming Lecture #4 CS 101 Autumn 2006 Tariq Jadoon.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 3 Transport Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2.
Chapter 6 Human Capital. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
Chapter 1 Principles of Programming and Software Engineering.
Chapter 8 The Role of Technology in Growth. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Chapter 7 Multimedia Networking. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2.
Chapter 16 Resources and the Environment at the Global Level.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
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:
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
The Critical Section Problem
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Lecture 12.2 Loop Invariants for Designing Array Algorithms.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Control Structures. 2  Control – the order in which instructions are performed in a program.  Control structures are well defined ways of determining.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
CSC 211 Data Structures Lecture 13
© 2006 Pearson Addison-Wesley. All rights reserved Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objectives  Function Templates  Recursion Functions.
Introduction to Computer Programming
Correctness issues and Loop invariants
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CS October 2008 The while loop and assertions
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
Chapter 6 Control Statements: Part 2
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Output Variables {true} S {i = j} i := j; or j := i;
Binary Search and Loop invariants
Review 1+3= 4 7+3= = 5 7+4= = = 6 7+6= = = 7+7+7=
Recursion.
Searching and Sorting Hint at Asymptotic Complexity
2.2 Simplifying Expressions
Chapter 4: Loops and Iteration
Presentation transcript:

Lecture 12.2 Loop Invariants for Designing Array Algorithms

© 2006 Pearson Addison-Wesley. All rights reserved Algorithms An important aspect of problem solving is the creation of algorithms. How does a programmer create an algorithm? Often this occurs by recognizing that the problem fits some pattern. What coding/design patterns do you know? Often programming requires knowledge of how to approach problems  object-oriented strategies  design by prototype  top-down design Programming == Problem Solving

© 2006 Pearson Addison-Wesley. All rights reserved Loop Design loopInitialization ; while ( loopCondition ) { workOfTheLoop ; makeProgress ; } loopInitialization ; while ( loopCondition ) { workOfTheLoop ; makeProgress ; } The basic loop pattern Examples list.reset(); while ( list.hasNext() ) { System.out.println( (String)list.next() ); } int k = 1; min = intArray[0]; while ( k != intArray.length ) { if (intArray[k] < min) { min = intArray[k]; } k++; }

© 2006 Pearson Addison-Wesley. All rights reserved diagram - diagram - diagram Pictures are a helpful way to visualize algorithms (especially when using containers). int k = 1; min = intArray[0]; while ( k != intArray.length ) { if (intArray[k] < min) { min = intArray[k]; } k++; } [0] [1] [2]... [k][length-1] processed This picture captures an important property of the loop -- the loop ___________.

© 2006 Pearson Addison-Wesley. All rights reserved Loop Invariant A loop invariant is an assertion that is true immediately before the loop condition. int k = 1; min = intArray[0]; while ( k != intArray.length ) { if (intArray[k] < min) { min = intArray[k]; } k++; } [0] [1] [2]...[k] [length-1] processed INV: All values within intArray[0]... intArray[k-1] are ≥ min & some value of intArray[0]... intArray[k-1] == min

© 2006 Pearson Addison-Wesley. All rights reserved Trace [0] [1] [length-1] processed int k = 1; min = intArray[0]; while ( k != intArray.length ) { if (intArray[k] < min) { min = intArray[k]; } k++; } INV: All values within intArray[0]... intArray[k-1] are ≥ min & some value of intArray[0]... intArray[k-1] == min Just before the loop k 1 INV: All values within intArray[0]... intArray[0] are ≥ min & some value of intArray[0]... intArray[0] == min

© 2006 Pearson Addison-Wesley. All rights reserved Reversing an Array What is wrong with this initial attempt at reversing the content of an array? char tmpChar; int k = 0; while ( k != charArr.length ) { tmpChar = charArr[k]; charArr[k] = charArr[charArr.length-k-1]; charArr[charArr.length-k-1] = tmpChar; k++; } Consider how the loop invariant captures this problem... INV: for all j, (0 ≤ j < k) charArr[j] == [0] [1] [2]... [j] [length-j-1] [length-1]

© 2006 Pearson Addison-Wesley. All rights reserved Coding from the Invariant 1) Select the loop invariant. 2) Determine initialization code. 3) Determine the loop condition. 4) Complete the loop body. loopInitialization ; while /* loopInvariant */ ( loopCondition ) { loopBody ; } Four Steps

© 2006 Pearson Addison-Wesley. All rights reserved Example: Coding from the Invariant 1) Select the loop invariant. 2) Determine the loop condition. 3) Determine the initialization. 4) Complete the loop body. // Assert: n is an int and n ≥ 0 loopInitialization ; while /* loopInvariant */ ( loopCondition ) { loopBody ; } // Assert: factorial == n! Four Steps Design a loop to calculate n factorial, written n! n! == 1 * 2 * 3 * 4 *... * n 3! == 1 * 2 * 3 == 6 5! == ?? Note, also, the following... 1! == 1 0! == 1

© 2006 Pearson Addison-Wesley. All rights reserved Design a loop to calculate n factorial, written n! Below is a suitable invariant for some int variable, called counter. 1) Select the loop invariant. 2) Determine the loop condition. 3) Determine the initialization. 4) Complete the loop body. // Assert: n ≥ 0 loopInitialization ; while /* loopInvariant */ ( loopCondition ) { loopBody ; } // Assert: factorial == n! Four Steps Example: Coding from the Invariant

© 2006 Pearson Addison-Wesley. All rights reserved Design a loop to calculate n factorial, written n! What loop condition combines with the invariant to ensure the assertion after the loop? (i.e., what value for counter must terminate the loop?) 1) Select the loop invariant. 2) Determine the loop condition. 3) Determine the initialization. 4) Complete the loop body. // Assert: n ≥ 0 loopInitialization ; while /* factorial == counter! */ ( loopCondition ) { loopBody ; } // Assert: factorial == n! Four Steps Example: Coding from the Invariant

© 2006 Pearson Addison-Wesley. All rights reserved Design a loop to calculate n factorial, written n! What loop initial values need to be assigned to counter and factorial for the loop invariant to be true when the loop begins? 1) Select the loop invariant. 2) Determine the loop condition. 3) Determine the initialization. 4) Complete the loop body. // Assert: n ≥ 0 loopInitialization ; while /* factorial == counter! */ ( counter != n ) { loopBody ; } // Assert: factorial == n! Four Steps Example: Coding from the Invariant

© 2006 Pearson Addison-Wesley. All rights reserved Design a loop to calculate n factorial, written n! What loop body both preserves the loop invariant and makes progress? counter++; factorial = factorial * counter; 1) Select the loop invariant. 2) Determine the loop condition. 3) Determine the initialization. 4) Complete the loop body. // Assert: n ≥ 0 counter = 0; factorial = 1; while /* factorial == counter! */ ( counter != n ) { loopBody ; } // Assert: factorial == n! Four Steps Example: Coding from the Invariant

© 2006 Pearson Addison-Wesley. All rights reserved The Complete Loop 1) Select the loop invariant. 2) Determine the loop condition. 3) Determine the initialization. 4) Complete the loop body. // Assert: n ≥ 0 counter = 0; factorial = 1; while /* factorial == counter! */ ( counter != n ) { counter++; factorial = factorial * counter; } // Assert: factorial == n! Four Steps Example: Coding from the Invariant