0

I have an assignment where we're tackling the traveling salesman problem. I'm not going to lie, the part I'm doing right now I actually don't understand fully that they're asking, so sorry if I phrase this question weirdly. I sort of get it, but not fully.

We're calculating an approximate distance for the salesman. We need to create a two-dimensional array, of bitsets I believe? Storing the values in binary anyway. 0 represents that the city hasn't been visited, and 1 represents that is has been visited.

We've been given an algorithm that helps significantly, and I should be able to finish it if anyone here can help with the first step:

Create memoisation table [N][(1 << N)]

(where N = number of cities). I get that 1 << N means convert the number of cities (e.g. 5) to binary, then move the set to the left by one place.

My main issues are:

  1. Converting N to binary (I think this is what I need to do?)
  2. Moving the set to the left by one
  3. Actually creating the 2-dimensional array of these sizes...

I could be wrong here, in fact that's probably pretty likely... any help is appreciated, thanks!

Corey Thompson
  • 398
  • 6
  • 18

1 Answers1

0

Here is the general rule "<<" operator means left shift and ">>" means right shift. Right shifting any number by 1 is equivalent to divide by 2 and left shift any numbers by 2 is equivalent to multiply by 2. For example lets say a number 7 (Binary 111). So 7 << 1 will become 1110 which is 7 * 2 = 14 and 7 >> 1 will become 11 which is 7 / 2 = 3 .

So for algorithm to convert a number N to a bitset array as binary is

  1. N mod 2 (take the remainder if you divide N by 2)
  2. Store the remainder in a collection (i.e, List, Array, Stack )
  3. Divide N by 2
  4. If N/2 >1 Repeat from step 1 with N/2
  5. Else reverse the array and you have your bitset.

Moving the set left to one, If you meant leftshift by one you can do it by N<<1

This is how you create 2 dimensional array in C++

[Variable Type] TwoDimensionalArray[size][size];

For this problem though I believe you might want to read about C++ bitset and you can easily implement it using bitset. For that you just have to figure out the size of the bitset you want to use. For example if the highest value of N is 15 then you need a bitset size of 4. Because with 4 bit the maximum number you can represent is 15 (Binary 1111). Hope this helps.

Muhammad Raihan Muhaimin
  • 5,559
  • 7
  • 47
  • 68
  • Thanks - I asked a friend about Bitset because I heard it was better but we aren't allowed to use it apparently. Also I realised you can just write (1 << integerValue) in C++ which works. Thanks for your help! – Corey Thompson Jun 03 '13 at 04:48
  • Hopefully you see this - I have a side question sort of. I'm trying to declare my array in a header file so I can use it in multiple functions, but I've got to set a size I guess? `int table[][]` doesn't work because I've got to set a size for the second dimension at least apparently. I'm not sure?! – Corey Thompson Jun 03 '13 at 05:16
  • Yes you have to set a size before. For a 2 x 2 array you have to make it like int table[2][2] – Muhammad Raihan Muhaimin Jun 03 '13 at 07:12