Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. Proof for the algorithm for m=n case. Let name middle element as "k"
  2. m1:=median (A, N) - O (1)
  3. m2:=median (B, N) - O (1)
  4. Proof:
  5. If m1 < m2, so m1 < k < m2 otherwise m2 < k < m1
  6. Without loss of generality –if m1 < k < m2, we know that the median of the two arrays must be
  7. Must be A[m1] < k <B [m2] so we can drop the irrelevant parts (A[0….m1] , B[m2……N]) by searching for new medians in each array.
  8.  
  9. Proof by contradiction:
  10. Again without loss of generality…
  11. We will assume that k is found within ((A[0….m1] U B[m2……N]))
  12. Array A contains (((N+1)/2) -1) elements smaller than m1
  13. Array B contains maximum (((N+1)/2) -1) elements smaller than m2
  14. Unified set is - ((A[0….m1] U B[m2……N])) :
  15. (((N+1)/2) -1) + (((N+1)/2) -1) = N-1  elements before m1
  16. Contradicting the definition of median which is N= [((2N+1) / 2)]
  17. We can come to a conclusion that k cannot be a part of ((A[0….m1] U B[m2……N]))
  18. Therefore it must be found somewhere ((A[m1….n] U B[0….m2]))
  19.  
  20. m1:=median (A+m1, N / 2) - O (1)
  21. m2:=median (B, N/ 2) - O (1)
  22. By reducing the size of the arrays by half each iteration we logarithmically decreasing the running time and thus improving efficiency – O (logN)
  23. And we repeat until size = 1 or 2
  24.  
  25.  
  26. Efficiency :
  27.  
  28. T(N) = T(N/2) + 1
  29. T(1) = 1
  30. T(N) = T(N/2) + 1 = T(N/4) + 1 + 1 = .......
  31. T(N/(2^k)+k)
  32. N/(2^k) = 1
  33. k = logN so - > T(N) = logN + 1
  34.  
  35. We can sum the efficiency of the algorithm to O (logN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement