Tirgul 2 Subjects of this tirgul Short discussion
Tirgul 2 • Subjects of this tirgul: – Short discussion about standard output/error. – Short discussion about comparators. – Main subject: asymptotic analysis.
Default I/O Streams • Class System has 3 default streams, available to every Java program: • Input from the keyboard goes into the ‘standard input’. This is the data member System. in of type java. io. Input. Stream • Output (usually to the terminal window) written through 2 streams: – ‘Standard output’ – System. out of type java. io. Print. Stream – ‘Standard error’ – System. err of type java. io. Print. Stream
Default I/O Streams • The standard output and error are directed by the Operating System. By default - both to the terminal. • The convention - standard error for error messages, standard output for regular output. • In UNIX, the user can redirect to a file: – standard output by adding “> my_out. txt”. For example: java My. Class param 1 > out. txt – both to the same file, by adding “>& my_out. txt” – You can’t redirect only the standard error, but redirecting to different files is possible (by outsmarting): (java My. Class > out. txt) >& err. txt
Comparators • You already know interface Comparable to compare objects. • Problem: some classes may be compared by different criteria (participants may be compared by ID, or by name) • Solution: create a Comparator for this specific class. – In the constructor specify the comparison criteria. – The method compare() receives two objects of this class and compares them (similar to Compare. To()). • Many Java API data structures can compare classes either by Comparable or by a Comparator.
Asymptotic Analysis • Motivation: Suppose you want to evaluate two programs according to their run-time for inputs of size n. The first has run-time of: and the second has run-time of: Answer: For small inputs, it doesn’t matter, both programs will finish before you notice. What about (really) large inputs?
Big - O • Definition: if there exists constants c>0 and n 0 such that for all n>n 0, • In other words, g(n) bounds f(n) from above (for large n’s) up to a constant. • Examples:
Big - Omega • Definition: if there exists constants c>0 and n 0 such that for all n>n 0, • In other words, g(n) bounds f(n) from below (for large n’s) up to a constant. • Examples:
Big - Theta • Definition: if: and • In other words, g(n) is a tight estimate of f(n) (in asymptotic terms). • Examples:
Example 1 (question 2 -4 -e. in Cormen) Claim: If Proof: Take (for n>n 0) then. Thus for n>n 0, Note: if there is no such for f(n) then the claim is not true: take for example f(n) = 1/n. For any constant c, take n>c and so: f(n) > (c/n)f(n) = c (f(n))2
Example 2 (question 2 -4 -d. in Cormen) • Does imply • Answer: No. For example, Then, but, for any constant c: so: so
Summations (from Cormen, ex. 3. 2 -2. , page 52) • note how we “got rid” of the integer rounding • The first term is n so the sum is also • It’s an example how the largest item dominates the growth of the term in an exponential decrease/increase.
Summations (example 2) (Cormen, ex. 3. 1 -a. , page 52) (r is a constant) : • note that • But it seems that we can do better by tightening our analysis a bit more. . .
Example 2 (Cont. ) • For an even n: • For an odd n: • Thus: so our upper bound was tight!
Recurrences • What is the running time of the “Towers of Hanoi” ? ? • Reminder: H(s, t, m, k) { if (k > 1) { H(s, m, t, k-1) move. Disk(s, t) H(m, t, s, k-1) } else { move. Disk(s, t) } } • We can express the running-time as a recurrence: h(n) = 2 h(n-1) + 1 h(1) = 1 • How do we solve this ?
Step 1: “guessing” the solution h(n) = 2 h(n-1) + 1 = 2[2 h(n-2)+1] + 1 = 4 h(n-2) + 3 = 4[2 h(n-3)+1] + 3 = 8 h(n-3) + 7 • When repeating k times we get: h(n)=2 k h(n-k) + (2 k - 1) • Now take k=n-1. We’ll get: h(n) = 2 n-1 h(n-(n-1)) + 2 n-1 - 1 = 2 n-1 + 2 n-1 -1 =2 n - 1
Step 2: proving by induction • If we guessed right, it will be easy to prove by induction that h(n)=2 n - 1 • For n=1 : h(1)= 2 -1=1 (and indeed h(1)=1) • Suppose h(n-1) = 2 n-1 - 1. Then, h(n) = 2 h(n-1) + 1 = 2(2 n-1 - 1) + 1 = 2 n -2 + 1 = 2 n -1 • So we conclude that: h(n) = O(2 n)
Another Example T(n) = 2 T(n/2) + 1 T(1) = 1 T(n) = 2 T(n/2) + 1 = 2 (2 T(n/4) + 1 = 4 T(n/4) + 3 = 4 (2 T(n/8) + 1) + 3 = 8 T(n/8) + 7 • And we get: T(n) = k T(n/k)+(k-1) For k=n we get T(n)= n T(1)+n-1=2 n-1 Now proving by induction is very simple. • Another way: “guess” right away T(n) <= c n - b (for some b and c we don’t know yet), and try to prove by induction: For n=1: T(1)=c-b, which is true when c-b=1 The induction step: T(n) <= 2 (c(n/2) - b) + 1 = c n - 2 b + 1 <= c n - b (the last step is true if b>=1). Conclusion: T(n) = O(n)
Common errors • A precise guess is important for the induction to work: h(n) = 2 n - 1, and not just h(n)=O(2 n) or h(n)=2 n. • For example, the method from the previous slide: If we guess just T(n)<= cn the induction step won’t work. Let’s try: [ by induction T(n/2) <= c(n/2) ] T(n) = 2 T(n/2) + 1 = c n + 1 > c n (!!!) • Notice that it is not correct to say “c n + 1 = O(n) so we proved T(n)=O(n)”. The induction step must be precise. If guessing T(n) <= cn then this is what you have to show.
Recursion Trees The recursion tree for the “towers of Hanoi”: • For each level we write the time added due to this level. In Hanoi, each recursive call adds one operation (plus the recursion). Thus the total is:
- Slides: 19