Algorithms and Data Structures Trees Solutions
Algorithms and Data Structures Trees Solutions
Make sure you put your name and lab section on every sheet that you
hand in.
Recurrence Relations and the Master Theorem
1. [10 pts] Given a recurrence relation, what are the first 5 elements of the sequence?
a. an=2an-2, a0=3, a1=-1 3, -1, 6, -2, 12
b. an=an-1+an-2+n+3, ao=1, a1=2 1, 2, 8, 16, 31
2. [12 pts] For each of the recurrence relations in question 1, state whether it is linear
and homogeneous. If not, state which condition is violated; if yes, state the degree (k).
a. Linear, homogeneous, degree=2
b. Linear, not homogeneous due to n
3. [20 pts] Given a sequence, what is the recurrence relation?
a. {2, 5, 14, 41, 122} a0=2, an=3an-1-1
b. {-1, 0, 1, 3, 13} ao=-1, a1=0, an=nan-1+a2n-2
4. [20 pts] Given a recurrence relation, what is the closed form (Hint: Theorem 1 and
2)?
a. an-an-1-6an-2=0, a0=1, a1=2 an=0.8(3)n+0.2(-2)n
b. an=-6an-1-9an-2 for n2, a0=3, a1=-3 an=3(-3)n-2n(-3)n
5. [20 pts] Given an algorithm, how can it be characterized using the Master Theorem?
For each of these give the values of a, b, g(n) and Big-O as well as a prose description
of how you determined them.
a. The end of the Trees lectures described a sort algorithm called Treesort (also
found on page 624 in Prichard). Use the Master Theorem to analyze its
complexity assuming that copy is the key operation.
Insertion of one element: g(n)=1, a=1, b=2
T(n)=T(n/2)+1, (lgn)
Insertion of n elements: (nlgn)
Inorder traverse of the binary tree: (n)
Complexity of the Treesort algorithm: (nlgn)+ (n)= (nlgn)
b. Consider the problem of sorting a deck of playing cards by face value (ignoring
suit) by placing them in a separate pile for each face value and then combining
the piles. Use the Master Theorem to analyze the complexity assuming that the
key operation is moving a card.
g(n)=2n for placing the cards in separate piles and combining them back
together, a=0, b=13
T(n)=0T(n/13)+2n, (n)
6. [18 pts] Let an denote the number of bit strings of length n in which contain three
consecutive 1s.
a. Find the recurrence relation of an.
an=an-1+an-2+an-3+2n-3
b. How many bit strings of length six contain three consecutive 1s?
a0=0, a1=0, a2=0
a3=a2+a1+a0+20=0+0+0+1=1, a4=a3+a2+a1+21=1+0+0+2=3
a5=a4+a3+a2+22=3+1+0+4 =8, a6=a5+a4+a3+23=8+3+1+8=20