Link: http://blog.csdn.net/jj12345jj198999/article/details/6604842
However, if 1 is excluded, 2 will be deleted for the same reason. However, the result set obtained in this way is not the largest, because we can easily see that only 2 can be deleted. This problem is actually to find a general method to determine which elements are contained in the set.
The idea of solving problems by induction is focused on reducing the scale of the problem. This method is also flexible to use. We can find an element in S or not in S to reduce the problem scale. We will perform this operation later. We will first use concise and clear induction assumptions:
Inductive hypothesis: we already know how to solve the problem of a set of N-1 elements.
The basic situation is simple: if the set contains only one element, the set must be mapped to itself, which is a one-to-one ing. Suppose we already have a set of n elements, and now we are looking for a subset S that meets the problem conditions. Obviously, finding an element that does not belong to the s set is easier than finding an element that is contained in the s set. We can think that if I is not mapped by other elements, I cannot be included in S. (In other words, if no edge is connected to the element I on the right of the graph, then I cannot be in S.) Otherwise, if I is in S, assume s has k elements, then the k elements are mapped to a maximum of K-1 elements, so this ing cannot be one-to-one ing. If such an I exists, we only need to remove it from the set. We now have a set of N-1 elements that map function f to itself. A' = A-{I }. Through induction, we know how to solve '. If such I does not exist, the ing is a one-to-one ing, which solves the problem.
The key to this solution is that we must remove I. We have proved above that I cannot belong to the s set. This is the advantage of induction. Once we have removed an element and reduced the scale of the problem, we have done it. However, we must be extremely careful. The problems after reduction are almost the same as those of the original. (Except scale) the only condition for set a and function f is that f maps a to itself. Since no elements are mapped to I, this condition still applies to the set after a-{I. When no element can be removed, this algorithm is executed.
Implementation: The above algorithm is described by using recursive procedures. In each step, we find an element that has no other elements mapped to it, remove it, and continue recursion. However, recursion is not required for implementation. We can use a counter C [I] for each element I. Initially, C [I] needs to be equal to the number of elements mapped to I. This can be calculated by scanning the Array (in n steps) and adding the corresponding counter value. Then, we put all the elements whose Count value is 0 in a queue. In each step, we remove element J from the queue (and also remove the corresponding value from the set ). Reduce the value of C [F (j)]. If C [F (j)] is 0, we put f (j) in the queue. When the queue is empty, the algorithm execution ends. The algorithm is described as follows:
Ing algorithm (F: integer array [1. N])
Begin
S: = A {A is an array composed of numbers from 1 to n}
For J: = 1 to n do C [J]: = 0;
For J: = 1 to n do increament (C [F (j)]);
For J: = 1 to n do
If C [J] = 0 then put J in queue;
While queue is not empty do
Remove I from the top of the queue;
S: = s-{I };
Decrement C [f (I)];
If C [f (I)] = 0 then put f (I) in queue
End;
Complexity: initialization requires O (n) operation time. Each element can be placed in a queue at most once. It only takes constant time to remove the element from the queue. The total number of steps is O (n ).
Conclusion: In this example, the problem is reduced mainly by removing elements from the set. Therefore, we try to find the simplest way to remove elements without changing the problem conditions. Because the only requirement for a function is that it must map a to itself, we will naturally select an element that has no other elements mapped.
Check the line segment Inclusion [Q3]
Problem: the input is an I1, i2. .. In set of a series of online interrupted line segments. For each line segment, IJ provides its two endpoints LJ (left endpoint) and RJ (right endpoint ). We want to mark all the line segments included in other line segments. In other words, a line segment IJ must be marked. If another line segment IK (k = J) exists and it must satisfy LK ≤ LJ and rk ≥ RJ. To simplify the process, we assume that all line segments are different (for example, any two line segments cannot have the same left and right endpoints at the same time, but they may have one of the two endpoints ). Figure 2 shows a collection of such line segments. (For better display, each line segment is placed on another line segment rather than on one line)
Figure 2: Line Segment Set
The use of induction in an intuitive way is mainly to remove a line segment I. The recursive method is used to solve the remaining line segments, and then the effect after I is checked. The problem is that you need to check all other line segments to determine whether they contain I or are included by I. This requires checking I and the remaining n-1 line segments, the comparison used in the algorithm will reach n-1 + N-2 +... + 1 = N (n-1)/2 times. To get a better algorithm, we need to do two things: first, we should select a special line segment to remove it, second, we use as much information as possible from solutions to smaller-scale problems.