def generate_pws_in_order(self, n, filter_func=None, N_max=1e6):
        states = [(-1.0, helper.START)]
        p_min = 1e-9 / (n**2)
        ret = []
        done = set()
        already_added_in_heap = set()
        while len(ret) < n and len(states) > 0:
            p, s = heapq.heappop(states)
            if p < 0:
                p = -p
            if s in done: continue
            assert s[0] == helper.START, "Broken s: {!r}".format(s)
            if s[-1] == helper.END:
                done.add(s)
                clean_s = s[1:-1]
                if filter_func is None or filter_func(clean_s):
                    ret.append((clean_s, p))
            else:
                for c, f in self._get_next(s).items():
                    if (f*p < p_min or (s+c) in done or
                        (s+c) in already_added_in_heap):
                        continue
                    already_added_in_heap.add(s+c)
                    heapq.heappush(states, (-f*p, s+c))
            if len(states) > N_max * 3 / 2:
                print("Heap size: {}. ret={}. (expected: {}) s={!r}"
                      .format(len(states), len(ret), n, s))
                print("The size of states={}.  Still need={} pws. Truncating"
                      .format(len(states), n - len(ret)))
                states = heapq.nsmallest(int(N_max * 3/4), states)
                print("Done")
        return ret