Two-pass algorithm for γL (grouping) based on hashing