Most average simple path, algorithm using "middle neighbor"

*EDIT*
Apologies as I've poorly explained, what I'm looking to do is find the middle (Median, average) distance a simple (visiting each point once) path could follow around a series of points. preferably with even steps between each point.
If there where 5 points, with the following coordinates,
1=[0,0], 2=[3,2], 3=[3,3], 4=[5,5], 5=[4,1]
and the following distance matrix
a=[0,1,2,3,4,5;1,0,3.605551275,4.242640687,5.385164807,5.385164807;2,3.605551275,0,1,2,4.123105626;3,4.242640687,1,0,2.236067977,2.236067977;4,5.385164807,2,2.236067977,0,1.414213562;5,5.385164807,4.123105626,2.236067977,1.414213562,0]
The shortest possible path around the points would be 1-3-2-5-1, the longest path would be 1-3-5-4-2. Assuming I'm using a brute force algorithm, the path I would like to find would be the median of all the generated paths, with the steps between each point as close as possible to the average distance between all points, which in this case is 3.2.
so for example starting at point 3, the code looks for the next point by finding what distance is closest to 3.2, points 4 and 5 are both 2.2 away whilst 1 is 4.2 away and 2 is 1 away. so the algorithm will select either 4 or 5. if it selects 5, it will then select the distance closest to 3.2 to move to the next point, however it now can not select 3 as it is a simple path, so it will select 2.
Is this possible? and is it also possible to have a code where it will select the route optimum route with the path closest to 3.2 every step?.
Thanks again

4 Commenti

You need to carefully define what you are doing. Vague statements like the roughly closest path are meaningless in terms of mathematics or in terms of an algorithm to compute what you need.
What is a move? What is an average simple path? I think you are using the word "average" in a very non-standard way here. So you MUST define what you want clearly.
Give an example, where you compute what you would want to see happen on a SMALL problem.
You cannot write code until you have a clear definition of exactly what the code must do.
If the first row [0,1,2,3,4] are the distances, and the first column [0;1;2;3;4] are the distances, then what do the other elements in the matrix represent?
Can you give the "average" path for the example you gave? Where does the path start from and where is it's goal - the point where you're trying to get to? Have you considered using the shortestpath() function?
Apologies as I've poorly explained, what I'm looking to do is find the middle (Median, average) distance a simple (visiting each point once) path could follow around a series of points. preferably with even steps between each point.
If there where 5 points, with the following coordinates,
1=[0,0], 2=[3,2], 3=[3,3], 4=[5,5], 5=[4,1]
and the following distance matrix
a=[0,1,2,3,4,5;1,0,3.605551275,4.242640687,5.385164807,5.385164807;2,3.605551275,0,1,2,4.123105626;3,4.242640687,1,0,2.236067977,2.236067977;4,5.385164807,2,2.236067977,0,1.414213562;5,5.385164807,4.123105626,2.236067977,1.414213562,0]
The shortest possible path around the points would be 1-3-2-5-1, the longest path would be 1-3-5-4-2. Assuming I'm using a brute force algorithm, the path I would like to find would be the median of all the generated paths, with the steps between each point as close as possible to the average distance between all points, which in this case is 3.2.
so for example starting at point 3, the code looks for the next point by finding what distance is closest to 3.2, points 4 and 5 are both 2.2 away whilst 1 is 4.2 away and 2 is 1 away. so the algorithm will select either 4 or 5. if it selects 5, it will then select the distance closest to 3.2 to move to the next point, however it now can not select 3 as it is a simple path, so it will select 2.
Is this possible? and is it also possible to have a code where it will select the route optimum route with the path closest to 3.2 every step?.
Thanks again
Well, at least your question makes sense now.
Obviously the simple solution is to generate all possible paths. Then choose among them to find the one that makes you happiest.
A greedy algorithm as you describe will rarely be optimal, but it is always faster than a brute force check of all paths. Is it possible? Of course, just write the code. Does code exist for this specific greedy algorithm? Surely not. But given the matrix of distances, would not be difficult to write. Just keep a list of all nodes you have visited, or those that remain to visit.
Does code exist to tabulate all possible paths? Be careful, as this set will grow with factorial(n), so will extremely quickly become intractable. Typically, I'd expect n=10 nodes will become difficult, and 12-15 nodes essentially impossible. (And since people never have problems with only 5 nodes, forget the idea.)
Can you solve this in a computationally feasible way? Possibly. Even probably. This really is still just a traveling salesman problem, but with a target distance for each step in the path. You know the set of all distances, so it is easy to compute the average distance. That gives you a target for the distance in any leg of the path. So now assign a cost along any leg that is lowest for a leg that is near the average distance, with increased costs for legs that are higher than the average or lower then the average.
That means you will solve a basic TSP, but with costs as assigned, and you want to minimize cost, not distance traveled. There are various schemes for TSP solvers to be found on the File Exchange.

Accedi per commentare.

 Risposta accettata

Having thought about this, all you need to do is create a new distance matrix, thus a cost matrix, based on the distances. Deviations from the target distance are costly, so you might choose a cost that is proportional to the deviation from the target distance, or perhaps the square of the deviation.
Then just throw it at any TSP solver.

1 Commento

In his example, his nodes don't always hit every node, and don't always return to the starting point, so is it still a traveling salesman problem? Could A* or dynamic programming be used here?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by