sol13 - COP3530 Solution 13 1. 1)Merge Sort initlal segment:

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP3530 Solution 13 1. 1)Merge Sort initlal segment: [21][25][49][25][93][62][72][8][37][16][54] merge to b : [21 25][25 49][62 93][8 72][16 37][54] copy to a : [21 25][25 49][62 93][8 72][16 37][54] merge to b : [21 25 25 49][8 62 72 93][16 37 54] copy to a : [21 25 25 49][8 62 72 93][16 37 54] merge to b: [8 21 25 25 49 62 72 93][16 37 54] copy to a: [8 21 25 25 49 62 72 93][16 37 54] merge to b : [8 16 21 25 25 37 49 54 62 72 93] 2)Quick Sort Step 1: [ 21 , 25 , 49 , 25 , 93 , 62 , 72 , 8 , 37 , 16 , 54] Step 2: [ 8 , 16 , (21) , 25 , 49 , 25 , 93 , 62 , 72 , 37 , 54] step 3: [(8), 16 , (21) ,(25), 49 , 25 , 93 , 62 , 72 , 37 , 54] step 4: [(8), (16), (21) ,(25), 25 , 37 ,(49), 93 , 62 , 72 , 54] step 5: [(8), (16), (21) ,(25),(25), 37 ,(49), 62 , 72 , 54 ,(93)] step 6: [(8), (16), (21) ,(25),(25),(37),(49), 54 ,(62), 72 ,(93)] step 7: [(8), (16), (21) ,(25),(25),(37),(49),(54),(62), (72),(93)] W < D ^ &  &  &  &  &   W D ^  > E E E ^ PROBLEM 3: part 1: Divide each of the matricices X and Y into 4 smaller matricies of size (n/2) x (n/2). Call thse matricies X11, X12, X21, X22, Y11, Y12, Y21, Y22. Then the resulting Z matrix can similary be split into 4 smaller matricies Z11, Z12, Z21 and Z22. Now define the following intermediate matricies: M1:=(X11 + X22) * (Y11 + Y22) M2:=(X21 + X22) * Y11 M3:=X11 * (Y12 ‐ Y22) M4:=X22 * (Y21 ‐ Y11) M5:=(X11 + X12) * Y22 M6:=(X21 ‐ X11) * (Y11 + Y12) M7:=(X12 ‐ X22) * (Y21 + Y22) From this we can construct Z as follows: Z11 = M1 + M4 ‐ M5 + M7 Z12 = M3 + M5 Z21 = M2 + M4 Z22 = M1 ‐ M2 + M3 + M6 part 2: //assume that you have square matricies matrix matrix_mult(matrix A, matrix B) { int n=A.size(); if(n==1) return new matrix(1,1,A[1][1]*B[1][1]); //a matrix w/ dimensions 1x1 and element A[1][1]*B[1][1] matrix A11=new matrix(A,1,1,n/2,n/2); matrix A12=new matrix(A,1,n/2+1,n/2,n); matrix A21=new matrix(A,n/2+1,1,n,n/2); matrix A22=new matrix(A,n/2+1,n/2+1,n,n); matrix B11=new matrix(B,1,1,n/2,n/2); matrix B12=new matrix(B,1,n/2+1,n/2,n); matrix B21=new matrix(B,n/2+1,1,n,n/2); matrix B22=new matrix(B,n/2+1,n/2+1,n,n); matrix M1=matrix_mult(A11+A22,B11+B22); matrix M2=matrix_mult(A21+A22,B11); matrix M3=matrix_mult(A11,B21‐B22); matrix M4=matrix_mult(A22,B21‐B11); matrix M5=matrix_mult(A11+A12,B22); matrix M6=matrix_mult(A21‐A11,B11+B12); matrix M7=matrix_mult(A12‐A22,B21+B22); matrix C11 = M1+M4‐M5+M7; matrix C12 = M3+M5; matrix C21 = M2+M4; matrix C22 = M1‐M2+M3+M6; return combine(C11,C12,C21,C22); } part 3: Assume that it takes f(n) operations to solve this problem for 2^n x 2^n matricies. Then using this algorithm we have that f(n)=7f(n‐1)+h*4^n, where h is a constant that depends on the number of additions performed at each iteration of the algorithm. Hence f(n)~(7+h)^n, so we get running time O(N^(log7 +h)) ~ O(N^2.8). ...
View Full Document

This note was uploaded on 01/15/2010 for the course COP 3530 taught by Professor Davis during the Fall '08 term at University of Florida.

Ask a homework question - tutors are online