How to find a number in a matrix efficiently?

In summary: I don't know, that's something that you would have to try and optimize.In summary, the professor has said that a pseudocode solution is fine.
  • #1
frankfjf
168
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
  • #2
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?
 
  • #3
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.
 
  • #4
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?
 
  • #5
The one that's 1 down and 1 to the right of the current element, I'd imagine.
 
  • #6
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:
  • #7
Hm. I'd say seeing if it was between the smallest and largest numbers, assuming it wasn't one of those.
 
  • #8
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?
 

Related to How to find a number in a matrix efficiently?

1. What are data structures?

Data structures are ways of organizing and storing data in a computer so that it can be accessed and modified efficiently. They are fundamental concepts in computer science and are often used to solve complex problems and improve the performance of algorithms.

2. Why are data structures important?

Data structures are important because they allow for efficient storage, retrieval, and manipulation of data. They can greatly impact the performance of algorithms and help solve complex problems in an organized and systematic way.

3. What are the most commonly used data structures?

The most commonly used data structures include arrays, linked lists, stacks, queues, trees, and graphs. Each of these data structures has unique properties and is suited for specific types of problems and applications.

4. How do you choose the right data structure for a problem?

Choosing the right data structure for a problem depends on several factors, including the type of data, the operations that need to be performed on the data, and the efficiency required. It is important to carefully analyze the problem and understand the strengths and weaknesses of different data structures before making a decision.

5. Can you give an example of a real-world application of data structures?

Data structures are used in many real-world applications, such as databases, search engines, operating systems, and social media platforms. For example, a search engine uses data structures like hash tables and trees to efficiently store and retrieve information from millions of web pages.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
11
Views
647
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
19
Views
2K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
939
  • Programming and Computer Science
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Math Proof Training and Practice
Replies
23
Views
952
Back
Top