top school in delhi ncr

49
te Carlo method” rate M random permutations of {1,…,20} H be the number of those in which exactly one person gets his/her card ut H/M

Upload: edholecom

Post on 27-May-2015

27 views

Category:

Education


0 download

DESCRIPTION

On this website people get all information of schools

TRANSCRIPT

Page 1: Top school in delhi ncr

“Monte Carlo method”

generate M random permutations of {1,…,20}

let H be the number of those in which exactly one person gets his/her card back

output H/M

Page 2: Top school in delhi ncr

Lower boundsnumber from {1,2,3,…,9} 3 yes/no questions

Can you always figure out the number?

Page 3: Top school in delhi ncr

number from {1,2,3,…,8} 3 yes/no questions

Lower bounds

Can you always figure out the number?

Page 4: Top school in delhi ncr

number from {1,2,3,…,8} 3 yes/no questions

Lower bounds

in {1,2,3,4} ?

in {1,2} ? in {5,6} ?

=1 ? =3 ? =5 ? =7 ?

1 2 3 4 5 6 7 8

Y N

Y

Y Y Y Y

Y N

NN

N

NN

Page 5: Top school in delhi ncr

number from {1,2,3,…,n} k yes/no questions

Lower bounds

Page 6: Top school in delhi ncr

number from {1,2,3,…,n} k yes/no questions

Lower bounds

k = log2 n

Page 7: Top school in delhi ncr

number from {1,2,3,…,n} k yes/no questions

Lower bounds

k = log2 n

INFORMATION THEORETIC LOWER BOUND:

k log2 n

Page 8: Top school in delhi ncr

n animals = {dog,cat,fish,eagle,snake, …} yes/no questions

Lower bounds

INFORMATION THEORETIC LOWER BOUND:

k log2 n

Page 9: Top school in delhi ncr

sorting by comparisons yes/no questions: is A[i]>A[j] ?

Lower bounds for sorting

A[1..n]

data are not used to “control” computation in any other way

1 2 31 3 22 1 3• 3 13 1 23 2 1

Page 10: Top school in delhi ncr

sorting by comparisons yes/no questions: is A[i]>A[j] ?

Lower bounds for sorting

A[1..n]

k log2 n! log2 n + log2 (n-1) + … log2 1 (n/2) log2 (n/2) = (n log n)

log a*b = log a + log b

Page 11: Top school in delhi ncr

Theorem:Any comparison based sorting algorithmrequires (n ln n) comparisons in the worst-case.

Lower bounds for sorting

Page 12: Top school in delhi ncr

Lower bounds for search in sorted array

INPUT: array A[1..n], element xOUTPUT: a position of x in A if x is in A otherwise

Page 13: Top school in delhi ncr

Lower bounds for search in sorted array

INPUT: array A[1..n], element xOUTPUT: a position of x in A if x is in A otherwise

Theorem:Any comparison based algorithm for searching an element in a sorted arrayrequires (ln n) comparisons in the worst-case.

Page 14: Top school in delhi ncr

Lower bounds for minimumINPUT: array A[1..n] OUTPUT: the smallest element of A

Page 15: Top school in delhi ncr

Lower bounds for minimumINPUT: array A[1..n] OUTPUT: the smallest element of A

INFORMATION THEORETIC LOWER BOUND:

at least (ln n) comparisons

ADVERSARY LOWER BOUND:

at least (n) comparisons

Page 16: Top school in delhi ncr

Counting sort

array A[1..n] containing numbers from {1,…,k}

1st pass: count how many times i {1,…,k} occurs2nd pass: put the elements in B

Page 17: Top school in delhi ncr

Counting sort

array A[1..n] containing numbers from {1,…,k}

for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=0for I 1 to k+1 do D[i+1] D[i]+C[i]

for j 1 to n do D[A[j]]++ B[ D[A[j]] ] A[j]

Page 18: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

1 2 3 2 2 4 2 1 1 2 3 3 2 1 2 4

C 4 7 3 2D 1 5 12 15

Page 19: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

1 2 3 2 2 4 2 1 1 2 3 3 2 1 2 4

C 4 7 3 2D 2 5 12 15

1

Page 20: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

1 2 3 2 2 4 2 1 1 2 3 3 2 1 2 4

C 4 7 3 2D 2 6 12 15

1 2

Page 21: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

1 2 3 2 2 4 2 1 1 2 3 3 2 1 2 4

C 4 7 3 2D 2 6 13 15

1 2 3

Page 22: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

1 2 3 2 2 4 2 1 1 2 3 3 2 1 2 4

C 4 7 3 2D 2 7 13 15

1 2 32

Page 23: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

1 2 3 2 2 4 2 1 1 2 3 3 2 1 2 4

C 4 7 3 2D 5 12 15 17

1 2 321 1 1 2 2 2 2 2 3 3 4 4

Page 24: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

stable sort = after sorting the items with the same key don’t switch order

running time = O(n+k)

Page 25: Top school in delhi ncr

Counting sort for i 1 to k do C[i] 0for j 1 to n do C[A[j]]++D[1]=1for I 1 to k-1 do D[i+1] D[i]+C[i]

for j 1 to n do B[ D[A[j]] ] A[j] D[A[j]]++

stable sort = after sorting the items with the same key don’t switch order

running time = O(n+k)

What if e.g., k = n2 ?

Page 26: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,k2 - 1}

1) sort using counting sort with key = x mod k 2) sort using counting sort with key = x/k

Running time = ?

Page 27: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,k2 - 1}

1) sort using counting sort with key = x mod k 2) sort using counting sort with key = x/k

Running time = O(n + k)

Page 28: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,k2 - 1}

example k=10

28214243233270182920

70202142324323281829

Page 29: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,k2 - 1}

example k=10

28214243233270182920

70202142324323281829

Page 30: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,k2 - 1}

example k=10

28214243233270182920

70202142324323281829

18202123282932424370

Page 31: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,kd - 1}

1) sort using counting sort with key = x mod k 2) sort using counting sort with key = x/k mod k 3) sort using counting sort with key = x/k2 mod k …d) sort using counting sort with key = x/kd-1 mod k

Page 32: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,kd - 1}

Correctness: after s-th step the numbersare sorted according to x mod ks

Proof: By induction. Base case s=1 is trivial.

1) sort using counting sort with key = x mod k

Page 33: Top school in delhi ncr

Radix sort

array A[1..n] containing numbers from {0,…,kd - 1}

Correctness: after s-th step the numbersare sorted according to x mod ks

Proof: Now assume IH and execute s+1st step. Let x,ybe such that x mod ks+1 < y mod ks+1. Then either x/ks mod k < y/ks mod k or x/ks mod k = y/ks mod k and x mod ks < y mod ks

Page 34: Top school in delhi ncr

Bucket sort

linear time sorting algorithm on average

Assume some distribution on input.

INPUT: n independently random numbers from the uniform distribution on the interval [0,1].

Page 35: Top school in delhi ncr

Bucket sortINPUT: n independently random numbers from the uniform distribution on the interval [0,1].

for i 1 to n do insert A[i] into list B[ A[i]*n ]

for i 0 to n-1 do sort list B[i] output lists B[0],…,B[n-1]

Page 36: Top school in delhi ncr

Bucket sortINPUT: n independently random numbers from the uniform distribution on the interval [0,1].

for i 1 to n do insert A[i] into list B[ A[i]*n ]

for i 0 to n-1 do sort list B[i]

output lists B[0],…,B[n-1]

0.13, 0.23, 0.56, 0.74, 0.18, 0.34, 0.12, 0.82, 0.14, 0.19

Page 37: Top school in delhi ncr

Bucket sortINPUT: n independently random numbers from the uniform distribution on the interval [0,1].

for i 1 to n do insert A[i] into list B[ A[i]*n ]

for i 0 to n-1 do sort list B[i]

output lists B[0],…,B[n-1]

0.13, 0.23, 0.56, 0.74, 0.18, 0.34, 0.12, 0.52, 0.14, 0.19 1 2 5 7 1 3 1 5 1 1

B[1]: 0.13, 0.18, 0.12, 0.14, 0.19B[2]: 0.23B[3]: 0.34B[5]: 0.56, 0.52B[7]: 0.74

Page 38: Top school in delhi ncr

Bucket sortINPUT: n independently random numbers from the uniform distribution on the interval [0,1].

for i 1 to n do insert A[i] into list B[ A[i]*n ]

for i 0 to n-1 do sort list B[i]

output lists B[0],…,B[n-1]

0.13, 0.23, 0.56, 0.74, 0.18, 0.34, 0.12, 0.52, 0.14, 0.19 1 2 5 7 1 3 1 5 1 1

B[1]: 0.12, 0.13, 0.14, 0.18, 0.19B[2]: 0.23B[3]: 0.34B[5]: 0.52, 0.56B[7]: 0.74

Page 39: Top school in delhi ncr

Bucket sort for i 1 to n do insert A[i] into list B[ A[i]*n ]

for i 0 to n-1 do sort list B[i]

output lists B[0],…,B[n-1]

assume we use insert-sort

worst-case running time?

Page 40: Top school in delhi ncr

Bucket sort for i 1 to n do insert A[i] into list B[ A[i]*n ]

for i 0 to n-1 do sort list B[i]

output lists B[0],…,B[n-1]

assume we use insert-sort

average-case running time?

X0, X1, … , Xn-1 where Xi is the number of items that fall inside the i-th bucket

Page 41: Top school in delhi ncr

Bucket sortX0, X1, … , Xn-1 where Xi is the number of items that fall inside the i-th bucket

X02 + X1

2 + … + Xn-12

What is E[X02 + X1

2 + … + Xn-12] ?

E[X02 + … + Xn-1

2 ] = E[X0

2] + … + E[Xn-12 ] =

n E[X02]

linearity of expectation

symmetry of the problem

Page 42: Top school in delhi ncr

Bucket sort

E[X02] What is E[X0] ?

0 (1-p)n

1 n (1-p) n-1

k binomial(n,k) pk (1-p)n-k

n pn

value of X0

p=1/n

Page 43: Top school in delhi ncr

Bucket sort

E[X02] E[X0] = 1

0 (1-p)n

1 n (1-p) n-1

k binomial(n,k) pk (1-p)n-k

n pn

p=1/n

E[X0] = k * binomial(n,k) pk (1-p)n-k k=0

n

Page 44: Top school in delhi ncr

Bucket sort

E[X02] E[X0] = 1

p=1/n

E[X0] = k * binomial(n,k) pk (1-p)n-k k=1

n

binomial (n,k) = (n/k) * binomial (n-1,k-1)

binomial(n,k) pk (1-p)n-k = 1k=0

n

Page 45: Top school in delhi ncr

Bucket sort

E[X02] E[X0] = 1

p=1/n

E[X0] = n* binomial(n-1,k-1) pk (1-p)n-k k=1

n

binomial(n,k) pk (1-p)n-k = 1k=0

n

= n*p binomial(n-1,k-1) pk-1(1-p)n-k

= n*p

k=1

n

Page 46: Top school in delhi ncr

Bucket sort

E[X02]

0 (1-p)n

1 n (1-p) n-1

k binomial(n,k) pk (1-p)n-k

n pn

p=1/n

E[X02]= k2 * binomial(n,k) pk (1-p)n-k

k=0

n

Page 47: Top school in delhi ncr

Bucket sort

E[X02]

p=1/n

E[X02]= k2 * binomial(n,k) pk (1-p)n-k

k=0

n

= k * binomial(n,k) pk (1-p)n-k

k=0

n

+ k(k-1) * binomial(n,k) pk (1-p)n-k

k=0

n

Page 48: Top school in delhi ncr

Bucket sort

E[X02]

p=1/n

E[X02]= k2 * binomial(n,k) pk (1-p)n-k

k=0

n

= k * binomial(n,k) pk (1-p)n-k

k=0

n

+ k(k-1) * binomial(n,k) pk (1-p)n-k

k=0

n

n*p

Page 49: Top school in delhi ncr

Bucket sort

E[X02]

p=1/n

E[X02]= k(k-1) * binomial(n,k) pk (1-p)n-k

+n*p

k=2

n

binomial (n,k) = (n/k) * binomial (n-1,k-1) = (n/k) * ((n-1)/(k-1)) * binomial (n-2,k-2)

E[X02] = n* (n-1) * p2 + n*p