I agree with Victor, in these circumstances you need to use the debugger to trace through the code - line by line if needed - to see what is happening and where the execution deviates from that expected from the program design.

I get a stack space overflow error message, and the program crashes
minCost() function is recursive. A stack space overflow error message usually indicates that there is a problem with the recursion terminating condition and that the terminating condition is not being met. In this case recursion only stops when tsp(city) returns 999 ??