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