Problems with my program to test if a graph is connected

Hello everyone,
I'm doing a project on topological robotics, and part of the program involves taking in the adjacency matrix of a graph, then testing to see if it's connected or not. The trouble is, I've tested it with several disconnected and connected graphs, and it says that they're all disconnected, no matter what I put in!
I would use "graphconncomp" but we don't have the Bioinformatics Toolbox I'm afraid.
Here's what I've got:
display(adjmatrix)
%Now we've got the adjacency matrix in, stored in 'adjmatrix,' we can do operations on it to check
%for connectedness. We do this by "removing" one vertex at a time.
connectedness=1;
%Define the "connectedness" variable. We'll set it as 1 by default, and set
%it to 0 later if we find the graph is not connected.
for column=1:vertices,
counter=0;
%The counter is used to check we don't have a row or column of all
%zeroes after each round of manipulations, which would indicate a
%disconnected graph.
for row1=1:vertices,
%What we do here is we start at one vertex. We check every other
%vertex to see if it's connected to it.
if adjmatrix(column,row1)>0
for row2=1:vertices,
%Don't make loops on the retracted graph.
if adjmatrix(column,row2)>0 && row1~=row2
%If it is, then we connect those vertices, since we
%retract the graph down.
adjmatrix(row1,row2)=1;
adjmatrix(row2,row1)=1;
else
end
end
else
%If we have a 0 in that space in the column, increment the
%counter.
counter=counter+1;
end
%After we've removed a vertex, we set its column and row in the
%adjacency matrix to 0.
adjmatrix(column,row1)=0;
adjmatrix(row1,column)=0;
end
%Here's where we check for connectedness. If we have a row or column
%with all zeroes, the graph is disconnected.
if counter==vertices
connectedness=0;
else
end
end
%Now we know if the graph's connected or not. So we display our finding to
%the user.
if connectedness==0
display('Sorry, your graph is not connected and you cannot move')
display('robots around on it. Come back with another graph.')
else
display('Your graph is connected and we can move robots around on it.')
display('Lets continue with the rest of the procedure.')
end
I suspect I've messed up somewhere in the nested loops, but I'm not sure what it is I'm doing wrong. Could you guys advise me please?
Thanks,
Emily

Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Asked:

on 24 Jan 2012

Community Treasure Hunt

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

Start Hunting!