Вы находитесь на странице: 1из 18

CS 260 Review 1

1. Write and solve recurrence equations for the time complexity of the following
algorithm.

function F ( n: integer ) : integer;
begin
if n <= 1 then
return (2)
else
return (F(n-1) * F(n-1))
end; { F }

What does function F compute? Can you modify it in such a way that it
computes the result just with n calls?

2. Two recursive algorithms solving the same computational task are given.
Time performance of these algorithms is describes by recurrence equations

T(1) = 1
T(n) = 4T(n/2) + c
1
n,

R(1) = 1
R(n) = 3R(n/2) + c
2
n.

Which algorithm has a better asymptotic performance? Justify your answer.

3. Write down a detailed pseudo-code implementing the procedure of deleting all
positions of an input list, which come before the first occurrence of a given
element x. The first occurrence of x should be kept in the list. In your
implementation you are only allowed to use the basic operations of the List
ADT, i.e. FIRST(L), END(L), RETRIEVE(p,L), LOCATE(x,L), NEXT(p,L),
PREVIOUS(p,L), INSERT(x,p,L), DELETE(p,L), MAKENULL(L).

4. What is the computational complexity of the operation PRINTLIST expressed
in terms of the size of the input? Assume that the input list is represented as a
singly-linked list and that operations FIRST, NEXT and RETRIEVE take
constant time. Your estimate should be tight, i.e. the time spent on the
execution of the operation PRINTLIST should be both big oh and big
omega of your function. Provide a justification of your answer.

procedure PRINTLIST( L: LIST );
{ PRINTLIST prints the elements of L in the order of occurrence }
var
p: position;

begin
p:=FIRST(L);
while p<>END(L) do begin
print(RETRIEVE(p,L));
p:=NEXT(p,L)
end
end; { PRINTLIST }

function END ( L: LIST ): position;
var
q: position;
begin
q:=L;
while q.next <> nil do
q:= q.next;
return (q)
end; { END }

5. Find a formula for the number of calls occurring during the execution of the
recursive function F(n) provided below.

function F( n: integer) : integer;
begin
if n <= 1 then
return (1)
else
return (F(n-2)+F(n-1))
end; { F }

What is the rate of growth of this number with respect to n?.

6. Remove recursion from the following program:

function comb ( n, m: integer ): integer;
begin
if ( n = 1 ) or ( m = 0 ) or ( m = n ) then
return (1)
else
return (comb(n-1,m) + comb(n-1,m-1))
end; { comb }

7. Suppose that we have lists containing pre-order and post-order listings of the
nodes a tree. Describe an algorithm, which operates on these lists and marks
leaves with letter L and internal nodes with letter I. Your algorithm may not
access the tree itself, only the lists containing the nodes.
Solutions of the review problems

1. Recurrence equations for the time complexity:

1
2
(1) ,
( ) 2 ( 1) .
T c
T n T n c




The solution of the recurrence equations:
We iterate the substitution operation times and we obtain

i
.

1 2 1
2
( ) 2 ( ) (2 2 ... 2 2 )
i i i
T n T n i c


0
0
2
)
We choose i n . This gives

1
T n .

1 2 3 1
2
( ) 2 (1) (2 2 ... 2 2 )
n n n
T c


We simplify the expression and we get

.
1
1 2
( ) ( )2
n
T n c c c



The formula for :

( ) F n
.
1
2
( ) 2
n
F n


The modification of : ( ) F n
We declare variable a as an integer. We replace the statement

return (F(n-1) * F(n-1))

by

begin
a:=F(n-1);
return (a * a)
end.

2. The main result of chapter 9 of the textbook and the method of example 9.4 on
page 302 show that T n is both O n and , and that is both
and . Since lo , we conclude that the second algorithm
has a better asymptotic performance.



( )
2
g 3
)
2
( )
2
g 3 2
2
( ) n ( ) R n
2
log 3
( O n
lo
(n
3. procedure F(x:elementtype; var L: LIST);
var
p: position;
begin
p := FIRST(L);
while p <> END(L) do
if same(RETRIEVE(p,L),x) then
p := END(L)
else
DELETE(p,L)
end; { F }

4. Operation PRINTLIST is both O n and , where n is the size of the input
list.

2
( )
2
( ) n
5. Let G n be the function denoting the number of calls, which occur during the
execution of . A direct inspection of the tree of recursive calls of shows
that

( )
( ) F n F
( ) ( 2) ( 1) 1 G n G n G n .

Since the executions of and of require exactly one call, therefore
.
(0) F (1) F
(0) (1) 1 G G

We show the steps leading to the solution of the above non-homogeneous
recurrence equation.

Step 1. We look for the general solution of the homogeneous equation

( ) ( 2) ( 1) G n G n G n .

We assume that the solution has the form , we plug into the
equation, and we obtain

n



2 1
.
n n n



Dividing both sides by gives

2 n


2
1 .





We solve this equation. We obtain


1
2
o
+
=
5
or
1 5
2
o

= .

We conclude that the general solution of the homogeneous equation has
the form


1 2
1 5 1 5
( )
2 2
n n
G n c c
| | |
+
= +
|
|
\ . \
|
|
|
.
,

where c , c are some constants.

1 2
Step 2. We look for a particular solution of the non-homogeneous equation

G n . ( ) ( 2) ( 1) 1 G n G n = + +

We verify directly that the constant function satisfies the
equation.

( ) 1 G n =
Step 3. We conclude from steps 1 and 2 that the general solution of the non-
homogeneous equation has the form


1 2
1 5 1 5
( ) 1
2 2
n n
G n c c
| | | |
+
= +
| |
| |
\ . \ .
1 2
,

where c , c are some constants.

Step 4. We compute the values of constants c , c from equations
1 2
(0) (1) 1 G G = = . We obtain

1 2
5 5
1 , 1
5 5
c c = + = .

Step 5. We conclude that the solution of the non-homogeneous equation has the
form


5 1 5 5 1 5
( ) 1 1 1
5 2 5 2
n n
G n
| || | | || |
+
= + +
| | | |
| | | |
\ .\ . \ .\ .
.

The first term of the above expression has the faster rate of growth than
the second term, since
1 5 1 5
2 2
+
>
( )
.

We conclude that function G n has the same rate of growth as

5 1 5
1
5 2
n
| || |
+
+
| |
| |
\ .\ .
.

6. function F(n, m: integer): integer;
var
K, L: List;
k: integer;
p: position;
begin
MAKENULL(K);
INSERT(1, FIRST(K), K);
INSERT(1, END(K), K);
for k := 1 to n-1 do begin
NEXT_LIST(K, L);
COPY(L, K)
end;
p := FIRST(K);
k := 1;
while (k < m+1) and (p <> END(K)) do begin
p := NEXT(p, K);
k := k+1
end;
return (RETRIEVE(p, K))
end; {F}

procedure NEXT_LIST( K: LIST; var L: LIST);
var
p: position;
x: integer;
begin
p := FIRST(K);
MAKENULL(L);
while NEXT(p, K) <> END(K) do begin
x := RETRIEVE(p,K)+RETRIEVE(NEXT(p,K),K);
INSERT(x, END(L), L);
p := NEXT(p, K)
end;
INSERT(1, FIRST(L), L);
INSERT(1, END(L), L)
end; { NEXT_LIST }

procedure COPY( K: LIST; var L: LIST );
var
p: position;
begin
MAKENULL(L);
p := FIRST(K);
while p <> END(K) do begin
INSERT(RETRIEVE(p, K), END(L), L);
p := NEXT(p,K)
end
end; { COPY }

7. while there are some unmarked cells do begin
find the first unmarked cell in the post-list and mark it with L;
find the same cell in the pre-list and mark it also with L;
mark with I all unmarked cells of the pre-list, which occur before
the cell you just marked with L;
find the same cells in the post-list and also mark them with I
end;

CS 260 Review 2

1. Implement operation UNION for sorted singly-linked list representations of
sets. Give the order of magnitude of its running time when applied to sets of
sizes m, n.

2. We wish to improve the speed of operations of an open hash table with B
1

buckets by replacing it with another open hash table with B
2
buckets. Write a
procedure constructing the new hash table from the old one.

3. Let n be any positive integer. Is it possible to construct a priority queue A of
size n with the property that at every iteration of operation DELETEMIN the
process substituting the process with the smallest priority will be eventually
placed on the last position? Provide either a construction or an argument
showing that a priority queue with the above property does not exist.

function DELETEMIN(var A: PRIORITYQUEUE): processtype;
var
i, j: integer;
temp: processtype;
minimum: processtype;
begin
new(minimum);
minimum := A.contents[1];
A.contents[1] := A.contents[A.last];
A.last := A.last 1;
i := 1;
while i <= A.last div 2 do begin
if (2*i = A.last) or (p(A.contents[2*i]) <
p(A.contents[2*i + 1])) then
j := 2*i
else
j := 2*i + 1
if p(A.contents[i]) > p(A.contents[j]) then begin
temp := A.contents[i];
A.contents[i] := A.contents[j];
A.contents[j] := temp;
i := j
end
else
return (minimum)
end;
return (minimum)
end;

4. Find a formula for the total number of calls occurring during the insertion of n
elements into an initially empty set. Assume that the insertion process fills up
the binary search tree level-by-level. Leave your answer in the form of a sum.

procedure INSERT(x: elementtype; var A: SET);
begin
if A = nil then begin
new(A);
A.element := x;
A.leftchild := nil;
A.rightchild := nil
end
else if x < A.element then
INSERT(x, A.leftchild)
else if x > A.element then
INSERT(x, A.rightchild)
end;


5. DELETE operation is applied to a BST of height k. What are the maximal and
the minimal numbers of calls of DELETEMIN, which may occur during the
execution? Justify your answer.

function DELETEMIN( var A: SET ): elementtype;
var
minimum: integer;
begin
if A.leftchild = nil then begin
minimum := A.element;
A := A.rightchild;
return (minimum)
end
else begin
minimum := DELETEMIN(A.leftchild);
return (minimum)
end
end;









procedure DELETE(x: elementtype; var A: SET);
begin
if A <> nil then
if x < A.element then
DELETE(x, A.leftchild)
else if x > A.element then
DELETE(x, A.rightchild)
else if (A.leftchild = nil ) and (A.rightchild = nil ) then
A := nil
else if A.leftchild = nil then
A := A.rightchild
else if A.rightchild = nil then
A := A.leftchild
else
A.element := DELETEMIN(A.rightchild)
end;

6. (i) Trace the execution of Dijkstras algorithm applied to the directed graph
described by the following adjacency matrix. Include all values of arrays D
and P at all stages of the execution. Describe all resulting shortest paths.

A[1,2]=4, A[1,3]=1, A[1,4]=5, A[1,5]=8, A[1,6]=10,
A[3,2]=2, A[4,5]=2, A[5,6]=1.

(ii) Provide an example of a directed graph with n vertices with the property
that at every step of the execution of Dijkstas algorithm exactly one field of
the array D is updated.

procedure Dijkstra;
begin
S := {1};
for i := 2 to n do begin
D[i] := C[1,i];
P[i] := 1
end;
for i := 1 to n-1 do begin
choose a vertex w in V-S such that
D[w] is minimum;
add w to S;
for each vertex v in V-S do
if D[v] > D[w] + C[w,v] then begin
D[v] := D[w] + C[w,v];
P[v] := w
end
end
end;
7. Depth-first search dfs and breath-first search bfs graph traversal algorithms
are applied to a full binary tree of height k. What are the maximal numbers of
vertices occurring inside the stack of the dfs and inside the queue of the bfs
during the execution? How many times are those maximal sizes repeated?
Assume that the vertices are ordered level-by-level and that both algorithms
respect this ordering.

procedure dfs(v: vertex);
var
x,y: vertex;
S: STACK of vertex;
begin
mark[v] := visited;
PUSH(v,S);
while not EMPTY(S) do begin
x := TOP(S):
if there is an unvisited vertex y inside L[x] then begin
mark[y] := visited;
PUSH(y,S)
end
else
POP(S)
end
end;

procedure bfs(v: vertex);
var
x,y: vertex;
Q: QUEUE of vertex;
begin
mark[v] := visited;
ENQUEUE(v,Q);
while not EMPTY(Q) do begin
x := FRONT(Q):
DEQUEUE(Q);
for each vertex y inside L[x] do
if mark[y] = unvisited then begin
mark[y] := visited;
ENQUEUE(y,Q)
end
end
end;



Solutions of the review problems

1. The solution presented below is a direct modification of operation
INTERSECTION discussed in the textbook on pages 115-117.

procedure UNION ( ahead, bhead: celltype; var pc: celltype);
var
acurrent, bcurrent, ccurrent: celltype;
begin
new(pc);
acurrent := ahead .next;
bcurrent := bhead .next;
ccurrent := pc;
while (acurrent <> nil) and (bcurrent <> nil) do begin
if acurrent .element = bcurrent .element then begin
new(ccurrent .next);
ccurrent := ccurrent .next;
ccurrent .element := acurrent .element;
acurrent := acurrent .next;
bcurrent := bcurrent .next
end
else
if acurrent .element < bcurrent .element then begin
new(ccurrent .next);
ccurrent := ccurrent .next;
ccurrent .element := acurrent .element;
acurrent := acurrent .next
end
else begin
new(ccurrent .next);
ccurrent := ccurrent .next;
ccurrent .element := bcurrent .element;
bcurrent := bcurrent .next
end
end;
if acurrent = nil then
while bcurrent <> nil do begin
new(ccurrent .next);
ccurrent := ccurrent .next;
ccurrent .element := bcurrent .element;
bcurrent := bcurrent .next
end
else
while acurrent <> nil do begin
new(ccurrent .next);
ccurrent := ccurrent .next;
ccurrent .element := acurrent .element;
acurrent := acurrent .next
end;
ccurrent .next := nil
end; {UNION}

If m,n are the sizes of sets A,B, then the running time of operation UNION is
O(m+n).

2. We follow the conventions of the implementation of DICTIONARY ADT
presented on pages 124-125 of the textbook.

DICTIONARY1 = array[0..B1-1] of celltype;
DICTIONARY2 = array[0..B2-1] of celltype;

We assume that operations MAKENULL1, MAKENULL2, MEMBER1,
MEMBER2, INSERT1, INSERT2, DELETE1, DELETE2 and hash functions h1
and h2 corresponding to sizes B1 and B2 are implemented inside data types
DICTIONARY1, DICTIONARY2.

procedure COPY(A1: DICTIONARY1, var A2: DICTIONARY2);
begin
MAKENULL2(A2);
for i := 0 to B1-1 do begin
current := A1[i];
while current <> nil do begin
INSERT2(current .element, A2);
current := current .next
end
end
end; {COPY}


3. The following algorithm shows how to attach numbers form 1 to n to the nodes of
the tree corresponding to the array representing the priority queue.

attach number n to the node at position n;
i:= n-1;
while i <> 0 do begin
take the path starting at the root and ending at the node
corresponding to the array index i;
pull all the nodes on this path up one level;
attach number n-i to the node being removed from the tree;
i := i-1;
end;

4. Let
1
2 2
k
n
+
. Then
2
log n
k
s < k = (

and the number of calls equals
1
0
( 1)2 ( 1)( 2 1)
k
i k
i
i k n

=
+ + + +

.
5. The minimal number is 0 and the maximal number is k.

6. (i)

iteration S w D[2] P[2] D[3] P[3] D[4] P[4] D[5] P[5] D[6] P[6]
initial {1} - 4 1 1 1 5 1 8 1 10 1
1 {1,3} 3 3 3 1 1 5 1 8 1 10 1
2 {1,3,2} 2 3 3 1 1 5 1 8 1 10 1
3 {1,3,2,4} 4 3 3 1 1 5 1 7 4 10 1
4 {1,3,2,4,5} 5 3 3 1 1 5 1 7 4 8 5
5 {1,3,2,4,5,6} 6 3 3 1 1 5 1 7 4 8 5

Shortest paths: 1 3 2, 1 3, 1 4, 1 4 5, 1 4 5 6.

(ii) The adjacency matrix:

A[1,2]=1,
A[1,3]=3, A[1,4]=4, A[1,5]=5, , A[1,n]=n,
A[2,3]=1, A[3,4]=1, A[4,5]=1, , A[n-1,n]=1

7. dfs: maximal size k+1; it is repeated 2
k
times.
bfs: maximal size 2
k
; it occurs only once.



CS 260 Review 3

1. A sorting algorithm based on swap operations of neighboring positions of an
input array is given. Is it possible to determine that the number of swap
operations occurring in its worse case is bounded below by a polynomial
function applied to the size of the array? If the answer is yes, provide an
optimal polynomial, i.e. a polynomial that provides a bound from below for
all such algorithms and for which equality occurs for some of them? In both
cases of your answer yes and no provide a justification.

2. What is the maximal number of swap operations performed during the process
of creating the initial heap during the execution of the heap-sort algorithm?
Assume that the input array has size n.
(i) Express your answer in the form of a sum.
(ii) Remove the symbol of summation from your formula.
(iii) Analyze the order of growth.
Hint: Make use of the formula:

1*2
1
+2*2
2
+3*2
3
++N*2
N
=(N-1)*2
N+1
+2.

procedure pushdown(first, last: integer);
var
r: integer;
begin
r := first;
while r <= last div 2 do
if last = 2*r then begin
if A[r].key > A[2*r].key then
swap(A[r],A[2*r]);
r := last
end
else
if A[r].key > A[2*r].key and
A[2*r].key <= A[2*r+1].key then begin
swap(A[r],A[2*r]);
r := 2*r
end
else if A[r].key > A[2*r+1].key and
A[2*r+1].key < A[2*r].key then begin
swap(A[r],A[2*r+1]);
r := 2*r+1
end
else
r := last
end; { pushdown }



procedure heapsort;
var
i: integer;
begin
for i := n div 2 downto 1 do
pushdown(i,n);
for i := n downto 2 do begin
swap(A[1],A[i]);
pushdown(1,i-1)
end
end; { heapsort }

3. Suppose we have a set of words, i.e. strings of letters a-z, whose total length is
n. Show how to sort these words in O(n) time.

4. Find and solve recurrence relations describing:
(i) the number of moves needed for moving n disks in the Towers of
Hanoi problem,
(ii) the number of bit operations needed to multiply integers if we
assume that two multiplications are enough to accomplish the
process of reduction of size from n to n/2.

5. The entries a
ij
of matrix A are computed according to the formula

a
ij
=1 for i=1, j>1,
a
ij
=0 for i>1, j=1,
a
ij
= (a
i-1,j
+ a
i,j-1
)/2 for i>1, j>1.

(i) What number of operations + is necessary to compute n anti-
diagonals of the matrix A? Provide an exact answer. Do not include
a
11
.
(ii) Express numbers a
ij
in terms of the entries of Pascals triangle.


Solutions of the review problems

1. Apply the algorithm to inversely ordered integers n,n-1,n-2,,1. Fixing disorders
of n with n-1,n-2,,1 requires n-1 swap operations, fixing disorders of n-1 with
n-2,n-3,,1 requires n-2 swap operations, , and fixing disorders of 2 with 1
requires 1 swap operation. The algorithm has to perform at least

(n-1)+(n-2)++1 = n*(n-1)/2

swap operations.

2. Find an integer k such that the integer interval [2
k
,2
k+1
) contains n. Introduce level
indices 0,1,2,,k, with k=[log
2
n]. Start the process with an array of inversely
ordered integers n,n-1,n-2,,1. For each run of pushdown swap operations are
performed until the bottom of the tree is reached. Let M be the total number of
swap operations that are performed.

(i)
M = height([n/2])+ height([n/2]-1)+ height([n/2]-2)++ height (1).

(ii) Counting swaps for the full tree with all levels 0,1,2,,k filled up we get an
estimate for M from above by the sum

1*2
k-1
+2*2
k-2
+3*2
k-3
++k*2
0
,

and this sum with the help of the provided formula may be expressed as 2
k+1
-k-2.
We obtain an estimate for M from above by 2n. Counting swaps for the full tree
with all levels 0,1,2,,k-1 filled up leads to an estimate for M from below by the
sum

1*2
k-2
+2*2
k-3
+3*2
k-4
++(k-1)*2
0
,

and this sum by the same approach as above may be expressed as 2
k
-(k-1)-2. We
get an estimate for M from below by n/4, for sufficiently large n.
(iii) The order of growth is linear.

3. Use a trie data structure for the process of sorting. First perform an insertion of all
words into an initially empty trie. This operation takes O(n) time. Trie nodes will
automatically place the letters of inserted words according to alphabetical
ordering. Traversal operation combined with printing out paths corresponding to
inputted words will take care of accessing the words in sorted order. Also this
operation takes O(n) time.



4. (i) Recurrence equation

T(1) = 1,
T(n) = 2*T(n-1)+1.

Solution: T(n)=2
n
-1.

(ii) Recurrence equation

T(1) = c
1
,
T(n) = 2*T(n/2)+c
2
*n.

Solution: T(n)=O(n*log
2
n).

5. (i) The first diagonal contains 1 number that requires 1 operation +. The second
diagonal contains 2 numbers that require 1 operation + (each). The n
th
diagonal
contains n numbers that require 1 operation + (each of them). Altogether we get

1+2+3++n = (n+1)*n/2.

(ii) Each diagonal contains 0 and 1 as its boundary points and a number of points
of the interval (0,1). The first diagonal consists of (0,1/2,1). The second diagonal
of (0,1/4,3/4,1). The third one of (0,1/8,1/2,7/8,1), etc. We observe that the n
th

diagonal splits the interval [0,1] into n+1 intervals and that if we interpret lengths
of these intervals as multiples of the length of the shortest interval, we obtain
rows of Pascals triangle (1,1), (1,2,1), (1,3,3,1), . If two relative consecutive
lengths of the given diagonal have the form a,b, then in the next diagonal they
contribute a+b (as an easy arithmetic computation shows) and this is exactly the
recursive rule for the Pascals triangle.

Вам также может понравиться