while (p<>END(L1) do begin Delete(p, L1); if p<>END(L1)p := NEXT(p, L1); end cfw_delete even elements in List two p := FIRST(L2); if p<> END(L2) then p := NEXT(p, L2); while (p<>END(L2) do begin Delete(p, L2); if p<>END(L2) then p := NEXT(p, L2); end cfw_
12. Suppose characters a, b, c, d, e, f, g, h, i, j have probabilities 0.02, 0.02, 0.03, , 0.04, 0.05, 0.10, 0.10, 0.13, 0.20, 0.31, respectively. Construct an optimal Huffman code and draw the Huffman tree. Use the following rules: a. Left: 0, right:
13. Suppose T is a Huffman tree, and that the leaf for symbol a has greater depth than the leaf for symbol b. Prove that the probability of symbol b is no less than that of a. It can be proven in many ways by induction. The simplest seems to be the fo
Another possible solution, in the similar style is the following. For any Huffman tree, if we combine two leaves, and use another symbol to represent it. Assigning the sum of probability of two leaves to the new symbol, we get a new Huffman tree for a new
Now, we suppose prob(a) > prob(b), which means a is not used before b is used in construction of Huffman tree. These imply that prob(a) > prob(T), because prob(b) and prob(T) must be the smallest two ones. With the same reason, prob(c) > prob(b), pro(T),
16. Suppose we use the 2-3 trees to implement the WMERGE (i.e. a MERGE(A,B), but the set is totally ordered and all the elements of A are smaller than the elements of B) and SPLIT. a. Show the result of splitting the tree of Question (1a) at 7. 25
SE2C03. Sample solutions to the assignment 4. Total of this assignment is 231 pts. 100%= 205. Each assignment is worth 5%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, a
abaAbabaababbababaaababaababb pass1 a b b pass2 aB pass3 Abb pass4 abb pass5 ab pass6 abb pass7 abbababaaabab # of comparisons 3 2 3 3 2 3 13 Total 29
3. Abstract Data Type DIGRAPH involves the following 3 operations on directed graphs : FIR
b. Linked adjacency lists Assume the ADT LIST is defined with proper functions implemented. function FIRST(v:integer): position begin return(FIRST_LIST(A[v]) end function NEXT(v:integer, i:position): position begin return(NEXT_LIST(A[v],i) end function
a. a a b c d e f g h i b a [b,5,nil] b [c,1] [d,3,nil] c [a,3] [d,2] [e,3] [h,2,nil] d [a,4] [b,4,nil] e [b,2] [d,3] [f,2] [i,4,nil] f [b,1] [d,2,nil] g [b,1] [c,3,nil] h [d,2] [g,3,nil] i [a,1,nil] 3 4 b c 1 4 2 1 1 d 3 2 3 2 3 2 1 3 e 3 2 f 5 g h
7. A root of a directed acyclic graph (dag) is a vertex r such that every vertex of the dag can be reached by a directed path from r. Write a program to determine whether a dag is rooted. Assume that the dag is represented using adjacency lists. Make
Hence: T(n,m) (C(0,k)+C(1,k)+.+C(i,k)+.+C(k,k) c + (2k+1-1)c = 2k c + (2k+1-1)c = (32k 1)c = (32n/2 1)c = O(2n/2). One also may suspect O(2n/2) solution, and prove it by induction. c. we use the recursive formulation, to fill entries of the table like the
b. Assume T(k,k)=T(j,0)=c for all j,k, and for making reasoning easier T(i,j)=0 if j<0. T(n,m)=T(n-1,m)+T(n-1,m-1)+1 (n,m) / (n-1,m) / \ (n-2,m) (n-2,m-1) . \ (n-1,m-1) / \ (n-1,m-1) (n-2,m-2)
One solution, by using recursion tree in pure form: In the wor
end return(L); end 5. The level-order listing of the nodes of a tree first lists the root, then all nodes of depth a, then all nodes of depth 2, and so on. Nodes at the same depth are listed in left-to-right order. Write a program to list the nodes of
9. a. Show the heap (partially ordered tree) that results if the integers 8, 7, 5, 6, 10, 2, 1, 3, 11, 9, 4 are inserted into an empty tree.
b. What is the result of 5 successive DELETEMIN operations on the tree from (a)?
Give an example of a binary search tree which is a complete tree. Can it be done for an arbitrary number of nodes? Prove your answer.
Yes. First, construct complete tree with n nodes. Get the inorder list of the complete tree, say, a1,a2,an. Let a
n(n + 1) = O(n 2 ) 2 i =1 inorder walk takes O(n) therefore total time: O(n 2 )
Best case: O(n lg n) : when tree growth is balanced, each insertion takes O(lg n) ,we have n insertion therefore total insertion takes O(n lg n) inorder walk
SE2C03. Sample solutions to the assignment 3. Total of this assignment is 274pts. 100%=254. Each assignment is worth 5%. If you think your solution has been marked wrongly, write a short memo stating where marking in wrong and what you think is right, and
2. AVL trees are a good implementation of binary search trees. Show (step by step) the AVL trees formed by inserting the numbers 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. 10 10 insert(10) insert(9) 9 8 9 insert(6) 8 7 6 10 6 9 repair 7 8 5 insert(5) 10 6 7 8 10
7 repair 5 3 2 4 1 6 8 9 10 2 3 4 inset(1) 5
7 repair 9 6 8 10 1 2 4 3
7 9 5 8 6 10
3. a, b, Solutions at the end. 4. Consider ADT Sets with the following operations: MEMBER, DELETE, INSERT. Show that a heap is not an efficient way to impl
5. Consider ADT Sets with the following operations: MEMBER, DELETE, INSERT, UNION, MERGE. Show that a binary search trees are not an efficient way to implement this ADT. What about 2-3 trees or red-black? Which operation is the most problematic? Provi
6. Consider ADT Sets (of numbers) with the following operations: MEMBER(x), DELETEMIN, INSERT(x), DELETEMAX, and MEDIAN, which is a function integer greater than or equal to x, i.e. 3.1 = 4, 4 = 4. What model(s) would you recommend and why? Provide bi
8. a If team 1 needs i games to win, then it has won n-i games already. Thus, we obtain the following recurrence: P(i, j) = 1 0 pn-i(P(i-1,j)+(1- pn-i(P(i,j-1) if i=0 and j>0 if i>0 and j=0 if i>0 and j>0
b (i,j) 0 1 2 3 4 0 0 1 1 1 1 1 0 0.5 0.
Use a depth-first search algorithm to identify all the articulation vertices and biconnected components in the graph from Question 9. Draw an appropriate depth-first graph and indicate all the low and dfnumber values.
A solution at the end
A possible solution M cfw_(A,I) cfw_(A,I),(B,G) cfw_(A,I),(B,G),(C,J) cfw_(A,I),(B,G),(C,J),(E,K) cfw_(A,I),(B,G),(C,J),(E,K),(M,N) cfw_(A,I),(B,H),(C,J),(D,G),(E,K),(M,N) cfw_(A,I),(B,H),(C,J),(D,G),(E,K),(F,J),(M,K) augmenting path relative to M A-I B-G