

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 to summer 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!


Summer 2012 – 13 Final Exam Friday July 26, 8:00 – 10:00 a.m., Bliss 205
Instructions. This final exam is scored out of 100. This final is open book and open notes: you can use the textbook, notes you have taken in class, and your homework solutions. Show your work, as partial credit will be given. You may use any algorithm that was covered in class. Just give the name of the algorithm and the chapter or page number in the book where the algorithm is given. If taken from your lecture notes, just say “lecture notes.”
Please draw a horizontal line to separate each answer from the next. Best of luck!
Problem 1 (20 points, 5 points each).
State whether the following are true or false. Give a brief proof for your answer.
n = O(n^2.^4 ) Sample solution. false
Problem 2 (30 points).
Consider the following algorithm, where A[] ↔ A[r] means that the values of A[] and A[r] are exchanged.
SmartAlg(A, `, r)
] then A[] ↔ A[r];Part a, (10 points). Write down the recurrence relation which describes the running time of SmartAlg(A, 0 , n − 1), where n is the size of A, and A is indexed between 0 and n − 1 inclusive.
Sample solution. T (n) = 3T (2n/3) + c.
Part b, (10 points). Draw the recursion tree corresponding to this recurrence.
Sample solution. Level i has 3i^ nodes, each with input n/ 1. 5 i^ and constant non-recursive cost. The tree has depth log 1. 5 n
Part c, (10 points). Solve the recurrence by summing up the recursion tree, or by other means if you wish.
Sample solution. We sum up the tree. Cost is the number of nodes, since each node has constant cost. Number of nodes is log ∑ 1. 5 n
i=
3 i
This has same order as 3log^1.^5 n. Which is same as nlog^1.^5 3. So running time is Θ(nlog^1.^5 3 ).
Problem 3 (20 points).
You are given an integer array A of size n, indexed from 0 to n − 1 inclusive. The elements in A are the integers between 0 and n − 1 inclusive, i.e., { 0 ,... , n − 1 }. For example, if n = 7 then a possible value for A is 3 1 0 2 4 6 5
Give an algorithm for sorting A subject to the following restrictions:
Give arguments for both the running time and correctness of your algorithm.
Notice that if either of the above restrictions is removed, then the problem becomes trivial. Hence you will receive no credit if you do not satisfy both restrictions.
Sample solution. Postcondition is (∀ j : 0 ≤ j < n : A[j] = j).
Pseudocode:
high level sketch first:
let A[0] = e swap A[0] with A[e], afterwards have A[e] = e. if A[0] = 0 move onto A[1] else repeat above until A[0] = 0 move onto next element A[i] such that A[i] 6 = i let A[i] = e swap A[i] with A[e], afterwards have A[e] = e. keep swapping A[i] with A[A[i]] until A[i] = i after each swap of A[i] with A[A[i]] we have A[e] = e where e is initial value of A[i] done when you reach end of array
Now the code: