Analysis of Algorithms:
CSE 5211
Fall 2010
Quiz 1 Points
40
Intro to Analysis of Algorithms:
CSE 4081
Fall 2010
Quiz 1 Points
40
Time: 45 min
Q1.
The following recursive algorithm finds all possible permutations of an input list of
characters.
Input:
a list
L[1.
.n]
of characters, and the initial size of the list
n
.
Output:
each possible permutation of
L
.
Algorithm
Permute
(list
L, int n, int level, array A)
(1) if (level = = n+1) then print A;
else
(2)
for each element
e
of L do
(3)
A[level] = e;
(4)
L = L – e;
// shrink the list by eliminating e
(4)
Permute (L, n, level+1, A);
end for;
end if;
End algorithm
Driver call
: Permute(L, sizeof(L), level=1, empty A[ , , ])
(
1a) (grad/ug: 2
) How many lists are printed for
n
=4?
4! = 4.3.2.1 = 24
(
1b) (g: 3, ug: 8
) Draw the recursion tree for L=[x y z]. Also, indicate the
postorder
numbering of the tree.
(
1c) (Grad only
: 5
) Modify
Permute
to make sure A[i] <=A[i+1] according to the
alphabet ordering (e.g., x<y<z), for each
i=1.
.(n1), in the output, i.e., rewrite the
algorithm to prun appropriate branches of the recursion tree.
You may check before printing (no pruning really), or check between (2) and (3) if e is
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '12
 Dmitra
 Algorithms, Dynamic Programming, Big O notation, Computational complexity theory, 2 pts, 4.3.2.1

Click to edit the document details