Docsity

Computer Algebra - Assignment 1 Solutions | MA 522, Assignments of Mathematics

Toggle sidebar
bg1
Homework 1 Solutions
MA 522 Fall 2008
1. Consider the searching problem:
Input A sequence of nnumbers A= [a1, . . . , an]and a value v.
Output An index isuch that v=A[i]or the special value NI L if vdoes not appear in A
(a) (20 points) Write a pseudocode for LINEAR-SEAR CH, which scans through the se-
quence, looking for v. Using a loop invariant, prove that your algorithm is correct. (Make
sure that your loop invariant fulfills the three necessary properties – initialization, main-
tenance, terminantion.)
Solution:
LINEAR-SEAR CH(A, v)
1for i←1to length[A]
2do if A[i] = v
3then return i
4return NIL
Correctness:
Loop invariant At the start of each iteration of the for loop we have A[j]6=vfor all j < i.
Initialization Before the first loop iteration the invariant holds since the statement is
empty.
Maintenance The loop invariant is maintained at each iteration, since otherwise at the
i-th iteration there is some some j < i such that A[j] = v. However, in that case for
the j-th iteration of the loop the value jis returned, and there is no i-th iteration of
the loop, a contradiction.
Termination When the loop terminates, there may be two cases: one is that it terminates
after i≤length(A)iterations, and returns i, in which case the if conditional ensures
that A[i] = v. The other case is that iexceeds length(A), in this case by the loop
invariant we have that for all j≤length(A)A[j]6=v, this returning N IL is correct.
(b) (10 points) How many elements of the input sequence need to be checked on the
avarage, assuming that the element being searched for is equally likely to be any element
in the array? How about the worst case? What are the avarage-case and worst-case run-
ning times of linear search in Θnotation? Justify your answer.
1
pf3
pf4

Partial preview of the text

Download Computer Algebra - Assignment 1 Solutions | MA 522 and more Assignments Mathematics in PDF only on Docsity!

Homework 1 Solutions

MA 522 Fall 2008

  1. Consider the searching problem :

Input A sequence of n numbers A = [a 1 ,... , an] and a value v.

Output An index i such that v = A[i] or the special value NIL if v does not appear in A

(a) (20 points) Write a pseudocode for LINEAR-SEARCH, which scans through the se- quence, looking for v. Using a loop invariant, prove that your algorithm is correct. (Make sure that your loop invariant fulfills the three necessary properties – initialization, main- tenance, terminantion.)

Solution:

LINEAR-SEARCH(A, v) 1 for i ← 1 to length[A] 2 do if A[i] = v 3 then return i 4 return NIL

Correctness:

Loop invariant At the start of each iteration of the for loop we have A[j] 6 = v for all j < i.

Initialization Before the first loop iteration the invariant holds since the statement is empty.

Maintenance The loop invariant is maintained at each iteration, since otherwise at the i-th iteration there is some some j < i such that A[j] = v. However, in that case for the j-th iteration of the loop the value j is returned, and there is no i-th iteration of the loop, a contradiction.

Termination When the loop terminates, there may be two cases: one is that it terminates after i ≤ length(A) iterations, and returns i, in which case the if conditional ensures that A[i] = v. The other case is that i exceeds length(A), in this case by the loop invariant we have that for all j ≤ length(A) A[j] 6 = v, this returning NIL is correct.

(b) (10 points) How many elements of the input sequence need to be checked on the avarage, assuming that the element being searched for is equally likely to be any element in the array? How about the worst case? What are the avarage-case and worst-case run- ning times of linear search in Θ notation? Justify your answer.

Solution: On avarage (^1) nn(n 2 +1) = n+1 2 elements being searched for. In the worst case it is n. The avarage case running time is n+1 2 = Θ(n) since for all n ≥ 1 we have

1 2

n ≤

n + 1 2

≤ n.

The worst case running time is Θ(n). (c) (10 points) If we assume that A is sorted, then we can check the midpoint of the sequence against v and eliminate half of the sequence from further consideration. BINARY SEARCH is an algorithm that repeats this procedure, halving the size of the remaining portion of the sequence each time. Write a pseudocode, either iterative or recursive, for binary search. Solution:

BINARY-SEARCH(A, p, q, v) 1 if q < p 2 then return NIL 3 m ← p + d(p − q)/ 2 e 4 if A[m] = v 5 then return m 6 if A[m] > v 7 then BINARY-SEARCH(A, 1 , m − 1 , v) 8 else BINARY-SEARCH(A, m + 1, length(A), v)

(d) (10 points) Use the master method to show that the solution to the binary-search recurrence T (n) = T (n/2) + Θ(1) is T (n) = Θ(lg n), and argue that the worst case running time for binary search is Θ(lg n).

Solution: We can use the Master Theorem Case 2 because from a = 1 and b = 2 we have nlogb^ a^ = n^0 = 1, so for k = 0 f (n) = Θ(1) = Θ(nlogb^ a^ lgk^ n). This gives that

T (n) = Θ(nlogb^ a^ lgk+1^ n) = Θ(lg n).

The running time of BINARY-SEARCH can be computed from the following recurrence equation:

T (n) = c + T (b

n 2

c).

Thus the above argument shows that the running time is T (n) = Θ(lg n).

(e) (10 points) Observe that the while loop of the INSERTION SORT procedure (in class) uses a linear search to scan (backward) through the sorted subarray A[1... j − 1]. Can we use binary search instead to improve the overall worst-case running time of insertion sort to Θ(n lg n)?

n

n 2

n 4

...

n 8

...

n 16

...

n 4

n 8

...

n 16

...

n 32

...

n 8

n 16

...

n 32

...

n 64

... Note that if a node would have a number < 1 then we write 0 in that node.

Let Σ(i) denote the sum of the nodes in line i. Then Σ(1) = n,

Σ(2) ≤

n 2

n 4

n 8

= n

n.

Note that we may have inequality above, in case n < 8. Similarly for sum of the (i + 1)-th row

Σ(i + 1) ≤

Σ(i) 2

Σ(i) 4

Σ(i) 8

= Σ(i)

Σ(i).

Thus we get that

Σ(i) ≤

)i− 1 n.

Summing up the rows we get the following upper bound:

T (n) ≤

∑^ lg^ n

i=

)i− 1 n ≤

∑^ ∞

i=

)i− 1 n = n

= 8n.

On the other hand n is a lower bound for the running time. Thus T (n) = Θ(n).

We can also verify that T (n) = Θ(n) by the substitution method. We show that T (n) = cn for some suitable c > 0 constant. We have

T (n) = T (

n 2

) + T (

n 4

) + T (

n 8

) + n

= c

n 2

  • c

n 4

  • c

n 8

  • n

cn + n = n

c + 1

We need to find c such that (^) ( 7 8

c + 1

= c.

This equations has the solution c = 8, so we have that T (n) = 8c.