Gaussian elimination element method

Source: Internet
Author: User
Tags gcd

Gaussian elimination method (Gauss elimination) Analysis & Puzzle & Template--czyuan Original

Gaussian elimination method is an algorithm in linear algebra, which can be used to solve the linear equations and to find the rank of the matrix and the inverse matrix of the reversible square.
The principle of the Gaussian elimination method is:
If the augmented matrix is transformed with an elementary row transformation, then Ax = b and cx = d are the same solution equations.
So we can transform the augmented matrix into a row step array with the elementary line transformation, and then return to find the solution of the equation.

The above is a review of the linear Algebra class, the following is the application of Gaussian elimination method in programming.

First, we first introduce the procedure of Gaussian elimination method:
(We set the number of equations in the equation is equ, the number of arguments is VAR, note: The general case is n equations, n arguments, but some problems deliberately let the number of equations and the number of arguments is different)

1. Convert the equations into an augmented matrix.

2. Transform the augmented matrix into a stepped array using the elementary line transformation.
The enumeration k is from 0 to equ–1, the currently processed column is col (initially 0), each time it is found below the K line (including the K line), the columns with the highest absolute value of the elements in the Col column are exchanged with the K line. If the elements in the Col column are all 0, then the COL + 1 column is processed, and K is unchanged.

3. Convert to a row step array, to determine the situation of the solution.

① No solution
When the equation appears in the form of (0, 0, ..., 0, a), and a! = 0 o'clock, the description is non-solvable.

② Unique Solution
The condition is k = equ, and the stepped array forms a strict upper triangular array. The solution set is solved by using the return generation.

③ Infinite Solution.
Conditions are K < equ, that is, can not form a strict upper triangle, the number of free arguments is equ–k, but some of the topics require to determine which arguments are not fixed.
Here is a separate description of this solution:
First, the free variable has var-k, that is, the indeterminate variable has at least var-k. We first consider all the elements to be indeterminate. In each equation, the number of indeterminate arguments is judged, and if it is greater than 1, the equation cannot be solved. If there are only 1 elements, then the variable can be calculated, that is, to determine the variable.

The above is to solve the problem of integer linear equations, the complexity of the method is O (N3). The method of solving the floating-point linear equations is similar, but when judging whether it is 0 o'clock, the EPS is added to eliminate the precision problem.


The following is a few OJ on the Gaussian elimination method to solve the problem of the linear equation Group:

POJ 1222 EXTENDED LIGHTS out
http://acm.pku.edu.cn/JudgeOnline/problem?id=1222
POJ 1681 Painter ' s problem
http://acm.pku.edu.cn/JudgeOnline/problem?id=1681
POJ 1753 Flip Game
http://acm.pku.edu.cn/JudgeOnline/problem?id=1753
POJ 1830 switch problem
http://acm.pku.edu.cn/JudgeOnline/problem?id=1830

POJ 3185 the water bowls

http://acm.pku.edu.cn/JudgeOnline/problem?id=3185
Switching windows, switching lights problem, is a typical solution to the problem of linear equations. The number of equations and the number of variables are the number of rows * columns, the direct set of template solution can be. However, when there is an infinite solution, it is necessary to enumerate the solution, because it is impossible to determine which solution is the optimal problem.

POJ 2947 Widget Factory
http://acm.pku.edu.cn/JudgeOnline/problem?id=2947
Solve the problem of congruence equation group. It is similar to the problem of solving linear equations in general, so long as the redundancy is added in the solution process.
Note: When the solution is unique to the equations, the solution is guaranteed to be between [3, 9].

POJ 1166 The Clocks
http://acm.pku.edu.cn/JudgeOnline/problem?id=1166
The classic BFS problem has various solutions, and the matrix can be multiplied by the inverse matrix.
But this problem is solved with Gaussian elimination method seems to have some problems (troubled me n days ...) Constant distress ...), because the Cycle 4 is not a prime, so in the process of the solution can not be taken (because the remainder may lead to the solution set to become larger), but the final solution set, or need to take the residual operation, then there is no guarantee that the final solution is correct ... In discuss the question for several days also no one answered ... I hope that the passing of the Daniel pointing down ~ ~

POJ 2065 SETI
http://acm.pku.edu.cn/JudgeOnline/problem?id=2065
The same is the problem of solving the congruence equations, because the p is a prime number in the problem, can be directly in the solution to take the remainder, the template solution can be. (although there is very little AC, it is still a problem of comparing water,)

POJ 1487 Single-player Games
http://acm.pku.edu.cn/JudgeOnline/problem?id=1487
A very troublesome topic ... The narrative in the topic seems to use the lexical definition of the compiler principle (the feeling that people don't want to do ...)
The idea of solving the equations is still very good-looking (provided that you read through the topic not less than 5 times ...), but if you convert the string expression of a tree into a equations is a difficult point, I use the stack + recursive approach decomposition. First, the idea of the stack to find out the number of children, and then recursively solve each child.
This equation group is also different ... The first is to solve the floating-point equations, we should pay attention to the accuracy problem, and then ask the uncertain variable, according to the method of the previous solution.
After a meal toss, this problem actually wrote 6000+b ... And the embarrassing thing is the giant C + + wa,g++ AC, perhaps the problem of accuracy ... Look at this topic, look at this code, there is no change of desire ...

HDU OJ 2449
http://acm.hdu.edu.cn/showproblem.php?pid=2449
Harbin live a pure Gauss problem, when the Crane Bull knocked for 1 hours ... The main thing is to write a score class, set a high-precision template (lazy point on Java ...) Get it done ~
Note the output at 0 and negative numbers.

FZE OJ 1704
http://acm.fzu.edu.cn/problem.php?pid=1704
Fu Otsuki a topic, or a classic switch problem, but the number of equations and the number of different variable (test template time to ~ ~), the final requirements of the order of augmented array, to use the High precision ~ ~

Sgu 275 to XOR or not to XOR
http://acm.sgu.ru/problem.php?contest=0&problem=275
Exercises
Http://hi.baidu.com/czyuan%5Facm/blog/item/be3403d32549633d970a16ee.html

Here you can write a satisfactory solution to the integer linear equation set template (floating point number is similar, do not provide) ~ ~

/*A set of equations for solving an integer solution.*/#include<iostream>#include<string>#include<cmath>using namespacestd;Const intMAXN = the;intEquvar;//there is a equ equation, var variable element. The number of augmented arrays is equ, 0 to Equ-1, and the number of columns is var + 1, 0 to Var, respectively.intA[MAXN][MAXN];intX[MAXN];//solution set.BOOLFREE_X[MAXN];//Determines whether the variable is indeterminate.intFree_num;voidDebug (void){    intI, J;  for(i =0; i < equ; i++)    {         for(j =0; J <var+1; J + +) {cout<< A[i][j] <<" "; } cout<<Endl; } cout<<Endl;} InlineintgcdintAintb) {    intT;  while(b! =0) {T=b; b= a%b; A=T; }    returnA;} InlineintLcmintAintBB) {    returnA * b/gcd (A, b);}//The Gaussian elimination method is the solution of the equation set (Gauss-jordan elimination). (-2 means a floating-point solution, but no integer solution, 1 means no solution, 0 represents a unique solution, greater than 0 represents an infinite solution, and returns the number of free arguments)intGauss (void){    intI, j, K; intMax_r;//The row with the highest absolute value for the current column.intCol//the currently processed column.    intTA, TB; intLCM; inttemp; intFree_x_num; intFree_index; //converted to a stepped array.Col =0;//the currently processed column.     for(k =0; K < equ && Col <var; k++, col++)    { //enumerates the currently processed rows. //The row that finds the largest absolute value of the col column element is exchanged with the K line. (To reduce the error when dividing)Max_r =K;  for(i = k +1; i < equ; i++)        {            if(ABS (A[i][col]) > abs (A[max_r][col]) Max_r =i; }        if(Max_r! =k) {//interchange with section K.             for(j = k; J <var+1; J + +) Swap (A[k][j], a[max_r][j]); }        if(A[k][col] = =0)        { //indicates that the COL column K line below is all 0, then the next column of the current row is processed.k--;Continue; }         for(i = k +1; i < equ; i++)        { //enumerates the rows to be deleted.            if(A[i][col]! =0) {LCM=LCM (ABS (A[i][col]), ABS (A[k][col)); Ta= Lcm/abs (A[i][col]), TB = LCM/ABS (A[k][col]); if(A[i][col] * A[k][col] <0) TB =-TB;//the case of a different number is two numbers added.                 for(j = col; J <var+1; J + +) {A[i][j]= a[i][j] * TA-A[K][J] *TB;    }}}} Debug (); //1. The absence of solution: there is an augmented array of degeneracy (0, 0, ..., a) such a line (a! = 0).     for(i = k; i < equ; i++)    { //for Infinite Solutions, if you want to determine which ones are free, then the exchange in the elementary row transformation will be affected, then the exchange should be recorded.        if(A[i][col]! =0)return-1; }    //2. The case of Infinity: in the augmented array of Var * (var + 1) (0, 0, ..., 0) Such a line, that is to say, does not form a strict upper triangular array. //and the number of rows that appear is the number of free arguments.    if(K <var)    {        //First, the free variable has var-k, that is, the indeterminate variable has at least var-k.         for(i = k-1; I >=0; i--)        {            //line I will certainly not be (0, 0, ..., 0) in the case, as such a line is in line K to equ. //Similarly, line I must not be (0, 0, ..., a), A! = 0 case, such a non-solvable.Free_x_num =0;//used to determine the number of indeterminate arguments in the row, if more than 1 are not solved, they are still indeterminate arguments.             for(j =0; J <var; J + +)            {                if(A[i][j]! =0&& Free_x[j]) free_x_num++, Free_index =J; }            if(Free_x_num >1)Continue;//The determined variable cannot be solved. //The description is only an indeterminate variable free_index, then the variable can be solved, and the variable is determined.temp = a[i][var];  for(j =0; J <var; J + +)            {                if(A[i][j]! =0&& J! = Free_index) Temp-= a[i][j] *X[j]; } X[free_index]= Temp/a[i][free_index];//To find out the variable element.Free_x[free_index] =0;//the variable is deterministic.        }        return varK//There are var-k of free-variable elements.    }    //3. The only solution: a strict upper triangular array is formed in the augmented array of Var * (var + 1). //calculate the Xn-1, Xn-2 ... X0.     for(i =var-1; I >=0; i--) {Temp= a[i][var];  for(j = i +1; J <var; J + +)        {            if(A[i][j]! =0) Temp-= a[i][j] *X[j]; }        if(temp% a[i][i]! =0)return-2;//indicates that there is a floating-point number solution, but no integer solution.X[i] = temp/A[i][i]; }return 0;}intMainvoid) {freopen ("Input.txt","R", stdin); intI, J;  while(SCANF ("%d%d", &equ, &var) !=EOF) {memset (A,0,sizeof(a)); memset (x,0,sizeof(x)); memset (free_x,1,sizeof(free_x));//at first, it was all uncertain of the variable element.         for(i =0; i < equ; i++)        {             for(j =0; J <var+1; J + +) {scanf ("%d", &A[i][j]); }        }//Debug ();Free_num =Gauss (); if(Free_num = =-1) printf ("No solution!\n"); Else if(Free_num = =-2) printf ("with floating-point solution, no integer solution!\n"); Else if(Free_num >0) {printf ("infinitely many solutions! The number of free arguments is%d\n", Free_num);  for(i =0; I <var; i++)            {                if(Free_x[i]) printf ("x%d is not sure, \ n", i +1); Elseprintf"x%d:%d\n", i +1, X[i]); }        }        Else        {             for(i =0; I <var; i++) {printf ("x%d:%d\n", i +1, X[i]); }} printf ("\ n"); }    return 0;} 

Gaussian elimination element method

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.