-
Notifications
You must be signed in to change notification settings - Fork 31
/
distance_alg.R
75 lines (63 loc) · 2.05 KB
/
distance_alg.R
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
starters <- V(subgraph603)$name
for (i in 1:21) {
assign(paste0("table", i), starters[i])
priors <- table1
for (k in 1:i) {
priors <- c(priors, get(paste0("table", k)))
}
neigh <- neighbors(workfrance, starters[i])$name
neigh <- neigh[!(neigh %in% priors)]
if (length(neigh) > 0) {
assign(paste0("table", i), c(starters[i], neigh[1]))
# complete remainder of table
for (j in 3:10) {
# get most distant person
priors <- table1
for (k in 1:i) {
priors <- c(priors, get(paste0("table", k)))
}
table <- get(paste0("table", i))
opts <- distances(workfrance, v = table, weights = NA) |>
as.data.frame()
opts <- opts[ ,colnames(opts)[!(colnames(opts) %in% priors)]]
new <- opts |>
colMeans() |>
which.max() |>
names()
# if the person is not new, take them out of the graph and
# get another distant person
table[j] <- new
assign(paste0("table", i), table)
}
} else {
assign(paste0("table", i), starters[i])
# complete remainder of table
for (j in 2:10) {
priors <- table1
for (k in 1:i) {
priors <- c(priors, get(paste0("table", k)))
}
# get most distant person
table <- get(paste0("table", i))
priors <- table1
for (k in 1:i) {
priors <- c(priors, get(paste0("table", k)))
}
opts <- distances(workfrance, v = table, weights = NA) |>
as.data.frame()
opts <- opts[ ,colnames(opts)[!(colnames(opts) %in% priors)]]
new <- opts |>
colMeans() |>
which.max() |>
names()
# if the person is not new, take them out of the graph and
# get another distant person
table[j] <- new
assign(paste0("table", i), table)
}
}
}
for (i in 1:21) {
print(mean(distances(workfrance, v = get(paste0("table", i)), to = get(paste0("table", i)), weights = NA)))
}
for (i in 1:21) print(length(unique(get(paste0("table", i)))))