1 / 28

Introduction to algorithm design and recursion

Introduction to algorithm design and recursion. CS125 Spring 2007 Arthur Kantor. Algorithm. An algorithm is a computational process for solving a problem. computational : a computer must be able to perform the steps of the process

langston
Download Presentation

Introduction to algorithm design and recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor

  2. Algorithm • An algorithm is a computational process for solving a problem. • computational: a computer must be able to perform the steps of the process • This concept was formalized by Alan Turing and Alonzo Church in the 1930ies • One of the great results of computer science

  3. Algorithm • An algorithm is a computational process for solving a problem. • solving • The algorithm must actually give the correct solution after it terminates • problem • Typically the problem is somewhat general. • E.g. “Sort any list of numbers” instead of “Sort this particular list of numbers”

  4. Algorithm design • The next few weeks we will be talking about designing algorithms to solve problems • We can talk about whether the algorithm is described precisely enough so that it can be translated into a computer program • We can talk about whether the algorithm solves the given problem • We do not need to discuss the actual code

  5. Recursive algorithm • A recursive algorithm solves the problem by possibly using the result of applying itself to a simpler problem • Example: Draw this picture

  6. Another example • Draw this picture

  7. Properties of all recursive algorithms • A recursive algorithm solves the large problem by using its solution to a simpler sub-problem • divide and conquer approach • Eventually the sub-problem is simple enough that it can be solved without applying the algorithm to it recursively • This is called the ‘base case’

  8. Another example • The factorial function: multiply together all numbers from 1 to n. • denoted n! n!=n*(n-1)*(n-2)*…2*1 n!= n*(n-1)! if n>0 1 if n==0

  9. Another example • The factorial function: multiply together all numbers from 1 to n. • denoted n! n!=n*(n-1)*(n-2)*…2*1 n!= n*(n-1)! if n>0 1 if n==0 • General case: Uses a solution to a simpler sub-problem • Base case: Solution is given directly

  10. n!= n*(n-1)! if n>0 • 1 if n==0 4! Walk-through 4!=

  11. n!= n*(n-1)! if n>0 • 1 if n==0 Java implementation of n! public int factorial(int n){ if (n==0) return 1; else return n*factorial(n-1); }

  12. public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  13. n=4 Returns 4*factorial(3) • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  14. n=4 Returns 4*factorial(3) n=3 Returns 3*factorial(2) • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  15. n=4 Returns 4*factorial(3) n=3 Returns 3*factorial(2) n=2 Returns 2*factorial(1) • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  16. n=4 Returns 4*factorial(3) n=1 Returns 1*factorial(0) n=3 Returns 3*factorial(2) n=2 Returns 2*factorial(1) • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  17. n=4 Returns 4*factorial(3) n=0 Returns 1 n=1 Returns 1*factorial(0) n=3 Returns 3*factorial(2) n=2 Returns 2*factorial(1) • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  18. n=4 Returns 4*factorial(3) n=1 Returns 1*factorial(0) n=3 Returns 3*factorial(2) n=2 Returns 2*factorial(1) 1 • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  19. n=4 Returns 4*factorial(3) n=3 Returns 3*factorial(2) n=2 Returns 2*factorial(1) 1 • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  20. n=4 Returns 4*factorial(3) n=3 Returns 3*factorial(2) 2 • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  21. n=4 Returns 4*factorial(3) 6 • public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4)

  22. public int factorial(int n){ • if (n==0) • return 1; • else • return n*factorial(n-1); • } factorial(4); factorial(4) 24

  23. n=4 Returns 4*factorial(3) n=3 Returns 3*factorial(2) How the computer does it • The computer remembers the entire stack of partially completed function calls • The execution of caller is paused until the called function returns with its answer Execution of factorial(4) is paused until factorial(3) returns with its answer Once factorial(3) returns, execution of factorial(4) continues

  24. Why recursion? • We could have implemented n! using a while loop (how?) • In general, any recursively defined function can be re-implmented with a while loop. • Sometimes it is much more natural to specify a function recursively than with a loop

  25. Another example • prod(list) returns a product of all the numbers in the list • prod({1,3,3,4}) should return 36

  26. Implementation of prod() float prod(float[] list){ return helperProd(list, 0); } float helperProd(float[] list, int k){ if (k==list.length) return 1; else return list[k]*helperProd(list,k+1); }

  27. float helperProd(float[] list, int k){ if (k==list.length) return 1; else return list[k]*helperProd(list,k+1); } walkthrough helperProd({1,3,3,4},0)

  28. Recursion in real life

More Related