0

I have a problem with the following definition

I have a course with some lessons

  1. What Java Is and How It Works
  2. Hello World Program
  3. Using Variables
  4. Strings: Working With Text
  5. While Loops
  6. For Loops
  7. "If"
  8. Getting User Input
  9. Do ... While
  10. Switch
  11. Arrays
  12. Arrays of Strings
  13. Multi-Dimensional Arrays
  14. Methods 15.Method Parameters
  15. Static
  16. Final
  17. String Builder and String Formatting

I have build a network with this course with matrix as following :
Node 1: first lesson
Node 2: second lesson
From Node 1 to Node 2 the edge will be 1 and to all other nodes will be 0
From Node 2 to Node 3 and from Node 2 to Node 4 the edge will be one and and to all other nodes it will be 0 because student can move from node 2 to 3 or 4

And so on with the same logic.

I got the following matrix:

D = [[0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1],
     [0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,1,1,0,1,1,1,1,0,0,0,0,0,0,1],
     [0,0,1,1,1,1,1,0,1,0,1,0,0,1,0,0,0,1],
     [0,0,0,0,1,1,1,0,0,0,1,0,0,1,0,0,0,0],
     [0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0],
     [0,0,0,0,1,1,1,0,1,0,0,1,1,1,0,0,0,0],
     [0,0,0,0,1,1,1,0,1,0,0,0,1,1,0,0,0,0],
     [0,0,0,0,1,1,1,0,1,0,0,0,0,1,0,0,0,0],
     [0,0,0,0,1,1,1,0,1,0,1,0,0,0,1,1,1,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0],
     [0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0]]  

and run the algorithm of salesperson the result is : [1, 3, 5, 15, 7, 10, 8, 9, 17, 18, 2, 4, 6, 16, 13, 11, 12, 14, 1]

I have tried to add -1 and to flip 0 with 1 and 1 with 0 and tried different matrix and still the data not near what the student should follow

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

1 Answers1

0

Your matrix only indicate the presence (or absence) of edges.

The TSP algorithm is made to work with weigthed edges. In your case all edges have the same wight, so you are not searching a shortest path but just any Hamiltonian path.

Plus, with 0 on edges that does not exist, it seems your algorithm belive it's free to use them (i.e. no cost to go from chapter 1 to chapter 3 instead of imposible). You don't want 0's and 1's in your matrix, but weigths (and infinite or really large weights for edges that does not exists).

mrBen
  • 276
  • 1
  • 5
  • 15