Algo midterm

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Match each of the following asymptotic notation with their correct definition. f(n) and g(n) are some functions ; a and b are constants. 1. Big-θ notation 2. Big-O notation 3. Big-Ω notation

1. For large values n the running time of f(n) is at least a⋅g(n) and at most b⋅g(n) 2. For large values of n the running time of f(n) is at most b⋅g(n) 3. For large values of n the running time of f(n) is at least b⋅g(n)

If the solution obtained by an approximation algorithm is : 10 The optimal solution is : 5 What will be the value of the approximation ratio? 1 5 2 0.5

2

For an undirected graph G, what will be the sum of degrees of all vertices. (degree of a vertex is the number of edges connected to it.)V: number of vertices, E: number of edges.

2|E|

In the exploration to show that the independent set problem is NP-Complete we have used which of the following NP-Hard problems?

3SAT

Consider the following algorithm 1 Bubble-sort(a) 2 for i = a.length() down to 1 3 for j = 1 to i-1 4 if a[j]>a[j+1] 5 swap(a[j],a[j+1]); 6 end if What is its basic operation (write the line number of code which would define the execution time of the code)?

4

Select correct inequality for the asymptotic order of growth of the below function. log𝑛 --- √n

<

Given the following algorithm foo(n) if n <= 1 return 1 else x = foo(n-1) for i = 1 to n x = x + i return x Determine the asymptotic running time. Assume that addition can be done in constant time.

Θ(𝑛2)

What is the solution of 𝑇(𝑛)=2𝑇(𝑛/2)+𝑛2 using the Master theorem?

Θ(𝑛2)Θ(n2), Case 3

Solve the following recurrence by giving the tightest bound possible. T(n) = 4T(n/4) + 4n

Θ(𝑛log𝑛)

Select correct inequality for the asymptotic order of growth of the below function. 𝑛𝑙𝑜𝑔𝑛 --- 2^𝑛

<

Select correct inequality for the asymptotic order of growth of the below function. 5^𝑛 --- 𝑛^5

>

Select correct inequality for the asymptotic order of growth of the below function. 𝑛! --- 2^𝑛

>

Select correct inequality for the asymptotic order of growth of the below function. 𝑛^2 --- 𝑛log𝑛

>

When we say algorithm A is asymptotically more efficient than B, what does that imply?

A will always be a better choice for large inputs

Let W(n) and A(n) denote respectively, the worst case and average case running time of an algorithm executed on an input of size n. which of the following is ALWAYS TRUE?

A(n) = O(W(n))

Which of the following correctly defines what a 'recurrence relation' is?

An equation (or inequality) that relates the nth element of a sequence to its predecessors (recursive case). This includes an initial condition (base case).

To find the optimal solution for 0-1 knapsack, what would be dimensions of the extra array that we would need? The knapsack has a capacity of W, and there are total of n items. Assume we are using the approach that was discussed in the exploration.

Array[W+1][n+1]

What is the correct loop invariant for the below code. A is an array. [The for loop runs for i=0, 1, ..., len(A)-1] for i in range(len(A)): answer += A[i] return answer

At the start of iteration i of the loop, the variable answer should contain the sum of the numbers from the subarray A[0:i].

Which of the following techniques can be called as intelligent exhaustive search?

Backtracking

Given two vertices s and t in a connected graph G, which of the two traversals, BFS and DFS can be used to find if there is a path from s to t?

Both BFS and DFS

We use reduction to prove that NP-Completeness of a problem X from A. As a part of reduction we must prove which of the following statements? Assume A is a NP-Hard problem. Statement P: A can be transformed to X in a polynomial time Statement Q: We can obtain solution to A from X in polynomial time

Both P and Q

Which of the following is/are property/properties of a dynamic programming problem?

Both optimal substructure and overlapping subproblems

In which of the following approaches we start with the base case and proceed to solve the bigger subproblems?

Bottom-up Approach

Following is a pseudocode for graph traversal approach: traverse (G, s) //Where G is the graph and s is the source node let Q be queue. Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked. mark s as visited. while ( Q is not empty) //Removing that vertex from queue,whose neighbour will be visited now v = Q.dequeue( ) //processing all the neighbours of v for all neighbours w of v in Graph G if w is not visited Q.enqueue( w ) //Stores w in Q to further visit its neighbour mark w as visited.

Breadth First Search

In molecular biology, DNAs and proteins can be represented as a sequence of alphabets. DNA sequences consist of A, T, G, C representing nucleobases adenine, thymine, guanine and cytosine. Two sample DNA sequences GACGGATTAG and GATCGGAATAG. You are given two sequences query sequence and database sequence. You need to find the similarity between them. Similarity between sequences is the longest matching subsequence. This problem is similar to which of the problems from the explorations?A.Knapsack B.N-Queens C.Longest Common Subsequence D.Change Making Problem

C.Longest Common Subsequence

Following is a pseudocode for a graph traversal technique: traversal-recursive(G, s): mark s as visited for all neighbours w of s in Graph G: if w is not visited: traversal-recursive(G, w) This pseudocode is for Breadth First Search approach

Depth First Search

A binary search algorithm searches for a target value within a sorted array. Binary search compares the target value to the middle element of the array; if they are unequal, the half in which the target cannot lie is eliminated and the search continues on the remaining half until the target value is found or until a search can no longer be performed. This problem can be solved using which of the techniques?

Divide and Conquer

Given two integer arrays to represent weights and profits of 'N' items, find a subset of these items that will give us maximum profit such that their cumulative weight is not more than a given number 'C'. Best technique to solve this problem is?

Dynamic Programming

Which of the following recurrence relations is correct representation of the towers of Hanoi problem that was discussed in the exploration?

F(n) = 2F(n-1) + c

What is the correct recurrence formula for the unbound knapsack problem that was discussed in the exploration? Consider the weight of the items w[1..n], value of the items v[1..n]

F(x) = max{ F[x-wi] + vi }

Mark True/False: A spanning tree of a graph should contain all the edges of the graph.

False

We are given an array of numbers and we are asked to find an optimal solution to maximize the sum of numbers (i.e continuous subsequence that has maximum sum). We would always end up with the same combination of numbers as answer.

False

When performing the topological sort we always find a unique solution.

False

For every decision problem there is a polynomial time algorithm that solves it. [ Select ] ["False", "True"] P=NP [ Select ] ["True", "False", "Unknown"] If problem A can be solved in polynomial time then A is in NP. [ Select ] ["False", "True"] If there is a polynomial time reduction from a problem A to Circuit SAT then A is NP-hard. [ Select ] ["False", "True"] If problem A is in NP then it must be NP-complete. [ Select ] ["False", "True"]

False, unknown, true, false, false

Write the loop invariant for the following code: item = -INF (minus infinite) for (i = 0 to n-1) if (A[i] > item) item = A[i]

For each iteration of the loop from i to n-1, the variable item holds the maximum value of the subarray processed so far.

Prim's and Kruskal's algorithms to find the MST follows which of the algorithm paradigms?

Greedy Approach

What does a backtracking algorithm do if it reaches a complete solution?

It either stops or continues searching for other solutions

If you are given different versions of the same algorithm with the following complexity classes, which one would you select? Quadratic Polynomial Logarithmic Linear

Logarithmic

What is the solution of 𝑇(𝑛)=2𝑇(𝑛/2)+𝑛/𝑙𝑜𝑔𝑛T(n)=2T(n/2)+n/logn using the Master theorem?

Master Method does not apply

In dynamic programming, the technique of storing the previously calculated values is called

Memoization

Which of the following data structures can be used to implement the Dijkstra algorithm most efficiently?

Min priority queue

Given an array A of size n, we want to access the ith element in the array, 0<i<n. What will be the time complexity of this operation using an efficient algorithm?

O(1)

Given a sorted array A of size n, we want to find if an element k belongs to this array. What will be the best time complexity to perform this search operation?

O(log n)

Given an array A of size n, we want to find if an element k belongs to this array. What will be time complexity of this search operation? Assume that we don't know anything about the order of elements in the array.

O(n)

Find time complexity: If you have been given a linked list and you want to search a value in it in using an efficient algorithm. What would the time complexity of your search operation?

O(n) because you cannot index a linkedlist so you can't perform binary search or similar, you must start at head and do .next until you find the item, resulting in O(n) worst case.

What are the major required aspects in a problem in order to apply Dynamic Programming Technique?

Optimal Substructure and Overlapping subproblems

In the Longest Common Subsequence problem assume we are comparing two strings of lengths m and n. In the bottom-up approach the solution we build a 2-Dimensional array called Cache[m][n]. The final solution was obtained by accessing which element of the cache?

The element in the bottom right corner of the cache[m][n]

Suppose you won a shopping spree and you can take any number of items from the Amazon store (assume that there is only one copy of each item). There are n items in the store each of value v[1..n] and weights w[1..n]. You are given a trolley that can only carry a total of W weight of items. Don't worry about the size of the items, they would some how magically fit in the trolley. You want to find the maximum value of items that you can pick. Being a programmer you decide to use dynamic programming to solve this problem. Write the recurrence formula that would represent the approach of your solution. Mention both the base case and the recursive case of the recurrence formula.

This is a version of the 0-1 Knapsack problem and thus the recurrence formula would be: base case: f(x, i) = 0 recursive case: max{vi + f(x - wi, i - 1), f(x, i-1)}

Backtracking is used to solve which of the problems:

To find all possible solutions

Dijkstra is used to solve for a shortest path in a weighted graph with non negative weights.

True

Is the following a property that holds for all non-decreasing positive functions f and g? (True=Yes/ False=No) If f(n) = O(n2) and g(n) = Theta(n2), then f(n) = O(g(n)).

True

Mark True/False: A graph can have many spanning trees.

True

We can use Divide and Conquer technique to solve a problem in which of the following scenarios?

We can break the problem into several subproblems that are similar to the original problems but smaller in size

The difference between Divide and Conquer Approach and Dynamic Programming is

Whether the sub-problems overlap or not

What is the basic operation (that which is executed maximum number of times) in the following code? reverse(a): for i = 1 to len(a)-1 x = a[i] for j = i down to 1 a[j] = a[j-1] a[0] = x

a[j] = a[j-1]

In the o-1 knapsack recurrence formula f(x,i) = max{ vi + f[x-wi , i-1] , f[x , i -1] } The first part vi + f[x-wi , i-1] represents : [ Select ] ["adding the ith item to the knapsack", "not adding the ith item to the knapsack"] The second part f[x , i -1] represents: [ Select ] ["adding the ith item to the knapsack", "not adding the ith item to the knapsack"]

adding the ith item to the knapsack not adding the ith item to the knapsack

given two numbers n, k, find an algorithm that outputs all combinations of k numbers in [1...n] which technique could be used to solve this problem?

backtracking technique (for all possible answers instead of one optimal answer)

In [ Select ] ["bottom-up approach", "top-down approach"] we start with the base case and build the solution starting from base case. In [ Select ] ["bottom-up approach", "top-down approach"] we start solving the the bigger problem proceed towards the base case.

bottom-up approach, top-down approach

Problem: Given an array of numbers find if there is a subset that adds to a given number. Return True if there exists such subset, else return False. The subset of numbers need not be continuous in the array. We don't know anything about the order of the elements in the array. Identify which of the following strategies can be used to solve this problem. Dynamic Programming: [ Select ] ["Can be used", "Cannot be used"] Backtracking: [ Select ] ["Can be used", "Cannot be used"] Brute force Approach: [ Select ] ["Cannot be used", "Can be used"] Divide and Conquer: [ Select ] ["Can be used", "Cannot be used"]

can, can, can, cannot

Which of the following equations correctly represent the factorial function. Factorial of a number n is given by: n! = n*(n-1)(n-2)...3*2*1

f(n) = n f(n-1)

Which of the following can be used to compare two algorithms?

growth rates of the two algorithms

Rank the following functions by increasing order of growth: log( n! ), 10000n2, log(n3), 2n, n2log(n)

log(n3), log( n! ), 10000n2, n2log(n), 2n

Mark True/False. Removing the maximum weighted edge from a Hamiltonian cycle will result in a Spanning Tree

true


Ensembles d'études connexes

Programming Fundalmentals II — Test 3

View Set