Docsity

Algorithms & Data Structures Final Exam Solutions: CMPS 256, Exams of Algorithms and Programming

Toggle sidebar
bg1
Faculty of Arts and Sciences
Department of Computer Science
CMPS 256 – Algorithms & Data Structures
Final Exam Solution
Problem 1 (25 points)
For each of the following statements indicate whether the statement is true or false. Briefly explain/justify
your answers. The justification is more important than the true/false answer.
1. Let L1,L2,L3, and L4be 4 sorted lists each containing nreal numbers (all distinct). In a
comparison-based algorithm, being able to build a binary search tree that is balanced for the list
L1∪L2∪L3∪L4(i.e. the union of the 4 lists) requires Ω(nlog n) time.
This is false. We can merge the 4 lists into one sorted list in O(n) time and then we can construct
the balanced BST from a sorted list in O(n) time.
2. Let Tbe a complete binary tree with nnodes. It is possible to find a path from the root of Tto a
given vertex v∈Tusing either BFS or DFS in O(log n) time.
This is false. Both DFS and BFS take O(|V|+|E|) time to traverse a graph, and therefore a tree
requires O(|V|) time.
3. Constructing a binary heap, binomial heap, or Fibonacci heap can be done in O(n) time (in the
worst case) if we have nitems.
This is false. It takes O(nlog n) time to construct a binomial heap.
4. Let G= (V , E) be an unweighted directed graph. Then, one can compute shortest paths between
all pairs of vertices u, v ∈V(G) in O(|V|+|E|) time.
This is false. It takes O(|V|·|E|) time to compute the shortest path between all pairs.
5. Let G= (V , E) be a graph with at most 2nedges, where nis the number of vertices. Then one
can check if Ghas a cycle in O(|V|) time.
This is true. Since |E|= 2n, we have |V|+|E|= 3n. Hence, running DFS takes O(n) time.
Problem 2 (25 points)
In this problem we will design algorithms for solving a problem known as k-sum; we will focus on 3-
sum and 4-sum. You are given an array of nintegers A(positive and negative integers). In the 3-sum
problem, the goal is to determine if there exists 3 integers in Asuch that x+y+z= 0. Similarly, in the
4-sum problem, the goal is to determine if there exists 4 integers in Asuch that w+x+y+z= 0. In
the below questions, you are not allowed to sort the array A.
1. Design an Θ(n3)-time algorithm for 3-sum and an Θ(n4)-time algorithm for 4-sum.
3sum(A)
for i from 0 to n - 1
for j from 0 to n - 1
for k from 0 to n - 1
if A[i] + A[j] + A[k] == 0 return true
return false
4sum(A)
for i from 0 to n - 1
for j from 0 to n - 1
for k from 0 to n - 1
for p from 0 to n - 1
if A[i] + A[j] + A[k] + A[p] == 0 return true
return false
pf3
pf4

Partial preview of the text

Download Algorithms & Data Structures Final Exam Solutions: CMPS 256 and more Exams Algorithms and Programming in PDF only on Docsity!

Faculty of Arts and Sciences

Department of Computer Science

CMPS 256 – Algorithms & Data Structures

Final Exam Solution

Problem 1 (25 points)

For each of the following statements indicate whether the statement is true or false. Briefly explain/justify your answers. The justification is more important than the true/false answer.

  1. Let L 1 , L 2 , L 3 , and L 4 be 4 sorted lists each containing n real numbers (all distinct). In a comparison-based algorithm, being able to build a binary search tree that is balanced for the list L 1 ∪ L 2 ∪ L 3 ∪ L 4 (i.e. the union of the 4 lists) requires Ω(n log n) time. This is false. We can merge the 4 lists into one sorted list in O(n) time and then we can construct the balanced BST from a sorted list in O(n) time.
  2. Let T be a complete binary tree with n nodes. It is possible to find a path from the root of T to a given vertex v ∈ T using either BFS or DFS in O(log n) time. This is false. Both DFS and BFS take O(|V | + |E|) time to traverse a graph, and therefore a tree requires O(|V |) time.
  3. Constructing a binary heap, binomial heap, or Fibonacci heap can be done in O(n) time (in the worst case) if we have n items. This is false. It takes O(n log n) time to construct a binomial heap.
  4. Let G = (V, E) be an unweighted directed graph. Then, one can compute shortest paths between all pairs of vertices u, v ∈ V (G) in O(|V | + |E|) time. This is false. It takes O(|V | · |E|) time to compute the shortest path between all pairs.
  5. Let G = (V, E) be a graph with at most 2n edges, where n is the number of vertices. Then one can check if G has a cycle in O(|V |) time. This is true. Since |E| = 2n, we have |V | + |E| = 3n. Hence, running DFS takes O(n) time.

Problem 2 (25 points)

In this problem we will design algorithms for solving a problem known as k-sum; we will focus on 3- sum and 4-sum. You are given an array of n integers A (positive and negative integers). In the 3-sum problem, the goal is to determine if there exists 3 integers in A such that x + y + z = 0. Similarly, in the 4-sum problem, the goal is to determine if there exists 4 integers in A such that w + x + y + z = 0. In the below questions, you are not allowed to sort the array A.

  1. Design an Θ(n^3 )-time algorithm for 3-sum and an Θ(n^4 )-time algorithm for 4-sum. 3sum(A) for i from 0 to n - 1 for j from 0 to n - 1 for k from 0 to n - 1 if A[i] + A[j] + A[k] == 0 return true return false

4sum(A) for i from 0 to n - 1 for j from 0 to n - 1 for k from 0 to n - 1 for p from 0 to n - 1 if A[i] + A[j] + A[k] + A[p] == 0 return true return false

  1. Now assume you are given a data structure called SET which allows for insert-to-set(i) and is-in- set(i) in O(1) time, where insert-to-set(i) inserts i into the data structure and is-in-set(i) checks if i is in the data structure. Design O(n^2 )-time algorithms for both 3-sum and 4-sum (it’s not the same algorithm for both but they have a lot of common features). 3sum(A) for i from 0 to n - 1 insert-to-set(i) for i from 0 to n - 1 for j from 0 to n - 1 temp = A[i] + A[j] if is-in-set(-temp) return true return false

4sum(A) for i from 0 to n - 1 for j from 0 to n - 1 insert-to-set(A[i] + A{j]) for i from 0 to n - 1 for j from 0 to n - 1 temp = A[i] + A[j] if is-in-set(-temp) return true return false

Problem 3 (25 points)

Everyone is familiar with the standard stack (last-in-first-out) and queue (first-in-first-out) data struc- tures. In this problem we will analyze a special type of queue which is implemented using two stacks A and B. In other words, assuming that a stack has three operations, push, pop, isempty, each with cost O(1), we implement the queue as follows:

ˆ enqueue(x): push x onto stack A ˆ dequeue(): if stack B is empty then pop the entire contents of stack A pushing each element in turn onto stack B. Now pop from stack B and return the result.

It is not hard to see that this implementation will result in the correct queue behavior. Show that a sequence of n operations (enqueue and dequeue) will take O(n) time in the worst case and not O(n^2 ).

  1. Using the aggregate method; Consider a sequence of n operations. The sequence of operations will involve at most n elements. The cost associated with each element will be at most 4 i.e. (pushed into Stack1, popped from Stack1, pushed to Stack2, and popped from Stack2). Hence, the actual cost of n operations will be upper bounded by T(n)= 4 n. Hence, the amortized cost of each operation can be T(n)/n = 4n / n = 4 = O(1).
  2. Using the accounting method; We guess that the amortized costs for ENQUEUE and DEQUEUE are 3 and 1. We show that the balance P(n) satisfies P(n) - P(0) >= 0 for all n. We have P(0) = 0. If an element is not popped, then it’s only pushed twice and popped once. Thus, the cost of 3 is paid for by ENQUEUE operation. The cost for last pop operation is paid for by the DEQUEUE. Note: Alternatively, we can set the costs for ENQUEUE and DEQUEUE as 4 and 0 respectively.
  3. Using the potential method. We guess the potential function P(n) = 2 * #Elements in Stack1. P(0) = 0 and P(n) - P(0) >= 0 for all n. ENQUEUE: Actual cost of PUSH is 1. Number of elements in Stack1 increases by 1
  1. Can you provide a topological sort of the given graph? If yes, then provide such a topological sort otherwise argue why it’s not possible. It is not possible to give a topological sort of this graph since it contains at least one cycle, ACFKJB.