6

I'm teaching myself how to program algorithms involving TSPs (Djikstra, Kruskal) and I'm looking for some start up advice. I am working with C# and SQL. Ideally I'd like to be able to do this strictly in SQL however I'm not sure if that is possible (I assume the runtime would be awful after 50 vertices).

So I guess the question is, can I do this is only SQL and if so what is the best approach? If not, and I have to get C# involved what would be the best approach there?

Hans
  • 12,902
  • 2
  • 57
  • 60
nikolifish
  • 503
  • 1
  • 5
  • 18

3 Answers3

6

It is only advisable to do simple calculations in SQL, like calculating sums. Sums are faster in SQL, because only sums are returned instead of all the records. Complicated algorithms like the ones you have in mind must be done in your c# code! First, the SQL language is not suited for such problems, second it is optimized for db accesses, making it very slow for other types of uses.

Read your data from your db with SQL into an appropriate data structure into your c# program. Do all the TSP related logic there and, if you want, store the result in the db, when finished.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

Well, I am not sure if SQL is the best option to accomplish this, but you could try using an adjacency matrix for the input. Many published algorithms are designed for this kind of input, and after that the only issue is putting the pseudocode into C#. Take a look that this: http://en.wikipedia.org/wiki/Adjacency_matrix.

You would be using a two dimensional array to represent the matrix.

Tim
  • 1,334
  • 2
  • 15
  • 29
1

i'm going to chime in for SQL. While it would not really be my first choice to work on TSP - it still can easily do this kind of thing - assuming of course that the data model is optimal for your efforts.

The first chore will be to define a data model that holds the information your algorithm needs, then populate with some sample data, then work out a query that can retrieve the arrays as needed.

finally you can decide if some simple SQL in that query would work for you, or perhaps an extension in the form of a stored procedure.

finally, you may opt to pull it out to your alternate language of choice.

Randy
  • 16,480
  • 1
  • 37
  • 55