-
Notifications
You must be signed in to change notification settings - Fork 36
Stack with time complexity of O(N logN) #26
base: master
Are you sure you want to change the base?
Conversation
@saharki , thanks for the PR, however it does not seem to work.
? |
Thanks for pointing it out. Although the last commit piles the items of your example to 3 levels (not 2 as you'd expect), it has the same output as the current stacking algorithm. In addition, in some cases, my implementation of the stacking algorithm is more compact than the current algorithm. For instance:
|
Why item "3" is put on its own lane? I assume you use 0 (or a negative value) for horizontal margin to avoid dealing with small fractions. |
@saharki , just to clarify: Suppose the algorithm has processed 100 items (out of 1000), and suppose it has allocated 10 rows.
Naive implementation would be to store the latest item for each row in array and iterate over the array when placing each item. In order to implement 2 and 3, I think a tree is required. The contents is "latest intervals for each row", and the ordering is "by end time". |
What you described here wouldn't be a greedy algorithm, which has its pros and cons. Of course my algorithm doesn't have the best output for this function that can be implemented, but the approach here is much KISS-er than the one you have suggested. My implementation of the stacking algorithm is an improvement comparing to the current one in time complexity and space-saving output. Therefore, I suggest this PR should be approved and I will continue working for a more "dense" implementation later on (which justifies new issue to be open). |
Why do you think so?
What is the algorithm exactly?
So what?
It is hard to tell if there are space savings or not. You have provided a single chart only, and it looks like the only cause original algorithm used 4 rows was margins.
Of course it is not up to me to decide, however:
|
3684dda
to
f47818f
Compare
lib/timeline/Stack.js
Outdated
for (let j = 0, jj = items.length; j < jj; j++) { | ||
const other = items[j]; | ||
shouldBail = shouldBailItemsRedrawFunction() || false; | ||
if (items[i].stack && items[i + 1] && items[i + 1].top === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
items[i]
-> item
items[i + 1]
-> nextItem
lib/timeline/Stack.js
Outdated
if (shouldBail) { return true; } | ||
shouldBail = shouldBailItemsRedrawFunction() || false; | ||
|
||
if (shouldBail) { return true; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this to the beginning of the for loop before getting in to the conditionals
Stacking with time complexity of
O(N log N)
instead ofO(N^2)
.Closes #20