All the possible path between two points without repetition
1 view (last 30 days)
Show older comments
MarshallSc on 26 Feb 2022
Commented: MarshallSc on 27 Feb 2022
I'm looking to get the value of all pairwise data between two points by an iteration. The iteration contains direct and indirect paths. For example if there are 3 points, and having all the pairwise scores between points being available:
a_11 = 0; a_12 = 0.2; a_13 = 0.6; a_32 = -0.3; % a_23 = -a_32 (same for all other scores)
Now if the value for example, the path between 1-2 is considered, we would have (direct + indirect):
a_12_iter1 = a_12 + (a_13 + a_32)% initial paiwsie scores between all points are known, a_12_iter1 means the first iteration
%considering the initial pairwise scores.
a_12_iter1 = 0.2 + (0.6 - 0.3) = 0.5
No path is chosen twice.
Another example is having 4 points (square):
a_12_iter1 = a_12 + (a_14 + a_43 + a_32 + a_13);
a_13_iter1 = a_13 + (a_12 + a_23 + a_14 + a_43 + a_24); %same procedure is used for all other values (a_14, a_24, ...)
The data gets large fast, another instant is a pentagon (5 points):
a_13_iter1 = a_13 + (a_12 + a_23 + a_15 + a_54 + a_43 + a_43 + a_25 + a_53 + a_14)% no path is chosen twice
I am looking for a code that can perform this operation if my original data is large (100 points). The code for 3 points is given as:
Does anyone know how I can write this code? Thank you!
Abolfazl Chaman Motlagh on 26 Feb 2022
You can solve this problem with your data as a matrix, or as a graph. the matrix make it easier i guess:
a traditional method with loop is kinnda this:
Connections = [0 0.2 0.6; -0.2 0 0.3; -0.6 -0.3 0]; % for 3 point problem
Path = zeros(3);
Path(i,j) = Connections(i,j); % First Term
Path(i,j) = Path(i,j) + Connections(i,k) + Connections(k,j);
you can see it is what you wanted.
of course inner loop can be vectorize to code below:
Path(i,j) = Connections(i,j) + sum(Connections(i,k)) + sum(Connections(k,j));
Path can also be initialize from Connections itself instead of zeros.
this can be your function for whatever velue and number of points you have.
function Path = Path_value_cal(Connections)
n = size(Connections);
Path = Connections;
Path(i,j) = Path(i,j) + sum(Connections(i,k)) + sum(Connections(k,j));
More Answers (0)
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!