CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

2/9/06CS 3343 Analysis of Algorithms1 Convex Hull  Given a set of pins on a pinboard  And a rubber band around them  How does the rubber band look when.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
8/29/06CS 6463: AT Computational Geometry1 CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Problem Solving Chapter 2. What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
COMPSCI 101 S Principles of Programming Lecture 1 – Introduction.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
1 Problem Solving using computers Data.. Representation & storage Representation of Numeric data The Binary System.
Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
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.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.
CS139 – Algorithm Development Aug 23, 2004 WELCOME!
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
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?
Tuesday, April 30 Dynamic Programming – Recursion – Principle of Optimality Handouts: Lecture Notes.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Recursion. Recursive Methods  A recursive method is a method that calls itself.  General Form of Simple Recursive Methods  Every recursive method has.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 6: Stepwise refinement revisited, Midterm review.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Recursion Powerful Tool
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Topic 6 Recursion.
Algorithm and Ambiguity
Applied Algorithms (Lecture 17) Recursion Fall-23
Chapter 16 Component-Level Design
Coding Concepts (Basics)
Searching: linear & binary
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Algorithm and Ambiguity
Module 1-10: Recursion.
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
Algorithms and Problem Solving
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Lecture 14 Previous Lecture
CS100J Lecture 15 Previous Lecture This Lecture Sorting
Basic Concepts of Algorithm
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Lecture 20 – Practice Exercises 4
Presentation transcript:

CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem –boundary conditions –validation n Pattern for processing input values up to (but not including) a stopping signal –Example n Processing exam grades –Java Constructs n Casts and Rounding –Reading, Lewis & Loftus, Section 3.9 n This Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case analysis –iterative refinement n Use of comments as higher-level statements

CS 100Lecture 52 Programming By Stepwise Refinement n An “algorithm” for you to follow when writing a program that solves problem P: if ( P is simple enough to code immediately ) if ( P is simple enough to code immediately ) Write the code that solves P; Write the code that solves P; else { else { Refine P into subproblems; Refine P into subproblems; Write the code that solves each subproblem; Write the code that solves each subproblem; } n Stepwise refinement is an example of –a divide-and-conquer algorithm, because it breaks problems down into simpler parts, –a recursive algorithm, because you use the same “algorithm” to write the code for any given subproblem. n The refinement of P into subproblems must include a description of how the code segments solving the subproblems combine to form code that solves P. n You can write the code segments that solve the subproblems in any order.

CS 100Lecture 53 Ways to Refine P into Subproblems n A program pattern –Do whatever n times –Process input values up until (but not including) a stopping value. n A sequential refinement n A case analysis n An iterative refinement

CS 100Lecture 54 Sequential Refinement n The refinement is structured so that solving P 1 through P n in sequence solves P. /* Solve problem P */ { /* Solve subproblem P 1 */ /* Solve subproblem P 1 */ /* Solve subproblem P 2 */ /* Solve subproblem P 2 */ /* Solve subproblem P n */ /* Solve subproblem P n */ }

CS 100Lecture 55 Sequential Refinement, cont. n Example 1: /* Drive from Ithaca to NYC. */ { /* Drive from Ithaca to Binghamton */ /* Drive from Ithaca to Binghamton */ /* Drive from Binghamton to NYC */ /* Drive from Binghamton to NYC */ } n Example 2: /* Drive from Ithaca to NYC. */ { /* Drive from Ithaca to Albany. */ /* Drive from Ithaca to Albany. */ /* Drive from Albany to NYC. */ /* Drive from Albany to NYC. */ }

CS 100Lecture 56 Sequential Refinement, cont. n Example 1, continued /* Drive from Ithaca to NYC. */ { /* Drive from Ithaca to Binghamton. */ /* Drive from Ithaca to Binghamton. */ /* Drive from Binghamton to NYC. */ /* Drive from Binghamton to NYC. */ /* Drive from Binghamton to /* Drive from Binghamton to Stroudsburg. */ Stroudsburg. */ /* Drive from Stroudsburg to /* Drive from Stroudsburg to NYC. */ NYC. */ }

CS 100Lecture 57 Sequential Refinement, cont. n Example 3 Let X 1,…,X n be an ordered sequence of variables. Let X 1,…,X n be an ordered sequence of variables. Let k be an integer between 1 and n. Let k be an integer between 1 and n. /* Rotate the values in X 1,…,X n left k places, where values shifted off the left end reenter at the right. */ E.g., suppose k = 3, n = 7, and the x’s contain letters. Before: x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 1 x 2 x 3 x 4 x 5 x 6 x 7 a b c d e f g a b c d e f g After: x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 1 x 2 x 3 x 4 x 5 x 6 x 7 d e f g a b c d e f g a b c

CS 100Lecture 58 Sequential Refinement, cont. /* Rotate the values in X 1,…,X n left k places, where values shifted off the left end reenter at the right. */ { /* Reverse the order of X 1,…,X k */ /* Reverse the order of X 1,…,X k */ /* Reverse the order of X k+1,…,X n */ /* Reverse the order of X k+1,…,X n */ /* Reverse the order of X 1,…,X n */ /* Reverse the order of X 1,…,X n */ }

CS 100Lecture 59 Sequential Refinement, cont. n Example 3, continued E.g., suppose k = 3 and n = 7 x 1 x 2 x 3 x 4 x 5 x 6 x 7 x 1 x 2 x 3 x 4 x 5 x 6 x 7 a b c d e f g a b c d e f g c b a d e f g c b a d e f g c b a g f e d c b a g f e d d e f g a b c d e f g a b c time 

CS 100Lecture 510 Case Analysis n The refinement is structured so that solving one of the subproblems P 1,…,P n solves P. Which P i is solved is determined during execution by testing conditions. /* Solve problem P */ if (P is an instance of case 1) /* Solve subproblem 1 */ /* Solve subproblem 1 */ else if (P is an instance of case 2) /* Solve subproblem 2 */ /* Solve subproblem 2 */ else if (P is an instance of case 3) /* Solve subproblem 3 */ /* Solve subproblem 3 */ else /* P is an instance of case n */ /* Solve subproblem n */ /* Solve subproblem n */......

CS 100Lecture 511 Case Analysis, cont. n Example 1 /* Let x be the absolute value of y. */ if ( y < 0 ) if ( y < 0 ) /* Let x be -y. */ /* Let x be -y. */ else else /* Let x be y. */ /* Let x be y. */ n Example 2 /* Let x be the absolute value of y. */ x = Math.abs(y); x = Math.abs(y); I.e., sometimes case analysis is counter-productive and there is a uniform way to solve the problem.

CS 100Lecture 512 Iterative Refinement n The refinement of P is structured so that repeated solution of subproblem P’ eventually solves P. /* Solve problem P */ while (P has not yet been solved) while (P has not yet been solved) /* Solve subproblem P’ */ /* Solve subproblem P’ */ n Question: How can repeatedly doing P’ solve P ? n Answer: P’ must be parameterized in terms of some state. Each execution of P’ must change the state so that progress is made, i.e., with each iteration, we move to a state that is “closer” to a solution for P. n The notion of “distance” must be well-founded, i.e., it must converge to 0 in a finite number of steps that get “closer”. n Different notions of “distance” lead to different programs.

CS 100Lecture 513 Iterative Refinement, cont. n Example: Running a maze /* There are n 2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper-leftmost room facing left. A sequence of doors leads to the lower- rightmost room. Get there. */ n Rule of thumb: Work some test cases by hand. Inspiration: Keep your left hand on the wall and keep walking (the left-hand rule).

CS 100Lecture 514 Iterative Refinement, cont. /* There are n 2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper- leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ while ( you are not in lower rightmost room ) while ( you are not in lower rightmost room ) /* Get “closer” to lower rightmost room. */ /* Get “closer” to lower rightmost room. */ n Different notions of “closer” lead to different versions of /* Get “closer” to lower rightmost room. */ /* Get “closer” to lower rightmost room. */ Notion A: wall length away, using the “left-hand rule”. Notion B: # of rooms away, using the “left-hand rule”.

CS 100Lecture 515 Iterative Refinement, cont. n Refinement A (using wall-length distance) /* There are n 2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper- leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ while ( you are not in lower rightmost room ) while ( you are not in lower rightmost room ) /* Get at least one wall “closer” to /* Get at least one wall “closer” to the lower rightmost room. */ the lower rightmost room. */ if (you are facing a door){ if (you are facing a door){ Go through the door; Go through the door; Turn 90 degrees counter-clockwise; Turn 90 degrees counter-clockwise; } else else Turn 90 degrees clockwise; Turn 90 degrees clockwise;

CS 100Lecture 516 Iterative Refinement, cont. n Refinement B (using #rooms distance) /* There are n 2 rooms arranged in an n-by-n grid. Some adjacent rooms have connecting doors. No doors lead outside. You are in the upper- leftmost room facing left. A sequence of doors leads to the lower-rightmost room. Get there. */ while ( you are not in lower rightmost room ) while ( you are not in lower rightmost room ) { /* Get at least one room “closer” to /* Get at least one room “closer” to the lower rightmost room. */ the lower rightmost room. */ while (you are not facing a door) while (you are not facing a door) Turn 90 degrees clockwise; Turn 90 degrees clockwise; Go through door; Go through door; Turn 90 degrees counter-clockwise; Turn 90 degrees counter-clockwise; }