The document outlines an algorithm design and analysis course taught over 12 weeks. It will cover topics like algorithm analysis, data structures, sorting, trees, hashing, and graph algorithms. Students will complete a presentation on either greedy algorithms, divide and conquer, randomized algorithms, or backtracking algorithms. The reading material is a textbook on data structures and algorithms. Examples are provided to illustrate running time analysis and the use of recursion to solve problems like printing out a positive integer digit-by-digit.
5. • Optional Topics :Greedy Algorithms, Divide and Conquer,
Randomized Algorithms, Backtracking Algorithms
• Compulsory Topics: Dynamic Programming
• 3 students for each Team. Everyone need to give the presentation.
7. Goals
• See that how a program performs for reasonably large input is just
as important as its performance on moderate amounts of input.
• Summarize the basic mathematical background needed for the rest
of the book.
• Briefly review recursion.
• Summarize some important features of C++ that are used
throughout the text.
8. What is this book about
• Problem: Suppose you have a group of N numbers and would like to
determine the kth largest.
9. • Solution 1:
Read the N numbers into an array, sort the array in decreasing order
by some simple algorithm such as bubble sort, and then return the
element in position k.
10. • Solution 2:
Read the first k elements into an array, sort the array in decreasing
order. Next, each remaining elements is read one by one. As a new
element arrives, it is ignored if it is smaller than the kth element.
Otherwise, it is placed in its correct spot, bumping the last element.
When the algorithm ends, the kth element is returned.
11. • Question:
Which is better?
Is either algorithm good enough?
( A simulation using a random file of 30 million elements and k =
15,000,000 will show neither algorithm finishes in a reasonable time;
several days)
12. What is this book about
• How to estimate the running time of a program for large inputs.
• How to compare the running times of two programs without actually
coding them.
• See some techniques for drastically improving the speed of a
program and for determining program bottlenecks.
14. Recursion
• We can define a function 𝑓, valid on nonnegative integers, that
satisfies 𝑓(0) = 0 and 𝑓(𝑥) = 2𝑓(𝑥 − 1) + 𝑥2
. A function that
is defined in terms of itself is called recursive.
• C++ implementation?
15. int f( int x)
{
if( x == 0)
return 0;
else
return 2*f(x-1) + x*x;
}
16. Two fundamental rules of
Recursion
• Base cases. You must always have some base cases, which can be
solved without recursion.
• Making progress. For the cases that are to be solved recursively, the
recursive call must always be to a case that makes progress toward
a base case.
17. Bad Case
int bad( int n)
{
if( n == 0)
return 0;
else
return bad( n/3+1)+n-1;
}
18. • Question: Print out positive number n. e.g. 76234. Assume you have
function printDigit(). This function take one digit and print it out, so
printDigit(4) will output a 4. The recursion function is printOut(in n)
• Prove it.
19. void printOut( int n)
{
if( n >= 0)
printOut( n/10);
printDigit( n%10);
}
20. • Proof ( By induction)
• First, if n has one digit, then the program is trivially correct, since it
merely makes a call to printDigit. Assume then that printOut works
for all numbers of k or fewer digits. A number of k+1 digits is
expressed by its first k digits is exactly [n/10], which, by the inductive
hypothesis, is correctly printed, and the last digit is n mode 10, so
the program prints out any (k+1) digit number correctly. Thus, by
induction, all numbers are correctly printed.