question on routing problem
2 次查看(过去 30 天)
显示 更早的评论
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?
回答(1 个)
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;
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graph and Network Algorithms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!