Skip to content

Translation rank of a matrix in O(n^3) #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ especially popular in field of competitive programming.*
- [Gauss & System of Linear Equations](./linear_algebra/linear-system-gauss.html)
- [Gauss & Determinant](./linear_algebra/determinant-gauss.html)
- [Kraut & Determinant](./linear_algebra/determinant-kraut.html)
- [Rank of a matrix](./linear_algebra/rank-matrix.html)

### Combinatorics
- [The Inclusion-Exclusion Principle](./combinatorics/inclusion-exclusion.html)
Expand Down
55 changes: 55 additions & 0 deletions src/linear_algebra/rank-matrix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!--?title Rank of a matrix-->

#Finding the rank of a matrix

**The rank of a matrix** is the largest number of linearly independent rows/columns of the matrix. The rank is not only defined for square matrices.

The rank of a matrix can also be defined as the largest order of any non-zero minor in the matrix.

Let the matrix be rectangular and have size $N \times M$.
Note that if the matrix is square and its determinant is non-zero, then the rank is $N$ ($=M$); otherwise it will be less. Generally, the rank of a matrix does not exceed $\min (N, M)$.

##Algorithm

You can search for the rank using [Gaussian elimination](./linear_algebra/linear-system-gauss.html). We will perform the same operations as when solving the system or finding its determinant. But if at any step in the $i$-th column there are no rows with an non-empty entry among those that we didn't selected already, then we skip this step and decrease the rank by one (initially the rank is set equal to $\max (N, M)$).
Otherwise, if we have found a row with a non-zero element in the $i$-th column during the $i$-th step, then we mark this row as a selected one and perform the usual operations of taking this row away from the rest.

##Complexity

This algorithm runs in $\mathcal{O}(n^3)$.

##Implementation

```cpp
const double EPS = 1E-9;

int compute_rank(vector<vector<int>> A) {
int n = A.size();
int m = A[0].size();

int rank = max(n, m);
vector<bool> row_selected(n, false);
for (int i = 0; i < m; ++i) {
int j;
for (j = 0; j < n; ++j) {
if (!row_selected[j] && abs(A[j][i]) > EPS)
break;
}

if (j == n) {
--rank;
} else {
row_selected[j] = true;
for (int p = i + 1; p < m; ++p)
A[j][p] /= A[j][i];
for (int k = 0; k < n; ++k) {
if (k != j && abs(A[k][i]) > EPS) {
for (int p = i + 1; p < m; ++p)
A[k][p] -= A[j][p] * A[k][i];
}
}
}
}
return rank;
}
```
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy