Week 2 ( 1/14-1/20)
This week's content was very math-oriented. Out of all topics, recursive and nonrecursive algorithms, use cases of Theta notation, recurrence relation, and backward substitution, and brute force algorithm, the topic I had the most trouble with was backward substitution. It is good practice to look at different examples to see this algorithm from different perspectives. I attended office hours, which helped me quite a lot. I compare this algorithm to proof by induction in math classes, showing the steps to identify a pattern that eventually leads to the general formula or equation needed to understand the recurrence relation and the initial condition. Below is the example I was having trouble with, along with how each step helped me identify the general number i.
M(n) = M(n-1) + 2 // recurrence relation
As an initial condition, we know that if the input number n is 1, there is no multiplication. We can express it as follows.
M(1) = 0 // initial condition
We can solve the recurrence relation using the backward substitution as below
M(n) = M(n-1) + 2 // replace “M(n-1)” with “M(n-2) +2”
= [M(n-2) + 2] + 2
= M(n-2) + 2 + 2 // replace “M(n-2)” with “M(n-3) +2”
= [M(n-3) + 2] + 2 + 2
= M(n-3) + 2 + 2 + 2
…
= M(n-i) +2*i // For a general number i, we will get this.
...
= M(1) + 2*(n-1) // Note that the initial condition M(1) = 0.
= 0 + 2*(n-1)
= 2*(n -1) (n)
No comments:
Post a Comment