算法导论 chapter4

不想你离开。 提交于 2020-01-10 03:58:53

exercises:

1.Show that the solution of T (n) = T (⌈n/2⌉) + 1 is O(lg n).

let T(n/2) <= c*lg(n/2),

then

T(n) = T(n/2) + 1 <= c*lg(n/2) + 1 = c*lgn  -c + 1 <= c*lgn,

 as long as c >=1

 

2.We saw that the solution of T (n) = 2T (⌊n/2⌋) + n is O(n lg n). Show that the solution of this recurrence is also Ω(n lg n). Conclude that the solution is Θ(n lg n).

 let T(n/2) >= c*lg(n/2)

then

T(n) =  T(n/2) + 1 >= c*lg(n/2) + 1 = c*lgn  -c + 1 >= c*lgn,

as long as 0<c<1

 

 

5.Show that the solution to T (n) = 2T (⌊n/2⌋ + 17) + n is O(n lg n).

let m = n/2 + 17 and  T(m) <= cmlg(m)

 

then

 T (n) = 2T (m) + n <= 2cm*lg(m) + n = 2c(n/2 + 17)*lg(n/2 + 17) + n

 

 problem:

Problems 4-6: VLSI chip testing

Start example

Professor Diogenes has n supposedly identical VLSI[1] chips that in principle are capable of testing each other. The professor's test jig accommodates two chips at a time. When the jig is loaded, each chip tests the other and reports whether it is good or bad. A good chip always reports accurately whether the other chip is good or bad, but the answer of a bad chip cannot be trusted. Thus, the four possible outcomes of a test are as follows:

Chip A says

Chip B says

Conclusion


B is good

A is good

both are good, or both are bad

B is good

A is bad

at least one is bad

B is bad

A is good

at least one is bad

B is bad

A is bad

at least one is bad

  1. Show that if more than n/2 chips are bad, the professor cannot necessarily determine which chips are good using any strategy based on this kind of pairwise test. Assume that the bad chips can conspire to fool the professor.

  2. Consider the problem of finding a single good chip from among n chips, assuming that more than n/2 of the chips are good. Show that ⌊n/2⌋ pairwise tests are sufficient to reduce the problem to one of nearly half the size.

  3. Show that the good chips can be identified with Θ(n) pairwise tests, assuming that more than n/2 of the chips are good. Give and solve the recurrence that describes the number of tests.

 answer:

 

    a. if over n/2 chips are good, it's easy to identify whether a chip is good or bad. we just need to test it with all the other chips and see what the other chips say about this chip. The majority wins.

       however if more than n/2 are bad, no matter what the result is, we can't believe it.

    b. we will do an operation to reduce the problem. we call it T(n), n is the number of chips

         if n is even,

            as there are more good one, the gap is at least 2

            split chips into two array: A, B

            for i in n/2

                test A[i] with B[i] , if failure is reported, remove them from A, B seperatly (so those are not removed are good,good or bad,bad)

            then we get two new arrays A'(A' <= A) and B'(B' <= B) , and they both have more good chips than bad ones(the gap is at least 1 as we divide by 2)

            select A'(or B'), do T(A') recursively

         if n is odd,

             the gap is at least 1,

             we take one chip C out and still split the rest into A, B and generate A', B'

              if at least half of A' say C is good, then we get the answer.

              else,  we T(A')

        

    c. find a good one and then the left work takes Θ(n) . according to b and the master theorem, then the whole thing takes Θ(n)

 

code:

c++ /Files/nocooldown/4.zip

java /Files/nocooldown/chapter4.zip

python /Files/nocooldown/4_python.zip

 

 

 

 

 

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!