Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DesertFox

macrumors member
Original poster
Nov 25, 2004
72
0
Hello,

Im trying to solve the infamous Traveling Salesman problem for my java class.

Basically what im doing is looping through the columns and rows of a 2-dimensional array and then trying to store the smallest value in the row/column in a method that i can later print to screen.

Here is the portion of the loop that is passing the info to the distanceTraveled method to add to the total distance travaled:

if ( a[ row ][ column ] == min )
{
int tempDist= a[ row ][ column ];
distanceTraveled( tempDist );
return column;
}

And here is the distanceTraveled method:

public static int DistanceTraveled ( int distance )
{
int total =+ distance;
return total;
}

...both files are in the same class. I would like to have the DistanceTraveled method add up the distances, but this is not happening since it is expecting an argument and if I pass a 0 just to call it it will return a 0.

So, my question, is how do I implement a counter that adds up the distances so I can return the total distance later in my program.

Thanks in advance!
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Hi

I'm not entirely sure what your code snippet is trying to do but a couple of things struck me about your code.

Code:
if ( a[ row ][ column ] == min )

If you are looking for the column with the minimum value then you need something like this:-

Code:
int minColumn = 0 ;
int min = Integer.MAX_VALUE ;

for ( column = 0 ; < numberOfColumns ; ++ column )
    if ( a[ row ][ column ] < min )
    {   
       min = a[row][column] ;
       minColumn = column ;
     }

return minColumn ;


Also the way you are calling DistanceTraveled() discards the calculated result for the total distance.

So something like this perhaps?


Code:
int minColumn = 0 ;
int min = Integer.MAX_VALUE ;

for ( column = 0 ; < numberOfColumns ; ++ column )
    if ( a[ row ][ column ] < min )
    {   
       min = a[row][column] ;
       minColumn = column ;
     }

//
// Assuming totalDistance is declared somewhere outside this scope!
//
totalDistance = distanceTraveled( min ) ;

return minColumn ;

I hope this helps.

b e n
 

DesertFox

macrumors member
Original poster
Nov 25, 2004
72
0
Thanks for the reply.

My for loops are correct, i just didnt want to post 2 big nested for loops. My problem is taking the values in array[ row ][ column ] and sending them to a method that will update the total distance.

in the top i have:
public static void main( String args[] )
{
int array1[][] = { { 0, 10, 15, 5, 12 }, { 10, 0, 25, 16, 28 }, { 15, 25, 0, 6, 7 }, { 5, 16, 6, 0, 19}, { 12, 18, 7, 19, 0} };

int totalDistance = 0;

then in the for loop im valling the method DistanceTraveled by: (this is not the whole for loop, only the calling part since this is where its giving me errors.

int tempDist= a[ row ][ column ];
DistanceTraveled( tempDist );


and then the DistanceTraveled method:
public static void DistanceTraveled ( int distance )
{
totalDistance =+ distance;
}


and the error im getting:
Can not find symbol: Variable totalDistance.

If someone knows whats going wrong here, please help.
 

MrBubbles

macrumors member
Apr 8, 2003
32
0
Mad City
The issue is that totalDistance is declared in the method "main" which means it only exists within that method. Your "distanceTraveled" method is looking for the variable either within itself or within the class -- it can't see anything that exists solely within "main". The easiest solution would be to declare totalDistance as a private member of your class and initialize it to zero in a constructor (you can't do it from within "main" because it's an instance variable and "main" is static).

Edit: I was looking back over your original code and saw that everything was declared static, so just declare totalDistance as private static. Then you should be able to initialize it and access it from anywhere.
 

scan

macrumors 6502
Oct 24, 2005
344
0
Im trying to solve the infamous Traveling Salesman problem for my java class.

Could you explain this infamous problem. I've never heard of the Traveling Salesman before. I think I have an idea what you want, but just to be sure, explain the problem.
 

scan

macrumors 6502
Oct 24, 2005
344
0
So, my question, is how do I implement a counter that adds up the distances so I can return the total distance later in my program.

From reading this, I think you what you want is an object with a static variable (counter in this case) so when you create an instances only 1 counter will be shared between these objects.
 

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
I'm not sure why you're trying to solve the travelling salesman problem as a beginner problem. This is definitely a 4th year university problem.

But anyways, you don't want to be calculating the distance travelled on the stack, but should instead make another table, where each cell is the minimum cumulative distance travelled.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.