矩阵线性相关则矩阵行列式_搜索线性时间中的排序矩阵
矩陣線性相關則矩陣行列式
聲明 (Statement)
We have to search for a value x in a sorted matrix M. If x exists, then return its coordinates (i, j), else return (-1, -1).
我們必須在排序的矩陣M中搜索值x 。 如果x存在,則返回其坐標(i,j) ,否則返回(-1,-1) 。
Let us consider the above matrix as an example. In this example, we are going to search for the value 12. Since 12 is present in the matrix, the algorithm should return its coordinates (2, 1)
讓我們以上述矩陣為例。 在此示例中,我們將搜索值12。由于矩陣中存在12,因此算法應返回其坐標(2,1)
簡單方法 (Simple Approach)
A simple approach is to traverse all the values in the matrix and check if it is equal to 12.
一種簡單的方法是遍歷矩陣中的所有值并檢查其是否等于12。
The worst case time complexity of the above algorithm will be O(n x m) = O(n2) when n = m
當n = m時,上述算法的最壞情況下時間復雜度將為O(nxm)= O(n2)
高效方法 (Efficient Approach)
The above algorithm behaves worse for large values of n and m. Let us look into the efficient algorithm now.
對于較大的n和m值,上述算法的性能較差。 現在讓我們研究高效算法。
算法 (Algorithm)
1. Start from Top Right position (0, m - 1) in the matrix M2. If the value is equal to x return (0, m - 1)3. Move one row down if the current value is less than x4. Move one column left if the current value is greater than xLet us apply this algorithm into our matrix M. We are going to search for the value 12 in M
讓我們將此算法應用到矩陣M中。我們將在M中搜索值12
Step 1 and 2步驟1和2Start from the Top Right value 5 at M[0][4]. 5 is less than 12, so 12 should be somewhere in the bottom of the matrix since all row and column values are sorted in ascending order. So we move one row down.
從M [0] [4]的右上角值5開始。 5小于12,因此12應該在矩陣底部的某個位置,因為所有行和列值都按升序排序。 因此,我們向下移動了一行。
The value 10 at M[1][4] is also less than 12, so we move one row down.
M [1] [4]上的值10也小于12,因此我們向下移動了一行。
3. The value 15 at M[2][4] is greater than 12, so 12 should be somewhere in the left of the matrix, so we move one column left.
3. M [2] [4]上的值15大于12,因此12應該在矩陣的左側某處,因此我們向左移動一列。
4. The value 14 at M[2][3] is also greater than 12, so we move one column left
4. M [2] [3]上的值14也大于12,因此我們向左移動一列
Step 5 and 6步驟5和65. The value 13 at M[2][2] is greater than 12, so we move one column left
5. M [2] [2]的值13大于12,所以我們向左移動一列
6. The value at M[2][1] is equal to 12, so we return its index (2, 1)
6. M [2] [1]處的值等于12,因此我們返回其索引(2,1)
The worst case time complexity of the above algorithm will be O(n + m) = O(2n) = O(n) when n = m, because we will be iterating all rows and all columns once if x is at the bottom left position (n - 1, 0)
當n = m時,上述算法的最壞情況下時間復雜度將為O(n + m)= O(2n)= O(n) , 因為如果 x 位于左下角 , 我們將對所有行和所有列進行一次迭代 位置 (n-1,0)
You can find the Java solution for this algorithm in the Github repo.
您可以在Github存儲庫中找到該算法的Java解決方案。
謝謝🙏 (Thank you 🙏 👋🤘)
Linkedin | Github
Linkedin | Github
翻譯自: https://medium.com/swlh/search-sorted-matrix-in-linear-time-c999234d39a
矩陣線性相關則矩陣行列式
總結
以上是生活随笔為你收集整理的矩阵线性相关则矩阵行列式_搜索线性时间中的排序矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到买鞋子试穿是什么意思
- 下一篇: bigquery数据类型_将BigQue