question on routing problem

1 次查看(过去 30 天)
jana
jana 2013-9-4
I am working on shortest path problem (dijiktra). My current code keeps track of one path from source to destination. I want to keep track of other paths if certain condition is satisfied. Any suggestions on how to keep track of other paths?
  4 个评论
jana
jana 2013-9-4
I am not sure how to include this into the code. I am fairly new to matlab.
jana
jana 2013-9-4
should I create a cell array for the distance matrix?

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2013-9-4
visited(1:n) = 0;
distance(1:n) = inf; % it stores the shortest distance between each node and the source node;
parent(1:n) = 0;
distance(s) = 0;
tracked_paths = {};
tracking_idx = 0;
for i = 1:(n-1),
temp = [];
for h = 1:n,
if visited(h) == 0 % in the tree;
temp=[temp distance(h)];
else
temp=[temp inf];
end
end;
[t, u] = min(temp); % it starts from node with the shortest distance to the source;
visited(u) = 1; % mark it as visited;
for v = 1:n, % for each neighbors of node u;
if ( ( netCostMatrix(u, v) + distance(u)) < distance(v) )
distance(v) = distance(u) + netCostMatrix(u, v); % update the shortest distance when a shorter path is found;
parent(v) = u; % update its parent;
else if netCostMatrix(u, v) + distance(u)) == distance(v)
# I want to keep track of this path and its distance.
tracking_idx = tracking_idx + 1;
tracking_paths{tracking_idx} = {distance(u), distance(v), parent};
end;
end;
end;

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by