-
Notifications
You must be signed in to change notification settings - Fork 0
/
switch_edge.m
67 lines (54 loc) · 1.95 KB
/
switch_edge.m
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
function E = switch_edge(E, EL, num_nodes)
% Switch edge connection
% Strategy: "Ta fra de rike og gi til de fattige"
over = []; % Nodes that can deposit one or more edges
under = []; % Nodes that can not deposit any edges
full = []; % Nodes that are connected to all other nodes
for i = 1:num_nodes
% Find node connections
neighbor_nodes = unpad(EL(:, i));
if length(neighbor_nodes) > 3
if length(neighbor_nodes) < (num_nodes-2)
over = [over, i]; % can deposit one edge
else
full = [full, i]; % is on full capacity
end
else
under = [under, i]; % has no edges to deposit
end
end
% Determine node to lose an edge: loser
if (isempty(under) || length(under)==1) && isempty(over)
return % Full capacity
elseif not(isempty(full))
loser = full(randi(length(full)));
else
loser = over(randi(length(over)));
over(over == loser) = [];
end
% Find loser-node's connected nodes
candidate_winners_ = unpad(EL(:, loser));
% Filter away nodes with full capacity
candidate_winners = setdiff(candidate_winners_, full);
% Find possible connections
idx=find(ismember(candidate_winners, [under, over]));
% Determine connection: winner
winner = candidate_winners(idx(randi(length(idx))));
x = (1:num_nodes);
y = [unpad(EL(:,winner)); winner];
z = setdiff(x,y); % Possible edges
if length(z) > 1
new_connection = z(randi(length(z)));
else
new_connection = z(1);
end
E = E.';
[r, p] = ismember([winner loser], E, 'rows');
[s, t] = ismember([loser winner], E, 'rows');
if r
E(p, :) = [winner new_connection];
elseif s
E(t, :) = [new_connection winner];
end
E = E.';
end