Hi, I want to solve salesman problem using brute force but choosing random numbers of roads. It is random method ? So I created table with number of points [n] and matrix with locations of this points [n][2] but I dont know what next..So now I have to check for example 1000 randomly possible roads and find witch od these roads is shortest ?
I'm afraid that we are going to need to have more information before we are able to help you.
-> What problem do you want to solve, can you share at least the instruction for the task?
-> What have you tried to solve your problem and what are you having trouble with?
-> If you have started to write some code please share it so we can help you based on that at least
you have to check every possible path between 2 points that follow the rules (can't go to the same point more than once is a part of TSP) and be able to abort if an attempted path cannot get from A to B (though I think TSP is fully connected but I can't recall?).
note that even for 'only' 1000 nodes this is a HUGE number of possible paths to have to check.
lets look at 5 nodes.
a,b,c,d,e
and you want to get from a to e
you have to check a-e, a-b-e, a-c-e, a-d-e, a-b-c-d-e, a-c-b-d-e, ... and more 4 point combos.
imagine that for 1000 nodes...
I was thinking shortest path, my above isnt right. Its cheapest to hit all once, not cheapest from a-b. you can ignore the short paths I listed. Even so its a giant number via brute force.
#include <iostream>
#include <algorithm>
void print(constint *a, int n) {
for (int i = 0; i < n; ++i) std::cout << a[i] << ' ';
std::cout << '\n';
}
int main() {
int n;
std::cout << "Number of points: ";
std::cin >> n;
auto a = newint[n];
std::iota(&a[0], &a[n], 0); // fill with values 0 to n-1
do {
print(a, n);
} while(std::next_permutation(&a[0], &a[n])); // calculate next lexicographic permutation
}
swaps.
if you have nodes: 1 2 3 4
start there:
1 2 3 4
4 2 3 1 //swap, in the original, first and last
1 4 3 2 //swap, in orig, second and last
1 2 4 3 //swap third and last.
now you have to do the third for each of the above.
for 1234
3214
1324
for 4231
3241
4321
... and on and on.
or you can generate them. This is faster than swapping, but swapping may be easier to see and understand? Think of it like how you count in hex, except you can't have duplicates.
00 //bad, dupe
01
02
03
...
0f
10
11 //bad, dupe... there is a pattern here
12
..
1d
1e
1f
20
21
22//bad, dupe..
...
remember you only need to store the cheapest combo. get the score for the first one, then when you find a score lower, save what permutation that is, until you have checked them all. you don't need to store all this stuff, just generate, evaluate, and move on.
then try the greedy algorithm started once from each node. Compare time taken and answer.
[edit]
BTW, you want to brute-force 1000 vertices?
What supercomputer farm are you planning to use?
Because brute-forcing that will take you several years.
the method I gave is very good and very simple to code. It may even give the best answer for fully connected problems (not 100% sure), but it fails to find the best on partially connected (I know this is true). Fail to find best still gives a pretty good answer.
@asxxx, is the link posted by againtry your assignment? Because that is definitely not brute-force.
The reason that Travelling Salesman is NP-hard is because doing it by brute-force is impractical, and the way to optimize the problem is not known or, as far as we can tell, possible to do.
Hence, we look for a good-enough way to do it quickly.
I've given up trying to figure out why some trollish fool reports me or others. The "slaughter" in the Lounge is more than enough of foolish idiocy to make me utterly indifferent to the whims of morons.
Maybe I was reported for suggesting to use C++ libraries when writing new C++ code instead of using outdated C libraries and ways to craft code.
You likely were reported as well because you "defended" me. *shrug*
I didn't even mention another C++ vs. C "issue," dynamic container arrays. *cough*std::vector*cough*