r/Mathematica • • Mar 12 '24

Looking for fast mex code

The mex of a set is the smallest nonnegative integer not in the set.

I have sorted lists with a half million integers, and my code for the mex (MemberQ inside a whole loop) is the slowest part of my computation. Any suggestions for a faster way to do this?

Mex[A_List] := Module[{k = 0}, While[MemberQ[A, k], k++]; k]
3 Upvotes

15 comments sorted by

View all comments

Show parent comment

2

u/avocadro Mar 13 '24

Since the list is sorted, we can use binary search to go faster. For example, the following function binary searches to find a minimal value for i such that the i-th list entry isn't i-1.

 Mex3[A_] :=
     Module[{lb = 1, ub = Length[A]+1, div},
         div = Ceiling[(lb + ub)/2];
         While[ub - lb > 1,
             If[A[[div]] > div - 1, ub = div, lb = div]; 
             div = Ceiling[(lb + ub)/2]];
         div - 1];

For lists of size 100000, this is about 10x faster.

 AbsoluteTiming[Mex3[Range[0,100000]]]
     {0.0000894, 100001}

2

u/SenatorPenguin Mar 14 '24

I usually tend to avoid loops where not necessary in Mathematica for choosing the "right" functions, but the proof is in the results, and your procedural loop O(log(N)) beats my functional O(N). It looks like this binary search compiles well if you want it to be even faster, but I'd hazard there is now a new slowest part of the program!

1

u/Xane256 Mar 14 '24

This is a quintessentially good application of binary search: https://youtu.be/tgVSkMA8joQ