CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n,

Slides:



Advertisements
Similar presentations
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Advertisements

Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
CS 202 Computer Science II Lab Fall 2009 October 29.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Function Recursion Examples “an example is worth a 1000 lectures”
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Chapter 4: Induction and Recursion
Recursion in C Computer Organization I 1 September 2009 © McQuain, Feng & Ribbens Recursion Around the year 1900 the illustration of the "nurse"
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Exploring Algorithms Traveling Salesperson Problem I: Brute Force, Greedy, and Heuristics Except as otherwise noted, the content of this presentation is.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
CSC 205 Java Programming II Algorithm Efficiency.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Greatest Common Divisor Jordi Cortadella Department of Computer Science.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
CSC 205 Programming II Lecture 9 More on Recursion.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
RECURSION Go back, Jack, and do it again.. Recursion 2  Recursion occurs whenever "something" is defined in terms of itself.  Structural recursion:
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
Chapter 2: Algorithm Analysis Application of Big-Oh to program analysis Logarithms in Running Time Lydia Sinapova, Simpson College Mark Allen Weiss: Data.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Introduction to Computer Systems
CSCI 125 & 161 Lecture 12 Martin van Bommel. Prime Numbers Prime number is one whose only divisors are the number 1 and itself Therefore, number is prime.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 3. Introduction to the Analysis of Algorithms.
Mathematical Analysis of Recursive Algorithm CSG3F3 Lecture 7.
Chapter 4: Induction and Recursion
Andy Wang Object Oriented Programming in C++ COP 3330
Fundamentals of Algorithms MCS - 2 Lecture # 11
Chapter Topics Chapter 16 discusses the following main topics:
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
CSC 222: Object-Oriented Programming
Introduction to C++ Programming Language
Greatest Common Divisor
Greatest Common Divisor
RECURSION.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Decrease-and-Conquer Approach
Chapter 19 Recursion.
Induction and Recursion
Andy Wang Object Oriented Programming in C++ COP 3330
CS 3343: Analysis of Algorithms
7.2 Recursive Definitions of Mathematical Formulas
Intro to Recursion.
Proving algorithms (programs) correct with induction
Data Structures and Algorithms
CSE 1342 Programming Concepts
CSC 205 Java Programming II
Decrease-and-Conquer
Recursion Chapter 18.
CSE 373 Data Structures and Algorithms
Recursive and Iterative Algorithms
How much does your program take ?
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
Introduction to Algorithms
Searching.
7.2 Recursive Definitions of Mathematical Formulas
Presentation transcript:

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 1 Recursive Function for gcd Recursive formula for the greatest common divisor of m and n, gcd(m, n): if m%n == 0, then gcd(m,n) = n, else gcd(n, m%n); /* iterative version of gcd function */ int GCD(int u, int v){ int temp; while (v!=0){ temp = u % v; u = v; v = temp; } return u; } /* recursive version of gcd function */ int gcd(int m, int n){ int ans; if (m%n == 0) ans = n; else ans = gcd(n, m%n); return ans; }

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 2 Recursive Function for Fibonacci Numbers Fibonacci number is defined by recursively as follows. F(1) = F(2) = 1, F(n) = F(n-1) + F(n-2); int fibonacci(int n) { int ans; if (n == 1 || n == 2) ans = 1; else ans = fibonacci(n - 2) + fibonacci(n - 1); return (ans); }

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 3 Recursive Algorithm for Josephus Problem Josephus problem was presented in lecture 25. Let J(n) be the last position left with n people sitting around a table. It was found that J(n) has the following relation J(1) = 1, J(n) = 2J(n/2) – 1, if n is an even number J(n) = 2J((n-1)/2) + 1, if n is an odd number int josephus(int n) { if (n == 1) return 1; else if (n%2 == 0) return (2*josephus(n/2)-1); else return ( 2*josephus((n-1)/2)+1 ); } Running time is O(log n).

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 4 Brute Force Algorithm for Josephus Problem Int a[n] for (i=0; i<n; ++i) { a[i] = 1; } int Josephus_brute_force(int a[], int n){ int i = 0, j = 0, h = 0; while (j<n-1) { if (a[i] == 1 && h == 1) { a[i] = 0; ++j; h = (h+1)%2; } else if (a[i] == 1 && h != 1) { h = (h+1)%2; } i = (i+1)%n; } for (i = 0; i<n; ++i) { if (a[i] == 1) return i+1; } Running time: the running time for this brute force algorithm is O(nlog n). It is worse than the recursive algorithm O(log n) What about in real running time? See testing. Conclusion: recursive algorithm is far more faster than the brute force algorithm, and the recursive algorithm is much easier to program. But is need more thinking in finding the recursive relation Question: Can we do better?

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 5 A More Efficient Algorithm for Josephus Problem = = a[0]a[1]a[2]a[3]a[4]a[5] J(40) = 2(2(2(2(2J(1)-1)+1)-1)-1)-1) J = 1 For i = 1; i<=5; i++ J = 2*J + (-1+2*a[i]); int super_josephus(int b[], int k) { int i, J = 1, ans; if (k == 1) ans = 1; else { for (i = k-2; i>=0; i--) J = 2*J + 2*b[i]-1; ans = J; } return ans; } The running time of this algorithm is O(log n). But it is about 3 times faster than the recursive algorithm

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 6 Recursive Selection Sort

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 7 Recursive Selection Sort (cont’d)

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 8 Trace of Selection Sort

CP104 Introduction to Programming Recursion 2 Lecture 29 __ 9 Recursive Binary Search Assume a[0], a[1], … …, a[n] is list of integers in increasing order. Design a recursive binary search function binary_sear(int a[], int x, int left, int right){ int ans; if (left == right && x == a[left]) ans = left; else if ( x != a[left]) ans = -1; else { mid = (left + right)/2; if ( x == a[mid]) ans = mid; else if (x > a[mid]) ans = binary_sear(a, x, mid +1, right); else ans = binary_sear(a, x, left, mid); }