How can i do that list for dijkstra algorithm?

9 views (last 30 days)
Hello, I have to optmizate a route using dijkstra algorithm. I'll import data from an excel archive like the following image (but with more data):
ID is a location and Value the distance between locations. From 1 to 2 the distance is 3, from 5 to 6 the distance is 4 and from 4 to 7 the distance its 1
I have to transform this data into a matrix to use in the dijkstra algorithm like this:
W = [3 3 4 5 4 1 2 1 1 3 1 4 2 1 2 1 3];
A = [3 1 1 1 1 2 3 4 5 6 7 8 2 3 4 6 7];
B = [6 2 3 4 5 6 7 7 8 9 9 9 3 4 5 7 8];
Example for the image:
W: [3 4 3 4 3 2 2 3 4 1 3 2] - Distances (Using Values data)
A: [1 2 4 5 7 8 1 2 3 4 5 6] - Original Location (Using ID in from left to right to the first 6 values and then up to down to the following 6 values)
B: [2 3 5 6 8 9 4 5 6 7 8 9] - Destinated Location (Using ID in from left to right to the first 6 values and then up to down to the following 6 values)
So, using the first column we have: Distance 3, from 1 to 2. Second column: Distance 4, from 2 to 3...
How can I automate that?

Answers (1)

Ameer Hamza
Ameer Hamza on 31 Oct 2020
Edited: Ameer Hamza on 31 Oct 2020
You can use MATLAB's graph() object to do such a thing
W = [3 4 3 4 3 2 2 3 4 1 3 2];
A = [1 2 4 5 7 8 1 2 3 4 5 6];
B = [2 3 5 6 8 9 4 5 6 7 8 9];
G = graph(A, B, W);
Plot the graph()
plot(G)
Find the shortest path
shortestpath(G, 1, 9) % shortest path between 1 and 9
  2 Comments
Neilton Felipe Santos
Neilton Felipe Santos on 31 Oct 2020
Thanks, but my doubt is about how do create a code that returns W, A and B based in an excel file like the example
Ameer Hamza
Ameer Hamza on 1 Nov 2020
Is your matrix always 3x3 or it can be of any size?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!