Top MCQs on Greedy Algorithms with Answers Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. So the problems where choosing locally optimal also leads to global solution are the best fit for Greedy. More on Greedy Algorithm Greedy Algorithms Quiz Top MCQs on Greedy Algorithms with Answers Please wait while the activity loads. If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled. If loading fails, click here to try again Question 1 A networking company uses a compression technique to encode the message before transmitting over the network. Suppose the message contains the following characters with their frequency: C character Frequency a 5 b 9 c 12 d 13 e 16 f 45 Note : Each character in input message takes 1 byte. If the compression technique used is Huffman Coding, how many bits will be saved in the message? 224 800 576 324 Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 1-Explanation: Total number of characters in the message = 100. Each character takes 1 byte. So total number of bits needed = 800. After Huffman Coding, the characters can be represented with: f: 0 c: 100 d: 101 a: 1100 b: 1101 e: 111 Total number of bits needed = 224 Hence, number of bits saved = 800 - 224 = 576 See here for complete explanation and algorithm.Question 2 What is the time complexity of Huffman Coding? O(N) O(NlogN) O(N(logN)^2) O(N^2) Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 2-Explanation: O(nlogn) where n is the number of unique characters. If there are n nodes, extractMin() is called 2*(n – 1) times. extractMin() takes O(logn) time as it calls minHeapify(). So, overall complexity is O(nlogn). Question 3 In question #2, which of the following represents the word "dead"? 1011111100101 0100000011010 Both A and B None of these Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 3-Explanation: character code-word f 0 c 100 d 101 a 1100 b 1101 e 111 The word dead can be represented as: 101 111 1100 101 However, the alternative codeword can also be found by assigning 1 to the left edge and 0 to the right edge of the tree, i.e. dead can also be represented as: 010 000 0011 010 See here for more details of the algorithm. Question 4 Which of the following is true about Kruskal and Prim MST algorithms? Assume that Prim is implemented for adjacency list representation using Binary Heap and Kruskal is implemented using union by rank. Worst case time complexity of both algorithms is same. Worst case time complexity of Kruskal is better than Prim Worst case time complexity of Prim is better than Kruskal None of these Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 4-Explanation: Kruskal's algorithm and Prim's algorithm are both used to find the minimum spanning tree (MST) of a weighted graph, but they have different approaches. Kruskal's algorithm: Kruskal's algorithm sorts the edges of the graph in the non-decreasing order of their weights and then iterates over the sorted edges, adding them to the MST if they do not create a cycle. It uses the disjoint-set data structure with the union by rank and path compression to efficiently detect cycles and maintain the disjoint sets. The time complexity of Kruskal's algorithm is primarily determined by the sorting step, which takes O(E log E) time, where E is the number of edges in the graph. The union-find operations for cycle detection take nearly constant time per operation, approximately O(α(V)), where α is the inverse Ackermann function and V is the number of vertices. Prim's algorithm: Prim's algorithm starts with an arbitrary vertex and grows the MST by adding the minimum-weight edge that connects a vertex in the MST to a vertex outside the MST. It commonly uses a priority queue, often implemented with a binary heap, to efficiently select the minimum-weight edge at each step. For Prim's algorithm with a binary heap implementation, the time complexity is O((E + V) log V), where E is the number of edges and V is the number of vertices in the graph. This time complexity arises from performing V extract-min operations on the binary heap and updating the key values of adjacent vertices. Comparing the time complexities of both algorithms: Kruskal's algorithm: O(E log E + α(V))Prim's algorithm (with binary heap): O((E + V) log V) Overall, the Worst case time complexity of both algorithms is the same. For more reference Please visit: https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/https://www.geeksforgeeks.org/prims-minimum-spanning-tree-mst-greedy-algo-5/ Hence Option (A) is correct. Question 5 Which of the following is true about Huffman Coding? Huffman coding may become lossy in some cases Huffman Codes may not be optimal lossless codes in some cases In Huffman coding, no code is prefix of any other code. All of the above Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 5-Explanation: Huffman coding is a lossless data compression algorithm. The codes assigned to input characters are Prefix Codes, means the codes are assigned in such a way that the code assigned to one character is not prefix of code assigned to any other character. This is how Huffman Coding makes sure that there is no ambiguity when decoding. Question 6Suppose the letters a, b, c, d, e, f have probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively. Which of the following is the Huffman code for the letter a, b, c, d, e, f? 0, 10, 110, 1110, 11110, 11111 11, 10, 011, 010, 001, 00011, 10, 01, 001, 0001, 0000 110, 100, 010, 000, 001, 111 GATE-CS-2007 Top MCQs on Greedy Algorithms with Answers Top 50 Algorithms MCQs with Answers Discuss itQuestion 6-Explanation: We get the following Huffman Tree after applying Huffman Coding Algorithm. The idea is to keep the least probable characters as low as possible by picking them first. The letters a, b, c, d, e, f have probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively. 1 / \\ / \\ 1/2 a(1/2) / \\ / \\ 1/4 b(1/4) / \\ / \\ 1/8 c(1/8) / \\ / \\ 1/16 d(1/16) / \\ e fQuestion 7Suppose the letters a, b, c, d, e, f have probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively. What is the average length of Huffman codes?32.18752.251.9375GATE-CS-2007 Top MCQs on Greedy Algorithms with Answers Top 50 Algorithms MCQs with Answers Discuss itQuestion 7-Explanation: We get the following Huffman Tree after applying Huffman Coding Algorithm. The idea is to keep the least probable characters as low as possible by picking them first. The letters a, b, c, d, e, f have probabilities 1/2, 1/4, 1/8, 1/16, 1/32, 1/32 respectively. 1 / \\ / \\ 1/2 a(1/2) / \\ / \\ 1/4 b(1/4) / \\ / \\ 1/8 c(1/8) / \\ / \\ 1/16 d(1/16) / \\ e f The average length = (1*1/2 + 2*1/4 + 3*1/8 + 4*1/16 + 5*1/32 + 5*1/32) = 1.9375 Question 8 Consider the undirected graph below: Using Prim's algorithm to construct a minimum spanning tree starting with node A, which one of the following sequences of edges represents a possible order in which the edges would be added to construct the minimum spanning tree? (E, G), (C, F), (F, G), (A, D), (A, B), (A, C) (A, D), (A, B), (A, C), (C, F), (G, E), (F, G) (A, B), (A, D), (D, F), (F, G), (G, E), (F, C) (A, D), (A, B), (D, F), (F, C), (F, G), (G, E) GATE-IT-2004 Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 8-Explanation: A. False The idea behind Prim’s algorithm is to construct a spanning tree – means all vertices must be connected but here vertices are disconnected B. False The idea behind Prim’s algorithm is to construct a spanning tree – means all vertices must be connected but here vertices are disconnected C. False. Prim’s is a greedy algorithm and At every step, it considers all the edges that connect the two sets, and picks the minimum weight edge from these edges. In this option, since weight of AD<AB, so AD must be picked up first (which is not true as per the options). D.TRUE. Therefore, Answer is D Prim’s algorithm is also a Greedy algorithm. It starts with an empty spanning tree. The idea is to maintain two sets of vertices. The first set contains the vertices already included in the MST, the other set contains the vertices not yet included. At every step, it considers all the edges that connect the two sets, and picks the minimum weight edge from these edges. After picking the edge, it moves the other endpoint of the edge to the set containing MST. Read more at: Prim’s Minimum Spanning Tree (MST) Question 9 Consider the weights and values of items listed below. Note that there is only one unit of each item. Question The task is to pick a subset of these items such that their total weight is no more than 11 Kgs and their total value is maximized. Moreover, no item may be split. The total value of items picked by an optimal algorithm is denoted by Vopt. A greedy algorithm sorts the items by their value-to-weight ratios in descending order and packs them greedily, starting from the first item in the ordered list. The total value of items picked by the greedy algorithm is denoted by Vgreedy. The value of Vopt − Vgreedy is ______ . 16 8 44 60 GATE CS 2018 Top MCQs on Greedy Algorithms with Answers Top MCQs on Dynamic Programming with Answers Top 50 Algorithms MCQs with Answers Discuss itQuestion 9-Explanation: First we will pick item_4 (Value weight ratio is highest). Second highest is item_1, but cannot be picked because of its weight. Now item_3 shall be picked. item_2 cannot be included because of its weight. Therefore, overall profit by Vgreedy = 20+24 = 44 Hence, Vopt - Vgreedy = 60-44 = 16 So, answer is 16. Question 10 A text is made up of the characters a, b, c, d, e each occurring with the probability 0.11, 0.40, 0.16, 0.09 and 0.24 respectively. The optimal Huffman coding technique will have the average length of: 2.40 2.16 2.26 2.15 UGC-NET CS 2017 Nov - III Top MCQs on Greedy Algorithms with Answers Discuss itQuestion 10-Explanation: a = 0.11 b = 0.40 c = 0.16 d = 0.09 e = 0.24 we will draw a huffman tree: now huffman coding for character: a = 1111 b = 0 c = 110 d = 1111 e = 10 length for each character = no of bits * frequency of occurrence: a = 4 * 0.11 = 0.44 b = 1 * 0.4 = 0.4 c = 3 * 0.16 = 0.48 d = 4 * 0.09 = 0.36 e = 2 * 0.24 = 0.48 Now add these lenght for average length: 0.44 + 0.4 + 0.48 + 0.36 + 0.48 = 2.16 So, option (B) is correct. 12 There are 20 questions to complete. You have completed questions question Your accuracy is Correct Wrong Partial-Credit You have not finished your quiz. If you leave this page, your progress will be lost. Correct Answer You Selected Not Attempted Final Score on Quiz Attempted Questions Correct Attempted Questions Wrong Questions Not Attempted Total Questions on Quiz Question Details Results Date Score Hint Time allowed minutes seconds Time used Answer Choice(s) Selected Question Text All doneNeed more practice!Keep trying!Not bad!Good work!Perfect! Last Updated : 27 Sep, 2023 Share your thoughts in the comments Add Your Comment Please Login to comment...