2

What is the complexityclass of a Linq Queryable.SelectMany.

I'm looking for Big O notation to predict runtime efficiency.

I checked Microsoft Docs and several other sources.

In this StackOverflow complexity is noted as O(N^2) what doesn't make sense to me due to probably more efficient attemps copying N memory blocks to a new connected one.

I predict the complexity to be O(N).

Can you point me to references where complexityclass for SelectMany is explicity named, or give hints why complexity is O(N^2).

Help is appreciated, Cookies for best answer. Thanks

Richard Wieditz
  • 406
  • 1
  • 4
  • 14
  • 2
    It operates on a collection of collections. How is that not O(n²)? – Robert Harvey Mar 18 '20 at 13:35
  • 2
    It somewhat depends on what `n` is, since you're talking about a sequence containing sub-sequences, where each sub-sequence potentially has a different length. I'd naively say it's `O(n + Σmₜ)`, where `n` is the length of the top-level sequence, and `Σmₜ` is the sum of lengths of the sub-sequences. If each sub-sequence also has length `n`, then this becomes `O(n + n²) = O(n²)`, I would assume? – canton7 Mar 18 '20 at 13:38
  • "If each sub-sequence also has length `n`" but are they related? Isn't it closer to `O(m*n)` where `m=n` may but not must be true. – Caramiriel Mar 18 '20 at 13:45
  • 2
    @Caramiriel Each of the sub-sequences may indeed have entirely different lengths, which is that I discussed in the first part of my comment. I was trying explain where OP might have come across `O(n²)`. If each sub-sequence has length `m` then yes, `Σmₜ` is `n*m`, and so it's `O(n + n*m) = O(n*m)`, I think? – canton7 Mar 18 '20 at 13:56

0 Answers0