芯片测试|一道分治算法分析题
问题重述
Professor Diogenes has supposedly identical integrated-circuit chips that in principle are capable of testing each other. The professor’s test jig accomodates 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 professor cannot trust the answer of a bad chip. Thus, the four possible outcomes of a test are as follows:
a. Show that if more than 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.
b. Consider the problem of finding a single good chip from among chips, assuming that more than of the chips are good. Show that pairwise tests are sufficient to reduce the problem to one of nearly half the size.
c. Show that the good chips can be identified with pairwise tests, assuming that more than chips are good. Give and solve the recurrence that describes the number of tests.
理论分析
a. Lets say that there are good chips and bad chips.
From this assumption, we can always find a set of good chips and a set of bad chips of equal size since.
Now, assume that chips in always conspire to fool the professor in the following:
“for any test made by the professor, chips in declare chips in as ‘good’ and chips in as ‘bad’.”
Since the chips in always report correct answers thus there exists symmetric behaviors, it is not possible to distinguish bad chips from good ones.
b.
Generalize the original problem to: “Assume there are more good chips than bad chips.”
Algorithm:
- Pairwise test them, and leave the last one alone if the number of chips is odd.
- If the report says at least one of them is bad, throw both chips away;
- otherwise, throw one away from each pair.
- Recursively find one good chip among the remaining chips. The recursion ends when the number of remaining chips is or.
- If there is only chip left, then it is the good chip we desire.
- If there are chips left, we make a pairwise test between them. If the report says both are good, we can conclude that both are good chips. Otherwise, one is good and the other is bad and we throw both away. The chip we left alone at step is a good chip.
Explanation:
- If the number of chips is odd, from assumption we know the number of good chips must be greater than the number of bad chips. We randomly leave one chip alone from the chips, in which good chips are not less than bad chips.
- Chip pairs that do not say each other is good either have one bad chip or have two bad chips, throwing them away doesn’t change the fact that good chips are not less than bad chips.
- The remaining chip pairs are either both good chips or bad chips, after throwing one chip away in every those pairs, we have reduced the size of the problem to at most half of the original problem size.
- If the number of good chips is () more than that of bad chips, we just throw away the chip we left alone when the number of chips is odd. In this case, the number of good chips is at least one more than that of bad chips, and we can eventually find a good chip as our algorithm claims.
- If the number of good chips is exactly one more than that of bad chips, there are cases.
- We left alone the good chip, and remaining chips are one half good and one half bad. In this case, all the chips will be thrown away eventually. And the chip left alone is the one we desire.
- We left alone the bad chip, there are more good chips than bad chips in the remaining chips. In this case, we can recursively find a good chip in the remaining chips and the left bad chip will be thrown away at the end.
c. As the solution provided in (b), we can find one good chip in
By the master theorem, we have. After finding a good chip, we can identify all good chips with that good chip we just found in tests, so the total number of tests is
算法分析
剖析问题,给出芯片测试问题的一般结论:
份报告中, 至少一半报 “好”, 则 为好芯片; 超过一半报 “坏” , 则 为坏芯片.
显然,如果利用暴力算法进行遍历计数,效率较低,时间复杂度为。这里考虑利用分治策略:
假设 为偶数,将 片芯片两两一组做测试淘汰,剩下芯片构成子问题,进入下一轮分组淘汰.
- 淘汰规则:
- 测试结果为 “好, 好” → 任留1片,进入下轮
- 其他情况 → 全部抛弃
- 递归截止条件:
当 为奇数时,增加一轮对轮空芯片的单独测试即可。
考虑利用队列 表示需要测试的一组芯片的.
利用二维数组 表示通过 对芯片进行测试的结果.
其中, .
下面给出伪代码:
对于规模为 的芯片测试问题,每次检测可至少降低一半的规模。即划归为规模 的子问题。
于是有:
从而时间复杂度为,空间复杂度为.
编程实现
假设有 11 个芯片,其中 7 片是好芯片,其 从 1~7。
建立二维数组 用于存储检测数据:
1 | int test[11][11] = {{1,1,1,1,1,1,1,0,0,0,0}, |
设计算法:
1 | int chip_testing(vector<int> A){ |
下面给出手工处理的过程。
- 当前测试芯片序列为
- 两两一组进行测试, ,对于测试结果为 “好,好”的,保留后一个
- 得到保留后的序列
- 再两两测试,取后一个,最终得到一个好的芯片.
算法的计算结果如下:
1 | test:(1, 2) = 1, 1 |
结果正确!



