Algorithms - Final Study Cards

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

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

Backtracking

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

False

(True or 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.

Divide and Conquer

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?

Adjacency Matrix

A graph can be represented as a matrix of size |V|x|V|. Cell (i,j) is marked as 1 if there is an edge between i and j vertices. Below is an example. Cell (1,2) is marked 0 while cell (2,1) is marked as 1 since there is a directed edge between 2 and 1.

To find all possible solutions

Backtracking is used to solve which of the problems:

4

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)?

a. True b. 4 c. O(n^2)

Consider the following algorithm Arr is a 2-Dimentinal array of size n x n. 1. def mystery(Arr): 2. for(i = 0 to n-2): 3. for(j = i+1 to n-1): 4. if Arr[ i ][ j ] != Arr[ j ][ i ] 5. return False 6. 7. return True a. What does this algorithm compute? b. What is its basic operation (write the line number of code that is executed maximum number of times)? c. What is the time complexity of this code?

True

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

2|E|

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.

O(log n)

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(1)

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(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.

Θ(n^2)

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.

Dynamic Programming

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?

Both BFS and DFS

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?

2 because p(n) = max(c/c* , c*/c), c* being the optimal solution and c being what the algorithm produced

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

Logarithmic

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

Both optimal substructure and overlapping subproblems

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

Adjacency List

In adjacency list, we have vertices in the form of a list, and each element (a vertex) in the list contains another list (or array or linked list) with the elements that are connected to the vertex.

Memoization

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

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

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?

3SAT

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

a. adding the ith item to the knapsack b. not adding the ith item to the knapsack

In the o-1 knapsack recurrence formula f(x,i) = max{ vi + f[x-wi , i-1] , f[x , i -1] } a. The first part vi + f[x-wi , i-1] represents : _____________ b. The second part f[x , i -1] represents: _____________

Bottom-up Approach

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

O Notation (Big O)

Informally, O(g(n)) is a set of all functions with a lower or same order of growth as g(n) (within a constant multiple, as n goes to infinity). Formally, A function T(n) is said to be in O(g(n)), denoted by T(n) ∈ O(g(n)), if T(n) ≤ c(g(n)) for all n ≥ n0, for some positive constant c.

Θ Notation (Theta)

Informally, Θ(g(n)) is a set of all functions that have the same order of growth as g(n) ( bounded both above and below by some positive constant multiples of g(n), as n goes to infinity) Formally, A function T(n) is said to be in Θ(g(n)), denoted by T(n) ∈ Θ(g(n)), if c1 g(n) ≤ T(n) ≤ c2 g(n) for all n ≥ n0, for some positive constants c1 and c2.

Ω Notation (Omega)

Informally, Ω(g(n)) is a set of all functions with a higher or same order of growth as g(n) (within a constant multiple, as n goes to infinity). we say a function T(n) "is Ω(g(n))" if T(n) ∈ Ω(g(n)). Formally, A function T(n) is said to be in Ω(g(n)), denoted by T(n) ∈ Ω(g(n)), if T(n) ≥ c(g(n)) for all n ≥ n0 , for some positive constant c.

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)).

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

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?

True

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

True

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

False

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

1. False 2. Unknown 3. True 4. False 5. False

Mark each of the following questions as True/False/Unknown: 1. For every decision problem there is a polynomial time algorithm that solves it. 2. P=NP 3. If problem A can be solved in polynomial time then A is in NP. 4. If there is a polynomial time reduction from a problem A to Circuit SAT then A is NP-hard. 5. If problem A is in NP then it must be NP-complete.

Greedy Approach

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

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

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

>

Select correct inequality for the asymptotic order of growth of the below function. 5^n ? n^5

<

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

>

Select correct inequality for the asymptotic order of growth of the below function. n! ? 2n

>

Select correct inequality for the asymptotic order of growth of the below function. n^2 ? nlog⁡n

<

Select correct inequality for the asymptotic order of growth of the below function. nlogn ? 2n

Θ(n log n)

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

f(x,i) = 0 if i=0 or x=0 f(x,i) = max{ vi + f[x-wi, i-1] , f[x, i-1]}

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.

Whether the sub-problems overlap or not

The difference between Divide and Conquer Approach and Dynamic Programming is

Loop Invariant

The word 'invariant' means 'never changing or something that is true always'. A loop invariant is some condition that holds true for every iteration of the loop.

Array[W+1][n+1]

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.

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

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

Both P and Q

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

Optimal Substructure and Overlapping subproblems

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

It either stops or continues searching for other solutions

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

a[j] = a[j-1]

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

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].

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

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

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]

T(n)=Θ(nlog2n) , Case 2

What is the solution of T(n)=2T(n/2)+n/logn using the Master theorem?

Θ(n^2), Case 3

What is the solution of T(n)=2T(n/2)+n^2 using the Master theorem?

False

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

A will always be a better choice for large inputs

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

growth rates of the two algorithms

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

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

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

Min priority queue

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

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

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

the loop invariant condition is that 'item' is always maximum among the first i elements of array A.

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]

Asymptotic

a line that approaches a curve but never touches

Breadth-First Search

start at root and traverse across each level. *BFS is used to solve many real-world problems: in analyzing networks, in GPS navigation systems, in crawlers in web engines, and finding the shortest path.

Depth-First Search

start at the root and go down an edge as far as we can, then we trace back to cover other depths. *DFS is used in topographical sorting, in the traveling salesman problem, in solving maze puzzles, etc.


Conjuntos de estudio relacionados

Maternal-Newborn Chapter 18 The Newborn

View Set

Supply Chain chapter 7 study guide

View Set

Teamwork & Collaboration Vocabulary

View Set

Grammar: Effective Sentences- Parallelism

View Set

Real Estate Principles Main Chapter 1 (350 Questions)

View Set