Kinldy Answer Q no 05 and than Qno 06 I need it really urgent 6. [10] Prove byin
ID: 3870290 • Letter: K
Question
Kinldy Answer Q no 05 and than Qno 06 I need it really urgent
6. [10] Prove byinduction that your closed formula from 5(c) is the number of times that the givenstatement is executed for a problem of size n.Note: if your proof is simply a proof that you have a correct closed formula for your recurrence from 5(c), then you will not get anymarks for this question. Your proof must refer back to the program in order to be correct. The lines of the code are numbered in order to makeiteasier for you to refer to them in your proof.
The Java code for this question:
Explanation / Answer
(c) T(n) = T(n-1) + n-2 for n>1
T(1) = 0
(d)
For basic condition: let n = 2. Then first time the condition "while (current.next != null)" is encountered then there is only one number in the list. So current.next is null, so loop is never entered.
So T(2) must be 0.
From the relation T(2) = T(1)+0 = 0
Let us assume T(n) has the right answer for the count of statement. For calculating T(n+1), first we have to add n numbers. The number of times the statement is executed is same as T(n). Now while we are going to add the (n+1) number, current will start from beginning. The loop will stop only when we reached the nth number, so to go from 1st to nth number, there has to be n-1 executions of current = current.next.
So T(n+1) = T(n) + n-1
Hence proved by induction.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.