美文网首页
2019-04-01 Basic Algorithms - Ra

2019-04-01 Basic Algorithms - Ra

作者: ANPULI | 来源:发表于2019-04-04 05:27 被阅读0次

Randomized Algorithms

basic prob

Problem 2

Input

Array A[1..2n] with n X's and n Y's

Output

index isuch that A[i] = X

Normal

loop the array
complexity O(n)

def find_X_slow(A):
    for i in range(len(A)):
        if A[i] == X:
            return i

Evil user inputs

[yyyy...xxx]

def find_X(A):
    for i in range(len(A)//2):
        j = random.randint(0,len(A)-1):
        if A[j] == x:
            return j
    find_X_slow

Claim

The average runtime for a fixed A is O(1)

Y_i, the runtime of the i-th iteration

E(Y_i) = \frac{C_1}{2^{i-1}}
E(Z) = \frac{C_2n}{2^n}

相关文章

网友评论

      本文标题:2019-04-01 Basic Algorithms - Ra

      本文链接:https://www.haomeiwen.com/subject/yedibqtx.html