📖
JavaBlog
  • Interface Introduction
  • Array Introduction
  • Greedy Algorithm
  • Bisect
  • Dynamic Programming
  • Divide and Conquer
Powered by GitBook
On this page
  • Master Theorem
  • Binary Search
  • Bisect
  • Randomized Quicksort

Was this helpful?

Divide and Conquer

Master Theorem

Master theorem. Let a ≥ 1, b ≥ 2, and c ≥ 0 and suppose that T(n) is a function on the non-negative integers that satisfies the recurrence

T(n)=aT(nb)+Θ(nc)\displaystyle T(n) = aT(\frac{n}{b}) + \Theta(n^c)T(n)=aT(bn​)+Θ(nc)

with T(0) = 0 and T(1) = Θ(1), where n / b means either ⎣n / b⎦ or ⎡n / b⎤. Then,

  • If c>logbac > log_bac>logb​a, then T(n)=Θ(nc)T (n) = Θ(n^c )T(n)=Θ(nc). (Combining cost more than subproblem)

  • If c=logbac = log_bac=logb​a, then T(n)=Θ(nclogn)T(n) = Θ(n^c log n)T(n)=Θ(nclogn).

  • If c<logbac < log_bac<logb​a, then T(n)=Θ(nlogba)T(n) = Θ(n^{log_ba} )T(n)=Θ(nlogb​a). (Combining cost less than subproblem)

Binary Search

For a sorted array, find the given item

  1. find the mid item, compare it to the target

  2. less than goes to left subarray; hi=mid−1hi = mid - 1hi=mid−1

  3. bigger than goes to right subarray; lo=mid+1lo = mid + 1lo=mid+1

  4. continue util find it at mid or find none

  5. After loop, i>ji > ji>jand will return -1

  • Divide and conquer happens at the same time, there is no combining, so c=0c=0c=0

  • b=2b=2b=2 divide

  • a=1a = 1a=1becuase we only consider one subproblem

  • c==log21==0,T(n)=Θ(nclogn)=Θ(logn)c == log_21 == 0, T(n) = \Theta(n^clogn) = \Theta(logn)c==log2​1==0,T(n)=Θ(nclogn)=Θ(logn)

Bisect

Bisect_left

Given an array a sorted nums and an element target , find the index such that all the elements to the left are smaller than target , all the elements to the right and the current element are bigger than or equal to target.

  • The index returned can be 0 or len(nums)

input:   [ 1, 2, 3] , 0
output:  0

input:   [ 1, 2, 3], 4
output:  4
  1. Find the middle item, compare it to the target, with lo=0,hi=len(nums)lo = 0, hi = len(nums)lo=0,hi=len(nums)

  2. If less than, consider right subarray by lo=mid+1lo = mid + 1lo=mid+1

  3. If bigger than or equal to, consider the left subarray; however since this current element can be the index we are looking for, so we include the mid,hi=midmid, hi=mid mid,hi=mid

  4. After all scanning, return either lo or hi since lo==hilo == hilo==hi

// all i < lo, arr[i] < x
int bisect_left(int arr, int x){
    int lo = 0, hi = arr.length, mid;
    while (lo < hi):
        mid = (lo+hi) / 2;
        if (a[mid] < x) lo = mid+1;
        else hi = mid;
    return lo;

}      

Bisect_right

Given an array a sorted nums and an element target , find the index such that all the elements to the left are smaller than or equal to target , all the elements to the right and the current element are bigger than target.

  • The index returned can be 0 or len(nums)

input:   [ 1, 2, 3] , 0
output:  0

input:   [ 1, 2, 3], 4
output:  4
  1. Find the middle item, compare it to the target, with lo=0,hi=len(nums)lo = 0, hi = len(nums)lo=0,hi=len(nums)

  2. If less than or equal to, consider right subarray; however since this current element can be the index we are looking for, so we include the mid,hi=midmid, hi=mid mid,hi=mid

  3. If bigger than, consider the left subarray lo=mid+1lo = mid + 1lo=mid+1

  4. After all scanning, return either lo or hi since lo==hilo == hilo==hi

// all i >= lo, arr[i] > x
int bisect_left(int arr, int x){
    int lo = 0, hi = arr.length, mid;
    while (lo < hi):
        mid = (lo+hi) / 2;
          if (a[mid] <= x) lo = mid + 1;
          else hi = mid; 

//        if (a[mid] > x) hi = mid;
//        else lo = mid + 1;
    return lo;

}      

Randomized Quicksort

Given an array of n≥2n ≥ 2n≥2 distinct elements a1<a2<…<ana_1 < a_2 <\ldots< a_na1​<a2​<…<an​ the probability that aia_iai​ and aja_jaj​ are compared during randomized quicksort is 2j−i+1\displaystyle \frac{2}{j-i+1}j−i+12​

Hint: think about the subarray ai<a2<…<aja_i < a_2 <\ldots< a_jai​<a2​<…<aj​, choosing anything other than aia_iai​or aja_jaj​will never compare aia_iai​and aja_jaj​anymore, because they are split into different branches of the chosen pivot element. To chose either aia_iai​or aja_jaj​ the probabilty is 2j−i+1\displaystyle \frac{2}{j-i+1}j−i+12​.

Proof:

Step 1, n == 2: a1a_1a1​and a2a_2a2​will be definitely compared, and the probability is 1.

Step 2, n≥3n\geq 3n≥3:

  • To choose either aia_iai​or aja_jaj​is with prob of 2n\displaystyle \frac{2}{n}n2​

  • To choose one outside ai...aja_i ... a_jai​...aj​is with prob of n−(j−i+1)n\displaystyle \frac{n - (j-i+1)}{n} nn−(j−i+1)​, then aia_iai​and aja_jaj​will be partitioned into the same branch of the chosen pivot element. By induction, that probability of them get compared within that subarray is 2j−i+1\displaystyle \frac{2}{j-i + 1}j−i+12​

  • Add them together, proof is concluded.

2n+n−(j−i+1)n∗2j−i+1=2n+(1−(j−i+1)n)∗2j−i+1=2n+2j−i+1−2n=2j−i+1\displaystyle \frac{2}{n} + \frac{n - (j-i+1)}{n} * \displaystyle \frac{2}{j-i + 1} = \frac{2}{n} + (1 - \frac{ (j-i+1)}{n})*\frac{2}{j-i + 1} = \frac{2}{n} + \frac{2}{j-i + 1} - \frac{2}{n} = \frac{2}{j-i + 1}n2​+nn−(j−i+1)​∗j−i+12​=n2​+(1−n(j−i+1)​)∗j−i+12​=n2​+j−i+12​−n2​=j−i+12​

f(n)=log2n=ncf(n) = log_2n = n^c f(n)=log2​n=nc  c=lognlog2n=\space c = log_{n}^{log_2{n}} =  c=lognlog2​n​=

PreviousDynamic Programming

Last updated 3 years ago

Was this helpful?