# 3 Count the instructions of the following code segment to determine

Content type
User Generated
Rating
Showing Page:
1/3
3. Count the instructions of the following code segment to
determine T(n), then select an appropriate f(n) such that
T(n) is O(f(n)). Finally, formally prove that T(n) is O(f(n))
// Assume that N is a constant that has been globally
defined.
// Assume instantiating a new class takes 1 instruction.
instruction the first time and 1 addition instruction every
iteration. Hint: Remember your summation identities! //
Assume the LinkedList functions getNext(), getValue(), and
getFirstNode() all take 1 instruction.
}
int sum = listSummation(list.getFirstNode());
print sum;
return 0;
}
Solution
The other goal in our choice of a big-oh bound is simplicity
in the expression of the function. Unlike tightness,
simplicity can sometimes be a matter of taste. However,
we shall generally regard a function f(n) as simple ifSimple
function
1. It is a single term and
2. The coecient of that term is 1.

Showing Page:
2/3
Suppose T1(n) is known to be Of1(n), while T2(n) is known
to be Of2(n). Further, suppose that f2 grows no faster than
f1; that is, f2(n) is Of1(n). Then we can conclude that T1(n)
+ T2(n) is Of1(n).
In proof, we know that there are constants n1, n2, n3, c1,
c2, and c3 such that
1. If n n1, then T1(n) c1f1(n).
2. If n n2, then T2(n) c2f2(n).
3. If n n3, then f2(n) c3f1(n).
Let n0 be the largest of n1, n2, and n3, so that (1), (2),
and (3) hold when n n0. Thus, for n n0, we have
T1(n) + T2(n) c1f1(n) + c2f2(n)
If we use (3) to provide an upper bound on f2(n), we can
get rid of f2(n) altogether and conclude that
T1(n) + T2(n) c1f1(n) + c2c3f1(n)
Therefore, for all n n0 we have
T1(n) + T2(n) cf1(n)
if we dene c to be c1 + c2c3.
This statement is exactly what we need to conclude that
T1(n) + T2(n) is Of1(n).

Showing Page:
3/3

Unformatted Attachment Preview
3. Count the instructions of the following code segment to determine T(n), then select an appropriate f(n) such that T(n) is O(f(n)). Finally, formally prove that T(n) is O(f(n)) for your f(n). // Assume that N is a constant that has been globally defined. // Assume instantiating a new class takes 1 instruction. // Assume the LinkedList function add(i) takes 1 instruction the first time and 1 addition instruction every iteration. Hint: Remember your summation identities! // Assume the LinkedList functions getNext(), getValue(), and getFirstNode() all take 1 instruction. } int sum = listSummation(list.getFirstNode()); print sum; return 0; } Solution The other goal in our choice of a big -oh bound is simplicity in the expression of the function. Unlike tightness, simplicity can sometimes be a matter of taste. However, we shall generally regard a function f(n) as simple ifSimple function ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

### Review

Anonymous
I was struggling with this subject, and this helped me a ton!

Studypool
4.7
Indeed
4.5
Sitejabber
4.4