clrs

Unbiased random number generator using a biased one

天大地大妈咪最大 提交于 2019-11-27 14:55:06
问题 You have a biased random number generator that produces a 1 with a probability p and 0 with a probability (1-p). You do not know the value of p. Using this make an unbiased random number generator which produces 1 with a probability 0.5 and 0 with a probability 0.5. Note : this problem is an exercise problem from Introduction to Algorithms by Cormen, Leiserson, Rivest, Stein.(clrs) 回答1: The events (p)(1-p) and (1-p)(p) are equiprobable. Taking them as 0 and 1 respectively and discarding the

What is a loop invariant?

隐身守侯 提交于 2019-11-27 02:28:47
I'm reading "Introduction to Algorithm" by CLRS. In chapter 2, the authors mention "loop invariants". What is a loop invariant? Tomas Petricek In simple words, a loop invariant is some predicate (condition) that holds for every iteration of the loop. For example, let's look at a simple for loop that looks like this: int j = 9; for(int i=0; i<10; i++) j--; In this example it is true (for every iteration) that i + j == 9 . A weaker invariant that is also true is that i >= 0 && i <= 10 . TNi I like this very simple definition: ( source ) A loop invariant is a condition [among program variables]

What is a loop invariant?

ⅰ亾dé卋堺 提交于 2019-11-26 12:33:50
问题 I\'m reading \"Introduction to Algorithm\" by CLRS. In chapter 2, the authors mention \"loop invariants\". What is a loop invariant? 回答1: In simple words, a loop invariant is some predicate (condition) that holds for every iteration of the loop. For example, let's look at a simple for loop that looks like this: int j = 9; for(int i=0; i<10; i++) j--; In this example it is true (for every iteration) that i + j == 9 . A weaker invariant that is also true is that i >= 0 && i <= 10 . 回答2: I like