-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDT4120_Øving13.jl
55 lines (41 loc) · 1.52 KB
/
TDT4120_Øving13.jl
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
44
45
46
47
48
49
50
51
52
53
54
55
# Øving 13 TDT4120
function certifysubsetsum(set, subsetindices, _sum)
tot = 0
for index in subsetindices
tot += set[index]
end
return tot == _sum
end
certifysubsetsum2(set, subsetindices, _sum) = _sum == sum(si -> set[si], subsetindices)
function certifytsp(path, maxweight, neighbourmatrix)
if path[1] != path[end]
return false
elseif !allunique(path[1:end-1])
return false
elseif length(path) != size(neighbourmatrix, 1) + 1
return false
end
weight_sum = 0
for i in 2:length(path)
weight_sum += neighbourmatrix[path[i-1], path[i]]
end
return weight_sum <= maxweight
end
# Don't try this at home kids
certifytsp2(path, maxweight, neighbourmatrix) = (path[1] == path[end]) && allunique(path[1:end-1]) && (length(path) == size(neighbourmatrix, 1) + 1) && (sum(i -> neighbourmatrix[path[i-1], path[i]], 2:length(path)) <= maxweight)
function alldistinct(list)
sort!(list)
for i in 2:length(list)
if list[i-1] == list[i]
return false
end
end
return true
end
alldistinct2(list) = length(unique(list)) == length(list)
function cliquetovertexcover(neighbourmatrix, minimumcliquesize)
newneighbourmatrix = .!neighbourmatrix # Invert all bools (elementwise not)
maximumvertexcoversize = size(neighbourmatrix, 1) - minimumcliquesize
return newneighbourmatrix, maximumvertexcoversize
end
cliquetovertexcover2(neighbourmatrix, minimumcliquesize) = .!neighbourmatrix, size(neighbourmatrix, 1) - minimumcliquesize