How to find a number in a matrix efficiently?

  • Thread starter Thread starter frankfjf
  • Start date Start date
  • Tags Tags
    Data Structures
AI Thread Summary
The discussion revolves around finding an efficient O(N) algorithm to determine if a number X exists in an N x N matrix where each row and column is sorted in increasing order. The initial approach considers diagonal traversal but struggles to formulate a clear solution. Participants suggest examining diagonal elements to leverage the matrix's properties, noting that the top left element is the smallest and the bottom right is the largest. They discuss the importance of comparing elements strategically, particularly focusing on the relationship between the target number and the matrix's bounds. The conversation also draws analogies to searching in a sorted array and guessing games to illustrate efficient search strategies, emphasizing the need for a method that narrows down possibilities quickly. The goal is to develop a pseudocode solution that adheres to the O(N) complexity requirement.
frankfjf
Messages
166
Reaction score
0
Hi guys, was wondering if someone could help me with a homework problem.

The problem: The input is an N x N matrix of numbers that is already in memory. Each individual row is increasing from left to right. Each individual column is increasing from top to bottom. Give an O(N) worst-case algorithm that decides if a number X is in the matrix.

Attempt at a solution: To be honest, I'm quite stumped. I know that the obvious solution is to check each element for the number, but that could be a O(N^2) complexity algorithm. I know that with a single loop one could traverse the matrix diagonally either starting from the top left and going toward the bottom right, or the other way around, but am not sure how to use this observation to come up with an O(N) complexity algorithm, assuming I'm off to a good start.

The professor has said that a pseudocode solution is fine.
 
Technology news on Phys.org
frankfjf said:
Hi guys, was wondering if someone could help me with a homework problem.

The problem: The input is an N x N matrix of numbers that is already in memory. Each individual row is increasing from left to right. Each individual column is increasing from top to bottom. Give an O(N) worst-case algorithm that decides if a number X is in the matrix.

Attempt at a solution: To be honest, I'm quite stumped. I know that the obvious solution is to check each element for the number, but that could be a O(N^2) complexity algorithm. I know that with a single loop one could traverse the matrix diagonally either starting from the top left and going toward the bottom right, or the other way around, but am not sure how to use this observation to come up with an O(N) complexity algorithm, assuming I'm off to a good start.

The professor has said that a pseudocode solution is fine.

Have you tried looking at the diagonal elements? What bounds can you expect from these elements given the restrictions that are placed on the matrix?
 
Only thing that comes to mind is that the top left element would have to be the smallest and the bottom right element would ahve to be the largest.
 
frankfjf said:
Only thing that comes to mind is that the top left element would have to be the smallest and the bottom right element would ahve to be the largest.
OK, assuming the number you're searching for is greather than the top left number and smaller than the bottom right number, what is the "ideal" element to compare next?
 
The one that's 1 down and 1 to the right of the current element, I'd imagine.
 
How about starting with a simpler case, if you had a known ascending array (a list, not a matrix) of numbers (each number in the list is greater than the preceding number) , what would be the quickest to see if a given number existed in the array of numbers?
 
Last edited:
Hm. I'd say seeing if it was between the smallest and largest numbers, assuming it wasn't one of those.
 
Another analogy then. A computer picks a random number between 1 and 1024, and you have to guess what the number is within so many tries. Each time you guess, the computer will tell you if the number is less than your guess, greater than your guess, or equal to your guess (you found the number). What strategy could you use to minimize the average number of guesses?
 
Back
Top