0

i have n points on a 2d plane, with n <= 12, and i need the distance of the shortest path available including all points, starting on any of them, but not making a closed circuit

i've been trying floyd-marshal, travel salesman problem and other algorithms without success.

the problem is considered EASY for my teacher so i don't think it would require arora approximations or so, but i dont know whats the best approach to solve this, but maybe some dynamic algorithm and something like

for i = 0 to n
    for j = 0 to n
    if path_distance(i,j) < mininum
        set minimum

any help?

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Daniel
  • 521
  • 1
  • 5
  • 5

3 Answers3

1

If you know that you only have twelve points and want to find the shortest path that visits every node exactly once, then you can always just brute-force the solution by trying all possible permutations of the nodes and computing the length of the path along that permutation. This doesn't scale well at all, but if you have a fixed upper bound on the number of nodes then it should be reasonable.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
1

If and only if n <= 12, I 'd recommend the Branch and Bound algorithm, which is an improved version of the brute force algorithm.

Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
1

I'll only provide a hint since this is homework:

Recursion is very useful when breaking down these kinds of problems.

You could write a function that takes a list of the route so far, the best/minimum distance so far, and a list of unvisited nodes. For-each unvisited node it would try adding it to the route so far and if that route was still shorter than the best/minimum distance so far then it would call itself with the new route and list of unvisited nodes.

GrahamS
  • 9,980
  • 9
  • 49
  • 63