ALGORITHM DESIGN:
OPTIMIZATION USING RECURSION
COMPUTER SCIENCE HL
NANJING FOREIGN LANGUAGE
FERRY KEMPERMAN
APRIK 2019
ALGORITHM DESIGN: THE THINKING PROCESS
• Algorithm design is designing a solution for a complex problem that is
……
• Non-ambiguous
• Executable
• Stepwise based
• Flow controlled
• Optimized for efficiency!
• What does this mean?
OPTIMIZED ALGORITHM DESIGN → EFFICIENCY
• An algorithm is considered designed well if
• 1. The algorithm works according to specs
• 2. The algorithm works as efficient as possible.
• Optimization is the process needed to design an
efficient algorithm.
WHAT IS AN EFFICIENT ALGORITHM EXACTLY?
• An efficient algorithm need to meet these three requirements:
• 1. A minimal number of instructions used to execute the algorithm
• 2. The minimal amount of RAM used during execution of the algorithm
• 3. The fastest retrieval of data and instructions from RAM to the CPU
possible.
• An optimized algorithm = an algorithm that is designed in such a way that
it meets these requirements.
• To put it simply: a non-optimized algorithm works correctly, but is slow
and resources heavy (CPU+RAM!).
TECHNIQUES TO DESIGN EFFICIENT ALGORITHMS
• So how to design an efficient algorithm?
• You create a flowchart or pseudocode as a design.
• How do you know this design is efficient and optimized?
• Well, how many instructions did you use? Can it be less?
• How does your algorithm store and retrieve data? Variables and
their datatypes? Can you make smarter choices here?
• Is the flow of your algorithm smartly designed? Do you make use
of repetitions (loops) and selection (if/case statements) in such a
way that the number of executed statements is minimized
LET’S COMPARE TWO DESIGNS………
Design 1: Using FOR loop
• DECLARE Factorial : INTEGER
• DECLARE Terms : INTEGER
• Factorial  0
• Terms  0
• INPUT “Specify N to calculate N!” Terms
• FOR Counter  1 TO Terms
• Factorial  Factorial * Counter
• ENDFOR
• OUTPUT “The factorial “ + Terms + “! = “ + Factorial
Design 2: Using WHILE loop
• DECLARE Factorial : INTEGER
• DECLARE Terms : INTEGER
• Factorial  0
• Terms  0
• Counter  0
• INPUT “Specify N to calculate N!” Terms
• WHILE Counter <= Terms
• Factorial  Factorial * Terms
• Counter  Counter + 1
• ENDWHILE
• OUTPUT “The factorial “ + Terms + “! = “ + Factorial
OR AN EVEN MORE EFFICIENT DESIGN…..USING
RECURSION!
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
• DECLARE Terms : INTEGER
• DECLARE Factorial : INTEGER
• INPUT “Give me a number N to calculate N!” Terms
• Factorial  CalculateFactorial(Terms)
• OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
LET’S BREAK IT DOWN
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
• On the right I have defined a function.
• A function is a top-down design concept that I am
going to explain in SL later on.
• A function is technically speaking a reusable code
and it follows the mathematical definition of a
function:
• Input → Function (process) → Output
• A program calls a function by its name and the
code inside the function is then executed.
• A function is comprised of four parts:
• Name → A unique identifier call the function
• Body → The indented code inside the function
that will be executed on function call
• Input parameter(s) → What values does the
function need to process?
• Return value → What is the output value of the
function?
• IMPORTANT: Parameters and return values have
FIXED datatypes.
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
Datatype
of return
value
Function
name
Input parameter
with datatype
integer: it expects
an integer!
Entire first
line: function
header
Function
body
HOW TO USE THE FUNCTION? THE FUNCTION CALL
• A function is simply used by referencing his name with the correct input.
• This is called a function call.
• Let’s take look at my earlier code:
• DECLARE Terms : INTEGER
• DECLARE Factorial : INTEGER
• INPUT “Give me a number N to calculate N!” Terms
• Factorial  CalculateFactorial(Terms)
• OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
The function is
called here by
this statement!
Let’s have a look
on how this
works….
Main program
• DECLARE Terms : INTEGER
• DECLARE Factorial : INTEGER
• INPUT “Give me a number N to calculate N!” Terms
• Factorial  CalculateFactorial(Terms)
• OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
Function definition
• FUNCTION CalculateFactorial (N: INTEGER) RETURNS
INTEGER
• IF N = 1
• THEN RETURN 1
• ELSE RETURN N * CalculateFactorial(N-1)
• ENDIF
• ENDFUNCTION
Execute the main.
User enters 3 to calculate 3!, variable Terms will contain 3.
Factorial  CalculateFactorial(3) // the function call
How to evaluate CalculateFactorial(3)?
Execute the body of the function with input parameter 3, so N=3 inside the function!
IF N=1, nope, so ELSE part executed….RETURN N * CalculateFactorial(N-1), but N=3 here.
So…..RETURN 3 * CalculateFactorial(3-1)……but the function is called again, by itself!!!! This is called recursion!
Let’s continue……how to evaluate CalculateFactorial(3-1)? Well, do the same. CalculateFactorial(2)…..
N=1, nope, so ELSE part….RETURN N+CalculateFactorial(N-1), with N=2 this time. RETURN 2*CalculateFactorial(1)….
And again…….CalculateFactorial(1), so N=1…then RETURN 1…….Which make 3*2*1 = 6, which is 3!, stored in
Factorial variable.
RECURSION, AN DESIGN STRATEGY
• Recursion is a design technique in which you design a function that calls itself.
• Every time a function calls itself we call this an incarnation.
• Recursive design is not easy! You need to recognize patterns that are recurring
to implement recursion.
• Recursion can optimize your algorithm’s efficiency significantly.
• No need for repetitive code.
• Recursion is not looping! What is the difference?
• Incarnation depth for recursion is sometimes limited depending on the
platform.
APPLICATIONS OF RECURSIVE DESIGN IN CS AND MATH:
FRACTALS
• FRACTALS: Patterns that repeat
• themselves on a smaller scale each
• time the function calls itself….
• A very famous one in math is
• The Mandelbrot Set in the picture.
• Koch curves, binary trees etc.
• Even….CS Art!!!

Algorithm Design: Optimization using recursion.

  • 1.
    ALGORITHM DESIGN: OPTIMIZATION USINGRECURSION COMPUTER SCIENCE HL NANJING FOREIGN LANGUAGE FERRY KEMPERMAN APRIK 2019
  • 2.
    ALGORITHM DESIGN: THETHINKING PROCESS • Algorithm design is designing a solution for a complex problem that is …… • Non-ambiguous • Executable • Stepwise based • Flow controlled • Optimized for efficiency! • What does this mean?
  • 3.
    OPTIMIZED ALGORITHM DESIGN→ EFFICIENCY • An algorithm is considered designed well if • 1. The algorithm works according to specs • 2. The algorithm works as efficient as possible. • Optimization is the process needed to design an efficient algorithm.
  • 4.
    WHAT IS ANEFFICIENT ALGORITHM EXACTLY? • An efficient algorithm need to meet these three requirements: • 1. A minimal number of instructions used to execute the algorithm • 2. The minimal amount of RAM used during execution of the algorithm • 3. The fastest retrieval of data and instructions from RAM to the CPU possible. • An optimized algorithm = an algorithm that is designed in such a way that it meets these requirements. • To put it simply: a non-optimized algorithm works correctly, but is slow and resources heavy (CPU+RAM!).
  • 5.
    TECHNIQUES TO DESIGNEFFICIENT ALGORITHMS • So how to design an efficient algorithm? • You create a flowchart or pseudocode as a design. • How do you know this design is efficient and optimized? • Well, how many instructions did you use? Can it be less? • How does your algorithm store and retrieve data? Variables and their datatypes? Can you make smarter choices here? • Is the flow of your algorithm smartly designed? Do you make use of repetitions (loops) and selection (if/case statements) in such a way that the number of executed statements is minimized
  • 6.
    LET’S COMPARE TWODESIGNS……… Design 1: Using FOR loop • DECLARE Factorial : INTEGER • DECLARE Terms : INTEGER • Factorial  0 • Terms  0 • INPUT “Specify N to calculate N!” Terms • FOR Counter  1 TO Terms • Factorial  Factorial * Counter • ENDFOR • OUTPUT “The factorial “ + Terms + “! = “ + Factorial Design 2: Using WHILE loop • DECLARE Factorial : INTEGER • DECLARE Terms : INTEGER • Factorial  0 • Terms  0 • Counter  0 • INPUT “Specify N to calculate N!” Terms • WHILE Counter <= Terms • Factorial  Factorial * Terms • Counter  Counter + 1 • ENDWHILE • OUTPUT “The factorial “ + Terms + “! = “ + Factorial
  • 7.
    OR AN EVENMORE EFFICIENT DESIGN…..USING RECURSION! • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION • DECLARE Terms : INTEGER • DECLARE Factorial : INTEGER • INPUT “Give me a number N to calculate N!” Terms • Factorial  CalculateFactorial(Terms) • OUTPUT “The factorial of “ + Terms + “! = “ + Factorial
  • 8.
    LET’S BREAK ITDOWN • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION • On the right I have defined a function. • A function is a top-down design concept that I am going to explain in SL later on. • A function is technically speaking a reusable code and it follows the mathematical definition of a function: • Input → Function (process) → Output • A program calls a function by its name and the code inside the function is then executed. • A function is comprised of four parts: • Name → A unique identifier call the function • Body → The indented code inside the function that will be executed on function call • Input parameter(s) → What values does the function need to process? • Return value → What is the output value of the function? • IMPORTANT: Parameters and return values have FIXED datatypes.
  • 9.
    • FUNCTION CalculateFactorial(N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION Datatype of return value Function name Input parameter with datatype integer: it expects an integer! Entire first line: function header Function body
  • 10.
    HOW TO USETHE FUNCTION? THE FUNCTION CALL • A function is simply used by referencing his name with the correct input. • This is called a function call. • Let’s take look at my earlier code: • DECLARE Terms : INTEGER • DECLARE Factorial : INTEGER • INPUT “Give me a number N to calculate N!” Terms • Factorial  CalculateFactorial(Terms) • OUTPUT “The factorial of “ + Terms + “! = “ + Factorial The function is called here by this statement! Let’s have a look on how this works….
  • 11.
    Main program • DECLARETerms : INTEGER • DECLARE Factorial : INTEGER • INPUT “Give me a number N to calculate N!” Terms • Factorial  CalculateFactorial(Terms) • OUTPUT “The factorial of “ + Terms + “! = “ + Factorial Function definition • FUNCTION CalculateFactorial (N: INTEGER) RETURNS INTEGER • IF N = 1 • THEN RETURN 1 • ELSE RETURN N * CalculateFactorial(N-1) • ENDIF • ENDFUNCTION Execute the main. User enters 3 to calculate 3!, variable Terms will contain 3. Factorial  CalculateFactorial(3) // the function call How to evaluate CalculateFactorial(3)? Execute the body of the function with input parameter 3, so N=3 inside the function! IF N=1, nope, so ELSE part executed….RETURN N * CalculateFactorial(N-1), but N=3 here. So…..RETURN 3 * CalculateFactorial(3-1)……but the function is called again, by itself!!!! This is called recursion! Let’s continue……how to evaluate CalculateFactorial(3-1)? Well, do the same. CalculateFactorial(2)….. N=1, nope, so ELSE part….RETURN N+CalculateFactorial(N-1), with N=2 this time. RETURN 2*CalculateFactorial(1)…. And again…….CalculateFactorial(1), so N=1…then RETURN 1…….Which make 3*2*1 = 6, which is 3!, stored in Factorial variable.
  • 13.
    RECURSION, AN DESIGNSTRATEGY • Recursion is a design technique in which you design a function that calls itself. • Every time a function calls itself we call this an incarnation. • Recursive design is not easy! You need to recognize patterns that are recurring to implement recursion. • Recursion can optimize your algorithm’s efficiency significantly. • No need for repetitive code. • Recursion is not looping! What is the difference? • Incarnation depth for recursion is sometimes limited depending on the platform.
  • 14.
    APPLICATIONS OF RECURSIVEDESIGN IN CS AND MATH: FRACTALS • FRACTALS: Patterns that repeat • themselves on a smaller scale each • time the function calls itself…. • A very famous one in math is • The Mandelbrot Set in the picture. • Koch curves, binary trees etc. • Even….CS Art!!!