Given two arrays, find the permutations that give closest distance between two arrays
Lets say I have two arrays of the same length n, named A and B.
These two arrays contain real values.
We define the distance between two array as the mean squared distance.
dist(A,B) = sqrt(sum((A-B)^2))
I wan to find the permutation of A that gives the min distance to B.
The naive method is to try every permutation of A and record the min distance. However this method is of complexity O(n!).
Is there an algorithm of complexity less than O(n!)?
algorithm
|
show 1 more comment
Lets say I have two arrays of the same length n, named A and B.
These two arrays contain real values.
We define the distance between two array as the mean squared distance.
dist(A,B) = sqrt(sum((A-B)^2))
I wan to find the permutation of A that gives the min distance to B.
The naive method is to try every permutation of A and record the min distance. However this method is of complexity O(n!).
Is there an algorithm of complexity less than O(n!)?
algorithm
For what purpose is this. Do you need the best solution or just a good enough solution?
– Yair Halberstadt
1 hour ago
good question though
– Aditya Gupta
1 hour ago
Can you eleborate a bit with an example problem and the answer you expect?
– Aldert
1 hour ago
I am not certain if I fully get the issue you are describing, sounds like you want to try all the possible permutations of a vector of coordinates to usePythagoras
and find which combination provides the shortest distance. So you are basically building a tree of all possible combinations, for this you would need to consider what is it you are after, degree of precision or if it needs to be exact, how many results you want (can be many), speed [of the algorithm]? Having this in mind what you could do to speed up such process is to trim your tree; again how exact does it need to be?
– Ordiel
1 hour ago
1
Why did you not approve Ivo Merchiers' answer? I'm just curious.
– גלעד ברקן
34 mins ago
|
show 1 more comment
Lets say I have two arrays of the same length n, named A and B.
These two arrays contain real values.
We define the distance between two array as the mean squared distance.
dist(A,B) = sqrt(sum((A-B)^2))
I wan to find the permutation of A that gives the min distance to B.
The naive method is to try every permutation of A and record the min distance. However this method is of complexity O(n!).
Is there an algorithm of complexity less than O(n!)?
algorithm
Lets say I have two arrays of the same length n, named A and B.
These two arrays contain real values.
We define the distance between two array as the mean squared distance.
dist(A,B) = sqrt(sum((A-B)^2))
I wan to find the permutation of A that gives the min distance to B.
The naive method is to try every permutation of A and record the min distance. However this method is of complexity O(n!).
Is there an algorithm of complexity less than O(n!)?
algorithm
algorithm
asked 2 hours ago
Wang Duo
13216
13216
For what purpose is this. Do you need the best solution or just a good enough solution?
– Yair Halberstadt
1 hour ago
good question though
– Aditya Gupta
1 hour ago
Can you eleborate a bit with an example problem and the answer you expect?
– Aldert
1 hour ago
I am not certain if I fully get the issue you are describing, sounds like you want to try all the possible permutations of a vector of coordinates to usePythagoras
and find which combination provides the shortest distance. So you are basically building a tree of all possible combinations, for this you would need to consider what is it you are after, degree of precision or if it needs to be exact, how many results you want (can be many), speed [of the algorithm]? Having this in mind what you could do to speed up such process is to trim your tree; again how exact does it need to be?
– Ordiel
1 hour ago
1
Why did you not approve Ivo Merchiers' answer? I'm just curious.
– גלעד ברקן
34 mins ago
|
show 1 more comment
For what purpose is this. Do you need the best solution or just a good enough solution?
– Yair Halberstadt
1 hour ago
good question though
– Aditya Gupta
1 hour ago
Can you eleborate a bit with an example problem and the answer you expect?
– Aldert
1 hour ago
I am not certain if I fully get the issue you are describing, sounds like you want to try all the possible permutations of a vector of coordinates to usePythagoras
and find which combination provides the shortest distance. So you are basically building a tree of all possible combinations, for this you would need to consider what is it you are after, degree of precision or if it needs to be exact, how many results you want (can be many), speed [of the algorithm]? Having this in mind what you could do to speed up such process is to trim your tree; again how exact does it need to be?
– Ordiel
1 hour ago
1
Why did you not approve Ivo Merchiers' answer? I'm just curious.
– גלעד ברקן
34 mins ago
For what purpose is this. Do you need the best solution or just a good enough solution?
– Yair Halberstadt
1 hour ago
For what purpose is this. Do you need the best solution or just a good enough solution?
– Yair Halberstadt
1 hour ago
good question though
– Aditya Gupta
1 hour ago
good question though
– Aditya Gupta
1 hour ago
Can you eleborate a bit with an example problem and the answer you expect?
– Aldert
1 hour ago
Can you eleborate a bit with an example problem and the answer you expect?
– Aldert
1 hour ago
I am not certain if I fully get the issue you are describing, sounds like you want to try all the possible permutations of a vector of coordinates to use
Pythagoras
and find which combination provides the shortest distance. So you are basically building a tree of all possible combinations, for this you would need to consider what is it you are after, degree of precision or if it needs to be exact, how many results you want (can be many), speed [of the algorithm]? Having this in mind what you could do to speed up such process is to trim your tree; again how exact does it need to be?– Ordiel
1 hour ago
I am not certain if I fully get the issue you are describing, sounds like you want to try all the possible permutations of a vector of coordinates to use
Pythagoras
and find which combination provides the shortest distance. So you are basically building a tree of all possible combinations, for this you would need to consider what is it you are after, degree of precision or if it needs to be exact, how many results you want (can be many), speed [of the algorithm]? Having this in mind what you could do to speed up such process is to trim your tree; again how exact does it need to be?– Ordiel
1 hour ago
1
1
Why did you not approve Ivo Merchiers' answer? I'm just curious.
– גלעד ברקן
34 mins ago
Why did you not approve Ivo Merchiers' answer? I'm just curious.
– גלעד ברקן
34 mins ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
The problem you describe is equivalent to the Minimum Cost Perfect Matching Problem which can be solved efficiently (and exactly) using The Hungarian Algorithm. In the Minimum Cost Perfect Matching Problem you have an input weighted bipartite graph where the two sets have the same size n, and each edge has a non-negative cost. The goal is to find a perfect matching of minimum cost.
In your case, the bipartite graph is a biclique. That is, every vertex in one set is connected to every vertex in the other set, and the cost of edge (i,j)
is (A[i] - B[i])^2
(where i
corresponds to index i
in array A and j
corresponds to index j
in array B.
add a comment |
Would it be a possibility to just sort both A and B? In that case I would assume the Euclidean distance to be minimal. I think this approach is mathematically sound, but I haven't proved it properly.
If B has to remain fixed, then you just need to invert the permutation needed to sort B and apply that to the sorted version of A.
This solution does assume that you want to find just a permutation and not the most simple permutation (since sorting and unsorting through permutations will not be incredibly efficient).
3
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
2
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
add a comment |
Construct a bipartite graph from the vectors. Find minimum weight perfect matching in this graph.
How to construct the graph.
- Let
A
,B
be two parts of the graph. Each withn
nodes. - Connect
i
inA
toj
inB
with an edge of weightabs(A[i] - B[j])
.
I believe this can be done in O(n^2)
.
See http://www.cse.iitd.ernet.in/~naveen/courses/CSL851/lec4.pdf
If each number in A
has only one closest number in B
then you can do this in O(n log n)
. This probably might be the case since you have the real numbers.
How?
- Sort A
O(n log n)
- Binary search for the closest number for each number in B.
O(n log n)
.
If the numbers are coming from the real world and have even a little bit of randomness to them, then the differences between each pair of the numbers is probably unique. You can verify if this is the case by running experiments on the input vectors. Then the problem becomes easy to solve yay!
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041397%2fgiven-two-arrays-find-the-permutations-that-give-closest-distance-between-two-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem you describe is equivalent to the Minimum Cost Perfect Matching Problem which can be solved efficiently (and exactly) using The Hungarian Algorithm. In the Minimum Cost Perfect Matching Problem you have an input weighted bipartite graph where the two sets have the same size n, and each edge has a non-negative cost. The goal is to find a perfect matching of minimum cost.
In your case, the bipartite graph is a biclique. That is, every vertex in one set is connected to every vertex in the other set, and the cost of edge (i,j)
is (A[i] - B[i])^2
(where i
corresponds to index i
in array A and j
corresponds to index j
in array B.
add a comment |
The problem you describe is equivalent to the Minimum Cost Perfect Matching Problem which can be solved efficiently (and exactly) using The Hungarian Algorithm. In the Minimum Cost Perfect Matching Problem you have an input weighted bipartite graph where the two sets have the same size n, and each edge has a non-negative cost. The goal is to find a perfect matching of minimum cost.
In your case, the bipartite graph is a biclique. That is, every vertex in one set is connected to every vertex in the other set, and the cost of edge (i,j)
is (A[i] - B[i])^2
(where i
corresponds to index i
in array A and j
corresponds to index j
in array B.
add a comment |
The problem you describe is equivalent to the Minimum Cost Perfect Matching Problem which can be solved efficiently (and exactly) using The Hungarian Algorithm. In the Minimum Cost Perfect Matching Problem you have an input weighted bipartite graph where the two sets have the same size n, and each edge has a non-negative cost. The goal is to find a perfect matching of minimum cost.
In your case, the bipartite graph is a biclique. That is, every vertex in one set is connected to every vertex in the other set, and the cost of edge (i,j)
is (A[i] - B[i])^2
(where i
corresponds to index i
in array A and j
corresponds to index j
in array B.
The problem you describe is equivalent to the Minimum Cost Perfect Matching Problem which can be solved efficiently (and exactly) using The Hungarian Algorithm. In the Minimum Cost Perfect Matching Problem you have an input weighted bipartite graph where the two sets have the same size n, and each edge has a non-negative cost. The goal is to find a perfect matching of minimum cost.
In your case, the bipartite graph is a biclique. That is, every vertex in one set is connected to every vertex in the other set, and the cost of edge (i,j)
is (A[i] - B[i])^2
(where i
corresponds to index i
in array A and j
corresponds to index j
in array B.
edited 1 hour ago
answered 1 hour ago
snakile
24k49138207
24k49138207
add a comment |
add a comment |
Would it be a possibility to just sort both A and B? In that case I would assume the Euclidean distance to be minimal. I think this approach is mathematically sound, but I haven't proved it properly.
If B has to remain fixed, then you just need to invert the permutation needed to sort B and apply that to the sorted version of A.
This solution does assume that you want to find just a permutation and not the most simple permutation (since sorting and unsorting through permutations will not be incredibly efficient).
3
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
2
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
add a comment |
Would it be a possibility to just sort both A and B? In that case I would assume the Euclidean distance to be minimal. I think this approach is mathematically sound, but I haven't proved it properly.
If B has to remain fixed, then you just need to invert the permutation needed to sort B and apply that to the sorted version of A.
This solution does assume that you want to find just a permutation and not the most simple permutation (since sorting and unsorting through permutations will not be incredibly efficient).
3
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
2
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
add a comment |
Would it be a possibility to just sort both A and B? In that case I would assume the Euclidean distance to be minimal. I think this approach is mathematically sound, but I haven't proved it properly.
If B has to remain fixed, then you just need to invert the permutation needed to sort B and apply that to the sorted version of A.
This solution does assume that you want to find just a permutation and not the most simple permutation (since sorting and unsorting through permutations will not be incredibly efficient).
Would it be a possibility to just sort both A and B? In that case I would assume the Euclidean distance to be minimal. I think this approach is mathematically sound, but I haven't proved it properly.
If B has to remain fixed, then you just need to invert the permutation needed to sort B and apply that to the sorted version of A.
This solution does assume that you want to find just a permutation and not the most simple permutation (since sorting and unsorting through permutations will not be incredibly efficient).
edited 1 hour ago
answered 1 hour ago
Ivo Merchiers
14110
14110
3
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
2
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
add a comment |
3
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
2
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
3
3
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
If this approach is wrong, I'd be glad to hear a counter-example, since I'm not sure what the issue with it is and I'm glad to learn something new.
– Ivo Merchiers
1 hour ago
2
2
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
It is mathematically sound. I don't have time to write up a proof, but it's along the lines of: If the best solution is not sorted, find two indices x, y such that A[x] < A[y] and B[x] > B[y]. Swap the two in A, and you have a better solution.
– Yair Halberstadt
1 hour ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
Seems correct in my use case. would be great if you have any reference to a proof though.
– Wang Duo
31 mins ago
add a comment |
Construct a bipartite graph from the vectors. Find minimum weight perfect matching in this graph.
How to construct the graph.
- Let
A
,B
be two parts of the graph. Each withn
nodes. - Connect
i
inA
toj
inB
with an edge of weightabs(A[i] - B[j])
.
I believe this can be done in O(n^2)
.
See http://www.cse.iitd.ernet.in/~naveen/courses/CSL851/lec4.pdf
If each number in A
has only one closest number in B
then you can do this in O(n log n)
. This probably might be the case since you have the real numbers.
How?
- Sort A
O(n log n)
- Binary search for the closest number for each number in B.
O(n log n)
.
If the numbers are coming from the real world and have even a little bit of randomness to them, then the differences between each pair of the numbers is probably unique. You can verify if this is the case by running experiments on the input vectors. Then the problem becomes easy to solve yay!
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
add a comment |
Construct a bipartite graph from the vectors. Find minimum weight perfect matching in this graph.
How to construct the graph.
- Let
A
,B
be two parts of the graph. Each withn
nodes. - Connect
i
inA
toj
inB
with an edge of weightabs(A[i] - B[j])
.
I believe this can be done in O(n^2)
.
See http://www.cse.iitd.ernet.in/~naveen/courses/CSL851/lec4.pdf
If each number in A
has only one closest number in B
then you can do this in O(n log n)
. This probably might be the case since you have the real numbers.
How?
- Sort A
O(n log n)
- Binary search for the closest number for each number in B.
O(n log n)
.
If the numbers are coming from the real world and have even a little bit of randomness to them, then the differences between each pair of the numbers is probably unique. You can verify if this is the case by running experiments on the input vectors. Then the problem becomes easy to solve yay!
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
add a comment |
Construct a bipartite graph from the vectors. Find minimum weight perfect matching in this graph.
How to construct the graph.
- Let
A
,B
be two parts of the graph. Each withn
nodes. - Connect
i
inA
toj
inB
with an edge of weightabs(A[i] - B[j])
.
I believe this can be done in O(n^2)
.
See http://www.cse.iitd.ernet.in/~naveen/courses/CSL851/lec4.pdf
If each number in A
has only one closest number in B
then you can do this in O(n log n)
. This probably might be the case since you have the real numbers.
How?
- Sort A
O(n log n)
- Binary search for the closest number for each number in B.
O(n log n)
.
If the numbers are coming from the real world and have even a little bit of randomness to them, then the differences between each pair of the numbers is probably unique. You can verify if this is the case by running experiments on the input vectors. Then the problem becomes easy to solve yay!
Construct a bipartite graph from the vectors. Find minimum weight perfect matching in this graph.
How to construct the graph.
- Let
A
,B
be two parts of the graph. Each withn
nodes. - Connect
i
inA
toj
inB
with an edge of weightabs(A[i] - B[j])
.
I believe this can be done in O(n^2)
.
See http://www.cse.iitd.ernet.in/~naveen/courses/CSL851/lec4.pdf
If each number in A
has only one closest number in B
then you can do this in O(n log n)
. This probably might be the case since you have the real numbers.
How?
- Sort A
O(n log n)
- Binary search for the closest number for each number in B.
O(n log n)
.
If the numbers are coming from the real world and have even a little bit of randomness to them, then the differences between each pair of the numbers is probably unique. You can verify if this is the case by running experiments on the input vectors. Then the problem becomes easy to solve yay!
edited 1 hour ago
answered 1 hour ago
Pratik Deoghare
17.4k2583135
17.4k2583135
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
add a comment |
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
Thanks, yours answer is correct! But @snakile replied earlier with good references so I decided to accept his.
– Wang Duo
36 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041397%2fgiven-two-arrays-find-the-permutations-that-give-closest-distance-between-two-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
For what purpose is this. Do you need the best solution or just a good enough solution?
– Yair Halberstadt
1 hour ago
good question though
– Aditya Gupta
1 hour ago
Can you eleborate a bit with an example problem and the answer you expect?
– Aldert
1 hour ago
I am not certain if I fully get the issue you are describing, sounds like you want to try all the possible permutations of a vector of coordinates to use
Pythagoras
and find which combination provides the shortest distance. So you are basically building a tree of all possible combinations, for this you would need to consider what is it you are after, degree of precision or if it needs to be exact, how many results you want (can be many), speed [of the algorithm]? Having this in mind what you could do to speed up such process is to trim your tree; again how exact does it need to be?– Ordiel
1 hour ago
1
Why did you not approve Ivo Merchiers' answer? I'm just curious.
– גלעד ברקן
34 mins ago