Problem Solving 4 Algorithms, Problem Solving and Recursion ICS-201 Introduction to Computing II Semester 071.

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Computer Science II Recursion Professor: Evan Korth New York University.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 19 Recursion.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
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.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Unit 7 1 Unit 7: Recursion H Recursion is a fundamental programming technique that can provide an elegant solution to a certain kinds of problems H In.
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.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
COSC 2006 Data Structures I Recursion II
Chapter 12 Recursion, Complexity, and Searching and Sorting
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1Recursion. 2 Outline thinking recursively recursive algorithms iteration vs. recursion recursive functions integer exponentiation (pow) infinite recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
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.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Program Development and Design Using C++, Third Edition
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Function Recursion to understand recursion you must understand recursion.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 9: introduction to recursion reading: 12.1
Topic 6 Recursion.
Data Structures with Java © Rick Mercer
Chapter 15 Recursion.
Building Java Programs
Recursion DRILL: Please take out your notes on Recursion
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Chapter 15 Recursion.
Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
slides adapted from Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Recursion Chapter 11.
slides created by Marty Stepp and Alyssa Harding
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp
Building Java Programs
Presentation transcript:

Problem Solving 4 Algorithms, Problem Solving and Recursion ICS-201 Introduction to Computing II Semester 071

Problem Solving Consider the following problem: Given a list of elements (as an array), find and print all elements that are repeated at least two or more times in the list. In other words find all elements that are not unique in the list.

Solution – Think of all possibilities Before we start forming solutions of the problem, let us think what possible lists can we be asked to look into. Case I: An array with no repeated elements. Case II: An array with one element repeated two or more times

Solution (contd.) Case III: An array with multiple elements repeated multiple number of times. After having a look at these cases, we can suggest see that the form of the solution will be an array of frequencies of repeated elements, for example for the above array. The above solution is of course, not perfect, but just a first attempt

Algorithm – Pseudo code Outline Hence we can formulate an outline of our algorithm. Let us call it Algorithm countFrequency. Algorithm countFrequency: Input: An array a[] of integers with possibly repeated integers Output: An array of b[] of integers which contains all frequencies of all elements appearing at least two or more times in a[]. Steps: ???

Solution (contd.) What are the possible steps of the algorithm? An obvious approach would be to count the frequency of each element in the array using another auxiliary array. This can be done using two for-loops through the array. So, for each element in a[ ], let us scan through all of a[ ] again, and if we find that element at another position in a[ ], let us increment the frequency of that element in b[ ].

Algorithm Pseudo-code Algorithm countFrequency: Input: An array a[] of integers with possibly repeated integers Output: An array of b[] of integers which contains all frequencies of all elements appearing at least two or more times in a[]. Steps: 1. for each element in a[ ], 2. scan through all of a[ ] again, 3. if we find that element at another position in a[ ], 4. Increment the frequency of that element in b[ ].

Algorithm countFrequency This approach is summarized as the following program: 1. public class CountFrequency 2. { 3. public static void main(String[] args) 4. { 5. int[] a = {1, 3, 1, 5, 6, 3, 2, 2, 7, 8}; 6. int[] b = new int[a.length]; 7. int val; //Initialize b to 1 for each element 10. for(int i = 0; i < a.length; i++) 11. b[i] = 1; 12.

Solution (contd.) 13. //Search for each element of a in the rest of a 14. for(int i = 0; i < a.length; i++) 15. {val = a[i]; 16. for(int j = 0; j < a.length; j++) 17. if(val == a[j]) b[i]++; 18. } //Print frequencies 21. System.out.println("val\tFrequency"); 22. for(int i = 0; i < b.length; i++) 23. if(b[i] > 0) 24. System.out.println(a[i] + "\t" + b[i]); 25. }}

The Output It can be seen that the program repeats output. So for example, in the array {1, 3, 1, 3, 3, 3, 2, 2, 7, 8} 3 appears 4 times, so in the output also 3 appears 4 times. 2 appears 2 times, so in the output also 2 appears 2 times. How can we improve this scenario?

Refining the Program Can you think of other approaches to the countFrequency problem? Can we use linear search or binary search to refine the output so that non-unique elements appear exactly once in the program? Can we use sorting techniques to speed up our algorithm, so that we can count non-unique elements much faster in a sorted array?

Recursion: Finding Length of A String Problem: Find a recursive algorithm for computing the length of a string. For example lengthString(“Work”) should return 4.

Solution – Possible Approach How can we recursively find the length of a string, for example “solution” recursively? Recall that recursive solution consists of two steps: – A Base Case – A Recursive Step What will be the base case here and what will be the inductive step?

Base and Recursive Cases The Base Case: An empty string has length 0. – lengthString(“”) = 0. The Recursive Step: The length of a string of n characters is equal to 1 + the length of the string of remaining n – 1 characters. Hence – lengthString(“Alpha”) = 1 + lengthString(“lpha”). – lengthString(“lpha”) = 1 + lengthString(“pha”) – lengthString(“pha”) = 1 + lengthString(“ha”) – lengthString(“ha”) = 1 + lengthString(“a”) – lengthString(“a”) = 1 + lengthString(“”) – we reach the base case

Combining the solutions So we can trace our steps backward and yield – lengthString(“a”) = 1 + lengthString(“”) = = 1 – lengthString(“ha”) = 1 + lengthString(“a”) = = 2 – lengthString(“pha”) = 1 + lengthString(“ha”) = = 3 – lengthString(“lpha”) = 1 + lengthString(“pha”) = = 4 – lengthString(“Alpha”) = 1 + lengthString(“lpha”) = = 5

Solution The following program shows the code for recursive lengthString. 1. public static int lengthString(String s) 2. { 3. if(s.equals(“”)) 4. return 0; 5. else 6. return 1 + lengthString(s.substring(1)); 7. }}

Iterative Version Of lengthString Consider converting lengthString to an iterative algorithm. We need to replace the selection structure (the “if” statement) with a loop. We also need to use a variable to store partial result of length of the String.

Iterative Version The following program implements iterativelengthString. 1. public static String iterativelengthString(String s) 2. { 3. int length = 0; //base case 4. for(int i = 0; !(s.substring(i).equals(“”)); i++) 5. { 6. length++; 7. } 8. return length; 9. }

Output 1. public static void main(String[] args) 2. { 3. System.out.println("Length of \"Alpha\" Recursively = " 4. + lengthString("Alpha")); 5. System.out.println("Length of \"Alpha\" Iteratively = " + iterativelengthString("Alpha")); 6. }

20 Recursive power example Write method pow that takes integers x and y as parameters and returns x y. x y = x * x * x *... * x (y times, in total) An iterative solution: // not recursive public static int pow(int x, int y) { int product = 1; for (int i = 0; i < y; i++) { product = product * x; } return product; }

Recursive power function Another way to define the power function: pow(x, 0) = 1 pow(x, y) = x * pow(x, y-1), y > 0 // recursive public static int pow(int x, int y) { if (y == 0) { return 1; } else { return x * pow(x, y - 1); }

22 How recursion works Each call sets up a new instance of all the parameters and the local variables As always, when the method completes, control returns to the method that invoked it (which might be another invocation of the same method) pow(4, 3) = 4 * pow(4, 2) = 4 * 4 * pow(4, 1) = 4 * 4 * 4 * pow(4, 0) = 4 * 4 * 4 * 1 = 64

23 Infinite recursion a definition with a missing or badly written base case causes infinite recursion, similar to an infinite loop – avoided by making sure that the recursive call gets closer to the solution (moving toward the base case) public static int pow(int x, int y) { return x * pow(x, y - 1); // Oops! Forgot base case } pow(4, 3) = 4 * pow(4, 2) = 4 * 4 * pow(4, 1) = 4 * 4 * 4 * pow(4, 0) = 4 * 4 * 4 * 4 * pow(4, -1) = 4 * 4 * 4 * 4 * 4 * pow(4, -2) =... crashes: Stack Overflow Error!

24 Activation records activation record: memory that Java allocates to store information about each running method – return point ("RP"), argument values, local variable values – Java stacks up the records as methods are called; a method's activation record exists until it returns – drawing the act. records helps us trace the behavior of a recursive method _ | x = [ 4 ] y = [ 0 ] | pow(4, 0) | RP = [pow(4,1)] | | x = [ 4 ] y = [ 1 ] | pow(4, 1) | RP = [pow(4,2)] | | x = [ 4 ] y = [ 2 ] | pow(4, 2) | RP = [pow(4,3)] | | x = [ 4 ] y = [ 3 ] | pow(4, 3) | RP = [main] | | | main

25 Tracing recursive methods Consider the following method: public static int mystery1(int x, int y) { if (x < y) { return x; } else { return mystery1(x - y, y); } For each call below, indicate what value is returned: mystery1(6, 13)____________ mystery1(14, 10)____________ mystery1(37, 10)____________ mystery1(8, 2)____________ mystery1(50, 7)____________

Tracing recursive methods public static void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } For each call below, indicate what output is printed: mystery2(1)____________ mystery2(2)____________ mystery2(3)____________ mystery2(4)____________ mystery2(16)____________ mystery2(30)____________ mystery2(100)____________

27 Tracing recursive methods public static int mystery3(int n) { if (n < 0) { return -mystery3(-n); } else if (n < 10) { return n; } else { return mystery3(n/10 + n % 10); } For each call below, indicate what value is returned: mystery3(6)____________ mystery3(17)____________ mystery3(259)____________ mystery3(977)____________ mystery3(-479)____________

Tracing recursive methods public static void mystery4(String s) { if (s.length() > 0) { System.out.print(s.charAt(0)); if (s.length() % 2 == 0) { mystery4(s.substring(0, s.length() - 1)); } else { mystery4(s.substring(1, s.length())); } System.out.print(s.charAt(s.length() - 1)); } For each call below, indicate what output is printed: mystery4("") ____________ mystery4("a") ____________ mystery4("ab") ____________ mystery4("bc") ____________ mystery4("abcd") ____________

29 Recursive numeric problems Problem: Given a decimal integer n and a base b, print n in base b. (Hint: consider the / and % operators to divide n.) Problem: Given integers a and b where a >= b, find their greatest common divisor ("GCD"), which is the largest number that is a factor of both a and b. Use Euclid's formula, which states that: GCD(a, b) = GCD(b, a MOD b) GCD(a, 0) = a (Hint: What should the base case be?)

30 Recursive printing problem Problem: Write a method starString that takes an integer n as an argument and returns a string of stars (asterisks) 2 n long (i.e., 2 to the nth power). For example: starString(0) should return "*" (because 2^0 == 1) starString(1) should return "**" (because 2^1 == 2) starString(2) should return "****" (because 2^2 == 4) starString(3) should return "********" (because 2^3 == 8) starString(4) should return "****************" (2^4 == 16)

31 Recursive string problems Problem: Write a recursive method isPalindrome that takes a string and returns whether the string is the same forwards as backwards. (Hint: examine the end letters.) Problem: Write a recursive method areAnagrams that takes two strings w1 and w2 and returns whether they are anagrams of each other; that is, whether the letters of w1 can be rearranged to form the word w2.

32 Recursion can perform badly The Fibonacci numbers are a sequence of numbers F 0, F 1,... F n such that: F 0 = F 1 = 1 F i = F i-1 + F i-2 for any i > 1 Problem: Write a method fib that, when given an integer i, computes the i th Fibonacci number. Why might a recursive solution to this problem be a bad idea? (Let's write it...) – Can we fix it? If so, how?

33 Revisiting Fibonacci... recursive Fibonacci was expensive because it made many, many recursive calls – fibonacci(n) recomputed fibonacci(n ) many times in finding its answer! – this is a common case of "overlapping subproblems" or "divide poorly and reconquer", where the subtasks handled by the recursion are redundant with each other and get recomputed

34 Recursive graphics reading: 12.4

35 Fractal images fractal: A mathematically generated, self- similar image. – Created by B. Mandelbrot in 1975 – Many can be drawn elegantly using recursive algorithms

Sample Fractals

37 A fractal can be drawn at many different levels. – Each level is another layer of self-similarity. – The larger figure is decomposed into smaller occurrences of the same figure. – The smaller figures can themselves be decomposed, and so on. – Let's write a program to draw the fractal below, an image called the Sierpinski Triangle. Fractal levels

How to generate a Sierpinski Triangle

39 Fractal code We can write a recursive method to draw the triangle figure at a certain level. public static void drawFigure(int level, Graphics g) {... } – The recursive aspect is that drawFigure for a given level should call itself for other levels as part of its work. – But these smaller levels appear at different positions, so we should require the triangle's 3 corner points as parameters. public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) {... }

40 The base case As usual, we begin with the base case. – The easiest figure to draw is a triangle at level 1. – We can use the fillPolygon method of Graphics g to do this. public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles... }

41 Thinking recursively A key observation: The end points needed to draw the smaller triangles (p4, p5, p6) are the midpoints between the larger triangle's endpoints (p1, p2, p3). – We can write a method to compute the midpoint between two Point objects. // returns the point halfway between p1 and p2 public static Point midpoint(Point p1, Point p2) { return new Point( (p1.x + p2.x) / 2, (p1.y + p2.y) / 2); }

42 Complete solution method public static void drawFigure(int level, Graphics g, Point p1, Point p2, Point p3) { if (level == 1) { // base case: simple triangle Polygon p = new Polygon(); p.addPoint(p1.x, p1.y); p.addPoint(p2.x, p2.y); p.addPoint(p3.x, p3.y); g.fillPolygon(p); } else { // recursive case, split into 3 triangles Point p4 = midpoint(p1, p2); Point p5 = midpoint(p2, p3); Point p6 = midpoint(p1, p3); // recurse on 3 triangular areas drawFigure(level - 1, g, p1, p4, p6); drawFigure(level - 1, g, p4, p2, p5); drawFigure(level - 1, g, p6, p5, p3); }}