-
Notifications
You must be signed in to change notification settings - Fork 181
Add note on asymptotical optimality of compose implementations #960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -769,6 +769,8 @@ disjoint t1@(Bin p1 m1 l1 r1) t2@(Bin p2 m2 l2 r2) | |||
-- 'compose' that forced the values of the output 'IntMap'. This version does | |||
-- not force these values. | |||
-- | |||
-- __Note:__ This is not asymptotically optimal. See note at 'Data.Map.compose', keeping in mind \( \log(m) \) is \( O(\min(m,W)) \), but not the other way around. |
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.
Actually, I'm not sure what would even be asymptotically optimal here. The current impl is O(n * min(m,W)
, but one could write
compose :: IntMap c -> IntMap Int -> IntMap c
compose bc !ab
| null bc = empty
| otherwise = mapMaybe (f bc !?) ab
where
f = Map.fromAscList . IntMap.toAscList
It seems to me this would be O(n * log m + m)
, and the extra m
makes some sense given we can't directly use element comparisons on lookup on IntMap
, so either we convert to Map
or rely on the word size bound.
No clue how the constants here play out, would need to benchmark.
I also wonder if there's a faster way to implement f
as I couldn't find any Map/IntMap conversion functions in their respective modules.
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.
The question of optimality for IntMap
doesn't seem to be well-posed, so it's probably not worth commenting on it. The cost of compose
is the cost of n
lookups, that seems a satisfactory enough answer so that formalizing the question of optimality is unnecessary, if possible at all..
It's easy to talk about the optimality of Map
because it matches the information-theoretic lower bound, which is independent of the representation, that makes it easy to formalize. For IntMap
, a meaningful lower bound would have to take into account the particular data type involved, that seems difficult to formalize.
In other words, I think you can just drop the comment about optimality. |
99aec6d
to
e08b262
Compare
Completely forgot about this. Took out the "this is not optimal" bit |
I thought about the claim here a bit, and I'd like to understand this better. Point 1 Point 2 |
Point 1 Point 2 |
Point 2 Point 1
I'm not convinced on this. Consider instead if we had to implement |
That's an interesting comparison that helps me see some implicit assumptions I made, thanks. There is a difference in the amount of information initially known about the input. If we make abstraction of the shape of trees, a This lower bound uses the same information-theoretic idea used to prove the |
I'd say it does, but for something else. The "number of comparisons to know which |
I'm on board that we need
Perhaps this is it. It is true that we know nothing about how Anyway, I will take your word for it and we can add this to the docs. |
e08b262
to
199ba3a
Compare
Thanks! |
No description provided.