Comments (10)
In addition, would it be better to add boundary check to the bottom of the while block as follows:
//searched through the whole array but not found
if (elementToCheck == 0 || elementToCheck == array.length-1) {
return -1;
}
from algorithms-sedgewick-wayne.
Thanks for the contribution.
I added the boundary check at the end of the loop.
I am not sure I understand your first comment though. If we update fibonacciBeforeN
and fibonacciN
at that point, we would be updating them twice. They are also updated at the beginning of the loop.
from algorithms-sedgewick-wayne.
Based on the comment put in chapter1/section4/Exercise22_BinarySearchAddSub.java:
// Based on http://algs4.cs.princeton.edu/14analysis/:
// Answer: Instead of searching based on powers of two (binary search),
// use Fibonacci numbers (which also grow exponentially).
// Maintain the current search range to be [i, i + F(k)] and keep F(k), F(k-1) in two variables.
// At each step compute F(k-2) via subtraction, check element i + F(k-2),
// and update the range to either [i, i + F(k-2)] or [i + F(k-2), i + F(k-2) + F(k-1)].
For the search range [i, i + F(k)], after checking element i + F(k-2), the next search range will be updated to either [i, i + F(k-2)] or [i + F(k-2), i + F(k-2) + F(k-1)].
If the next search range is [i, i + F(k-2)], based on the description of the algorithm(the third & fourth lines of the comment above), F(new k) is actually F(k-2), thus F(new k-1) should be F(k-3) and F(new k-2) should be F(k-4), meaning "fibonacciN" and "fibonacciBeforeN" should be both updated twice.
from algorithms-sedgewick-wayne.
Are we not going to skip ranges when updating fibonacciN
and fibonacciBeforeN
twice?
I did a test changing
if (key < array[elementToCheck]) {
high = low + fibonacciBeforeN;
}
to
if (key < array[elementToCheck]) {
high = low + fibonacciBeforeN;
aux = fibonacciBeforeN;
fibonacciBeforeN = fibonacciN - fibonacciBeforeN; // F(k-2)
fibonacciN = aux; // F(k-1)
}
and the last test fails (the -2 element is not found in the search).
from algorithms-sedgewick-wayne.
It's caused by still trying to shrink next search range through fibonacci subtraction after F(k) = 1 and F(k-1) = 1, in which case F(k-2) is already 0 (corresponding range was either [i, i] or [i, i + 1]).
In addition, after checking element i + F(k-2), can we update the range to either [i, i + F(k-2) - 1] or [i + F(k-2) + 1, i + F(k-2) + F(k-1)], because i + F(k-2) is already checked?
from algorithms-sedgewick-wayne.
Post my code for your review:
private int binarySearch(int[] array, int key) {
int aux;
int fibonacciBeforeN = 0;
int fibonacciN = 1;
// Compute F(k)
while (fibonacciN < array.length - 1) {
aux = fibonacciN;
fibonacciN = fibonacciBeforeN + fibonacciN;
fibonacciBeforeN = aux;
}
int low = 0;
int high = low + array.length - 1;
while (low <= high) {
if (fibonacciBeforeN > 0) {
// Compute F(k-2)
aux = fibonacciBeforeN;
fibonacciBeforeN = fibonacciN - fibonacciBeforeN; // F(k-2)
fibonacciN = aux; // F(k-1)
}
int elementToCheck = low + fibonacciBeforeN;
if (elementToCheck > high) {
elementToCheck = high;
}
if (key < array[elementToCheck]) {
high = elementToCheck - 1;
// shrink range one more step
if (fibonacciBeforeN > 0) {
// Compute F(k-2)
aux = fibonacciBeforeN;
fibonacciBeforeN = fibonacciN - fibonacciBeforeN; // F(k-2)
fibonacciN = aux; // F(k-1)
}
} else if (key > array[elementToCheck]) {
low = elementToCheck + 1;
} else {
return elementToCheck;
}
}
return -1;
}
from algorithms-sedgewick-wayne.
Test cases:
public static void main(String... args) {
Exercise22_BinarySearchAddSub exercise22_binarySearchAddSub = new Exercise22_BinarySearchAddSub();
int N = 30;
for (int i = 1; i <= N; i++) {
int[] array = IntStream.iterate(2, e -> e + 2).limit(i).toArray();
int M = (i << 1) + 1;
for (int j = 1; j <= M; j++) {
StdOut.print(Arrays.toString(array));
int index = exercise22_binarySearchAddSub.binarySearch(array, j);
StdOut.printf(" KEY: %5d; INDEX: %5d\n", j, index);
}
StdOut.println();
}
}
from algorithms-sedgewick-wayne.
Another version for your reference.
private int binarySearch(int[] array, int key) {
int aux;
int fibonacciBeforeN = 0;
int fibonacciN = 1;
// Compute F(k)
while (fibonacciN < array.length - 1) {
aux = fibonacciN;
fibonacciN = fibonacciBeforeN + fibonacciN;
fibonacciBeforeN = aux;
}
int low = 0;
int high = array.length - 1;
while (low <= high) {
while (fibonacciBeforeN > 0 && fibonacciN >= high - low) {
// Compute F(k-2)
aux = fibonacciBeforeN;
fibonacciBeforeN = fibonacciN - fibonacciBeforeN; // F(k-2)
fibonacciN = aux; // F(k-1)
}
int elementToCheck = low + fibonacciBeforeN;
if (key < array[elementToCheck]) {
high = elementToCheck - 1;
} else if (key > array[elementToCheck]) {
low = elementToCheck + 1;
} else {
return elementToCheck;
}
}
return -1;
}
from algorithms-sedgewick-wayne.
Thanks for the contribution.
I added the boundary check at the end of the loop.I am not sure I understand your first comment though. If we update
fibonacciBeforeN
andfibonacciN
at that point, we would be updating them twice. They are also updated at the beginning of the loop.
Sorry for my mention of bottom check above, it's actually a defect. For array [2, 4] and key 2, the search result is -1, which is not correct.
from algorithms-sedgewick-wayne.
With the latest changes the code seems to be working well.
I updated the exercise here: 6b35bd2
Thanks for the contribution!
from algorithms-sedgewick-wayne.
Related Issues (20)
- Error in exercise 1.1.19 as values become bigger than int max value HOT 1
- Ex 1.3.36 - Catenation of Queues/Stacks can be done in constant time HOT 1
- Ex_1.4.26_3Collinearity exceptional case: two different lines whihc have the same slope HOT 1
- Ex 1.4.7 - k++ not considerd for tilde approximation HOT 2
- ex 1.5.13 assign to the root of a heavier tree HOT 2
- Ex 2.3.1: omit one extra line HOT 1
- The solution of exercise 4.3.32, specified set, seems to be incorrect for me. HOT 2
- Parent K value if pq[] starts from 0 HOT 1
- object error HOT 1
- 4.1.14 what about two stacks? HOT 2
- 4.1.16. single BFS traversal HOT 1
- 2.2.17 beforeLow usefulness HOT 1
- 4.2.29 Minor improvement suggestion HOT 1
- EXERCISE 3.3.24 - worst case for red-black BSTs HOT 12
- Exercise 6.37 - maxflow with sink removed HOT 1
- Error in Double hashing for linear probing HOT 1
- Potential Issue with 4.4.34 HOT 3
- Clarification Required on find() Access Time in Exercises 1.5.4 and 1.5.3 HOT 1
- Question about Chapter 1, Section 3, Exercise 9 HOT 1
- src/chapter4/section1/Exercise30_EulerianHamiltonianCycles.java HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from algorithms-sedgewick-wayne.