Analysis of Algorithms Final Review
Which algorithm most closely follows the divide-and-conquer paradigm
Merge Sort
Which sorting technique is the most appropriate for sorting 1 GB of data with 100MB of available main memory
Merge sort
Is the following recurrence solvable with the master theorem? T(n) = 2T(n/2) + (n/lgn)
No
The time required to compute the hash value h(k) is
O(1)
The time complexity of the brute force algorithm used to find the longest common subsequence is
O(2n)
The time-complexity of Djikstra's algorithm is
O(VlgV + E)
What is the time complexity of adding an element to a heap?
O(lgn) + O(h)
What is the average case complexity of insertion sort
O(n^2)
What is the running time of heapsort?
O(nlgn)
Given a graph, suppose we have calculated the shortest path from a source to all other vertices. If we modify the graph s.t. the weights of all edges become double the original weight, then the shortest path:
Remains the same. Only the total weight of the path changes.
Use the substitution method to solve the recurrence T(n) = 2T((n/2) + 17) + n
T(n) = O(nlgn)
In general, the time taken by an algorithm grows with
The size of its input
Dynamic programming is used when
The solution has optimal substructure
T/F: Heap exhibits the properties of a binary tree
True
Will Djikstra's algorithm work for an undirected graph
Yes, if an undirected edge is viewed as the combination of two edges directed at opposite directions
With hashing, the element is stored in slot h(k). Thus, in order to compute the slot from the key k, we use
a hash function h
How is the safe edge chosen and added to set A in Kruskal's algorithm?
by choosing a least-weight edge
Consider the problem of search an element x in an array A of size n. The problem can be solved in O(lgn) in which cases? a. array is sorted b. array is unsorted c. array is reversely sorted
case a and case c
The simplest collision resolution technique is
chaining
A ________ of a connected graph is a minimal set of edges whose removal separate the graph into two components
cut
A ________________ algorithm solves each sub-problem only once, then saves its answer in a table. This avoids the work of recomputing the answer every time it solves each sub-problem
dynamic-programming
The algorithm design techniques for simple insertion sort is
incremental
An edge is a light edge satisfying a given property if its weight is the
minimum of any edge satisfying the property
Dynamic programming uses additional memory to save computation time; this serves as an example of a
time-memory tradeoff
The second step of the substitution method for solving recurrences is
use mathematical induction to find constants and show solution works
Merge sort has a time complexity of
θ(nlgn)
The expected running time (average case) of quicksort is
θ(nlgn)
What is the time complexity of T(n) = 2T(n/2) + cn; n > 0, c = some constant
θ(nlgn)
The longest common sequence for "ABCDGH" and "AEDFHR" is:
"ADH" of length 3
What are the two conditions needed to apply a greedy algorithm
1. Greedy choice property 2. Optimal substructure property
Depth-First Search (DFS)
A search technique that explores the edges out of the most recently discovered vertex that still has unexplored edges leaving it. Once all edges have been explored, the search "backtracks" to explore edges leaving the vertex from which it was discovered
Quicksort and merge sort are which type of algorithm
Divide and conquer
What paradigm can be used to find the solution of the following problem in minimum time: Given a set of non-negative integers, and a value k, determine if there is a subset of the given set with a sum equal to k
Dynamic Programming
The two algorithms for solving the minimum-spanning tree problem are:
Kruskal's algorithm , Prim's algorithm
Greedy Choice Property
A global optimum can be reached by selecting the local optimums
What is "programming" in the context of dynamic programming
A tabular method to solve subproblems
In a DFS of an undirected graph G, every edge of G is either
A tree edge or a black edge
Why is quicksort often the best practical choice for sorting given its worst-case running time θ(n^2)?
Because it has a fast expected running time (θ(nlgn))
Which colored vertices represent the frontier between discovered and undiscovered vertices?
Gray
The running time of Prim's algorithm using a Fibonacci heap is
O(E+VlgV)
We want to create a heap using the elements from an array consisting of n elements. The time complexity for building the heap will be
O(nlgn)
in-place sorting
Only a constant number of array elements are stored outside the input array at any time
Chaining
Placing all elements that hash to the same slot into the same linked list
Heap can be used as which data structure?
Priority queue
What is meant by the degree of a vertex in an undirected graph?
The number of edges incident on it
Optimal Substructure Property
The optimal solution for the problem can be formed on the basis of the optimal solution to its subproblems
T/F: A recursion tree is best used to generate good guesses, which can then be verified with the substitution method
True
T/F: For undirected graphs, The number of odd degree vertices is even, and the sum of degrees of all vertices is even
True
T/F: Quicksort uses partitioning
True
The worst-case running time of an algorithm
gives us an upper bound on the running time for any input
In BFS, if (u,v) belongs to E and vertex u is black, then the vertex is either gray or black; that is, all vertices adjacent to black vertices:
have been discovered
Which algorithm would work best to sort data as it arrives one piece at a time, perhaps from a network?
insertion sort
Consider a hash table of size 13. Which index positions would the keys 27, 130 map to?
1, 0
What is the sequence of four steps for developing a dynamic-programming algorithm?
1. Characterize the structure of an optimal solution 2. Recursively define the value of an optimal solution 3. Compute the value of an optimal solution, typically in a bottom-up fashion 4. Construct an optimal solution from computed information
Priority Queue
A data structure for maintaining a set S of elements, each with an associated value called a key
T/F: Djikstra's algorithm may not terminate if the graph contains negative-weight edges
False
T/F: In an undirected graph, self-loops are possible
False
T/F: Prim's algorithm executed on a planar graph will perform best when the priority queue is implemented using an array
False
T/F: The array [10, 3, 5, 1, 4, 2] is a max heap
False
T/F: The recurrence relation T(n) = 4T(n/2) + n^2/lgn can be solved using the master theorem
False
We say that an edge (u,v) in E crosses the cut (S, V-S) if one of its endpoints is in S and the other is in:
V-S
The running time of an algorithm on a particular input is the number of
primitive operations or "steps" executed
For an edge (u,v), a process of testing whether we can improve the shortest path to v found so far by going through u and updating the shortest path estimate is called
relaxation
Use the master theorem to solve the recurrence T(n) = 2T(n/2) + nlgn
θ(nlg * lgn)
Consider the strings "PQRSTPQRS' and "PRATBRQRPS". What is the length of the longest common subsequence?
7
Dynamic programming is typically applied to _____________
optimization problems
Use the master theorem to solve the recurrence T(n) = 4T(n/2) + n
θ(n^2)
Suppose we are sorting an array of eight integers using heapsort and have just finished heapifying (either max or min). The array looks like: [16, 14, 15, 10, 12, 27, 28]. How many heapify operations have been performed on the root of the heap?
2
Rank the following functions in increasing order of growth: 1. n^(lgn) 2. sqrt(n) 3. n^(3+sin(n)) 4. lgn^n
2,4,3,1
In practice, merge sort beats out insertion sort when n > ____
30
Consider a hash table of size 11 whose hashing function is: h(item) = item%11. Insert the following integers: 54, 26, 93, 17, 77, 31
77 ___ 26 93 17 __ 31 54
What is the hash key value of 'Donaldson' if you assign each letter its corresponding number in the alphabet (ex. f =6) and use 9 as the divisor?
8
Divide and Conquer paradigm
Break the problem into several subproblems similar to the original problem but smaller in size, solve the subproblems recursively, combine the solutions to solve the original problem.
______________ algorithm solves the single-source shortest paths problem on a weighted directed graph G = (V,E) for the case in which all edge weights are non-negative
Djikstra's
When designing a good hash function, the two schemes heuristic in nature are:
Hashing by division, hashing by multiplication
Given an unsorted array with the property "every element is at most k distance from its position in the sorted array, where k is a positive integer smaller than the size of the array". Which sorting algorithm can be easily modified for sorting this array, and what is the time-complexity
Heap sort, O(nlgk)
Which is the order of the following algorithms with respect to their time complexity in the best case? 1. quick sort 2. selection sort 3. merge sort 4. insertion sort
Insertion sort < quick sort < merge sort < selection sort
Which sorting algorithm could easily be parallelized? (which would require the least extra programming to have them work together to sort a list)
Merge sort
What data structure is used to implement Djikstra's shortest path's algorithm on unweighted graphs so that it runs in linear time?
Queues
What is the model of the implementation technology for analyzing an algorithm?
Random-access machine model
Given an array A = [5, 2, 4, 6, 1, 3] what is the sequence of elements after the third step if insertion sort is applied?
[2, ,4, 5, 6, 1, 3]
The master theorem applies to recurrences of the following form: T(n) + aT(n/b) + f(n) What are the constraints for the constants a and b?
a >= 1; b > 1
Given the input (4322, 1334, 1471,9679,1989, 6171, 6173, 4199) and the hash function x%10, which of the statements are true? a. 9679, 1989, 4199 hash to the same value b. 1471, 6171, hash to the same value c. all elements hash to the same value d. each element hashes to a different value
a and b
Which of the following statements are true: a. A hash function takes a message of arbitrary length and generates a fixed length code b. A hash function takes a message of fixed length and generates a code of variable length c. A hash function may give the same hash value for distinct messages
a and c
Let P be a quicksort program to sort numbers in ascending order using the first element as the pivot. Let t1 and t2 be the number of comparisons made by P for inputs {1, 2, 3, 4, 5} and {4, 1, 5, 3, 2} respectively. What is t1's relationship to t2?
t1 > t2
What type of graph do Minimum Spanning Tree algorithms work on?
weighted, connected, undirected
When does mergesort stop breaking the list into sublists?
when each sublist has a single element
Collision
when two keys hash to the same slot
To keep track of progress, breadth-first search colors each vertex
white, gray or blakc
In DFS, each vertex is initially _______, is _________ when it is discovered in the search, and is ______ when it is finished (when its adjacency list has been completely examined)
white, gray, black
A shortest path from u to v is a path of minimum weight from u to v. The shortest-path weight from u to v is defined as:
δ(u,v) = min{wp)) s.t. p is a path from u to v}
What is the value of the recurrence T(n) = T(sqrt(n)) + θ(lglgn)
θ((lglgn)^2)
In a hash table in which collisions are resolved by chaining, an unsuccessful search will take an average-case time of
θ(1 + α)
The running time of DFS
θ(V+E)
We say that g(n) is an asymptotically tight bound for f(n) if f(n) = ?
θ(g(n))
Insertion sort has a worst-case running time of
θ(n^2)
On an input array of n numbers, the quicksort algorithm has a worst-case running time of
θ(n^2)