-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSProgram.swift
More file actions
46 lines (35 loc) · 1.34 KB
/
TSProgram.swift
File metadata and controls
46 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//Traveling salesman problem
import Foundation
let distanceMatrix = [[0,2,9,10],[1,0,6,4],[15,7,0,8],[6,3,12,0]]
var citiesArray = [0,1,2,3]
var permutationArray : [[Int]] = []
func permutation(_ startingPosition : Int,_ lastPosition: Int) {
if startingPosition == lastPosition {
permutationArray.append(citiesArray)
}
else {
for index in startingPosition...lastPosition {
citiesArray.swapAt(startingPosition,index)
permutation(startingPosition + 1,lastPosition)
citiesArray.swapAt(startingPosition, index)
}
}
}
var costOfThePath = Int.max , minimumPathIndex = -1
func calculateMinimumCost(_ individualPaths: [Int], _ positionOfArray: Int) {
var calculatedCost = 0
for eachCity in 0..<individualPaths.count - 1 {
calculatedCost += distanceMatrix[individualPaths[eachCity]][individualPaths[eachCity + 1]]
}
calculatedCost += distanceMatrix[individualPaths[individualPaths.count - 1]][0]
if costOfThePath > calculatedCost {
costOfThePath = calculatedCost
minimumPathIndex = positionOfArray
}
}
permutation(1,citiesArray.count - 1)
for eachPath in 0..<permutationArray.count {
calculateMinimumCost(permutationArray[eachPath],eachPath)
}
print(permutationArray[minimumPathIndex].map{$0 + 1})
print(costOfThePath)