Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure "lexicographical" order in Sets.cartesianProduct #908

Closed
gissuebot opened this issue Oct 31, 2014 · 6 comments
Closed

ensure "lexicographical" order in Sets.cartesianProduct #908

gissuebot opened this issue Oct 31, 2014 · 6 comments
Assignees
Labels
Milestone

Comments

@gissuebot
Copy link

Original issue created by tomas.zalusky on 2012-02-24 at 05:44 PM


Hi,

the current implementation of Sets.cartesianProduct returns Lists where the first value changes most quickly and last value most slowly.
If ordering inside Set matters (in case of some ordered set implementation) and one makes the cartesian product of set with itself, the iteration order is not stable.

My use case:

I have to find a best Hamiltonian path in a (not so large) graph, given a restriction that the first and last node must be in specified subsets of nodes. The algorithm tries all nonequal pairs of possibly-first and possibly-last nodes, evaluates quality of a path and in case of a tie the minimal ordering should decide. Example: (for simplicity edges don't matter and assume all possible paths have same quality)

Let G1 be graph with nodes a,b,c, possiblyFirst=[a,b,c], possiblyLast=[c].
Cartesian products: [[a,c],[b,c],[c,c]].
First successful: [a,c].
Result: [a,b,c].

Let G2 be graph with nodes a,b, possiblyFirst=[a,b], possiblyLast=[a,b].
Cartesian products: [[a,a],[b,a],[a,b],[b,b].
First successful: [b,a].
Result: [b,a].

So removing node c breaks order between a and b.

I am aware of javadoc of cartesianProduct method https://google.github.io/guava/releases/v11.0.1/apidocs/com/google/common/collect/Sets.html#cartesianProduct%28java.util.List%29 stating the order is not guaranteed. Should it rather do? Other methods (difference, union, intersection) do it too.

Moreover, in the javadoc example Sets.cartesianProduct(ImmutableSet.of(1, 2),ImmutableSet.of("A", "B", "C")) the state result is in different order that in reality which is little confusing, however I admit the "returns a set containing six lists:" statement is precise.

If Guava team is open to such a change, I would try to add a patch of Sets.CartesianSet.

The current workaround is Sets.cartesianProduct(originalList.reverse()); and reverse resulting lists back.

Thank you

Tomas Zalusky

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-02-24 at 09:19 PM


Hm. I am open to the possibility of specifying the order, and if we do, most-significant-to-least-significant (the opposite of what you say it does now) does seem less surprising.

It's worth nothing that the change may break some users who are depending on the order even though they shouldn't. So it's not completely free in that sense, but still justifiable if we think it's best.

I assume the code changes wouldn't be too many, just some tweaks in the math?


Status: Acknowledged

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2012-02-26 at 06:09 PM


It should be relatively straightforward; I can code it up if we decide to pursue this.

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-03-16 at 09:19 PM


(No comment entered for this change.)


Labels: Type-Enhancement

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-05-15 at 01:55 PM


(No comment entered for this change.)


Labels: Package-Collect

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-06-22 at 06:16 PM


(No comment entered for this change.)


Status: Research

@gissuebot
Copy link
Author

Original comment posted by lowasser@google.com on 2012-11-08 at 06:01 PM


We fixed this back in September; we just forgot to mention it.

86aa9f0


Status: Fixed
Owner: lowasser@google.com
Labels: Milestone-Release14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants