Precise coverage of learning notes (4)-Algorithm Optimization

Source: Internet
Author: User

I. Optimization

The Code implemented in the previous section has two major defects in terms of running efficiency:

1. Before each recursive call, copy the current State Matrix and delete all the rows and columns that intersect the current row to obtain a new matrix. When the matrix is very large, the copy operation requires a large amount of time and space.

2. In actual situations, the matrix m is generally a sparse matrix. The number of 0 is much more than the number of 1. If we can only process cells containing 1, it will greatly improve the time-space efficiency of operation.

Ii. Optimize the data structure used

The following optimization algorithms are proposed by knuth. The main idea is to use the cross linked list to store all the values in the Matrix. 1. The specific descriptions are as follows:

1. First define a common cross linked list node class

Struct cNode {cNode * left; // left node pointer cNode * right; // right node pointer cNode * up; // top node pointer, to column node, the pointer cNode * down for the last element of this column; // The next node pointer. For column nodes, the pointer int name for the first element of this column; // for common nodes, is the number of the subset, that is, the row number; // The column node, that is, the column number };

2. A Column Title class is derived from the above node class. This column title has a size data field, which is used to store the number of elements in this column, that is

Struct ccolumnhead: Public cNode
{
Int size; // number of elements in this column
};

3. Data Node class, storing each 1 in the original matrix,

Struct cgrid: Public cNode {ccolumnhead * columnhead; // pointer of the column header node };

4. Create a header node pointing to the column title two-way linked list and storing the feasible solution variables.

Set <int> solution; // stores the feasible solution ccolumnhead * master; // head node of the column head linked list

 

Iii. Core functions

The core of an algorithm is two functions.

1. Cover Function

Cover function. The input is a column number C, and the pseudo code of the function flow is

(1) Remove the entire column C from the column chain table

(2) traverse all data node R in Column C from the first data node of column C, and each R will perform the following operations:

A. Traverse other node cells in the row of R from the right to the right, and perform the following operations on each cell:

B. Set the column number of the cell to N, and remove the cell from column N. The value of the size field of Column Title N is reduced by 1.

The purpose of the cover function is to delete all nodes related to this column from the matrix.

2. Cover execution instance

Let's look at an instance. Suppose

The current status of the matrix is. The number in the brackets indicates the number of columns containing 1. h indicates the head node of the column head linked list and starts to point to the first column.

h->  1(2) 2(2) 3(2) 4(3) 5(2) 6(2) 7(4) A    1    0    0    1    0    0    1    B    1    0    0    1    0    0    0    C    0    0    0    1    1    0    1    D    0    0    1    0    1    1    0    E    0    1    1    0    0    1    1    F    0    1    0    0    0    0    1 

Matrix 1 -- original matrix

To remove column 1 and its related nodes from the matrix, the specific process is

(1) Remove column 1 from the matrix. H points to column 2, and the matrix status is

h->  2(2) 3(2) 4(3) 5(2) 6(2) 7(4) A    0    0    1    0    0    1    B    0    0    1    0    0    0    C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1 

Matrix 2 -- remove the matrix after column 1
Note: Although column 1 is removed from the matrix, its own information does not change, and the original information of Column 1 is

     1(2)A    1   B    1   C    0   D    0   E    0   F    0   

It can be seen that the rows related to column 1 are a and B.

The next task is to delete other nodes of Rows A and B from Matrix 2,

(2) First, traverse all data nodes of Row A in Matrix 2 (from the right of Column 1 to the right). Obviously, column A and column 4 and Column 7 have intersections, delete these two nodes. The counter values in the 4th and 7th columns are reduced by 1,

In this way, Row A is removed from Matrix 2, and the resulting matrix is

h->  2(2) 3(2) 4(2) 5(2) 6(2) 7(3) B    0    0    1    0    0    0    C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1  

Similarly, all data nodes (with only 4th columns) of Row B are removed from the matrix, and the counter value of the column where each data node is located is reduced by 1. The new status of matrix m is

h->2(2) 3(2) 4(1) 5(2) 6(2) 7(3) C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1   

Matrix 4-status after Row B is removed

From the above process, we can see that cover does not cut off the horizontal and vertical relationships between the deleted elements and other elements, thus laying the foundation for restoring the matrix state.

3. uncpver Function

The input of the uncover function is also a column number C, which is opposite to the function of cover. It inserts column C into the matrix, that is, restoring the Matrix to execution.

The status before cover (c.

The process of uncover is the opposite to that of cover.

The pseudo code of the function flow is

(1) from the last data node of column C, traverse up all data node R of column C, and each R will perform the following operations:

A. Traverse other node cells in the row from the left of R to the left, and perform the following operations on each cell:

B. Set the column number of the cell to N, add the cell to column N, and add the value of the size field of Column Title n to 1.

(2) Add the entire column C to the slave column chain table

4. Uncover execution instance

Set the current matrix status

     2(2) 3(2) 4(1) 5(2) 6(2) 7(3) C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1

The column to be inserted is 1 and the information of Column 1 is

     1(2)A    1   B    1   C    0   D    0   E    0   F    0   

The information of Rows A and B is

2 3 4 5 6 7
A 0 0 1 0 0 1
B 0 0 1 0 0 0

The specific execution process of uncover is

(1) first, restore B from the last row of column 4 to the matrix, that is, restore node 4 of Column B to the matrix, and Add 1 to the counter of column 4. The matrix status is

h->  2(2) 3(2) 4(2) 5(2) 6(2) 7(3) B    0    0    1    0    0    0    C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1  

(2) Restore Row A to the matrix. The last node of Row A is 7. Therefore, restore Column 7 first. The matrix is

h->  2(2) 3(2) 4(2) 5(2) 6(2) 7(4) A    0    0    0    0    0    1    B    0    0    1    0    0    0    C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1 

Then restore node 4 to the matrix. The matrix state is

h->  2(2) 3(2) 4(3) 5(2) 6(2) 7(4) A    0    0    1    0    0    1    B    0    0    1    0    0    0    C    0    0    1    1    0    1    D    0    1    0    1    1    0    E    1    1    0    0    1    1    F    1    0    0    0    0    1 

(3) Insert column 1 to the matrix. The matrix is

     1(2) 2(2) 3(2) 4(3) 5(2) 6(2) 7(4) A    1    0    0    1    0    0    1    B    1    0    0    1    0    0    0    C    0    0    0    1    1    0    1    D    0    0    1    0    1    1    0    E    0    1    1    0    0    1    1    F    0    1    0    0    0    0    1   

So far, the matrix has completely recovered to the state before cover (1.
 

Iv. Basic Steps for optimizing Algorithms

The algorithm function is search.

1. If the matrix is null, that is, master-> right = Master, the function returns true.

2. Select a column with a minimum of 1 and set its column title to C.

3. Execute cover (c)

4. traverse the r of each data node in Column C and perform the operations from (a) to (d) on each R node.

(A) traverse all other cell nodes in the row of R from the right side of R, set the column header node in the cell to N, and perform the cover (n) operation)

(B) recursively call the search function and save the result to the variable flag.

(C) If the result in the previous step is true, add row R to the solution set and return true.

(D) if the result of step (B) is false, traverse all other nodes in the row of R from the left of R to the left, set the column header node in the cell to n.

, Perform the uncover (n) Operation)

5. Execute uncover (c)

6. The function returns false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.