

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Aub finals for programming algorithms and data structures dated 2012-2013, with solutions
Typology: Exams
Uploaded on 05/14/2023
10 documents
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Fall 2012 – 2013 Semester Final Exam Wednesday January 9, 8:00 a.m. 2 hours
Instructions.
This final is scored out of 100. This final is open book and open notes: you can use the textbook, notes you have taken in class, your homework solutions, and all material that I have posted to the course Moodle site. Show your work, as partial credit will be given. You may use any algorithm that was covered in class: give the name of the algorithm and a page number in the textbook where the algorithm is presented. If taken from your lecture notes, just say “lecture notes.”
Please start each answer on a new page.
Exam policy: if you are unsure about the meaning of a question, raise your hand and I will come to you. I will discuss only what is on the exam sheet. I will NOT discuss anything that you have written down.
Best of luck!
Problem 1. (20 points) State whether the following are true or false (5 points each)
a. n^3 = Ω(n^3 )
Sample solution. True
b. lg(nn) = Θ(n lg n)
Sample solution. True
c. lg(nn) + n^2 = Θ(n lg n)
Sample solution. False
d. n^4 + 4n^3 + 5n^2 + 7 ∼ 2 n^4
Sample solution. False
Problem 2. (20 points) Consider the following recurrence
T (n) = 3T (n/3) + n.
a. (10 points) Draw the recursion tree for this recurrence.
b. (10 points) Solve the recurrence by summing up the recursion tree. GIve a tight bound, i.e., a solution of the form T (n) = Θ(f (n)). Half credit for proving the upper bound only, or proving the lower bound only.
Sample solution. Level i of the recursion tree has 3i^ nodes, each with input n/ 3 i. Each level has cost n. The height is log 3 n. Hence cost of tree is n log 3 n, which is Θ(n lg n).
Problem 3. (30 points) You are given an array A of size n. An element A[i] is out-of-order iff A[i] < max(A[0..i − 1]) where max(A[0..i − 1]) is the maximum element in A[0],... , A[i − 1]. In other words, an element is out-of-order if it is smaller than the maximum of all the elements below it.
You are given that A contains k out-of-order elements, with k ≤ n/ lg n. You are not given the actual value of k.
Give pseudocode for an algorithm to sort A in time O(n).
Grading is as follows:
Sample solution. Pseudocode:
Traverse A, maintaining the maximum m of the elements that are not out-of-order. That is, if the next element is larger, then set m to it, otherwise leave m unchanged.
Use m to determine whether the next element is out-of-order (< m) or not (≥ m).
While traversing, copy all elements that are not out-of-order to another array B, and copy all elements that are out-of-order to a third array C.
By its construction, B is ordered.
C is not necessarily ordered, and has size k. Sort C using mergesort in time O(k lg k).
Merge B and C to give the result of sorting A.
Running time:
Merge and traversal take time O(n). So total running time is O(n + k lg k). Now k ≤ n/ lg n, so k lg k ≤ (n/ lg n) lg(n/ lg n) = (n/ lg n)(lg n − lg lg n) = n − n lg lg n/ lg n = O(n). Hence total running time is O(n + k lg k) = O(n).
Problem 4. (30 points) Let G = (V, E) be a directed graph. A source node in G is a node with no incoming edges. Write pseudocode for an algorithm that determines if G contains a source node. Your algorithm should have average case running time O(V + E).
Grading is as follows: