introduction to algorithm design and recursion cs125 spring 2007 arthur kantor
TRANSCRIPT
![Page 1: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/1.jpg)
Introduction to algorithm design and recursion
CS125 Spring 2007
Arthur Kantor
![Page 2: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/2.jpg)
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
![Page 3: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/3.jpg)
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”
![Page 4: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/5.jpg)
Recursive algorithm
• A recursive algorithm solves the problem by possibly using the result of applying itself to a simpler problem
• Example: Draw this picture
![Page 6: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/6.jpg)
Another example
• Draw this picture
![Page 7: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/7.jpg)
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’
![Page 8: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/8.jpg)
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
![Page 9: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/9.jpg)
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
![Page 10: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/10.jpg)
4! Walk-through
4!=
n!= n*(n-1)! if n>0 1 if n==0
![Page 11: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/11.jpg)
Java implementation of n!
public int factorial(int n){
if (n==0)
return 1;
else
return n*factorial(n-1);
}
n!= n*(n-1)! if n>0 1 if n==0
![Page 12: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/12.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
![Page 13: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/13.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
![Page 14: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/14.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
![Page 15: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/15.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
n=2Returns 2*factorial(1)
![Page 16: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/16.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
n=2Returns 2*factorial(1)
n=1Returns 1*factorial(0)
![Page 17: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/17.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
n=2Returns 2*factorial(1)
n=1Returns 1*factorial(0)
n=0Returns 1
![Page 18: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/18.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
n=2Returns 2*factorial(1)
n=1Returns 1*factorial(0)
1
![Page 19: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/19.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
n=2Returns 2*factorial(1)
1
![Page 20: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/20.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
2
![Page 21: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/21.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
n=4Returns 4*factorial(3)
6
![Page 22: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/22.jpg)
factorial(4);
factorial(4)
public int factorial(int n){ if (n==0)
return 1; else
return n*factorial(n-1);
}
24
![Page 23: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/23.jpg)
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
n=4Returns 4*factorial(3)
n=3Returns 3*factorial(2)
Execution of factorial(4) is paused until factorial(3) returns with its answer
Once factorial(3) returns, execution of factorial(4) continues
![Page 24: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/24.jpg)
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
![Page 25: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/25.jpg)
Another example
• prod(list) returns a product of all the numbers in the list
• prod({1,3,3,4}) should return 36
![Page 26: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/26.jpg)
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);}
![Page 27: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/27.jpg)
walkthrough
helperProd({1,3,3,4},0)
float helperProd(float[] list, int k){ if (k==list.length) return 1; else return list[k]*helperProd(list,k+1);}
![Page 28: Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor](https://reader035.vdocument.in/reader035/viewer/2022062321/56649ec65503460f94bd18cd/html5/thumbnails/28.jpg)
Recursion in real life