Skip to content

Add O(n log n) LIS via patience sorting - #287

Open
mahdiehmalekian wants to merge 5 commits into
dwavesystems:mainfrom
mahdiehmalekian:patience-sort
Open

Add O(n log n) LIS via patience sorting#287
mahdiehmalekian wants to merge 5 commits into
dwavesystems:mainfrom
mahdiehmalekian:patience-sort

Conversation

@mahdiehmalekian

Copy link
Copy Markdown

Implements longest strictly-increasing subsequence using the classic patience-sort algorithm (piles + binary search + predecessor reconstruction).
Notes:

Strictly increasing (uses >= in the binary search, not >) — equal values don't extend a subsequence. This is intentional; relaxing it changes semantics for callers relying on distinct endpoints.
Elements need only support < / >= (tuples work).
No external deps.

AI use: I prompted Claude with my own code based on the psuedocode from Wikipedia https://en.wikipedia.org/wiki/Longest_increasing_subsequence which was modified to give strictly increasing output and asked it to clean up the code and document it. I checked the resulting code. I also used Claude to generate the test suite and checked it myself.

@randomir
randomir self-requested a review July 22, 2026 19:47
@boothby
boothby self-requested a review July 30, 2026 21:51
@mahdiehmalekian

Copy link
Copy Markdown
Author

Note to reviewers: This is one of several clique-embedder PRs (four up already, including this). Not urgent and not a blocker for the next PR. This one is only depended on a couple of PRs down the road.

@thisac thisac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add a releasenotes entry.

Comment thread minorminer/utils/longest_strictly_increasing_subsequence.py Outdated
Comment thread minorminer/utils/longest_strictly_increasing_subsequence.py Outdated
Comment thread minorminer/utils/longest_strictly_increasing_subsequence.py Outdated
Comment thread minorminer/utils/longest_strictly_increasing_subsequence.py Outdated


def longest_strictly_increasing_subsequence(
sequence: list[Any],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this name. It's fine, but something slightly less generic might be better? Maybe comparables? Just sequence to me sounds more like a type.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name already indicates we're returning a subsequence, so sequence naturally names the input we extract it from. The comparability requirement is documented in the args docstring

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And Longest Increasing Subsequence problem is standard in computer science, see eg. https://en.wikipedia.org/wiki/Longest_increasing_subsequence

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only meant the sequence name, longest_strictly_increasing_subsequence is fine. 🙂

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great.
I don't terribly mind changing the name but I think with that function name, then sequence is actually the more natural input name.

Comment thread minorminer/utils/longest_strictly_increasing_subsequence.py Outdated
Co-authored-by: Theodor Isacsson <tisacsson@dwavesys.com>

Add release note

Address reviewer's comments
Comment thread tests/utils/test_longest_strictly_increasing_subsequence.py Outdated
Comment thread tests/utils/test_longest_strictly_increasing_subsequence.py Outdated
Comment thread tests/utils/test_longest_strictly_increasing_subsequence.py Outdated

@thisac thisac left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mahdiehmalekian !

Comment thread releasenotes/notes/add-lsis-6a27ee13ca010ea7.yaml Outdated
Co-authored-by: Theodor Isacsson <tisacsson@dwavesys.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants