Você está na página 1de 26

Questions and exercises for Review

1. What steps should one take when solving a problem using a computer? First construct an exact model in terms of which we can express allowed solutions. Finding such a model is already half the solution. (Any branch of mathematics or science can be called into service to help model the problem domain.) Once we have a suitable mathematical model, we can specify a solution in terms of that model. 2. Explain some issues when dealing with the representation of real world objects in a computer program. Issues when dealing with the representation of real world objects in a computer program how real world objects are modeled as mathematical entities, the set of operations that we define over these mathematical entities, how these entities are stored in a computer's memory (e.g. how they are aggregated as fields in records and how these records are arranged in memory, perhaps as arrays or as linked structures), and the algorithms that are used to perform these operations. 3. Explain the notions: model of computation; computational problem; problem instance; algorithm; and program Model of Computation: An abstract sequential computer, called a Random Access Machine or RAM. Uniform cost model. Computational Problem: A specification in general terms of inputs and outputs and the desired input/output relationship. Problem Instance: A particular collection of inputs for a given problem. Algorithm: A method of solving a problem which can be implemented on a computer. Usually there are many algorithms for a given problem. Program: Particular implementation of some algorithm. 4. Show the algorithm design algorithm. Algorithm design (informal problem) 1 formalize problem (mathematically) [Step 0] 2 repeat 3 devise algorithm [Step 1] 4 analyze correctness [Step 2] 5 analyze efficiency [Step 3] 6 refine 7 until algorithm good enough 8 return algorithm 5. What might be the resources considered in algorithm analysis? Determine the amount of some resource required by an algorithm (usually depending on the size of the input). The resource might be: running time memory usage (space) number of accesses to secondary storage number of basic arithmetic operations network traffic Formally, we define the running time of an algorithm on a particular input instance to be the number of computation steps performed by the algorithm on this instance. 6. Explain the big-oh class of growth. Typically, problems become computationally intensive as the input size grows. Hence we are studying the asymptotic efficiency of algorithms. Informally, time to solve a problem of size, n, T(n) is O(log n) if T(n)=clog(2)n Formally: O(g(n)) is the set of functions f s.t. f(n)<cg(n) for some constant c>0 and n>N(sufficiently large N) Alternatively we may write lim(n->inf)f(n)/g(n)<=c and say g is an upper bound for f. O(g) is the set of functions that grow no faster than g. g(n) describes the worst case behavior of an algorithm that is O(g) EX: nlgn + n=O(n^2)

lg^kn=O(n) for all k in N 7. Explain the big-omega class of growth. Typically, problems become computationally intensive as the input size grows. Hence we are studying the asymptotic efficiency of algorithms. Informally, time to solve a problem of size, n, T(n) is O(log n) if T(n)=clog(2)n Formally: O(g(n)) is the set of functions f s.t. f(n)<cg(n) for some constant c>0 and n>N(sufficiently large N) Alternatively we may write lim(n->inf)f(n)/g(n)<=c and say g is an upper bound for f. omega(g) is the set of functions f s.t. f(n)>cg(n) for some constant c and n>N omega(g(n)): class of functions f(n) that grow at least as fast as g(n) g(n) describes the best case behaviour of an algorithm that is omega(g) 8. Explain the big-theta class of growth. Typically, problems become computationally intensive as the input size grows. Hence we are studying the asymptotic efficiency of algorithms. Informally, time to solve a problem of size, n, T(n) is O(log n) if T(n)=clog(2)n Formally: O(g(n)) is the set of functions f s.t. f(n)<cg(n) for some constant c>0 and n>N(sufficiently large N) Alternatively we may write lim(n->inf)f(n)/g(n)<=c and say g is an upper bound for f. teta(g(n)):class of functions f(n) that grow at same rate as g(n) ex: n^2/2 3n=teta(n^2) we have to determine c1>0, c2>0, n0 in N s.t.: c2n^2<=n^2/2 3n<=c1n^2 for any n>n0 dividing by n^2 yields: c2<=1/2-3/n<=c1 this is satisfied for c2=1/14, c1=1/2, n0=7 9. What are the steps in mathematical analysis of nonrecursive algorithms? Steps in mathematical analysis of nonrecursive algorithms: Decide on parameter n indicating input size Identify algorithms basic operation Determine worst, average, and best case for input of size n Set up summation for C(n) reflecting algorithms loop structure Simplify summation using standard formulas 10. What are the steps in mathematical analysis of recursive algorithms? Steps in mathematical analysis of recursive algorithms: Decide on parameter n indicating input size Identify algorithms basic operation Determine worst, average, and best case for input of size n Set up a recurrence relation and initial condition(s) for C(n) - the number of times the basic operation will be executed for an input of size n (alternatively count recursive calls). Solve the recurrence to obtain a closed form or estimate the order of magnitude of the solution 11. From lowest to highest, what is the correct order of the complexities O(n^2), O(3n), O(2n), O(n2 lg n), O(1), O(n lg n), O(n^3), O(n!), O(lg n), O(n)? O(1),O(logn),O(n),O(2n),O(3n),O(nlogn),O(n^2logn),O(n^2),O(n^3),O(n!) 12. What are the complexities of T1(n) = 3n lg n + lg n; T2(n) = 2n + n3 + 25; and T3(n, k) = k + n, where k less-than or equal to n? From lowest to highest, what is the correct order of the resulting complexities? O(t1)=nlogn O(t2)=2^n O(t3)=n t3,t1,t2 13. Suppose we have written a procedure to add m square matrices of size n n. If adding two square matrices requires O (n^2 ) running time, what is the complexity of this procedure in terms of m and n? O((m-1)n^2)

14. Suppose we have two algorithms to solve the same problem. One runs in time T1(n) = 400n, whereas the other runs in time T2(n) = n2. What are the complexities of these two algorithms? For what values of n might we consider using the algorithm with the higher complexity? T1-->O(n) T2-->O(n^2) n<=400 15. How do we account for calls such as memcpy and malloc in analyzing real code? Although these calls often depend on the size of the data processed by an algorithm, they are really more of an implementation detail than part of an algorithm itself. Usually calls such as memcpy and malloc are regarded as executing in a constant amount of time. Generally they can be expected to execute very efficiently at the machine level regardless of how much data they are copying or allocating. Of course, their exact efficiency may depend on the computer on which they execute as well as other factors (particularly in the case of malloc, which depends on the state of the system at the moment it is called. 16. Explain the stack ADT.

supports two operations: push(x) inserts x on top of stack pop() removes top element of stack and returns it, if stack is empty then error occurs 17. Explain the list ADT.

supports 3 operations: insert(x) insterts x infront of the list delete(x) removes element from the front of the list and if the list is empty an error occurs search(k): searches the list for the element with key k 18. There are occasions when arrays have advantages over linked lists. When are arrays preferable? When we need to access an element fast without having to go through all the elements of the list. This is preferable when we have a large amount of information, because accessing an element for whatever reason is much faster. Arrays are also advantageous when storage is at a premium because they dont require additional pointers to keep their elements linked together. 19. Explain the queue ADT. A queue differs from a stack in that its insertion and removal routines follows the first-in-first-out (FIFO) principle.  Elements may be inserted at any time, but only the element which has been in the queue the longest may be removed.  Elements are inserted at the rear (enqueued) and removed from the front (dequeued) supports 2 main operations: enque(o) inserts element in the rear of the queue dequeue(): removes the element from the fron of the queue and returns it 20. Sometimes we need to remove an element from a queue out of sequence (i.e., from somewhere other than the head). What would be the sequence of queue operations to do this if in a queue of five requests, req1, . . . , req5, we wish to process req1, req3 , and req5 immediately while leaving req2 and req4 in the queue in order? What would be the sequence of linked list operations to do this if we morph the queue into a linked list? coada: using queue operations we deque req1 for processing, deque req2 and re-enque it, deque req3 for processing, dequeue req4 and re-enqueue it and dequeue req5 for processing. Because we re-enqueue req2 and req4 the queue contains only these requests in order. 21. Recall that each of the linked list data structures presented at the laboratory has a size member. The SLList and DLList data structures also contain a first and last member. Why are each of these members included? By updating these members dynamically as elements are inserted and removed we avoid the O(n) runing complexity of traversing the lis each time its tail element or size is requested. By maintaining these memebers, fetching a lists tail element or size becomes an O(1) operation withouth adding any complexity to the operations of inserting and removing elements.

(Size emmeber to know permanently how many records do we have inside the list. First and last elements are needed in order to know where to start and where to stop the operations on the lists if we traverse them entirely) 22. When would you use a doubly-linked list instead of a singly-linked one? Why?
A doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list. A doubly linked list can be traversed in both directions (forward and backward). A singly linked list can only be traversed in one direction. A node on a doubly linked list may be deleted with little trouble, since we have pointers to the previous and next nodes. A node on a singly linked list cannot be removed unless we have the pointer to its predecessor.

On the flip side however, a doubly linked list needs more operations while inserting or deleting and it needs more space (to store the extra pointer).

23.

Show the result of inserting the numbers 32, 11, 22, 15, 17, 2, -3 n a doubly linked list with a sentinel.

-3 -> 2 -> 17 -> 15 -> 22 ->11 -> 32 24. Show the result of inserting the numbers 32, 11, 22, 15, 17, 2, -3 n a circular queue of capacity 9.

-3,2,17,15,22,11,32,0,0 25. Show the result of inserting the numbers 32, 11, 22, 15, 17, 2, -3 n a stack of capacity 12.

-3, 2, 17, 15, 22, 11, 32, 0,0,0,0,0 26. Determine the value returned by the function (depending on n), and the worst case running time, in big-Oh notation, of the following program function mistery(n) r:=0 for i:=1 to n-1 do for j:=i+1 to n do for k:=1 to j do r:=r+1 return(r); (n-1)n(n+1)/3, O(n^3) 27. Determine the value returned by the function (depending on n), and the worst case running time, in big-Oh notation, of the following program function pesky(n) r:=0 for i:=1 to n do for j:=1 to i do for k:=j to i+j do r:=r+1; return(r); n(n+1)(n+2)/3, O(n^3) 28. Define the term "rooted tree" both formally and informally. Rooted tree: collection of elements called nodes, one of which is distinguished as root, along with a relation ("parenthood") that imposes a hierarchical structure on the nodes  Formal definiton: A single node by itself = tree. This node is also the root of the tree Assume n=node and T1,T2,....,Tk=trees with roots n1,n2,...nk construct a new tree by making n be the parent of nodes n1,n2,...nk

Common data structure for non-linear collections 29. Define the terms ancestor, descendant, parent, child, sibling as used with rooted trees. ancestor: it is on the unique path from root to x descendant: if x is on unique path to him parent: A node that has a child is called the child's parent node (or ancestor node, or superior). A node has at most one parent. child: A node is a structure which may contain a value, a condition, or represent a separate data structure (which could be a tree of its own). Each node in a tree has zero or more child nodes, which are below it in the tree (by convention, trees are drawn growing downwards). sibling: siblings are nodes that share the same parent node 30. Define the terms path, height, depth, level as used with rooted trees. For a rooted tree T = (V, E) with root r V: Path: n1, n2, ..., nk such that ni = parent ni+1 for 1<=i<= k. length(path): no. of nodes -1 The depth of a vertex v in V is depth(v) = the length of the path from r to v The height of a vertex v in V is height(v) = the length of the longest path from v to a leaf The height of the tree T is height(T) = height(r) The level of a vertex v in V is level(v) = height(T) depth(v) The subtree generated by a vertex v in V is a tree consisting of root v and all its descendants in T. 31. Show the preorder traversal of the tree given in Fig. 1

Fig. 1. Example Tree preorder: 1, 2, 5, 3, 6, 10, 7, 11, 12, 4, 8, 9. 23. Show the postorder traversal of the tree given in Fig. 1 postorder: 5, 2, 10, 6, 11, 12, 7, 3, 8, 9, 4, 1. 24. Show the inorder traversal of the tree given in Fig. 1 inorder: 5, 2, 1, 10, 6, 3, 11, 7, 12, 8, 4, 9. 25. Construct the tree whose preorder traversal is: 1, 2, 5, 3, 6, 10, 7, 11, 12, 4, 8, 9, and inoder traversal is 5, 2, 1, 10, 6, 3, 11, 7, 12, 8, 4, 9.

26. Construct the tree whose postorder traversal is: 5, 2, 10, 6, 11, 12, 7, 3, 8, 9, 4, 1, and inoder traversal is 5, 2, 1, 10, 6, 3, 11, 7, 12, 8, 4, 9.

27. Show the vector contents for an implementation of the tree in Fig. 1. 1 2 3 4 5 6 7 8 9 10 11 12 0 1 1 1 2 3 3 4 4 6 7 7 28. Show the contents of the data structures (in a sketch) for an implementation of the tree in Fig. 1 using lists of children. 1-->2-->3-->4* 2-->5* 3-->6-->7* 4-->8-->9* 5* 6-->10* 7-->11-->12* 8* 9* 10* 11* 12* 29. Show the contents of the data structures (in a sketch) for an implementation of the tree in Fig. 1 using leftmost child - right sibling method. 1 21* 2 523 3 634 4 84* 5 *5* 6 10 6 7 7 11 7 * 8 *89 9 *9* 10 * 10 * 11 * 11 12 12 * 12 * 30. Show the binary search tree which results after inserting the nodes with keys 5, 2, 10, 6, 11, 12, 7, 3, 8, 9, 4, 1 in that order, in an empty tree.

31. Show the binary search tree resulted after deleting keys 10, 5 and 6 in that order from the binary search tree of Fig. 2.

32. How do we find the smallest node in a binary search tree? What is the runtime complexity to do this in both an unbalanced and balanced binary search tree, in the worst case? How do we find the largest node in a binary search tree? What are the runtime complexities for this? The smallest node in a binary search tree is the node that is furthest to the left. To locate this node we descend through the tree by following left pointers until reaching the end of the branch. In an unbalanced bynari search tree this requires O(n) time in the worst case, where n is the number of nodes in the tree. This occurs when the tree consists of a single branch to the left fo example. However, if we keep the tree balanced, no branch will be longer than lgn nodes. Thus, the running complexity of searching for the smallest node in this case is O(lg n). Finding the largest node is a similar process, except that the largest node is the one that is furthest to the right of the tree. The runtime complexities for these are the same as for locating the smallest node. If we are interested in determining only the smallest/largest element n a set of data repeatedly we use a priority queue 33. Compare the performance of operations insert (add), delete and find for arrays, doubly-linked lists and BSTs.

34. What is the purpose of static BSTs and what criteria are used to build them? Reduce time of search: More frequently accessed keys kept closer to root 35. If we would have two functions: bitree_rem_left (for removing the left subtree) and bitree_rem_right (for removing the right subtree), why should we use a postorder traversal used to remove the appropriate subtree? Could a preorder or inorder traversal have been used instead? It is essential to use a postorder traversal here because a subtree must be removed in its entirety before removing its parent. A preorder traversal ends up removing the parent first, thus freeing the parent and making it impossible to access its children. An inorder traversal also doesnt work because we still end up removing the parent before its right subtree. (Because in order to make a decision when removing a subtree we have to know all the information. This can be done only if we have already visited the left subtree and the root. Therefore the inorder or preorder traversal cannot be used) 36. When might we choose to make use of a tree with a relatively large branching factor, instead of a binary tree, for example? Larger branching factors keep a tree shorter for a given number of nodes, provided the tree remains relatively balanced. Therefore a large branching factor is desirable when an application is particularly sensitive to the height of the tree. Search trees are a good example, although typically the difference in performance attributed to larger branching factors is

not that significant when the tree resides in memory. This is one reason that binary trees are most common for searching in memory. However, when searching in the considerably slower world of secondary storage, a larger branching factor can make a substantial difference. In this situation, typically some type of B-tree is used. 37. In a binary search tree, the successor of some node x is the next largest node after x. For example, in a binary search tree containing the keys 24, 39, 41, 55, 87, 92, the successor of 41 is 55. How do we find the successor of a node in a binary search tree? What is the runtime complexity of this operation? We take the right child. Complexity is O(1). 38. A multiset is a type of set that allows members to occur more than once. How would the runtime complexities of inserting and removing members with a multiset compare with the operations for inserting and removing members from a set? When inserting a memeber into a set in which members may not be duplicated we must search the entire set to ensure that we don not duplicate a memeber. This is an O(n) process. Removing a memebr from a set is O(n) as well because we may have to search the enitire set again. In a multiset, inserting a member is considerably more efficient because we do not have to traverse the memebers looking for duplicates. Therefore, we can insert a new memeber in O(1) time. In a multiset removing a member remains an O(n) process because we still must search for the member we want to remove. 39. The symmetric difference of two sets consists of those members that are in either of the two sets, but not both. The notation for the symmetric difference of two sets, S1 and S2, is S1 ? S2. How could we implement a symmetric difference operation using the set operations union, intersection and difference? Could this operation be implemented more efficiently some other way? A B= (A\B) U (B\A) {= (A U B) (A  B)} Therefore we could implement this operation using 2 calls to set_differenece followed by a call to set_union. This produces a worst-case runningtime of T(m,n)=3mn times some constant, for a complexity of O(mn) where m is the size of A and n is the size of B. 40. Sketch the algorithm for HashInsert in a hash table using open addressing. Hashinsert (B,k) 1 i<--0 2 repeat 3 j<--h(k) 4 if B[j]=NIL 5 then B[j]<--k 6 return j 7 else i<--i+1 8 until i=m 9 error hash table overflow 41. Why are hash tables good for random access but not sequential access? For example, in a database system in which records are to be accessed in a sequential fashion, what is the problem with hashing? Hash tables are excellent for random access because each key hashes us precisely to where we need to be in the table to access the data or atleast within a few steps when a collision occurs. However hash tables do not support sequential access. After hashing to some position we have no way to determine where the next smallest or largest key resides. Compare these with a linked list containing elements that are sorted. Assuming some initial position in the list the next key is easy to determine: we simply look at the next element in the list. (Because the elements situated next to eachother might not be neighbours in a sequential fashion) 42. What is the worst-case performance of searching for an element in a chained hash table? How do we ensure that this case will not occur? A chained hash-table performs the worst when all elements hash into a single bucket. In this case searching for an element is O(n) where n is the number of elements in the table. A ridiculous hash function that would result in this performance is b(k)=c where c is some constant within the bounds of the hash-table. Selecting a good hash function ensures that this case will not occur. If the hash function approximates uniform hashing well, we can expect to locate an element in constant time. 43. What is the worst-case performance of searching for an element in an open-addressed hash table? How do we ensure that this case will not occur? The worst-case performance of seraching for an element in an open adressed hash table occurs once the hash table is completely full and the element we are searching for is not in the table. In this case searching for an element is an O(m)

operation, where m is the nr of position in the table. This case can occur with any hash function. To ensure reasonable performance in an open adressed hash table, we should not let the table become more than 80% full. 44. Explain the generation of hash codes using memory addresses, integer cast and component sum. Memory address: We reinterpret the memory address of the key object as an integer Good in general, except for numeric and string keys  Integer cast: We reinterpret the bits of the key as an integer Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int, and float in C) Component sum: We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows) Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in C) 45. Explain the generation of hash codes using polynomial accumulation. Polynomial accumulation: We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) a0 a1 an1 We evaluate the polynomial p(x) = a0 + a1 x + a2 x2 ++ an1xn1 at a fixed value x, ignoring overflows Especially suitable for strings (e.g., the choice x = 33 gives at most 6 collisions on a set of 50,000 English words) Polynomial p(x) can bevevaluated in O(n) timevusing Horners rule: The following polynomials are successively computed, each from the previous one in O(1) time p0(x) = an1 pi (x) = ani1 + xpi1(z) (i = 1, 2, , n 1)  We have p(x) = pn1(x) 46. How can one implement a compression function using the MAD technique? Division: h2 (y) = y mod m The size m of the hash table is usually chosen to be a prime The reason has to do with number theory and is beyond the scope of this course Multiply, Add and Divide (MAD): h2 (y) = (ay + b) mod m a and b are nonnegative integers such that a mod m <>0 Otherwise, every integer would map to the same value b 47. Explain the quadratic hashing rehashing strategy. Linear hashing h(k,i)=(h(k)+i) mod m 0 <= i <= m-1; checks B[h'(k)], then B[h'(k)+1], , B[m-1] primary clustering effect Quadratic hashing h(k,i)=(h(k)+c1i+c2i^2) mod m h: an auxiliary hash function; 0 <= i <= m-1; c1 <> 0 and c2 <> 0: auxiliary constants checks B[h'(k)]; next checked locations depend quadratically on i secondary clustering effect  Note: i is the number of trial (0 for first) 48. Explain the double hashing rehashing strategy. Linear hashing h(k,i)=(h(k)+i) mod m 0 <= i <= m-1; checks B[h'(k)], then B[h'(k)+1], , B[m-1] primary clustering effect

Double hashing h(k,i)=(h1(k)+ih2(k)) mod m h1, h2: auxiliary hash functions; initially, checks position B[h1(k)] is checked; successive positions are h2(k) mod m away from the previous positions (sequence depends in two ways on key k) h2(k) and m must be relatively prime (to allow for the whole table to be searched). To ensure this condition: take m=2k and make h2(k) generate an odd number or take m prime make h2(k) return a positive integer m smaller than m h1(k)=k mod m h2(k)=1+(k mod m) 49. Show the hash table which results after inserting the values 5, 2, 10, 6, 11, 12, 7, 3, 8, 9, 4, 1 in a chained hash table with N=5 and hash function h(x)=x mod N. 0 5-->10* 1 6-->11-->1* 2 2-->12-->7* 3 3-->8* 4 9-->4* 50. Show the hash table which results after inserting the values 5, 2, 10, 6, 11, 12, 7, 3, 8, 9, 4, 1 in an open addressing hash table with N=16, N'=13 using double hashing, The hash functions are h1(x)=x mod N, and h2(x)=1+ (x mod N').

51. What are the operations for the priority queue ADT? Priority queue: an ADT based on the set model with the operations: insert and deletemin (as well as the usual createEmpty for initialization of the data structure).  Additional support operations: min() returns, but does not remove, an entry with smallest key size() isEmpty() 52. Compare the performance of priority queues using sorted and unsorted lists. Unsorted list  Performance: insert takes O(1) time (we can insert the item at the beginning or end of the list) deleteMin and min take O(n) time (we have to scan the entire list to find the smallest key) Sorted list  Performance: insert takes O(n) time (we have to fiind a place where to insert the item) deleteMin and min take O(1) time (the item is at the beginning of the list) 53. What is a partially ordered tree? Partially ordered tree: Binary tree At the lowest level, where some leaves may be missing, we require that all missing leaves are to the right of all leaves that are not on the lowest level. Tree is partially ordered: the priority of node v is no greater than the priority of the children of v 54. Show the result of inserting the value 14 in the POT of Fig. 2.

Fig. 2 A partially ordered tree.

55.

Explain the notion "heap".

Complete binary tree of height, h, iff: it is empty or its left subtree is complete of height h-1 and its right subtree is completely full of height h-2 or its left subtree is completely full of height h-2 and its right subtree is complete of height h-1.  A complete tree is filled from the left: all the leaves are either on the same level or two adjacent ones, and all nodes at the lowest level are as far to the left as possible.  Heaps are based on the notion of a complete tree A binary tree has the heap property if and only if: it is empty or the key in the root is larger than that in either child and both subtrees have the heap property.  A heap can be used as a priority queue highest priority item is at the root value of the heap structure: we can both extract the highest priority item and insert a new one in O(log n) time 56. What is an AVL tree? Problem with BSTs: worst case operation may take O(n) time. One solution:  AVL tree: binary search tree with a balance condition: For every node in an AVL tree T, the height of the left (TL) and right (TR) subtrees can differ by at most 1: |hL - hR| <= 1 Balance condition must be easy to maintain, and it ensures that the tree depth is O(log n) Requiring that the left and right subtrees have the same height does not suffice (tree may not be shallow) 57. Draw the AVL tree which result from inserting the keys 52, 04, 09, 35, 43, 17, 22, 11 in an empty tree.

58.

Draw the AVL tree resulting after deleting node 09 from the tree of Fig. 3.

Fig. 3. An AVL tree. 59. Describe the left-right double rotation in an AVL tree. Left-right: k1 < k2, k1 < k3, k2 < k3 left rotation around the left child of a node followed by a right rotation around the node itself Rotate to make k2 topmost node

60.

Draw the AVL tree resulting after deleting node 35 from the tree of Fig. 4.

Fig. 4. Another AVL tree. 61. What can you say about the running time for AVL tree operations? a single restructure is O(1) using a linked-structure binary tree  find is O(log n) height of tree is O(log n), no restructures needed  insert is O(log n) initial find is O(log n) restructuring up the tree, maintaining heights is O(log n) remove is O(log n) initial find is O(log n) restructuring up the tree, maintaining heights is O(log n) 62. What is a 2-3 tree? 2-3 tree properties:

Each interior node has two or three children. Each path from the root to a leaf has the same length. A tree with zero or one node(s) is a special case of a 2-3 tree. 63. Show the 2-3 tree which results after inserting the key 13 in the tree of Fig. 5.

Fig. 5. A 2-3 tree. 64. Show the 2-3 tree which results after deleting the key 8 in the tree of Fig. 5

65. What is a 2-3-4 tree? 2-3-4 tree refer to how many links to child nodes can potentially be contained in a given node. For non-leaf nodes, three arrangements : A node with one data item always has two children A node with two data items always has three children A node with three data items always has four children In short, a non-leaf node must always have one more child than it has data items. Empty nodes are not allowed. 66. What were the disjoint sets with union and find designed for? Applicable to problems where: start with a collection of objects, each in a set by itself; combine sets in some order, and from time to time ask which set a particular object is in  Equivalence classes: If set S has an equivalence relation (reflexive, symmetrical, transitive) defined on it, then the set S can be partitioned into disjoint subsets S1, S2,, ... S with U(k)Sk=S  Equivalence problem: given a set S and a sequence of statements of the form a b process the statements in order in such a way that at any time we are able to determine in which equivalence class a given element belongs 67. Define the operations of the union-find set ADT. Operations: union(A, B) takes the union of the components A and B and calls the result either A or B, arbitrarily. find(x), a function that returns the name for the component of which x is a member. initial(A, x) creates a component named A that contains only the element x. 68. Draw a sketch showing a lists implementation for the union-find set ADT with sets: 1: {1, 4, 7}; 2: {2, 3, 6, 9}; 8:{8, 11, 10, 12}.

69. Draw a sketch showing a tree forest implementation for the union-find set ADT with sets: 1: {1, 4, 7}; 2: {2, 3, 6, 9}; 8:{8, 11, 10, 12}.

70. How can one speed up union-find ADT operations? Union by size (rank): When performing a union, make the root of smaller tree point to the root of the larger  Implies O(n log n) time for performing n unionfind operations: Each time we follow a pointer, we are going to a subtree of size at least double the size of the previous subtree Thus, we will follow at most O(log n) pointers for any find. Path compression: After performing a find, compress all the pointers on the path just traversed so that they all point to the root Implies O(n log* n) time for performing n union- find 71. Give an adjacency matrix representation for the graph of Fig. 6.

Fig. 6. A graph. 72. Give an adjacency list representation for the graph of Fig 6. 73. How do adjacency list/matrix representations for graphs compare and when should one ore another be used? It is faster to work with adjacency matrices, but they use more space than adjacency lists, so you will choose one or the other depending on whivh resource is more important to you. Adj list usefull when e<<n. 74. Apply Dijkstras algorithm for the graph of Fig. 6 starting at node 1. 1,3,4,2,8,12,5,6,7,10,11,14,9,13 75. Apply Prims algorithm to the graph of Fig 7.

Fig 7. Another graph. 1,4,7,6,5,8,10,11,13,15,12,14,3,2 76. Apply Kruskals algorithm to the graph of Fig. 8

Fig. 8. Yet another graph. (4,6) (4,7) (7,8) (9,12) (12,13) (2,3) (7,5) (4,7) (8,10) (8,11) (4,1) 77. Apply Bellman-Ford algorithm to the graph of Fig. 9.

Fig.9. A digraph a,b,c,e,d 78. Apply Floyds algorithm to the graph of Fig 10.

Fig. 10. Another graph. 79. Use breadth-first search to determine the articulation points of the graph of Fig. 10. 80. Use depth-first search to determine the articulation points of the graph of Fig. 10. 81. Determine the transitive closure of the graph of Fig. 10. 82. Apply topological sort to the dag of Fig. 11.

Fig. 11. A Directed Acyclic Graph. 0,9,6,1,2,3,4,5,7,8 83. Develop a short algorithm for finding cycles in a graph. 84. Consider the following MAXMIN algorithm. How many comparisons does it use? Is it likely to be faster or slower than the divide-and-conquer algorithmin practice? procedure maxmin2(S) comment computes maximum and minimum of S[1..n] in max and min resp. 1. if n is odd then max:=S[n]; min:=S[n] 2. else max:=; min:= 3. for i := 1 to n/2 do 4. if S[2i 1] S[2i] 5. then small:=S[2i 1]; large:=S[2i] 6. else small:=S[2i]; large:=S[2i 1] 7. if small < min then min:=small 8. if large > max then min:=small 85. A sequence of numbers < a1, a2, a3,, an > is oscillating if ai < ai+1 for every odd index i and ai > ai+1 for every even index i. Describe and analyze an efficient algorithm to compute the longest oscillating subsequence in a sequence of n integers. 86. Find the following spanning trees for the weighted graph shown in Figure 12, below.

(a) A breadth-first spanning tree rooted at s. (b) A depth-first spanning tree rooted at s. (c) A shortest-path tree rooted at s. (d) A minimum spanning tree. You do not need to justify your answers; just clearly indicate the edges of each spanning tree. Yes, one of the edges has negative weight.

Figure12. A weighted graph. 87. Describe and analyze an algorithm to compute the size of the largest connected component of black pixels in an n n bitmap B[1..n; 1..n]. For example, given the bitmap in Figure 12 (below) as input, your algorithm should return the number 9, because the largest conected black component (marked with white dots on the right) contains nine pixels.

Figure 13. Bitmap example. 88. Describe and analyze an algorithm that determines whether a given graph is a tree, where the graph is represented by an adjacency list. 89. Solve the recurrence T(n) = 5T(n/17) + O(n^(4/3)) 90. Solve the recurrence T(n) = 1/n + T(n 1), where T(0) = 0. 91. Suppose you are given an array of n numbers, sorted in increasing order. (a) Describe an O(n)-time algorithm for the following problem: Find two numbers from the list that add up to zero, or report that there is no such pair. In other words, find two numbers a and b such that a + b = 0. (b) Describe an O(n2)-time algorithm for the following problem:Find three numbers from the list that add up to zero, or report that there is no such triple. In other words, find three numbers a, b, and c, such that a+b+c = 0. [Hint: Use something similar to part (a) as a subroutine.] 92. Sketch the selection sort algorithm and show how it works on the array containing the keys 82, 31, -13, 45, 99, -1, -7, 22. void selection(ITEM[] a, int l, int r) { for (int i = l; i < r; i++) { int min = i; for (int j = i+1; j <= r; j++) if (less(a[j], a[min])) min = j; exch(a, i, min); } }

For each i from l to r-1, exchange a[i] with the minimum element in a[i],..., a[r]. As the index i travels from left to right, the elements to its left are in their final position in the array (and will not be touched again), so the array is fully sorted when i reaches the right end. 93. Sketch the insertion sort algorithm and show how it works on the array containing the keys 82, 31, -13, 45, 99, -1, -7, 22. void insertion(ITEM[] a, int l, int r) { int i; for (i = r; i > l; i--) compExch(a, i-1, i); for (i = l+2; i <= r; i++) { int j = i; ITEM v = a[i]; while (less(v, a[j-1])) { a[j] = a[j-1]; j--; } a[j] = v; } } For each i, it sorts the elements a[l], ..., a[i] by moving one position to the right elements in the sorted list a[l], ..., a[i-1] that are larger than a[i], then putting a[i] into its proper position. 94. Sketch the merge sort algorithm and show how it works on the array containing the keys 82, 31, -13, 45, 99, -1, -7, 22. Merge-Sort(A,p,r) if p<r then q<--[(p+r)/2] Merge-Sort(A,p,q) Merge-Sort(A,q+1,r) MERGE(A,p,q,r) MERGEA(A,p,q,r) n1<--q-p+1 n2<--r-q create arrays L[1...n1+1] and R[1...n2+1] for i<-1 to n1 do L[i]<--A[p+i-1] for j<--1 to n2 do R[j]<--A[q+j] L[n1+1]<--inf R[n2+1]<--inf i<--1 j<--1 for k<--p to r do if L[i]<=R[j] then A[k]<--L[i] i<--i+1 else A[k]<--R[j] j<--j+1 95. Sketch the quick sort algorithm and show how it works on the array containing the keys 82, 31, -13, 45, 99, -1, -7, 22. Quicksoert(A,p,r) if p<r then q<--PARTITION(A,p,r)

Quicksort(A,p,q-1) Quicksort(A,q+1,r) PARTITION(A,p,r) x<--A[r] i<--p-1 for j<--p to r-1 do if A[j]<=x then i<--i+1 exchange A[i]<->A[j] echange A[i+1]<->A[r] return i+1 96. Sketch the radix sort algorithm and show how it works on the array containing the keys 82, 31, -13, 45, 99, -1, -7, 22. Radix-Sort(A,d) for i<--1 to d do use a stable sort to sort array A on digit i (Suppose you have a sequence of integers to sort. Consider their decimal representation and suppose it requires two digits. Then we sort them by first sorting them by to their last digit and then by their first digit.) 97. Sketch the counting sort algorithm and show how it works on the array containing the keys 2, 1, 5, 5, 9, 3, 7, 2. for i <-- 0 to k 2. do C[i] <--0 3. for j <-- 1 to length[A] 4. do C[A[j]] C[A[j]]+1 6. for i <--1 to k Q(k) 7. do C[i] = C[i] + C[i-1] 9. for j <-- length[A] downto 1 10. do B[C[A[j]]] <--A[j] 11. C[A[j]] <--C[A[j]]-1 (CountingSort is an algorithm which requires that the keys are in the range of 1 to k and exploits the fact that keys can be used for indexing an array, hence it is not based on comparison. First we count the occurrences of each key of the sequence A and store the count in array C: Then we determine the number of elements which are less than or equal to each of the keys by calculating the prefix sums:) 98. Sketch the bucket sort algorithm and show how it works on the array containing the keys 0.82, 0.31, 0.13, 0.45, 0.99, 0.1, 0.7, 0.22. Bucket-Sort(A) n<--length[A] for i<--1 to n do insert A[i] into list B[[nA[i]]] for i<-0 to n-1 do sort list B[i] with insertion sort concatenate the lists B[0],B[1],...B[n-1] together in order (Assume that our keys are real numbers in the interval [0,1). We create say 10 buckets for each of the intervals [i / 10, (i +1) / 10) and store each element in its appropriate bucket.  Finally we sort the buckets with e.g. insertion sort) 99. What are the criteria used to evaluate the running time of an internal sorting algorithm? Criteria used to evaluate the running time of an internal sorting algorithm are: The number of steps required to sort n records; The number of comparisons between keys needed to sort n records (if the comparison is expensive); The number of times the records must be moved 100. How do we select a sort algorithm? It depends on the situation:  Is the size of the input large (e.g. all courses) or small (e.g. courses a student is taking)?  For a small input size O(n2) algorithms like InsertionSort perform better, for a large size O(n lg n) algorithms.  Do the elements to be sorted require lots of memory? If so, we can avoid moving them around during sorting by using an auxiliary sequence with pointers to the elements instead and moving only the pointers. If they are small, we better sort them directly.

Can elements have the same keys? If so, do we require a stable sort? O(n2) algorithms tend to be stable, O(n log n) in place algorithms not. However, we can make any unstable algorithm stable by adding a key with the position of the elements in the original array. This costs extra space and extra time for the comparisons. If we decided anyway to sort the sequence of pointers rather than the elements, we can use the position of the elements in the unsorted sequence in comparisons. In this case, no additional space is required.  Do we require guarantees on the sorting time, e.g. in a hard realtime environment (e.g. in control systems, networks)? This rules out Quicksort because of its Q(n2) worst case behavior Do we have a limited amount of space available, like in embedded processor? This rules out MergeSort since it requires in the order of n extra space and it makes Quicksort questionable since it requires also in the order of n extra space in the worst case. However, we can improve Quicksort to require only in the order of lg n extra space.  Can the sequence be so large that it does not completely fit into main memory and virtual memory is going to be used? If so, sorting algorithms with good local behavior are to be preferred. If we are at element A[i] in the Heapify procedure of HeapSort, then the next element accessed with be A[2 i] or A[2 i+1], and so forth, so elements are accessed all over the array in quick succession. The Partition procedure of Quicksort accesses A[i], then A[i+1], etc., as well as A[j], then A[j1], etc., so has a good local behavior. Most O(n^2) algorithms have good local behavior. Is the input so big that is cannot fit into mainmemory and too big for virtual memory? Then we have to use external sorting algorithms anyway.  Do we know more about the input which we can exploit for sorting in Q(n)? If the keys are in a small range of integers (e.g. the age of a person, year of printing), we can use CountingSort. If each key is a sequence of keys which can be compared on their own we can use RadixSort. If the keys are real number over an interval and are distributed evenly, we can use BucketSort. 101. What strategies ca be in volved for selecting a new E-vertex with branch and bound? A: General strategy: generate all children of thecurrent E-vertex before selecting a new Evertex. Strategies for selecting a new E-vertex: LIFO order: depth first, using a stack. FIFO order: breadth first, using a queue. Best-first order: use a priority queue. In each case, a bounding function is also used to kill vertices. 102. Compare the backtracking and branch and bound search strategies. Backtracking easy to implement little memory required slow to run  Branch & Bound difficult to implement large amounts of memory maybe faster than backtracking 103. Describe the local search strategy. Sometimes an optimal solution may be obtain if: Start with a random solution. Apply to the current solution a transformation from some given set of transformations to improve the solution. The improvement becomes the new "current" solution. Repeat until no transformation in the set improves the current solution.  Note: the method makes sense if we we can restrict our set of transformations to a small set, so we can consider all transformations in a short time (e.g. for a problem of size n, O(n2)O(n3) transformations)  Transformations are called local transformations, and the method is called local search 104. Apply the local search strategy to the problem of finding the MST of the graph shown in Figure 14, below:

Fig. 14. Another weighted graph. 105. Describe the Divide and Conquer method for algorithm design. Divide and conquer method for algorithm design: Divide: If the input size is too large to deal with in a straightforward manner, divide the problem into two or more disjoint subproblems Conquer: Use divide and conquer recursively to solve the subproblems Combine: Take the solutions to the subproblems and merge these solutions into a solution for the original problem 106. Describe the steps taken when applying dynamic programming strategy when developing an algorithm. Development of a dynamic programming algorithm involves four steps: Characterize the structure of an optimal solution. Recursively define the value of an optimal solution. Compute the value of an optimal solution in a bottom-up fashion. Construct the optimal solution. 107. Suppose we have 220 128-bit elements that we would like to sort. What would be the efficiency of sorting these using quicksort? What would be the efficiency of sorting these as radix-216 numbers using radix sort? Which approach would be better? Suppose we have 210 128-bit elements rather than 220 elements. How do quicksort and radix sort compare in this case?A: Sorting with quicksort requires O (n lg n) = (220)(20) = (2.10)(107) times some constant amount of time. Considering the elements as radix-216 numbers, the number of digit positions, p, is 8, and the number of possible digit values, k, is 216. Therefore, sorting with radix sort requires O (pn + pk) = (8)(220) + (8)(216) = (8.91)(106) times some constant amount of time. If the space requirements of radix sort are acceptable, radix sort is more than twice as efficient as quicksort. In the second case, sorting with quicksort requires O (n lg n) = (210)(10) = 10,240 times some constant amount of time. Radix sort requires O (pn + pk) = (8)(210) + (8)(216) = 532,480 times some constant amount of time, or 50 times as much time as quicksort! Here is an example of why k is typically chosen to be close to and no more than n. Had we used a radix of 28, radix sort would have required O (pn + pk) = (16)(28) + (16)(28) = 8160 times some constant amount of time, and would have been slightly better than quicksort. However, it is worth noting that the space requirement of radix sort may negate small benefits in time in many cases. 108. In a sorted set, the successor of some node x is the next largest node after x. For example, in a sorted set containing the keys 24, 39, 41, 55, 87, 92, the successor of 41 is 55. How do we find the successor of an element x using binary search? What is the runtime complexity of this operation? A: In a sorted set, to determine the successor of some element x using binary search, first we locate x. Next, we simply move one element to the right. The runtime complexity of locating either x or its successor is O (lg n). 109. Suppose we model an internet using a graph and we determine that the graph contains an articulation point. What are the implications of this? A: Graphs have many important uses in network problems. If in a graph modeling an internet we determine that there is an articulation point, the articulation point represents a single point of failure. Thus, if a system residing at an articulation point goes down, other systems are forced into different connected components and as a result will no longer be able to communicate with each other. Therefore, in designing large networks in which connectivity is required at all times, it is important that there be no articulation points. We can curb this problem by placing redundancies in the network.

110. Consider a graph that models a structure of airways, highways in the sky on which airplanes are often required to fly. The structure consists of two types of elements: navigational facilities, called navaids for short, and airways that connect navaids, which are typically within a hundred miles of each other. Airways may be bidirectional or one-way. At certain times some airways are not available for use. Suppose during one of these times we would like to determine whether we can still reach a particular destination. How can we determine this? What is the runtime complexity of solving this problem? A: If we perform breadth-first search from our starting point in the airway structure, we can reach any destination if we discover it during the search. Otherwise, the destination must reside in a component of the graph that became unreachable when an airway was made unavailable. The closed airway constitutes a bridge in the graph. This problem can be solved in O (V + E ) time, where V is the number of navaids and E is the number of airways in the structure. This is the runtime complexity of breadth-first search. 111. Suppose we would like to use a computer to model states in a system. For example, imagine the various states of a traffic-light system at an intersection and the decisions the system has to make. How can we use a graph to model this? A: Directed graphs are good for modeling state machines, such as the traffic-light system mentioned here. In a directed graph, we let vertices represent the various states, and edges represent the decisions made to get from one state to another. Edges in the graph are directed because a decision made to get from one state to the next does not imply that the decision can be reversed. 112. The transpose of a directed graph is a graph with the direction of its edges reversed. Formally, for a directed graph G = (V, E ), its transpose is indicated as GT. How could we form the transpose of a graph assuming an adjacency-list representation? What is the runtime complexity of this? A: To form the transpose G T of a graph G = (V, E ), we traverse the adjacency list of each vertex u in V. As we traverse each list, we make sure that vertex v and u have both been inserted into G T by calling graph_ins_vertex for each vertex. Next, we call graph_ins_edge to insert an edge from v to u into G T. Each call to graph_ins_vertex runs in O (V ) time. This operation is called 2E times, where E is the number of edges in G. Of course, some of these calls will not actually insert the vertex if it was inserted previously. Each call to graph_ins_edge runs in O (V ) time. This operation is called once for each edge in G as well. Thus, using this approach, the overall time to transpose a graph is O (V E ). 113. The following recursive definition has an error. What is it, and how can we fix it? For a positive integer n, the definition, in its proper form, is common in formally computing the running time of divide-and-conquer algorithms, such as merge sort. Merge sort divides a set of data in half, then divides the halves in half, and continues this way until each division contains a single element. Then, during the unwinding phase, the divisions are merged to produce a final sorted set.

A: The problem with this definition is that it never reaches the terminating condition, n = 0, for any initial value of n greater than 0. To fix the problem, it needs an obtainable terminating condition. The condition n = 1 works well, which means we should also change the second condition in the function. A recursive definition with an acceptable terminating condition is presented here: This happens to be the correct definition for the running time of merge sort. Such a function is called a recurrence. In more formal analysis, recurrences are used frequently to describe the running times of recursive algorithms. 114. Analyze the asymptotic time complexity T(n) for the following two divide-and-conquer algorithm. You may assume that n is a power of 2. int foo(A) { n = A.length; if (n==1)

} 115. Consider a game tree in which there are six marbles, and players 1 and 2 take turns picking from one to three marbles. The player who takes the last marble loses the game. a) Draw the complete tree for this game. b) If the game tree were searched using the alpha-beta pruning technique, and nodes representing configurations with the smallest number of marbles are searched first, which nodes are pruned? c) Who wins the game if both play their best? 116. Give an algorithm that takes an array of n numbers (n is even) and finds the maximum one and the minimum one in 3(n/2)-2 comparisons. Initialize min and max For i = 0 to n/2: if x[2*i]<x[2*i+1]: if x[2*i]< min: min = x[2*i] if x[2*i+1]> max: max = x[2*i+1] else: if x[2*i+1]< min: min = x[2*i+1] if x[2*i]> max: max = x[2*i] if n is even, the loop starts at i=2, if odd i=1. This results in (3(n-2)/2)+1 comparisons if even or 3(n-1)/2 if odd. 117. Suppose you have n integers in the range from 0 to n3-1. Explain how to sort them in O(n) time. 118. Develop an algorithm to solve the basic Tower of Hanoi problem, i.e., 3 towers, N disks, and the given rules. 119. How could you make an algorithm for finding the longest path in a graph with only negative weights? 120. Suppose there are three alternatives for dividing a problem of size n into subproblems of smaller size: if you solve 3 subproblems of size n/2 , then the cost for combining the solutions of the subproblems to obtain a solution for the original problem is Theta(n2 sqrt(n)); if you solve 4 subproblems of size n/2, then the cost for combining the solutions is Theta(n2 ); if you solve 5 subproblems of size n/2 , then the cost for combining the solutions is Theta(n log n). Which alternative do you prefer and why? 121. A palindrome is a word w1w2 . . .wk whose reverse wkwk1 . . .w1 is the same string, e.g. abbabba. Consider a string A = a1a2 . . . an. A partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, aba|b|bbabb|a|b|aba is a palindrome partitioning of ababbbabbababa. Design a dynamic programming algorithm to determine the coarsest (i.e. fewest cuts) palindrome partition of A. 122. Consider a directed graph G = (V,E) where each edge is labeled with a character from an alphabet Sigma, and we designate a special vertex s as the start vertex, and another f as the final vertex. We say that G accepts a string A = a1a2 . .

return A[0]; } int half = (int) n/2 int[] A1 = new int[half]; int[] A2 = new int[n-half]; for (int i=0; i <= half-1; i++) { for (int j=0; j <= 1; j++) { if (j==0) A1[i] = A[2i]; else A2[i] = A[2i+1]; } } A2[n-half-1] = A[n-1]; b1 = foo(A1); b2 = foo(A2); if (b1>b2) return b1; else return b2;

. an if there is a path from s to f of n edges whose labels spell the sequence A. Design an O((|V | + |E|)n) dynamic programming algorithm to determine whether or not A is accepted by G. 123. Give the pseudocode for a greedy algorithm that solves the following optimization problem. Justify briefly why your algorithm finds th optimum solution. What is the asymptotic running time of your algorithm in terms of n? 124. There are n gas stations S1,Sn on E60 highway from Cluj-Napoca to Oradea. On a full tank of gas, your car goes for D miles. Gas station S1 is in Cluj-Napoca, and each gas station Si for 2 <= i <= n, i sat di < D kilometers after the previous gas station Si-1, and gas station Sn is in Oradea. What is the minimum number of gas stops you must make when driving from Cluj-Napoca to Oradea? 125. Discuss the solution for Travelling salesman problem using branch & bound technique. 126. Apply backtracking technique to solve the following instance of subset sum problem : S={1,3,4,5} and d=11. 127. The configuration of a playground is given as an n by n grid. Each of the elements in the grid has an elevation, expressed as a positive integer number. Someone places a ball at coordinates (i,j) on this playground. The ball then moves to a lower elevation in neighboring locations. List all the possibilities for the ball to move in order to leave the playground. What is the running time of your algorithm? 128. There is a n <= 26 persons, identified by capital letters, each person having at least n/2 friends. One of these persons owns a book, which the rest of the persons wish to read. Find all ways of passing the book to all the friends of the owner, starting at the owner, and reaching each friends exactly once. Finally, the book should be handed back to the owner. What is the running time of your algorithm? 129. Assume we are given a connected, undirected graph G, with n nodes and m edges. Develop an algorithm to set directions for the edges of the graph such a way that from every vertex there should be an even number of outgoing arcs. What is the running time of your algorithm? 130. Develop and algorithm which constructs shedules for round-robin tournaments (for games like tennis, chess, voleyball, soccer, etc.), where the number of players is not a power of 2. Players (at most 30) are identified by numbers. What algorithm design method did you use? What is the running time of your algorithm? 131. On a chess board, there are m obstacles given by their board coordinates. Also, on that board there is a bishop located at coordinates (i,j) (the upper left corner has coordinates (0,0)). Find the path with the minimum number of moves, which obey the rules of chess game, for the bishop to reach a given position (p,q), avoiding the obstacles. 132. Draw the B+Tree which results after insertion of key 70 in the B+tree below.

133. Draw the B+Tree which results after insertion of key 95 in the B+tree below.

134. Draw the B+Tree which results after deletion of key 25 in the B+tree below.

135. What is a multi-way search tree of order m? A: Multiway Search Trees (MWSTs) are a generalization of BSTs MWST of order n: Each node has n or fewer sub-trees: S1 S2.Sm, m n Each node has n 1or fewer keys k1 k2 km1 : m1 keys in ascending order k(Si) ki k(Si+1) , k(Sm1) < k(Sm) Suitable for disks: Nodes correspond to disk pages Pros: tree height is low for large n fewer disk accesses Cons: low space utilization if non-full MWSTs are non-balanced in general!

136. Use Use the alpha/beta procedure to identify which parts of the tree can be pruned. Clearly identify alpha and beta pruning.

137. An architect has been commissioned to design a new building, called the Data Center. Gary wants his top architectural protg to design a scale model of the Data Center using precision-cut sticks, but he wants to preclude the model from inadvertently containing any right angles. Gary fabricates a set of n sticks, labeled 1,2,...,n, where stick i has length xi. Before giving the sticks to the protg, he shows them to you and asks you whether it is possible to create a right triangle using any three of the sticks. Give an efficient algorithm for determining whether there exist three sticks a, b, and c such that the triangle formed from them having sides of lengths xa, xb, and xc is a right triangle (that is, xa2 + xb2 = xc2). 138. Argue that any comparison based sorting algorithm can be made to be stable, without affecting the running time by more than a constant factor. 139. Define a linked list with a loop as a linked list in which the tail element points to one of the lists elements and not to NULL. Assume that you are given a linked list L, and two pointers P1, P2 to the head. Write an algorithm that decides whether the list has a loop without modifying the original list. The algorithm should run in time O(n) and additional memory O(1), where n is the number of elements in the list. 140. Use a min-heap to give an O(n log k)-time algorithm which merges k sorted lists into one sorted list, where n is the total number of elements in all the input lists. 141. Let G = (V,E) be an undirected graph, and s be a node in V . Edge (u, v) 2 E is called a circular edge if the distance from s to u is identical to the distance from s to v. Give a linear algorithm that given s finds all circular edges in graph. Explain what data structures you use, and provide complexity analysis. 142. Give (in pseudocode) an O(|V | + |E|) algorithm that takes as input a directed acyclic graph and two vertices s, t, and returns the number of paths from s to t in G. Your algorithm needs only to count the paths, not list them. Hint: Use topological sort as a part of your solution. 143. Give an algorithm to either output a message No counterfeit coin, or identify which of three coins: A, B and C is counterfeit. 144. Construct a divide and conquer algorithm that divides a set S set of coins into three equal subsets, and uses part (a) to solve small (i.e. 3-coin) sets. 145. Suppose you have analysed a file and determined that the frequency of occurrence of certain characters is as follows: Character a b c d e f Occurences 15 7 5 8 30 10 a) Construct the Huffman tree for the characters b) List the codes for each character c) Use the tree to compress the following strings: i) faded ii) abed iii) feed

Você também pode gostar