Hi,
I'm trying to implement a version of the travelling salesman problem in c using the follwoing algorithm:
********************************************************
**********************************************************
And here is what i have in c for the above:
**********************************************************
************************************************************
The procedure is called with "Algorithm(A)", where A is char A[] = "A";.
When the program reaches the recursive call (Algorithm(tempu) it does the call and when it has finished the call it doesn't finish the for loop. I mean it just does the next line, uv[j] = 'u';, and then exits, it doesn't go back and continue as it had only done j=2 in the for loop.
Can anyone spot where i've gone wrong?
Could this be due to no memory allocation using malloc() or calloc()?
Any help will be appreciated.
Thanks.
Arfan.
I'm trying to implement a version of the travelling salesman problem in c using the follwoing algorithm:
********************************************************
Code:
salesman (u)
begin
if every node is now visited
then
begin
put (u,start) in the circuit
print the circuit and its weight
remove (u,start) from the circuit
end
else
for every node v other than u
if v is not visited
then
begin
put (u,v) in the circui
set v to be visited
call salesman (v)
set v to be unvisited again
take (u,v) out of the circuit again
end
end
And here is what i have in c for the above:
**********************************************************
Code:
char v[6];
char tempu[3];
char uv[6] = "vuuuu";
char temp[3];
int j;
strcpy(v, "ABCDE");
void Algorithm(char u[2])
{
if (uv[0] == 'v' && uv[1] == 'v' && uv[2] == 'v' && uv[3] == 'v' && uv[4] == 'v')
{
strcpy(temp, u);
strcat(temp, A);
DrawPath(temp);
}
for (j=0; j<5; j++)
{
printf("\nj = %d", j);
if (v[j] == u[0])
{
// Do Nothing
}
else if (uv[j] == 'u')
{
strcpy(temp, u);
temp[1] = v[j];
DrawPath(temp);
uv[j] = 'v';
tempu[0] = v[j];
Algorithm(tempu);
uv[j] = 'u';
}
}
}
The procedure is called with "Algorithm(A)", where A is char A[] = "A";.
When the program reaches the recursive call (Algorithm(tempu) it does the call and when it has finished the call it doesn't finish the for loop. I mean it just does the next line, uv[j] = 'u';, and then exits, it doesn't go back and continue as it had only done j=2 in the for loop.
Can anyone spot where i've gone wrong?
Could this be due to no memory allocation using malloc() or calloc()?
Any help will be appreciated.
Thanks.
Arfan.