Data Structures & Algorithms Multiple Choice Questions on “Generating Permutations”.
1. The dictionary ordering of elements is known as? Answer: a 2. How many permutations will be formed from the array arr={1,2,3}? Answer: c 3. What will be the lexicographical order of permutations formed from the array arr={1,2,3}? Answer: c 4. What is the name given to the algorithm depicted in the pseudo code below? a) bubble sort Answer: c 5. Heap’s algorithm requires an auxiliary array to create permutations. Answer: b 6. What is the time complexity of Heap’s algorithm? Answer: d 7. What will be the output for following code? a) AB,BA, 8. What will be the time complexity of the given code? a) O(n2) Answer: b 9. What will be the output of the following code? a) AA,BA, Answer: c 10. What will be the output of the following code? a) AA, Answer: d 11. What will be the output of the code that generates permutations and also has the ability to handle duplicates, for the input str[]=”AA”?
a) Lexicographical order
b) Colexicographical order
c) Well order
d) Sorting
Clarification: Lexicographical order is also known as dictionary order. It is a generalized method of the way words are alphabetically ordered in a dictionary.
a) 2
b) 4
c) 6
d) 8
Clarification: No.of permutations for an array of size n will be given by the formula nPn. So for the given problem, we have 3P3=6 or 3!=6.
a) {{2,1,3},{3,2,1},{3,1,2},{2,3,1},{1,2,3},{1,3,2}}
b) {{1,2,3},{1,3,2},{2,3,1},{2,1,3},{3,2,1},{3,1,2}}
c) {{1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}}
d) {{2,1,3},{3,1,2},{3,2,1},{2,3,1},{1,2,3},{1,3,2}}
Clarification: The number of permutations for the problem will be 6 according to the formula 3P3. When ordered in lexicographical manner these will be {{1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}}.procedure generate(n : integer, Arr : array):
if n = 1 then
output(Arr)
else
for i = 0; i <= n - 2; i ++ do
generate(n - 1, Arr)
if n is even then
swap(Arr[i], Arr[n-1])
else
swap(Arr[0], Arr[n-1])
end if
end for
generate(n - 1, Arr )
end if
b) heap sort
c) heap’s algorithm
d) prim’s algorithm
Clarification: The given algorithm is called Heap’s algorithm. It is used for generating permutations of a given list.
a) true
b) false
View Answer
Clarification: Heap’s algorithm does not require any extra array for generating permutations. Thus it is able to keep its space requirement to a very low level. This makes it preferable algorithm for generating permutations.
a) O(n log n)
b) O(n2)
c) O(n*n!)
d) O(n!)
Clarification: The recurrence relation for the heap’s algorithm is given by the expression T(n)=n * T(n-1). It is calculated by using substitution method. It is found to be equal to O(n!). #include
b) BA,AB,
c) AB,BA
d) BA,AB,
Answer: a
Clarification: The given code prints the permutations of the an array. So there will be 2 permutations for an array of 2 elements. Note that the comma is printed even for the last permutation.#include
b) O(n * n!)
c) O(n!)
d) O(n log n)
Clarification: The recurrence relation for the heap’s algorithm is given by the expression T(n)=n * T(n-1). But as each permutations takes n time to be printed so the overall complexity will be O(n*n!).#include
b) AB,BA,
c) BA,AB,
d) AB,AB,
Clarification: The given code prints the permutations of the an array. So there will be 2 permutations for an array of 2 elements. In this code the difference is that BA is printed before AB. Note that the comma is printed even for the last permutation.#include
b) AA,AA
c) A,A
d) AA,AA,
Clarification: The given code prints the permutations of the given array but it is not able to handle duplicates due to which it prints 2P2 permutations even in this case. So the output becomes AA,AA,.
a) AA
b) AA,AA
c) A,A
d) A
Answer: a
Clarification: If a code is able to handle duplicates then the two A’s are not considered to be different elements due to which only one permutation will be formed. So the output will be AA only.