The point I would like to make: if the groups in the $membership vector are not numbered consecutively and starting at 1, then max($membership) is wrong. A safer alternative is length(unique()). But that’s a lot of overhead for an unlikely situation. Is that worth it?
In theory, the C core of igraph will always return membership vectors where the communities are numbered consecutively and there are no empty communities. We have a function for that in the C core called igraph_reindex_membership(). I think that all community detection functions in the C core call this function in the end, and if not, that’s a bug in the C core and should be fixed there.