Você está na página 1de 5

CSCI-UA.

0310-001/002 Basic Algorithms

September 12, 2016

Solutions to Problem 1 of Homework 1 (10 Points)


Name: Name (NetID)

Due: Tuesday, September 13

Collaborators: NetID1, NetID2


For each of the following pairs of functions f (n) and g(n), state whether f is O(g); whether f is
o(g); whether f is (g); whether f is (g); and whether f is (g). (More than one of these can be
true for a single pair!)
(a) f (n) = 3n9 + log(n) + 38;

g(n) =

4n20 +5n2 +4
111

52n.

Solution: f(n) is O(g) and o(g)


20
2 +4
(3n9 + log(n) + 38)k < ( 4n +5n
52n)c
111
log(n)+38
4n11 +5n7 +4n9
1 + ( 3n9 )k < (
52n8 )c
111
so for all values possible values of c there exists a k such that this expression holds for all n
greater than n sub zero.
(b) f (n) = log(n2 + 3n);

g(n) = log(n4 1).

Solution: f is O(g), (g), (g)


at higher values n2 and n4 dominate each expression
so at higher values f(n) is about 2 log(n) and g(n) is about 4 log(n) so constants must exist
that satisfy the provisions of those three relationships.
2

(c) f (n) = log(2n + n2 );

g(n) = log(n372 ).

Solution: f(n) is (g) and w(g)


2
At higher values 2n dominates f(n) so f(n) is about n2 log(2) and g(n) is 372 log(n)
so f(n) is growing much faster than g(n)
(d) f (n) = n37 2n ;

g(n) = n2 5n .

Solution: f(n) is O(g) and o(g)


at higher values 2n and 5n dominate, so f(n) grows much slower than g(n)
(e) f (n) = (nn )3 ;

g(n) = n(n ) .

Solution: f(n) is O(g) and o(g)


3
f (n) = n3n and g(n) = nn so f(n) grows much more slowly.

Name (NetID), Homework 1, Problem 1, Page 1

CSCI-UA.0310-001/002 Basic Algorithms

September 12, 2016

Solutions to Problem 2 of Homework 1 (10 points)


Name: Name (NetID)

Due: Tuesday, September 13

Collaborators: NetID1, NetID2


Let A[1, . . . , n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is
called an inversion of A.
(a) (2 points) List all inversions of the array h8, 5, 2, 7, 9i.
Solution: (1,2),(1,3)(1,4)(2,3)
(b) (3 points) Which arrays with distinct elements from the set {1, 2, . . . , n} have the smallest
and the largest number of inversions and why? State the expressions exactly in terms of n.
Solution: Smallest: (1,2,3...n)
Largest:(n,n-1,n-2...,2,1)
An array with all elements in order would have the smallest number of inversions, because
every element would be smaller than all the elements that followed it, and an array with
all the elements in reverse order would have the largest number of inversions, because every
element would be greater than the elements that followed it. There would be 0 inversions in
the smallest array and n(n1)
inversions in the largest.
2
(c) (5 points) What is the relationship between the running time of Insertion-Sort and the
number of inversions I in the input array? Justify your answer.
Solution: The run time is (n + I) because the inner loop is executed I times, because the
inner loop eliminates 1 inversion when it is executed. The outer loop always executes n times,
so the run time is the sum of the inversions and the number of elements.

Name (NetID), Homework 1, Problem 2, Page 1

CSCI-UA.0310-001/002 Basic Algorithms

September 12, 2016

Solutions to Problem 3 of Homework 1 (10 points)


Name: Name (NetID)

Due: Tuesday, September 13

Collaborators: NetID1, NetID2


The following two functions both take as arguments two n-element arrays A and B:
Magic-1(A, B, n)
for i = 1 to n
for j = 1 to n
if A[i] B[j] return FALSE
return TRUE

(a) (2 points) Both of these procedures return TRUE if and only if the same condition holds on
the arrays A and B holds. Describe this condition (in words).
Solution: Both methods reurn ture if every element in A is less than every element in B.
(b) (5 points) Analyze the worst-case running time for both algorithms using the -notation.
Solution: Both methods worst case runtime is (n2 )
(c) (3 points) Does the situation change if we consider the best-case running time for both
algorithms?
Solution: Yes, magic-1s best case runtime is (1) while magic-2s is (n)

Name (NetID), Homework 1, Problem 3, Page 1

CSCI-UA.0310-001/002 Basic Algorithms

September 12, 2016

Solutions to Problem 4 of Homework 1 (12 points)


Name: Name (NetID)

Due: Tuesday, September 13

Collaborators: NetID1, NetID2


Consider sorting n numbers stored in array A by first finding the largest element of A and exchanging it with the element in A[n]. Then find the second largest element of A and exchange it with
A[n 1]. Continue in this manner for the first n 1 elements of A.
(a) (5 points) Write (non-recursive) pseudocode for this algorithm, which is known as selection
sort. What loop invariant does this algorithm maintain? Why does it need to run for only
the first n 1 elements, rather than for all n elements? Give the best-case and worst-case
running times of selection sort in -notation.
Solution:
Selection Sort(A)
for i = A.length 1 to 1
max = i
for j = i to 0
if A[j] A[max]
max = j
if max 6= j
swap A[j] and A[max]
The loop invariant maintains that the array is sorted between A[i] and A[list.length-1] The
algorithm only needs to run for the first n-1 elements because each iteration moves the largest
element to its correct position in the array. Thus the final element must be the smallest in
the array, located in the first position. The best and worst case run time is (n2 )
(b) (2 points) Compare the running time of selection sort to the one of insertion sort.
Solution: Both have the same worst case complexity of (n2 ) but Insertion sort has best
time complexity of (n). Insertion sort is slighty faster on average because less comparisons
are involved.
(c) (5 points) Devise a recursive variant of your algorithm in (a) by following the divide-andconquer paradigm. Find a recurrence relation describing the running time of your algorithm
and solve it.
Solution:

Name (NetID), Homework 1, Problem 4, Page 1

RecSec(A, key)
if key == 1
return
RecSec(A, key 1)
int max=key
for key to 0
if A[i] > A[key]
max=i
swap A[key] and A[max]
T (n) = n + T (n 1) if n = 1 T(n)=1
n + n 1 + n 2 + ...1 = n(n+1)
2

Name (NetID), Homework 1, Problem 4, Page 2

Você também pode gostar