Skip to content

Commit

Permalink
ha
Browse files Browse the repository at this point in the history
  • Loading branch information
pgujjula committed Apr 4, 2024
1 parent 9bd14da commit 275e1b9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions docs/ALGORITHM.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,21 @@ This investigation suggests the following algorithm for producing `smooth3`:
4. Go to step 2.
```

## Generalizing the idea
# The applyMerge function

The idea in the algorithm above is quite general, and instead of using it on
`(*)`, `powersOf2`, and `powersOf3`, we can apply it to any binary function `f`
that is monotonically increasing in both arguments, and any ordered lists `xs`
and `ys`.
and `ys`. Then we can define a general
```haskell
applyMerge :: (Ord c) => (a -> b -> c) -> [a] -> [b] -> [c]
```
where `applyMerge f xs ys` is an ordered list of all `f x y`, where `x xs` and `y ys`. In particular, `smooth3 = applyMerge (*) powersOf2 powersOf3`.

## Implementation and complexity
We can use a priority queue to maintain the frontier of elements. To determine in step 3 whether a down- or right-neighbor is unblocked, we maintain two `IntSet`s, for the `x`- and `y`- indices of all elements in the frontier. Then in step 3, if the `x`- and `y`-index of a neighbor are not in the respective `IntSet`s, the neighbor is unblocked and can be added to the frontier.

After producing $n$ elements, the size of the frontier is at most $O(\sqrt{n})$ elements. <i>(TODO: prove this)</i> Manipulating the priority queue and the `IntSet`s to yield one element takes $O(\text{log } \sqrt{n}) = O(\text{log } n)$ time. Therefore, producing $n$ elements of `applyMerge f xs ys` takes $O(n \log n)$ time and $O(\sqrt{n})$ space.

# Prior work
In <code>[data-ordlist](https://www.stackage.org/lts/package/data-ordlist)</code>,
Expand Down

0 comments on commit 275e1b9

Please sign in to comment.