Question 1 |

Which of the following standard algorithms is not a Greedy algorithm?

Dijkstra's shortest path algorithm | |

Prim's algorithm | |

Kruskal algorithm | |

Huffman Coding | |

Bellmen Ford Shortest path algorithm |

**Greedy Algorithms**

**Discuss it**

Question 2 |

Suppose we run Dijkstra’s single source shortest-path algorithm on the following edge weighted directed graph with vertex P as the source. In what order do the nodes get included into the set of vertices for which the shortest path distances are finalized? (GATE CS 2004)

P, Q, R, S, T, U | |

P, Q, R, U, S, T | |

P, Q, R, U, T, S | |

P, Q, T, R, U, S |

**Greedy Algorithms**

**Discuss it**

Question 2 Explanation:

Question 3 |

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:

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 |

**Greedy Algorithms**

**Discuss it**

Question 3 Explanation:

Total number of characters in the message = 100. Each character takes 1 byte. So total number of bits needed = 800. AfterHuffman 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 4 |

What is the time complexity of Huffman Coding?

O(N) | |

O(NlogN) | |

O(N(logN)^2) | |

O(N^2) |

**Greedy Algorithms**

**Discuss it**

Question 4 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 calles minHeapify(). So, overall complexity is O(nlogn).
See here for more details of the algorithm.

Question 5 |

In question #2, which of the following represents the word

*"dead"*?1011111100101 | |

0100000011010 | |

Both A and B | |

None of these |

**Greedy Algorithms**

**Discuss it**

Question 5 Explanation:

The

*Huffman Tree*generated is:character code-word f 0 c 100 d 101 a 1100 b 1101 e 111The 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 6 |

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 |

**Greedy Algorithms**

**Discuss it**

Question 6 Explanation:

Question 7 |

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 |

**Greedy Algorithms**

**Discuss it**

Question 7 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 8 |

Suppose 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, 000 | |

11, 10, 01, 001, 0001, 0000 | |

110, 100, 010, 000, 001, 111 |

**Greedy Algorithms**

**GATE-CS-2007**

**Discuss it**

Question 8 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

Question 9 |

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

3 | |

2.1875 | |

2.25 | |

1.9375 |

**Greedy Algorithms**

**GATE-CS-2007**

**Discuss it**

Question 9 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 10 |

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

**Greedy Algorithms**

**GATE-IT-2004**

**Discuss it**

Question 10 Explanation:

A and B are

**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 weight of AB<AD so must be picked up first. D.**TRUE.**It represents a possible order in which the edges would be added to construct the minimum spanning tree. 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: http://www.geeksforgeeks.org/greedy-algorithms-set-5-prims-minimum-spanning-tree-mst-2/
There are 10 questions to complete.