EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Slide 1 Insert your own content. Slide 2 Insert your own content.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
1 Chapter 40 - Physiology and Pathophysiology of Diuretic Action Copyright © 2013 Elsevier Inc. All rights reserved.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Chapters 1 & 2 Theorem & Postulate Review Answers
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Coordinate Plane Practice The following presentation provides practice in two skillsThe following presentation provides practice in two skills –Graphing.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Teacher Name Class / Subject Date A:B: Write an answer here #1 Write your question Here C:D: Write an answer here.
Addition Facts
BALANCING 2 AIM: To solve equations with variables on both sides.
ZMQS ZMQS
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
O X Click on Number next to person for a question.
© S Haughton more than 3?
5.9 + = 10 a)3.6 b)4.1 c)5.3 Question 1: Good Answer!! Well Done!! = 10 Question 1:
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
1 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I.
In the game Twenty Questions, the instructor creates a PowerPoint slide with hyperlinks to separate slides containing questions. The Instructor or a student.
Twenty Questions Subject: Twenty Questions
Take from Ten First Subtraction Strategy -9 Click on a number below to go directly to that type of subtraction problems
Linking Verb? Action Verb or. Question 1 Define the term: action verb.
Squares and Square Root WALK. Solve each problem REVIEW:
Energy & Green Urbanism Markku Lappalainen Aalto University.
Do you have the Maths Factor?. Maths Can you beat this term’s Maths Challenge?
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
This, that, these, those Number your paper from 1-10.
Properties of Exponents
1 First EMRAS II Technical Meeting IAEA Headquarters, Vienna, 19–23 January 2009.
Event 4: Mental Math 7th/8th grade Math Meet ‘11.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
1 Ke – Kitchen Elements Newport Ave. – Lot 13 Bethesda, MD.
1 Unit 1 Kinematics Chapter 1 Day
FIND THE AREA ( ROUND TO THE NEAREST TENTHS) 2.7 in 15 in in.
O X Click on Number next to person for a question.
Copyright © 2012 Pearson Education, Inc. Chapter 14: More About Classes.
Introduction to Recursion and Recursive Algorithms
Lecture 12 Recursion part 1
 2003 Prentice Hall, Inc. All rights reserved. 1 Recursion Recursive functions –Functions that call themselves –Can only solve a base case If not base.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
Program Development and Design Using C++, Third Edition
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Topic 6 Recursion.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

Recursion  Recursive functions –Functions that call themselves –Can only solve a base case  If not base case –Break problem into smaller problem(s) –Launch new copy of function to work on the smaller problem (recursive call/recursive step) Slowly converges towards base case Function makes call to itself inside the return statement –Eventually base case gets solved Answer works way back up, solves entire problem

Recursion  Example: factorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 –Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… –Base case (1! = 0! = 1)

1 2 // Recursive factorial function. 3 #include #include 9 10 using std::setw; unsigned long factorial( unsigned long ); // function prototype int main() 15 { 16 // Loop 10 times. During each iteration, calculate 17 // factorial( i ) and display result. 18 for ( int i = 0; i <= 10; i++ ) 19 cout << setw( 2 ) << i << "! = " 20 << factorial( i ) << endl; return 0; // indicates successful termination } // end main Data type unsigned long can hold an integer from 0 to 4 billion.

25 26 // recursive definition of function factorial 27 unsigned long factorial( unsigned long number ) 28 { 29 // base case 30 if ( number <= 1 ) 31 return 1; // recursive step 34 else 35 return number * factorial( number - 1 ); } // end function factorial 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = ! = ! = ! = The base case occurs when we have 0! or 1!. All other cases must be split up (recursive step).

Example Using Recursion: Fibonacci Series  Fibonacci series: 0, 1, 1, 2, 3, 5, 8... –Each number sum of two previous ones –Example of a recursive formula: fib(n) = fib(n-1) + fib(n-2)  C++ code for Fibonacci function long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else return fibonacci( n - 1 ) + fibonacci( n – 2 ); }

Example Using Recursion: Fibonacci Series f( 3 ) f( 1 ) f( 2 ) f( 1 )f( 0 )return 1 return 0 return + +

Example Using Recursion: Fibonacci Series  Order of operations – return fibonacci( n - 1 ) + fibonacci( n - 2 );  Do not know which one executed first –C++ does not specify –Only &&, || and ?: guaranteed left-to-right evaluation  Recursive function calls –Each level of recursion doubles the number of function calls 30 th number = 2^30 ~ 4 billion function calls –Exponential complexity

1 2 // Recursive fibonacci function. 3 #include unsigned long fibonacci( unsigned long ); // function prototype int main() 12 { 13 unsigned long result, number; // obtain integer from user 16 cout << "Enter an integer: "; 17 cin >> number; // calculate fibonacci value for number input by user 20 result = fibonacci( number ); // display result 23 cout << "Fibonacci(" << number << ") = " << result << endl; return 0; // indicates successful termination The Fibonacci numbers get large very quickly, and are all non-negative integers. Thus, we use the unsigned long data type.

26 27 } // end main // recursive definition of function fibonacci 30 unsigned long fibonacci( unsigned long n ) 31 { 32 // base case 33 if ( n == 0 || n == 1 ) 34 return n; // recursive step 37 else 38 return fibonacci( n - 1 ) + fibonacci( n - 2 ); } // end function fibonacci Enter an integer: 0 Fibonacci(0) = 0 Enter an integer: 1 Fibonacci(1) = 1 Enter an integer: 2 Fibonacci(2) = 1 Enter an integer: 3 Fibonacci(3) = 2

Enter an integer: 4 Fibonacci(4) = 3 Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 10 Fibonacci(10) = 55 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = Enter an integer: 35 Fibonacci(35) =

Recursion vs. Iteration  Repetition –Iteration: explicit loop –Recursion: repeated function calls  Termination –Iteration: loop condition fails –Recursion: base case recognized  Both can have infinite loops  Balance between performance (iteration) and good software engineering (recursion)