CMPS 256 - Advanced Algorithms and Data Structures: Final Exam, Exams of Data Structures and Algorithms

Aub finals for programming algorithms and data structures dated to summer 2012-2013, with solutions

Typology: Exams

2011/2012

Uploaded on 05/14/2023

unknown user
unknown user 🇱🇧

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPS 256 ADVANCED ALGORITHMS AND DATA STRUCTURES
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.
1. lg(n100) = O(n)Sample solution. true
2. n2.3+ 100n2n=O(n2.4)Sample solution. false
3. (10n+ 3)(3 lg n+ 4) = O(n)Sample solution. false
4. n! = O(nn)Sample solution. true
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)
1. if A[r]< A[`]then A[`]A[r];
2. if r`+ 1 3then
3. t(r`+ 1)/3; //integer division
4. SmartAlg(A, `, r t)
5. SmartAlg(A, ` +t, r )
6. SmartAlg(A, `, r t)
7. else return
Part a, (10 points). Write down the recurrence relation which describes the running time of
SmartAlg(A, 0, n 1), where nis the size of A, and Ais indexed between 0 and n1 inclusive.
Sample solution. T(n)=3T(2n/3) + c.
Part b, (10 points). Draw the recursion tree corresponding to this recurrence.
Sample solution. Level ihas 3inodes, each with input n/1.5iand constant non-recursive
cost. The tree has depth log1.5n
1
pf3

Partial preview of the text

Download CMPS 256 - Advanced Algorithms and Data Structures: Final Exam and more Exams Data Structures and Algorithms in PDF only on Docsity!

CMPS 256 — ADVANCED ALGORITHMS AND DATA STRUCTURES

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.

  1. lg(n^100 ) = O(n) Sample solution. true
  2. n^2.^3 + 100n^2

n = O(n^2.^4 ) Sample solution. false

  1. (10n + 3)(3 lg n + 4) = O(n) Sample solution. false
  2. n! = O(nn) Sample solution. true

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)

  1. if A[r] < A[] then A[] ↔ A[r];
  2. if r − ` + 1 ≥ 3 then
  3. t ← (r − ` + 1)/3; //integer division
  4. SmartAlg(A, `, r − t)
  5. SmartAlg(A, ` + t, r)
  6. SmartAlg(A, `, r − t)
  7. else return

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:

  1. Your algorithm can change A using only swaps (exchanges) of elements
  2. Your algorithm must have worst-case running time in O(n)

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:

  1. for i := 0 to n − 1
  2. {invariant: (∀ j : 0 ≤ j < i : A[j] = j)}
  3. while A[i] 6 = i
  4. {let A[i] = e}
  5. A[i] ↔ A[A[i]]
  6. {A[e] = e}
  7. endwhile
  8. {A[i] = i}