R2 = Iso Lef T R1

R2 = Iso Lef T R1

ALGORITHMS -CSE202-SPRING 2000 Divide and Conquer and Sorting, Some Solutions Name Tree Isomorphism 0 T Problem Give an efficient algorithm to decide , given two rooted binary trees T and , 0 T whether T and are isomorphic as rooted trees. Solution 0 0 0 0 T r oot T L; R ; L ;R Let r oot be the root of and be the root of , and let be the left and 0 0 T T T f right subtrees of T and respectively. If embeds in , then the isomorphism must 0 0 r oot L L map r oot to , and must map every member of either entirely to or entirely to 0 Iso R and vice versa. Thus, we can define the isomorphism relationship on sub-trees as NIL;NIL=True Isox; N I L=IsoN I L; x=False x 6= NIL follows: Iso , if and otherwise Isor ;r = 1 2 IsoLef tr ; Lef tr AN D I soRig htr ; R ig htr 1 2 1 2 OR IsoLef tr ; R ig htr AN D I soRig htr ; R ig htr : 1 2 1 2 2 n The above immediately translates into a program, and the time, I claim, is O . 0 2 T; y 2 T x; y The reason is that for any nodes x , the above calls itself on at most y P once (in fact, precisely when x and are at the same depths of their trees.) Let be 0 0 x P R oot y the path from R oot to and be that from to ; in a tree there is exactly one’ such path. Then, of the four recursive calls at any level, at most one involves nodes from 0 P both P and . Inductively, at any recursion depth, at most one call compares a sub-tree y d containing x to one containing . Since all sub-trees called at depth of the recursion are depth d in their respective trees, the claim follows. Therefore the total number of 2 n calls is at most O . 1 PROBLEM 1(8-3IN CLR): Stooge sort Solution: [1 :::n] a) We will prove by induction that Stooge sort correctly sorts the input array A . = 1 n = 2 n = 1 i = j i +1 > j The base cases are for n and .If then , and thus ; [i] n =2 then, line 4 returns A (which is correct). If then lines 1 and 2 guarantee that a bigger element will be returned in a position following that of a smaller element. Thus, n =1 n =2 Stooge sort correctly sorts A when and when . 3 The inductive case happens when n . The inductive hypothesis is that Stooge sort correctly sorts arrays of length <n. We must show that Stooge sort correctly sorts arrays of length n. The input array A is essentially split into three pieces: the first third and the last n 2k k = bn=3c third are both of length k , and the second third is of length ,where . i +1 = n Note 1: j in the first or “top” call to Stooge sort. Note 2: Observe that the = bn=3c n=3 middle third is at least as long as each of the other thirds (since k ,but 2k = n 2bn=3c n2n=3 = n=3 n ; this is actually necessary for the algorithm to work). In line 6, Stooge sort is applied to the first two-thirds of A (i.e., the subarray of length k A n 3 n consisting of the first and middle thirds of ). We are assuming that ,so = bn=3c 1 n k n k and is strictly less than . This means that we can invoke the inductive hypothesis on this subarray (i.e., we assume by inductive hypothesis that the 0 A first two-thirds of A will be sorted in the first two-thirds of the new array ). 0 A n k A After line 6, the largest k elements of will be in the last positions of (i.e., 0 x they will be in the middle third and/or the last third of A ). Here’s why: Let be any one A x A of the k largest elements in .Case1:If is in the last third of , then it will be in the 0 x last third of A , and our claim is rather trivially true. Case 2: Assume is in the first two- x k A n k thirds of A.Since is one of the largest elements in , at least other elements x k n k in A are “smaller” (actually, ) than .Atmost, of these elements can be in the n 2k last third of A. Therefore, at least of these elements are in the first two-thirds of n 2k A A. We can conclude that there are at least elements in the first two-thirds of n 2k = n 2bn=3cn2n=3 = n=3 bn=3c = k that are smaller than x.But . 0 n 2k A Because k , the first k positions of will consist of elements that are all 0 x A x smaller than x. That means that will be in the middle third of . In either case, will 0 be in the last two-thirds of A . 0 In line 7, Stooge sort is applied to the last two-thirds of A (i.e., the subarray of length 0 k A n 3 n consisting of the middle and last thirds of ). We are assuming that ,so = bn=3c 1 n k n k and is strictly less than . This means that we can invoke the inductive hypothesis on this subarray (i.e., we assume by inductive hypothesis that the 0 00 A last two-thirds of A will be sorted in the last two-thirds of the new array ). A Recall that after line 6, the largest k elements of will be in the last two-thirds (i.e., 0 0 k A k A A the last n positions) of . After line 7, the largest elements of (or ) will make 00 up the last third of A , and they will be in sorted order. 00 In line 8, Stooge sort is applied to the first two-thirds of A (i.e., the subarray of 00 k A length n consisting of the first and middle thirds of ). We are assuming that 3 k = bn=3c 1 n k n n ,so and is strictly less than . This means that we can invoke the inductive hypothesis on this subarray (i.e., we assume by inductive hypothesis 00 B that the first two-thirds of A will be sorted in the first two-thirds of the new array ). 2 00 A A Recall that after line 7, the largest k elements of will be in the last third of k in sorted order. After line 8, the other elements (i.e., the n smallest elements) will make up the first two-thirds of B , and they will be in sorted order; also, the last third of 00 A B B will be the same as the last third of . Thus, , which is the final array returned by the algorithm, will consist of all the elements of A in sorted order. n = 3T 2n=3 + O 1 b,c) The recurrence is T , since we split arrays into three smaller arrays, each of size 2/3 the size of the big array (don’t worry about floor and 1 ceiling operators here); the nonrecursive portion of the code is O (i.e., the complexity is bounded by a constant). Its solution is, by Case 1 of the Master Theorem (with log 3 3=2 = 3 b = 3=2 = log 3 T n n log 3 a , ,and ,say), . Now, to compute , 3=2 3=2 log 3 e 2:71 :71 > 2:7 observe that it is equal to . Thus, since 2 , we can say that log 3=2 e 2:7 n n T . The worst-case running time of Stooge sort is worse than all worst-case 2 2 n n n n running times of insertion sort ,mergesort lg , quicksort and n n heapsort lg . Begone, Stooges! Note: You could also look at the corresponding recursion tree, which will have depth log n n = 1 n = 2 (remember that and are both base cases). For simplicity, let’s 3=2 n say that the depth of all leaves in the recursion tree is log . Then, when we add up 3=2 the number of subproblems at each recursive level, we get: log n+1 log n+1 3=2 3=2 log n 13 3 1 0 1 3=2 3 +3 +::: +3 = = 13 2 (Think: “first in” - “first out” .) 1 common base log n 3=2 For simplicity, let’s consider 3 . By Logarithm Property 6 on p.443 in Appendix log n log 3 3=2 3=2 = n O 1 A.5, 3 (look familiar?). Remember that work is being done at each node in the recursion tree. 3 PROBLEM N.1:Stooge sort (problem 8-3, pag. 169 of CLR, questions a,b,c). Solution: [1 :::n] a) We will prove by induction that Stooge sort correctly sorts the input array A . = 2 The base case is for n . In this case the first two lines of Stooge sort guarantee that a bigger element will be returned in a position following that of a smaller element. n = 2 Thus, Stooge sort correctly sorts A when . The inductive case happens when 3 n .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    11 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us