Data Structures & Algorithms in JAVA Fourth 1 also a number of selected problems in the course book. problems to be solved in the rst place, preferably during the session. Each section idicates scheduled for TDDB 56 in the academic year 2006/2007. Each section indicates prioritized The problems are divided into 5 sections, corresponding to the ve problem solving sessions nature, reecting the requirements to pass the course. problems were included in the previous exams. Most of the problems are of the very basic The collection developed gradually during the previous editions of this course. Many of the Edition, John Wiley & Sons, Inc., 2006. Michael T. Goodrich and Roberto Tamassia tion consists of explicitly stated problems and of references to problems in the course book: tionerna) of the DALG-part of TDDB 56 and of TDDB 57 and for self studies. The collec- This is a collection of typical problems recommended for the problem solving sessions (Lek- Preface 2006-09-26 Revised 2006 by Jan Maluszynski TDDB 56/57 Data Structures and Algorithms Exercises Fall 2006 n n n n n n n n n n n n (n + 1)2 2 O(n3 ) (n 1)3 2 O(n2 ) 2 +1 2 O(2 ) 3 1 2 O(2 ) 2 jsin(n)j 2 O(1) (n + 1)2 2 (n3 ) (n 1)3 2 (n2 ) 2 +1 2 (2 ) 3 1 2 (2 ) (n + 1)2 2 (n3 ) (n 1)3 2 (n2 ) 2 +1 2 (2 ) 3 1 2 (2 ) k 4 is a constant, and log is the logarithm such that for p i = 1; :::7. f (n)g (n) 2 O(f (n)g (n)) O(f (n) + g (n)) = O(max(f (n); g (n))) f (n) and g (n) be functions such that for all n f (n) 0, g (n) 0. Show: Does the set include functions with n k+1 log n k n+1 ; nlog k ; n log n; nk ; n3 ; k ; n ; k O(fi ) O(fi+1 ) f f and g , and for g and h? 2 2 (g) i g 2 (f ). (Thus actually partitions the class of all functions into equivalence classes.) 7. Show that terms of those for 6. Prove that if f log f (n) 2 O(log g(n)) then f (n) 2 O(g(n))? Justify your answer. 2 O(g) and g 2 O(h) then f 2 O(h). What are n0 and c for f and h in O(n sin 2n + 2) and O(n + 2) O(log2 3n) and O(log3 2n) 5. Is it true that if (b) (a) 4. Which inclusion relations, if any, hold between the following expressions: (b) (a) 3. Let equal growth rate? f1 ; :::; f8 of base 2. Order them according to their growth rate, i.e. put them in the sequence 2. Consider the following functions, where (m) (l) (k) (j) (i) (h) (g) (f ) (e) (d) (c) (b) (a) totic analysis technique. 1. Prove or disprove the following statements by using the denitions, and by the asymp- Textbook problems: R-4.11-19, R-4.22-30 Prioritized problems: 1,6,8,9,10,12,13 Le 1 Complexity, Analysis of Algorithms A and B have worst case time complexity, respectively, O(n2 ) and O(n log n). A is shorter than the running time of B . Give three (n2 ) and (n log n). worst case worst case best case best case a i 1 new_line print * integer n): 1 to a do to n do PrintStars( for j from a for i from 1 procedure a Is it possible that the Is it possible that the input data? (n). T (n): Justify your answers. 3 algorithm, where input data consists of an integer i and a sorted table of integers of 12. Analyse the worst case time complexity and the space complexity of the following T (n) 2 O(0), T (n) 2 O(7), T (n) 2 O(n), T (n) 2 (7)? Is it possible that the input data? (n). Which of the following statements hold for its running time i procedure foo(integer n): for i from 1 to 4 do x (n). call to a procedure running in time O(log n) procedure Complicated(integer n): for i from 1 to n do for j from n 1 downto 3 do for k from 1 to 5 do procedure VeryOdd(integer n): for i from 1 to n do if i is odd then for j from i to n do x x+1 for j from 1 to i do y y+1 an instruction that runs in time O(1) procedure Mystery(integer n): for i from 1 to n 1 do for j from i + 1 to n do for k from 1 to j do 11. Consider the following procedure: (d) (c) (b) (a) Is it possible that the input data? 10. Analyse the time complexity of the following algorithms: T (n) 2 (n2 ) for every data the algorithm takes time algorithm executes in time (d) For the T (n) 2 (n2 ) for some data the algorithm takes time algorithm executes in time (c) For the T (n) 2 (n2 ) for every (n). input data? time complexity of an algorithm is algorithm executes in time (b) The T (n) 2 (n2 ) for some time complexity of an algorithm is algorithm executes in time (a) The 9. Provide justied answers to the following questions: worst case time complexities are dierent possible reasons of this phenomenon. Is this phenomenon still possible if the On some data the running time of 8. Algorithms 1 0 1 to n do to n do integer n): exit loop1 if i = n then exit loop2 else A [i] A[i] + 1 if A[i] = 2 then A[i] 0 i i+1 loop2: for j from print A[j ] i loop1: A[i] for i from 1 PrintC( array A[1..n] procedure 4 14. Analyse the time complexity and the space complexity of the following algorithm: function Factorial(integer n): integer if n = 0 then return 1 else return n Factorial(n 1) gram: 13. Write a recurrence equation characterising the time complexity of the following pro- n. function (integer x, table A[1::n]): boolean first 1 last n found false while first last and not found do index b(first + last)=2c if i = A[index] then found true elsif i < A[index] then last index 1 else first index + 1 return found length T [0::n] in such a way that neither stack overows unless the total number of elements in both stacks together is n +1. The stack operation should run in O (1) time. Q is implemented as a ring buer with 3 elements. Show the states of n symbols read from the ADT queue using two stacks. double-ended queue (deque) is a sequence that can be modied by adding and re- adds the element E adds the element E D, and removes it from at the end of the deque deleteEnd(D) returns the last element of the deque D addEnd(E; D) D, D. D. and removes it from at the front of the deque deleteF ront(D) returns the rst element of the deque D addF ront(E; D) operations takes only a constant time. 5 Explain how deque can be represented in contiguous memory so that each of the four following four abstract operations: moving elements both at the front and at the end of the sequence Thus, we have the 5. A (d) What is the amortized time of all these operations? the worst case time for a single enqueue operation and a single dequeue operation? such that the queue becomes empty only after the last dequeue operation. What is (c) Assume that you enqueue and dequeue n data items. Thus altogether we perform n enqueue operations and n dequeue operations. The order of these operations is (b) Describe it in pseudocode. (a) Explain the idea. 4. Describe how to implement the input is a palindrome or not. What is the running time of your algorithm. an algorithm that uses a stack to check whether a sequence of palindrome if it is the same as its reversal (e.g. the string anna. Design Enqueue(Q; a); Enqueue(Q; b); Enqueue(Q; c); Dequeue(Q); Dequeue(Q); Enqueue(Q; d) the buer after execution of the operations (b) A queue (a) Explain how to implement two stacks in one table 3. A string is a 2. required permutation. it. If the answer is no explain why no sequence of the operations can generate the If the answer is yes show the sequence of the respective operations performed to obtain (b) queue (a) stack memory is: W X V. For each of them check whether it is possible to obtain it if the temporary character sent rst to the output). Consider the permutations X U W V Y and Y U permutation of the input sequence (the left-most character of the permutation is the the memory and sending them to the output. In this way we obtain at the output a The reading operations may be interleaved with operations removing characters from character read may be rst placed in a temporary memory or sent directly to the output. 1. The sequence X Y U V W is read once, character-by-character from left to right. Each Textbook problems: R-5.1-3, R-5.6-9, R-9.5-6, R.9.8 Prioritized problems: R-5.3, R-5.6, 1,2,3,5,6, R-9.5, 7,12 Le 2 Stacks and Queues, Hashing, Trees k mod 5 bit marking technique instead of the delete technique based Which of the b c a c b c betic order of the labels? 6 Which of the four usual kinds of binary tree traversals visits the nodes in the alpha- c 9. Consider the following binary tree. (c) The last letter and the last digit. (b) The last two digits, (a) The last two letters, following variants of the hash function give the best distribution in the hash table: of the hash function is determined by two characters of the number. three digits. Assume that all combinations appear with equal probability. The value car identication numbers as keys. Each number consists of three letters followed be 8. The police plans to implement a register of stolen cars in form of a hash table using the on re-hashing of the probe sequence? Illustrate both techniques on an example. tage of using the deleted h2 ( k ) = 5 deleted bit technique. 7. Consider hashing based on open addressing with linear probing. What is the disadvan- The deletion is to be handled by the (c) open addressing with double hashing, where (b) open addressing with linear probing, (a) coalesced chaining, when the following technique is used: Insert(15; c); Insert(8; a); Insert(14; b); Delete(15); Insert(32; d); Insert(4; e); Insert(7; f ) operations Show the contents of the initially empty array after performing each of the following The hash value is calculated as the key value modulo the table length. the inserted elements are integers. 6. We use an array with indices 0 to 6 to implement a hash table of size 7. The keys of 10. Consider the following tree traversal algorithm, where the argument n refers to the root Le 3 Search Trees, Heap, Union/Find of the tree. procedure TraverseTree(n: var S: ADT Stack treenode); Prioritized problems: R-10.4, 1,3,5,6,7,10 MakeEmptyStack(S) Textbook problems: R-10.1-4, R-10.6-7, R-10.9-10, R-10.23-24 Push(n) while not n do IsEmptyStack(S) 1. Consider the following binary tree Pop(S) Print label of n foreach child c of n 15 in reverse order do Push(c) 6 (a) Which of the traversal orders is implemented by this algorithm? (b) Transform the algorithm, so that it uses ADT queue instead of ADT stack. 21 5 Which of the traversal orders is implemented by the transformed algorithm? 11. Show which of the following binary trees are 10 18 25 (a) Justify that this is a binary search tree. T1 T4 = Delete(15; T3). (a) full (b) Show the trees: (b) complete = Insert(12; T ), T2 = Insert(7; T1 ), T3 = Insert(9; T2 ), (c) Is this also an AVL tree? (c) perfect a b c 2. Consider the sequence of keys obtained by a traversal of a binary search tree. d Is it possible to reconstruct the tree, provided that the traversal was: (a) preorder (b) postorder e f g (c) inorder h (d) levelorder . 3. Which of the following tree topologies can appear in correct AVL trees? 12. Show the array-list representation of each of the following binary trees. 0 2 1 3 4 .. .. .. .. a b 5 i c d f e g j .. .. .. .. A .. .. .. .. . .. .. .. .. .. ... .. B C .. .. .. .. .. ... .. .. .. .. .. .. .. .. .. D k h E F 4. Is it true that every AVL tree of height h + 1? 7 .. .. .. .. . Prove or disprove. 8 h has less nodes than an AVL tree of height 14 14 18 21 18 21 24 24 31 32 (c) 48 40 50 62 68 75 14 T4 14 18 21 18 31 24 24 32 Insert(41); Insert(54); Delete(68) 7 7 T2 21 are 3 9 and analyze the time complexity of insertion. operation will be performed on it. Discuss representation of (2,3) trees in the memory height 2, with all internal nodes being 3-nodes, and show how an example insertion of the course book dene a notion of (2,4) tree in p. 455 and the denition of (a,b) tree in p.455 (2,3) tree. Give an example of a (2,3) tree of ii. Discuss advantages and disadvantages of splay trees with respect to AVL trees. 1 2 5 i. Insert 4 in the following splay tree: rotations are performed? performed on the tree above (all operations are applied to the same tree). What Show step by step how the operations 17 42 (b) Consider the following AVL tree: 7 T3 7 T1 28 (a) Which of the following are AVL trees? Justify your answers. 6. Following the denition of 5. 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 1 2 2 1 1 2 2 2 2 1 1 complete tree to ternary trees, where each node has at most three O(1) time. operation. ADT priority queue with In this implementation there is no ecient way for accessing 2 3 5 10 B 9 12 11 6 1 C PathCompressFind and Union-by-Size): 10 Find(2); Merge(A,C); Find(4); Find(11) ations (called in the course book Show how this tree is transformed by the following sequence of Find and Merge oper- 7 8 4 A 10. Consider the following disjoint sets represented by up-trees (p.526 of the textbook): and deleted. Analyse the time- and space complexity of your algorithm. the heap of a priority queue so that the maximal element can be eciently accessed and deleting the maximal element of the queue. Design an algorithm which transforms DeleteMin 9. Consider the usual heap-based implementation of the children of a node in children. Dene an array-list representation of such trees and show how to access the 8. Extend the notion of Which of them, if any, can be a heap? B: A: 7. Consider the following tables: provides information about students. Each record 7. 6. [abd; cad; bbc; aba; ccc; dab]. $ A[j-1] (n log n) in the worst case. 11 starting with most signicant fragments? keys, starting from the least signicant ones. What would be a disadvantage when (a) The phases of Radix Sort consist in sorting of the consecutive fragments of the answer. What can we conclude about the best-case time for such algorithms? Justify your (c) Any comparison-based sorting algorithm takes time of sorting algorithm? (b) We want to sort singly linked list. Why should Quick Sort not be our rst choice (a) What is the worst-case data for Insertion Sort? Is it stable? What is its worst-case time complexity? A[j] procedure Bubblesort(A[0..n-1]) for i from 0 to n-2 do for j from n-1 downto i+1 do if A[j] < A[j-1] then Bubblesort . (c) Is Radix Sort stable? Justify or give a counterexample. is large (as in the above example). (b) Give a motivation to why Bucket Sort is less appropriate when the key universe You need only to show the form of the table after each phase of subkey sorting. characters of the keys form the subkeys for sorting: (a) Illustrate the operation of Radix Sort on the following table of keys, where the before the male students in the alphapetic order of their names. O(n), which puts all female students in alphabetic order of their names male or a female. The array is sorted according to the names. Design an algorithm running in time 5. Consider the following sorting algorithm, called 4. n includes, among others, the name of a student and the information if the student is a 3. An array of records indexed 1 to Could the algorithm be Insertion Sort, Selection Sort or Heap Sort? 1 2 6 5 4 3 7 8 At some stage of sorting the table is in the following form: 8 7 6 5 4 3 2 1 2. The following table is submitted as input data to a sorting algorithm. (c) In-place Quick Sort. (b) Heap Sort. (a) Insertion Sort. illustrate the operation of: [7; 4; 12; 2; 5] 1. For the following sequence of integers: Textbook problems: R-11.9-11, R-11.17-19, R-11.21, R-11.25 Proritized problems: 1,2,3,4,5,7,9, R-11.9, R-11.10, 11,12 Le 4 Sorting, Selection n strings of maximal length m characters and average length of m p k, for some parameter k > 0, k Insertion Sort is called k. k and size n of the sorted table. Will these Quick Select Quick Sort Insertion Sort n = 5: 12 O(n). O(n). O(n3 ). The worst case complexity of Quick Select is The worst case complexity of Quick Sort is The best case running time of Insertion Sort is 12. Explain whether each of the following statements is true or false case data and illustrate them by examples for 11. For each of the following algorithms discuss what are the respective best case and worst Sort or Radix Sort are better suited for this problem. in the internal memory use the time complexity arguments to justify whether Quick collections of raw data sorted by phone numbers. Assuming that all data can be placed 10. Telephone directories sorted in the alphabetic order of names should be produced from expressions change if we replace Insertion Sort by Selection Sort? used by the Insertion Sort, as a function of For each of the variants above nd an expression describing how much time is totally the subtables of size greater than instead for this subtable. Thus, the recursive calls to Quick Sort are restricted to B When Quick Sort is called for a subtable of size almost sorted and is passed to Insertion Sort for nal sorting. i.e. such subtables are left unsorted. When this algorithm terminates the table is A We make Quick Sort to ignore all subtables of size sorting algorithm. For example, consider the following variants of Quick Sort: 9. Quick Sort is not ecient for small tables. Therefore it is often combined with other (b) Repeat the analysis for the average case time complexity. time of one of these algorithms better than the other on all worst-case data? (a) Show the worst-case time complexity for Quick Sort and Radix Sort. Is the running input set). characters (the average was taken over the possible sets of strings, not over a particular 8. We want to sort requirement. implement Radix Sort with a variant of Bucket Sort which does not fulll the phases starting from the least signicant fragments of the keys? Discuss, how to (b) What requirement should be satised by Bucket Sort to use it in Radix Sort with A B . D C 4 3 2 6 F E 5 9 3 3 f 5 a 4 6 1 e c 7 c: c: the the index. The indices impose an ordering on every subset of vertices. 13 IsIndex(i,G) = true i i is an index dierent from the empty index Vertex(i,G) = vertex determined by index i ExistEdge(v,u,G) = true i the edge hv; ui is in G no such neighbour). First(v,G) = index of v's rst neighbour (empty if v has no neighbours)) Next(i,v,G): = the minimal index of a neighbour of v greater than i (empty if there is Consider the following operations of ADT Directed Graph: Insertedge(v,u,G): insert the edge hv; ui in G DeleteEdge(v,u,G): delete the edge hv; ui in G called 3. Assume that the vertices of a Directed Graph are numbered; the number of a vertex is (c) Show a Topological Sort of the graph. neighbours are selected in the increasing weights of the connecting edges. (b) Show the order of visiting the nodes by breadth-rst search from vertex neighbours are selected in the increasing weights of the connecting edges. (a) Show the order of visiting the nodes by depth-rst search from vertex d 8 b 2 2. Consider the following directed graph with weighted edges: Could it be depth-rst search or breadth-rst search? 1 order indicated by the numbers. 1. In a search starting in vertex A of the following graph the vertices were visited in the Textbook problems: R-13.3, R-13.6-9, R-13.12-13 Prioritized problems: 1,2,3, R-13.7, R-13.8, 4,5,7 Le 5 Graphs number of vertices, maximal number of edges outgoing from a vertex O(n + e) where n is the number of vertices and e is the number of OT: DS, P2. P2: P1, TP. CC: P2, FL. FL: DM, DS. AA: DS, P1. TP: DM, P1. DS: TM, P1. P1: none. DM prerequisites : none. G where courses correspond to vertices and G. 14 a general answer to this question for an arbitrary graph of courses. example curriculum? Justify your answer. Use the notion of path length to give What is the minimum number of terms needed to schedule all courses in the in the same term provided that all prerequisites were oered in previous terms. (c) Each of the courses is taught during a whole term. Several courses may be taught steps of the computation performed on the graph known graph algorithm to determine a study plan for Johan. Show the consecutive (b) Johan is a part-time student. He can only take one course at a time. Use a well- prerequisites correspond to edges. What kind of graph is it? (a) Represent the information as a graph none) other courses as prerequisites for admission. They are listed below: guages (FL) and Optimisation Techniques(OT). Each of the courses has some (possibly ming 2 (P2), Theory of Programming (TP), Analysis of Algorithms (AA), Formal Lan- Compiler Construction (CC), Data Structures (DS), Programming 1 (P1), Program- 5. A study curriculum includes (among others) the following courses: Discrete Math (DM), graph is a tree. (c) Explain how to extend your algorithm so that it can check whether the input (b) Justify that the algorithm has the required complexity. (a) Sketch the idea. edges of the graph. should work in time 4. Design an algorithm that checks existence of cycles in undirected graphs. The algorithm Hint: Implement the instruction using some of the graph operations above. foo foreach neighbour u of v do Analyze the time complexity of the abstract instruction: k e number of edges, v argument graphs use the following parameters: for graphs represented as adjacency matrices and as adjacency lists. To characterize Find lower and upper bounds of the asymptotic time complexity of these operations The ground must be prepared before building the exterior walls or the chim- walls are insulated. Peter will not move in until the interior walls are painted and the exterior Painting of the interior walls can only be done when the oor is ready. doors are already set. The insulation of exterior walls can only be done when the windows and the To cover the roof Peter must rst build it. built. The oor can only be layed when the roof is covered and the interior walls are The doors are set after building the interior walls. The windows are set after building the exterior walls. The interior walls are built after the exterior walls. To build the roof Peter must rst build the external walls and the chimney. ney. resulting schedule. 15 your graph by discussing the intermediate stages of its operation and show the the way that all restrictions are observed. Explain how this algorithm works for the graph algorithms discussed in the course can be used to schedule the tasks in (b) Peter wants to perform each of the tasks during consecutive weekends. Which of direct restrictions on task ordering than those stated above. tasks and edges representing their ordering. The graph must not introduce more Represent the above restrictions as a directed graph with vertices representing (a) There are some restrictions on the ordering of these tasks: windows, covering the roof, insulating the exterior walls, moving in. ney, building the roof, painting the interior walls, laying oor, setting doors, setting preparing the ground, building exterior walls, building interior walls, building the chim- 7. Peter is building a holiday house. He has broken the project into the following tasks: it works. directed acyclic graph. Analyze complexity of your solution and show on example how 6. Modify DFS algorithm so that it can be used for computing a topological sort of a