question on routing problem
조회 수: 1 (최근 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!