Find First Place Where F <= 0 in O(log n) Time

In summary, an algorithm for solving the homework equation in O(log n) time is provided. However, the algorithm might not work if the input is not finite.
  • #1
Jcampuzano2
5
0

Homework Statement



Consider a strictly decreasing function F: ℕ → ℤ. We want to find the "first place where f is at or below the horizontal axis." Assume we can compute ƒ(i) for any input i in constant time. Clearly, we can solve the problem in O(n) time by evaluating ƒ(1), ƒ(2), ƒ(3),... until we see a non-positive number. Give an O(log n) algorithm.

Homework Equations


The only given is that ƒ(0) > 0.

The Attempt at a Solution



I'm a little confused on this problem. I don't have a finite input size, so I can't just access the last element in an array like I'm used to on algorithm problems. My only guess so far is that I could could pick a random number and test whether it is less than 0. If it is, then I can say that that can be considered the last element in an set, and perform a binary search to find the first place where the value is less than 0, but what if it takes greater than O(log n) time just to find this initial value?

So for example I test say f(100). if that is less than 0, then my input set can be say 0, 1, ... 100. and do a binary search using this as input. Would this be the correct way to go about this? It's literally the only way I can think of.
 
Physics news on Phys.org
  • #2
I'm a bit confused. If an algorithm can be said to be O(n), it means that n is defined, that there is a number of input data. If you indeed let i not be constrained, then saying that an algorithm is O(n) doesn't make sense to me.

Could it be that n is constrained by the largest ℕ that can be expressd depending on the data type of i?
 
  • #3
DrClaude said:
I'm a bit confused. If an algorithm can be said to be O(n), it means that n is defined, that there is a number of input data. If you indeed let i not be constrained, then saying that an algorithm is O(n) doesn't make sense to me.

Could it be that n is constrained by the largest ℕ that can be expressd depending on the data type of i?

That's part of where my confusion comes from as well. This problem isn't given with any data types in its context. I might just have to ask for more info on this one. The only part of the problem I left out because it was explained in the problem is the following sentenct:

We want to find

n = min{ i ∈ ℕ : ƒ(i) ≤ 0} .

But that was explained in the next sentence "first place where f is at or below the horizontal axis." Maybe that helps, but I don't think it does. Correct me if I'm mistaken.
 
  • #4
Jcampuzano2 said:
Maybe that helps, but I don't think it does.
I'm still as confused. And the more I think about it, the more I'm convinced that the question only makes sense if the value of i is bounded.
 
  • #5


Your approach is on the right track, but there is a more efficient way to do it in O(log n) time.

One possible solution is to use a modified binary search. Start by choosing a random number k and evaluating ƒ(k). If ƒ(k) is less than or equal to 0, then k is the first place where ƒ is at or below the horizontal axis. If ƒ(k) is greater than 0, then we know that the first place where ƒ is at or below the horizontal axis must be to the left of k. So we can recursively apply the same process on the left half of the input set (from 1 to k-1). This will continue until we find the first place where ƒ is at or below the horizontal axis.

The key here is that at each step, we are dividing the input set in half, so the time complexity will be O(log n). This is because each time we divide the input set in half, we are essentially reducing the size of the input set by half. So after k steps, the input set will have a size of n/2^k. Solving for k, we get k = log n, which gives us the time complexity of O(log n).
 

Related to Find First Place Where F <= 0 in O(log n) Time

1. What is "Find First Place Where F <= 0"?

"Find First Place Where F <= 0" is a problem in computer science and mathematics that involves finding the first occurrence of a value less than or equal to zero in a given set of data. It is often used in algorithms to efficiently search for a specific value in a large dataset.

2. What does "O(log n) Time" mean?

"O(log n) Time" refers to the time complexity of an algorithm, which measures how long it takes for an algorithm to run based on the size of the input data. In this case, O(log n) means that the algorithm will take logarithmic time to run, which is generally considered to be very efficient.

3. How can "Find First Place Where F <= 0" be applied in real life?

This problem can be applied in various real-life scenarios, such as searching for the first negative number in a stock market data set or finding the first instance of a temperature dropping below freezing in a weather data set. It can also be used in computer science applications, such as searching for a specific element in a sorted array.

4. What makes O(log n) time complexity more efficient than other time complexities?

O(log n) time complexity is considered more efficient than other time complexities, such as O(n) or O(n^2), because it has a much slower growth rate. This means that as the input data size increases, the time it takes for the algorithm to run will also increase, but at a much slower rate compared to other time complexities. This makes it an ideal choice for large datasets.

5. Is it possible to improve the time complexity of "Find First Place Where F <= 0"?

Yes, it is possible to improve the time complexity of "Find First Place Where F <= 0" by using more advanced algorithms and data structures, such as binary search. This can reduce the time complexity to O(1) or constant time, making it even more efficient. However, this may require more resources and may not be necessary for smaller datasets.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
944
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
31
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
Back
Top