## Computer Science Quiz - III: Part 2## Topic - 6 Algorithms1) Assume that T(n) is a function that the recurrence defines. T(n) = 2T(n/2) + n when n >= 2 and T(1) = 1, Which of the following claims is accurate? - T(n) = O(n)
- T(n) = O(n logn)
- T(n) = O(n
^{1/2}) - T(n) = O(logn)
Given Recurrence Relation: T(n) = 2T(n/2) + n ... (i)
T(n) = aT(n/b) + f(n) ... (ii) Comparing (i) and (ii), we get a = 2, b = 2, and f(n) = n Case I: f(n) = O(n^log n = O(n^log n = O(n^1) n = O(n), Equation holds Hence, T(n) = O(n). Therefore, (a) is the correct answer. 2) There is a total of25 horses, and you need to identify the top 3 speed horses. To determine their relative speeds, races can be held between a maximum of five horses. You can never determine the horse's true speed in a race. Identify the number of races necessary to findthe top three horses. - 5
- 6
- 7
- 8
Run the seventh race now with the following horses. 1) The second and third-place horses in the sixth race 2) Select the second and third-fastest horses in the group that are tied for first in the sixth race. 3) Select the animal in the group that finished second fastest in the sixth race.
Therefore, (c) is the correct answer. 3) There are n unique elements in an unordered list. To discover an element in this list that is neither the maximum nor the minimum, comparisons must be made a total of - O(n)
- O(logn)
- O(n logn)
- O(1)
We simply need to compare any 3 of the elements. The middle element of the three will be one of the not maximum and minimum elements. Since there are constant numbers of comparisons, the time complexity is (1). Therefore, (d) is the correct answer. 4) Two lists are supposed to be concatenated in O(1) time. Which of the subsequent list implementations should be used? - Singly Linked List
- Doubly Linked List
- Circular Singly Linked List
- Circular Doubly Linked List
Singly, doubly, and circular singly linked lists cannot be used to implement the given idea as we do not have a pointer to the last node. To concatenate two lists of either type, we need to traverse one of the lists to get access to the last node, which requires O(n) time. A circular doubly linked list can be concatenated in constant time O(1). We only need to execute the correct sequence of code. Let the front and last of the pointer of list1 and list2 be front1 and last1 and front2 and last2, respectively.
We can rename the pointer of the new node as front = front1 and last = last2. 5) The worst-case time complexities of the Tower of Hanoi with n disks problem, the binary search on the sorted list of n elements, the quick sort on an array of n elements and the heap sort on an array of n elements are as follows - O(2
^{n}), O(n logn), O(n^{2}), O(nlogn), respectively - O(2
^{n}), O(logn), O(ln logn), O(n logn), respectively - O(2
^{n}), O(logn), O(n logn), O(n^{2}), respectively - O(2
^{n}), O(logn), O(n^{2}), O(n logn), respectively
The - We can move only one disk at a time, and
- A larger disk cannot be placed above a smaller disk.
The time complexity of this problem is O(2 In the worst-case, the time complexity of binary search is O(logn) In the worst-case, the time complexity of heap sort remains O(n logn) but the time complexity of quick sort changes to O(n 6) 6 Q Take into account the following claims: I. A leaf node is always where a max-smallest heapelement can be found. II. The root node's child is always the second-largest element in a max-heap. III. A binary search tree can be used to quickly create a max-heap in Q(n) time. IV. A max-heap can be converted into a binary search tree in Q(n) time. Which of the aforementioned claims is true? - I, II, and IV
- I, II, and III
- II, III, and IV
- I, III and IV
The first statement is true. Leaf nodesin a max heap always containthe smallest element. Therefore, we must search for the minimum value for each leaf node. The worst-case complexity is O(n). The second statement is also true. The largest element is always found at the top, and the second largest element is always the root child. The third statement is true because building a max heap requires O(n) time. The fourth statement is false since it takes O(n logn) time to create a binary search tree from a max-heap. Therefore, (b) is the correct answer. 7) When used on an array that is sorted or nearly sorted, which of the following sorting algorithms, in its typical implementation, performs the best (maximum 1 or two elements are misplaced)? - Heap Sort
- Merger Sort
- Quick Sort
- Insertion Sort
In the best case, the time complexity of insertion sort becomes O(n) since it compares the elements and inserts them at their right position. So, when it comes to sorting a nearly sorted array, it will not take more time. On the other hand, heap sort, and merge sort will take O(n logn) time, and quick sort will take O(n 8) Think about a scenario where a swap procedure is exceedingly expensive. Which of the following sorting algorithms ought to be chosen in order to reduce the overall number of swap operations? - Insertion Sort
- Selection Sort
- Merge Sort
- Heap Sort
9) If an element in an array X is larger than every element to its right, it is referred to as the leader of the array. a good approach for finding every leader in an array - Use a left-to-right pass of the array to solve it in linear time.
- Use a right-to-left pass of the array to solve it in linear time.
- Use divide and conquer to solve the problem in Q(nlogn) time
- Find a solution in time theta (n
^{2}).
The plan is to count up the most elements that have been reached so far while scanning the array's elements from right to left. Print the value whenever the maximum fluctuates. To put the concept into practice, take the following actions: - The last index position is where we begin. There are never any elements to its right. Hencethe last place is always the leader.
- After that, we continue iterating through the array until theindex position = 0. We constantly monitor the maximum value.
- Every time a maximum value is encountered higher than the highest maximum value previously encountered, we either print or save the value because it is the leader.
This can be done in linear time. Hence, (b) is the correct answer. 10) Assume that G is an undirected weighted graph and e is an edge in G with the maximum weight. Consider the minimum weight spanning tree in G that has the edge e in it. That which is always TRUE among the following statements? - All the edges in G have unique weight
- All the edges in some cut sets in G are of maximum weight
- G has a unique minimum spanning tree
- Edge e cannot be a part of a cycle in G
Consider the following graph taken as an example: There are three edges with maximum weight. The edge ef will be the part of the mst.
Option A: This statement is false because we cannot conclude that each edge is of unique weight. Although edges can be the same weight. Option B: The cut set {ab, ac} contains both the edges of maximum weight. Hence, option (b) is true. Option C: This statement is false. Here, we have the option to choose between edges ab and ac. So, we can guarantee uniqueness. Option D: This statement is also false. There are possibilities that e can be a part of a cycle. 11) The appropriate data structure usedto utilize in order to implement Dijkstra's shortest path algorithm on unweighted graphs is: - Stack
- Heap
- BST
- Queue
Both the heap and the priority queue are particularly attractive data structuresallowing: - Add a component to the heap that has a priority.
- The highest-priority piece should be taken out of the heap or priority queue and returned.
- Access to the Peak element that has maximum prioritywithout deleting it.
Maintaining a list of components and searching through it for the one with the highest priority is a straightforward approach to create a heap or priority queue data type. This gives Dijkstra's shortest path algorithm on an unweighted graph an implementation time of O(n). 12) Which of the following claims about the Bellman-Ford shortest path algorithm is true? I. If there is a negative weighted cycle, it is always found. II. Determines whether any negative weighted cycle can be reached from the source. - Only I
- Only II
- Both I and II
- Neither I nor II
The Bellman-Ford shortest path algorithm is used to determine a single-sourceshortest path. Therefore, it can only detect cycles that can be reached from a certain source and cannot find cycles with anegative weight that are not reachable from the given source. Think about a disconnected situation where a negative weight cycle cannot be reached at all from the source.If there is a negative weight cycle reachable from the source, after (number of vertices- 1)number of iterations, the algorithm will detect it. So, saying it will always find the negative weight cycle is wrong. Hence, the statement I is false, and the statement II is true. 13) Think about the tree arcs of a BFS traversal starting at source node W in an unweighted, connected, undirected graph. Using the tree T created by the tree arcs, one may compute - The shortest route between each pair of vertices
- The shortest route from W to each graph vertex.
- Only those nodes that are leaves of T have the shortest pathways from W to them.
- The graph's longest path.
The tree formed by doing BFS traversal from node W gives shortest path of all the nodes from W. In an unweighted graph, BFS always finds the shortest route from the source to every other vertex. Therefore, (b) is the correct answer. 14) Assume that G is an undirected connected weighted graph with a set of distinct positive edge weights. Which of the following statements (if any) is/are TRUE if each edge weight is increased by the same amount? I. There will be no change in the minimum spanning tree. II. The shortest distance between all the vertices will remain same. - Only I
- Only II
- Both I and II
- Neither I nor II
The shortest route might alter. The reason is thatany pair of edges, such as a and b, may have a different number of edges in various pathways. For illustration, suppose the shortest path between a and bhas 4edges and a weight of 12. Allow for a second path with two edges and a weight of 17. The weight of the shortestpath after incrementing each edge weight with 5is now 12+ 4*5 = 32. The other path's weight is boosted by 2*5, making it 17+ 10 = 27. Thus, the shortest way switches to another path with aweight of 27. There is no modification to the Minimum Spanning Tree. Keep in mind that in Kruskal'salgorithm, the edges are sorted first. If all weights are increased, the edges' order won't change. 15) What is the weight of the minimum spanning tree of the given graph? - 28
- 36
- 34
- 40
Using Kruskal's algorithm, we first sort all the edges in descending edge weight. And try to select the edges with minimum edge weight and we have to make sure we avoid any type of cycle formation in the spanning tree:
So, the weight of the minimum spanning tree = 2 + 2 + 2 + 3 + 4 + 4 + 5 + 12 = 34. Therefore, (c) is the correct answer. 16) The Floyd Warshall algorithm for computing all-pair shortest routes is based on the following: - Greedy Method
- Dynamic Programming
- Divide and Conquer
- Subtract and Conquer
All pairs of shortest-path issues can be resolved using the Floyd Warshall algorithm. Finding the shortest distances between each pair of vertices in an edge-weighted directed graph is the goal of the problem. Dynamic programming serves as its foundation. Therefore, (b) is the correct answer. 17) Consider a chain of matrices that have been multiplied: A1 x A2 x A3 x A4 x A5, where A1, A2, A3, A4 and A5 matrices with dimensions of 5 x 10, 10 x 35, 35 x 15, 15 x 25, and 25 x 100 respectively. What will the minimum total number of scalar multiplications in the parenthesisation of A1 x A2 x A3 x A4 x A5 be _____________? - 18775
- 34000
- 9000
- 19375
Formula to calculate the cost of multiplication: m[i, j] represents the cost of multiplying matrix number i to j. m[i, j] = { 0 if (i == j)} m[i, j] = { min[m[i, k] + m[k+1, j] + P The range of k will be i <= k < j. We will calculate the cost on each value of k and select the minimum cost. We have created this table using the matrix dimension:
The cost of multiplying one matrix is zero that's why we have filled zero in the respective cells.
m[1, 2] = m[1, 1] + m[2, 2] + P = 0 + 0 + 5 * 10 * 35 = 1750 Similarly, we can find the cost of m[2, 3], m[3, 4] and m[4, 5].
m[1, 3] = m[1, 1] + m[2, 3] + P = 0 + 5250 + 5 * 10 * 15 = 0 + 5250 + 750 = 6000
m[1, 3] = m[1, 2] + m[3, 3] + P = 1750 + 0 + 5 * 35 * 15 = 4375 We have got the minimum cost for k = 2. Similarly, we can calculate the cost of m[2, 4] and m[3, 4]. The complete table will look like the following: The cost of multiplying the matrices is 18750. 18) In order to find the longest common subsequence between two strings,we use the concept of dynamic programming. Using an array L[M, N], the values of L(i, j) could be determined using dynamic programming based on the correct recursive definition of L(i, j). Which of the following claims about the dynamic programming answer to the recursive definition of L(i, j) is TRUE? - To correctly compute the values of L(i, j), all elements ofL(M, N)must be initialized to 0.
- Both a row-majororder and a column-majororder of L(M, N)can be used to compute the values of l(i, j).
- Both the row-majororder and the column-major order of L (M, N)cannot be used to compute the values of l(i, j).
- If either p < ror q <s, L[p, q] must be calculated before L[r, s].
Option A is False because it is not necessary to initialized all the elements of L[M, N] to 0. We only fill zeros in the first row and the first column. Option B is True. By doing the computation either in row major or column major, we can perform the desired computation. Option C is False. It negates the option B which is not true. Option D is False because to compute the value of l[i, j], we only need the values of l[i-1, j], l[i, j-1] and l[i-1, j-1]. The condition p<r or q<s do not completely satisfy the condition. 19) Match the following:
- A - (iii), B - (iv), C - (ii), D - (i)
- A - (iv), B - (iii), C - (ii), D - (i)
- A - (iii), B - (iv), C - (i), D - (ii)
- A - (i), B - (ii), C - (iii), D - (iv)
- An algorithm for lossless data compression is Huffman coding. Input characters are given variable-length codes, the lengths of which are determined by the frequency of the matching characters.
**It is based on the greedy method.** - All pairs of shortest-path issues can be resolved using the Floyd Warshall algorithm. Finding the shortest distances between each pair of vertices in an edge-weighted directed graph is the goal of the problem.
**Dynamic programming serves as its foundation.** - The selection sort method sorts an array by repeatedly selecting the first element from the unsorted portion that is the least in value while still taking into account ascending order. The least entry (regarding ascending order) from the unsorted subarray is chosen and relocated to the start of the unsorted subarray in each iteration of the selection sort.
**Subtract and conquer is the theory behind the concept**. **The Divide and Conquer concept serves as the foundation for the sorting algorithm known as Merge Sort**. This algorithm divides the array into two equal parts, which are then combined in a sorted way.
Therefore, option (c) is the correct answer. 20) Think about the sequence abbbcddddee. Each letter in the string needs to have a binary code that meets the requirements listed below: - The code assigned to one letter for any pair of letters cannot be the prefix of the code assigned to the other letter.
- The letter that appears earlier in the dictionary order is given a code whose length is at most equal to the code given to the other letter for any two letters with the same frequency.
What is the shortest encoded string length among all the binary code assignments that meet the two properties mentioned above? - 24
- 25
- 26
- 27
The properties mentioned in the question are the same as for Huffman coding.
i. For creating a Tree, we will select the alphabet with the minimum frequency and create the Huffman tree in bottom-up fashion. ii. We will assign binary 1 to the right edge of a node and binary 0 to the left edge of a node. To assign the code, we will traverse the tree from the root to all the leaf nodes containing alphabet.
Binary Code Assigned to each alphabet: a - 0100, Length = 4 b - 00 , Length = 2 c - 0101, Length = 4 d - 1, Length = 1 e - 011, Length = 3 Shortest length of the string in binary = 4 * 1 + 2 * 3 + 4 * 1 + 1 * 4 + 3 * 2 = 4 + 6 + 4 + 4 + 6 = 24 Therefore, (a) is the correct answer. Next TopicComputer Science Quiz - II: Part 1 |