Path: news.mathworks.com!not-for-mail
From: "Ongun Palaoglu" <ongun@mac.com>
Newsgroups: comp.soft-sys.matlab
Subject: Logic error in simple Dijkstra code
Date: Sat, 25 Apr 2009 05:44:01 +0000 (UTC)
Organization: Florida institute of Technolgy
Lines: 55
Message-ID: <gsu7v1$ko9$1@fred.mathworks.com>
Reply-To: "Ongun Palaoglu" <ongun@mac.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240638241 21257 172.30.248.35 (25 Apr 2009 05:44:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 25 Apr 2009 05:44:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1541435
Xref: news.mathworks.com comp.soft-sys.matlab:535356


NoOfNodes  = 5; %ask user the number of nodes
rand();
figure(1);
clf; %clear figure
hold on;
%rand generates random numers between [0 1]. to make it >1, Multply by
%50.
xaxis = rand(1,NoOfNodes)*100;
yaxis = rand(1,NoOfNodes)*100;
plot(xaxis,yaxis,'.'); grid on; hold on; 

for i = 1:NoOfNodes
    nodes{i}.x = xaxis(i);
    nodes{i}.y = yaxis(i);
    nodes{i}.visited = 0;
end
numNotVisitedNodes  = NoOfNodes - 1;
nodes{1}.visited = 0;
sourcenode  = nodes{1};
text(sourcenode.x,sourcenode.y, num2str('Starting Node'));
text(nodes{NoOfNodes}.x,nodes{NoOfNodes}.y, num2str('Ending Node'));
i = 1;
while (numNotVisitedNodes > 0)
    if (nodes{i}.visited == 0)
        distance(1:NoOfNodes) = 0;
        count = 1;
        for k = 1:NoOfNodes
            if (k ~= i)
                if ((nodes{k}.visited == 0) && (nodes{i}.visited == 0))
                    distance(count) = sqrt((((nodes{k}.x - nodes{i}.x)) ).^2 + (nodes{k}.y - nodes{i}.y).^2); % distances of the nodes
                end
                count = count + 1;
            end
        end
        [minDistance, nextNode] = min(distance(1:NoOfNodes-numNotVisitedNodes));
        nodes{i}.nextNode = nextNode;
        nodes{nextNode}.visited = 1;
        line([nodes{i}.x nodes{nodes{i}.nextNode}.x], [nodes{i}.y nodes{nodes{i}.nextNode}.y], 'Color','r','LineWidth',1); 
        hold on;
        text(nodes{nodes{i}.nextNode}.x,nodes{nodes{i}.nextNode}.y, num2str('i+1'));
        sourcenode = nodes{nodes{i}.nextNode};
        nodes{i}.visited = 1;
        i = nextNode;
        numNotVisitedNodes  = numNotVisitedNodes - 1;
    else
        i = i+1;
    end
end


Hello I wam workin on my project. there is a logic error on the
 [minDistance, nextNode] = min(distance(1:NoOfNodes-numNotVisitedNodes));

part of my code. it suppose to ignore the visited nodes. however it starts again from the very first node amd goes to second shortest node.  
Can somepne help me out for this part. thank you