算法导论 4-6 VLSI芯片测试 - Zerohuan的专栏 - 博客频道 - CSDN.NET


http://blog.csdn.net/shoulinjun/article/details/14128635
对于分治(Divide and Conquer)的题目,最重要是
1.如何将原问题分解为若干个子问题,
2.子问题中是所有的都需要求解,还是选择一部分子问题即可。

还有一点其实非常关键,但是往往会被忽视:分解后的子问题除了规模较原问题小之外,必须和原问题具有相同的性质。
在子问题的划分时,只有这一点保证,才能递归求解子问题,从而得到solution。
算法导论 4-6 VLSI芯片测试 - Zerohuan的专栏 - 博客频道 - CSDN.NET
4-6  VLSI芯片测试
    Diogenes教授有n个被认为是完全相同的VLSI芯片,原则上它们是可以互相测试的。教授的测试装置一次可测二片,当该装置中放有两片芯片时,每一片就对另一片作测试并报告其好坏。一个好的芯片总是能够报告另一片的好坏,但一个坏的芯片的结果是不可靠的。这样,每次测试的四种可能结果如下:

    A芯片报告         B芯片报告     结论

    B是好的          A是好的      都是好的,或都是坏的
    B是好的          A是坏的      至少一片是坏的
    B是坏的          A是好的      至少一片是坏的
    B是坏的          A是坏的      至少一片是坏的

a)证明若多于n/2的芯片是坏的,在这种成对测试方法下,使用任何策略都不能确定哪个芯片是好的。假设坏的芯片可以联合起来欺骗教授。
b)假设有多于n/2的芯片是好的,考虑从n片中找出一片好芯片的问题。证明[n/2]对(下界)测试就足以使问题的规模降至近原来的一半。
c)假设多于n/2片芯片是好的,证明好的芯片可用Θ(n)对测试找出。给出并解答表达式测试次数的递归式。
http://blog.csdn.net/shoulinjun/article/details/14128635
You have n mobile phones and a contraption that holds two phones at the
same time and allows them to test each other. Once you load the phones into
the contraption, they test each other and each outputs whether the other
one is good or bad. If a phone is actually good than it will always output a
correct result. Unfortunately, if it is bad, its reply is unrelated to the real
state of the other phone and hence cannot be trusted. In other words, the
badphone can each time output whatever it wants. [For all practical pur-
poses,you can assume that the bad phone always outputs something that
willbe as unhelpful to you as possible.] Loading two phones and letting
them test each other will be called a test. We have 4 cases that might result
from a single test:

Chip A says
Chip B says
Conclusion
B is good
A is good
Either good or 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

We will assume that there are more than n /2 good phones. Final goal is
to identify all the good and all the bad phones. In the question we are
after a deterministic algorithm, i.e. you cannot make use of any type of
randomization.
1.Explain how to find a single good phone. [Hint: Start by explaining
how to use O(n) tests to reduce the problem size by a constant factor.]
2.Use the previous part to show how to identify all of the good phones
by performing Θ(n) tests.
这个题目其实就是算法导论的一题课后题: VLSI芯片测试
思路: 目标是从n个手机中找到一个good phone.
注意题目给出了一个条件,原问题中超过一半的手机是好的。如果用分治,子问题必须同样满足good phones超过半数这一个性质。

先利用超过半数手机是好的这一性质,构造一个On^2)的算法。
Consider a phone x, 拿它与所有的phonestests
if超过半数的手机说它是good, then it's good.
Else it's bad.
打个比喻,法院审判一个犯人,陪审团中超过半数的人是公正的(如果 x is good, 他们一定投反对票),其余的人随便瞎给判决。无论其余的人怎么投票,根据少数服从多数的原则,最终犯人是否无罪释放取决于那超半数的公正的陪审团成员的判决。
注意: 一个重要的细节问题
总共n部手机,拿出一个手机后,剩余n-1部手机, 这n-1个手机中还是超半数是好手机吗??(如果不是,算法1不再work)
答案是肯定的!!! 题目要求n是偶数n中good phones > bad phones,
                                    因此, n个手机中 good phones 至少比 bad phones多 2个 (反证法,若只好手机比坏手机多1个,则n是奇数,矛盾)
所以n个手机任意去掉一个手机后,仍然满足超半数是好手机的性质。
首先,直观想法:对半开,假设n是偶数,两两结对,做test
测试结果有4个 cases: (-)(-)(-)(-)
注意到后三种情况,至少有一个是坏的phone(用反证法非常容易证明)
但是同时还要满足 超半数是good
选择留下少于一半的元素(超半数是good)的,
或者说,选择丢掉超过半数的元素(超半数是bad)的。

Bingo:丢掉测试结果是(好-坏,坏-好,坏-坏)(超半数是bad的)的phones,剩下的元素就是超半数是好的。
但是不要高兴得太早,子问题还有一个性质:《规模》
(好-好)的规模还一定是少于半数的,还得从其中丢掉一部分元素!!!
但是(好-好)只能保证 both good or both bad.
遇到了个小问题,不用怕,要相信自己的思路是对的。
(好-好)的所有元素中去掉一半元素,规模不就满足<n/2
但是如何剔除呢??
第一种思路,选择一半的(好-好)对去掉,不对。不能保证超半数是good的性质了;
第二种思路, 每一组(好-好)对中选择一个元素。Bingo!由于要么bothgood, or both bad, 这样做一定能保证,剩余的元素中good> bad;
(还有一点需要证明,(-)对一定会出现吗?反证法:若不存在,其余三种情况都是至少有一个是坏的,因此超半数是bad,矛盾)
Complexity:每次两两结对做tests,耗时On
故 Tn)<= T(n/2) +O(n)所以复杂度是线性。

总结:
分治的子问题必须和原问题具有相同的性质。

算法分析:
(a) 可以采用时间复杂性最高但最准确的方法进行穷举:对每一个芯片,都让其他所有芯片和其配对检测,由于坏芯片的数量大于n/2,所以没有办法从检测结果判断芯片的好坏,所以~。

(b) 根据问题叙述的四种情况,可以想到用分治法来二分的解决问题,首先说下思路:
总:通过每次的划分子问题,并且保持满足好芯片多于坏芯片这一条件的循环不变式,我们就可以通过不断减小问题规模最终得到好芯片。
  (1) 首先将原来的n个芯片随机的两两配对,这样就得到了[n/2](下界)个检测对,可以假设,在n中有x个好芯片,y个坏芯片,检测对中有g对检测结果两个都为好的检测对,其中有a对为两个都为好芯片,b对两个都为坏芯片:
  (2) 根据n的奇偶性不同分别进行分析:
    1. 如果n为偶数,在剩下的([n/2] - g)对中坏芯片的数量大于等于好芯片,又x > y(这说明x - y >= 2),则在g对中a > b;
    2. 如果n为奇数,就有一个芯片剩下没有配对,那么在x - y = 1的情况下,根据剩下那个芯片的好坏不同就有可能出现不同的情况:
      如果g为奇数,即使g对以外的检测中坏芯片好芯片各占一半的,也能保证在g对中a - b >= 2,这样舍弃剩下未检测的芯片也能使新取出来的检       测序列中好芯片多于坏芯片;
      如果g为偶数:
         如果这个剩下的芯片是好的话,就有a >= b,留下这个芯片没有问题;
         如果这个芯片是坏的话,就有a - b >= 2(因为g为偶数,要大只能大2个),留下这个坏芯片也没有问题。
  (3) 这样在就得到一个规模为[n/2]的新检测序列,并且这个新序列也满足好芯片多于怀芯片的条件,这时就可以重复1, 2步进行递归或迭代,直到规模为1是,一定可以得到好芯片。

(c) 根据(b)的叙述,显然可以得到:T(n) = T(n/2) + n/2。
http://www.cppblog.com/jinq0123/archive/2007/09/24/goodbadchiptest.html
任意拿两片芯片互相测试,则有
1)结果都为真,则说明两片都为真,或者都为假。
2)其他结果,则最少有一为假。

在任意偶数多的芯片里,如果好芯片多于坏芯片,将所有芯片两两分组,根据抽屉原理,则有
1)必有两个好芯片分在一组。
2)同为好芯片的组数一定多于同为坏芯片的组数。

测试流程
1)将芯片两两分组,比如1和2,3和4。。。。2k-1和2k。互相测试,则必有结果同为真的组。
2)保留结果同为真的组,丢弃其他组。必有好芯片组多于坏芯片组。(所以当只有两组或者一组同为真时,则必为真,测试结束)
3)结果同为真的组芯片必定同好或者同坏,所以可以丢弃一半。从所有同真组中任意取出一个丢弃另一个,组成新的测试组,继续两两分组,直到同真组只有2个或者1个测试结束,坚持到最后的就是好芯片。

说 明:同真组可能会变成奇数个,当为奇数组时,任意选一组取其中一个(假设为A),在剩余组中各取一个来测试A,如果测试结果A为好芯片过半或者等于一半, 则A为好芯片,测试结束。否则A为坏芯片,判定A为好芯片的必为坏芯片,剔除后剩余部分形成新的测试组,继续两两分组。。。

总的原理和淘 金差不多,刚开始好的芯片多,在每次剔除芯片时一定要保证剔除的坏芯片数量一定要多于或者等于好芯片的数量,这样就能保证在剩余的芯片中好的一定多于坏 的。当组数为奇数时采用投票制,多于半数的投票有效(等于也有效,因为好的多于坏的,相等则被测试的一定为好的)。

因为每次最少剔除一半的芯片,所以最坏情况出现在每次只能剔除一半芯片的时候,按等比数列递减。当有N个芯片时,测试次数为n+(n/2)+(n/4)...=2n(实际上当为奇数组时,次数会更多,不过算不过来了,省略^_^ )

http://vlsicad.ucsd.edu/courses/cse101/hw_solutions/hw1_5.pdf
Question 5. CLRS 4-6.
a. If more than n/2 chips are bad, then a subset of the bad chips can act like good chips.
The number of black chips that do this can be the same as the number of good chips
(which is less than n/2), making it impossible to discern between these black chips
and the good chips (both sets of the same size). This means that it is possible for a
subset of the bad chips (the number of black chips in this subset is equal to the
number of good chips) to report accurately whether the other chip is good or bad.
Assuming that these bad chips always do this, it would be impossible to tell the
difference between the good chips and these bad chips. That is, there does not exist a
method by which to determine which are the good chips. Here is an illustration
below that illustrates this point:

b. The answer to this question lies in the two questions that are asked by Professor
Kahng on the discussion board. The first question is 1) if you put two chips on the
tester, and the result says that one of them is bad, what happens if you “throw both of
the chips away”? In this case, if you throw both of the chips away, then since you
know that at least one of the chips is bad, then you are still left with a set of chips that
have a majority of good chips. The second question is 2) if you have K pairs of chips,
each of which is an “identical pair” (i.e., X-X, where X can be either Good or Bad –
you can’t tell) – and, the 2K chips together have a majority of Good chips – is there
any way that you can come up with a smaller set of chips which still have a majority
of Good chips? The answer to this question is yes. You can easily do this by
throwing away one of the chips from each pair. Then, you still have a majority of
good chips. Therefore, combine the two questions, so to speak. If you are given n
chips, begin a pairwise comparison. If one or more is labeled as bad, throw them
both out. If they are both labeled as good, store them in a pile, but at the end of the
pairwise comparisons of all the chips, throw out one chip from each pair which said
that the chips were both good. If you do this, then you are guaranteed to have a set of
chips with a majority of good chips, where the size of the set is half the size of the
original set. After finishing this algorithm you will be left with one chip, which will
be good.
c. The recurrence relation corresponding to this solution is T(n) = 2T(n/2) + n/2, which
according to the master method is equal to (n).
http://staff.ustc.edu.cn/~csli/graduate/algorithms/book6/chap04.htm

Video: https://class.coursera.org/algorithms-001/lecture/51
Read full article from 算法导论 4-6 VLSI芯片测试 - Zerohuan的专栏 - 博客频道 - CSDN.NET

Labels

LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

Popular Posts