A2 Adsa
A2 Adsa
Please answer each of the following problems. Refer to the course webpage for the collab-
oration policy, as well as for helpful advice for how to write up your solutions. For this
assignement, you need to provide two files, one with code and another with report.
FileName for Submission: Your RollNo A2.c (for code) and Your RollNo A2.pdf (for report)
Note on notation: On a few problems in this homework set, we use “big-O” notation to state
the problem. For all of the problems where O appears, it is fine to to take the definition of
“O(n)” very informally to mean “grows (at most) roughly linearly in n:” for example, 100 · n
grows roughly linearly with n, so 100 · n = O(n). Similarly, 100 · n + 100 = O(n).√ But n2
does not grow roughly linearly with n, it grows√much faster, so n2 6= O(n). And n grows
much more slowly than n, so we would still say n = O(n). We use similar notation for other
functions, like O(log(n)).
1. Proof of correctness. (5+5 points) Consider the following algorithm that is supposed
to sort an array of integers. (a) Provide a proof that this algorithm is correct. (Hint:
you may want to use more than one loop invariant.), and (b) Write the code in C and
validate through different inputs.
# Swaps two elements of the array. You may assume this function is correct.
Swap(array A, int x, int y):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
2. New friends. (20 points) Each of n users spends some time on a social media site. For
each i = 1, . . . , n, user i enters the site at time ai and leaves at time bi ≥ ai . You are
interested in the question: how many distinct pairs of users are ever on the site at the
same time? (Here, the pair (i, j) is the same as the pair (j, i)).
Example: Suppose there are 5 users with the following entering and leaving times:
1
User Enter time Leave time
1 1 4
2 2 5
3 7 8
4 9 10
5 6 10
Then, the number of distinct pairs of users who are on the site at the same time is three:
these pairs are (1, 2), (4, 5), (3, 5).
(a) (4+4 pts) Given input (a1 , b1 ), (a2 , b2 ), . . . , (an , bn ) as above, there is a straightfor-
ward algorithm that takes about1 n2 time to compute the number of pairs of users
who are ever on the site at the same time. (a) Give this algorithm and explain
why it takes time about n2 . (b) Write the code in C programming language for this
algorithm.
(b) (6+6 pts) (a) Give an O(n log(n))-time algorithm to do the same task and analyze
its running time. (Hint: consider sorting relevant events by time). (b) Write the
code in C programming language for this algorithm.
Algorithm 1: findMinimum
Input: List A = [a1 , . . . , an ] of n items
Output: mini {a1 , . . . , an }
if n=1 then
return
A1 = A[0 : n/2]
A2 = A[n/2 : n]
return min(findMinimum(A1 ), findMinimum(A2 ) )
(c) (3pts) Fill in the blank in the pseudo-code: what should the algorithm return in the
base case? Briefly argue that the algorithm is correct with your choice.
(d) (3pts) Analyze the running time of this recursive algorithm. How does it compare
to your solution in part (a)?
(d) (6pts) Write the C program for this recursive algorithm.
1
Formally, “about” here means Θ(n2 ), but you can be informal about this.
2
4. Recursive local-minimum-finding. (20 points)
(a) Suppose A is an array of n integers (for simplicity assume that all integers are
distinct). A local minimum of A is an element that is smaller than all of its neighbors.
For example, in the array A = [1, 2, 0, 3], the local minima are A[1] = 1 and A[3] = 0.
i. (2 points) Design a recursive algorithm to find a local minimum of A, which
runs in time O(log(n)).
ii. (2 points) Prove formally that your algorithm is correct.
iii. (2 points) Formally analyze the runtime of your algorithm.
iv. (4 points) Write C program for your algorithm.
(b) Let G be a square n × n grid of integers. A local minimum of A is an element that
is smaller than all of its direct neighbors (diagonals don’t count). For example, in
the grid
5 6 3
G = 6 1 4
3 2 3
some of the local minima are G[1][1] = 5 and G[2][2] = 1. You may once again
assume that all integers are distinct.
i. (2 points) Design a recursive algorithm to find a local minimum in O(n) time.
ii. (2 points) We are not looking for a formal correctness proof, but please explain
why your algorithm is correct.
iii. (2 points) Give a formal analysis of the running time of your algorithm.
iv. (4 points) Write C program for your algorithm.