-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplyRule2.m
More file actions
66 lines (55 loc) · 1.67 KB
/
Copy pathapplyRule2.m
File metadata and controls
66 lines (55 loc) · 1.67 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function [A, nodes] = applyRule2(A, nodes)
N = size(A,1);
% Find all unused node (circle node)
circleNodeIndexes = [];
for i = 1:N
if isequal(nodes{i}.shape, 'o')
circleNodeIndexes = [circleNodeIndexes; i];
end
end
% Chose 1 random node
if length(circleNodeIndexes) <=0
return; % Exit if no circle nodes are found
end
newNodeIndex = circleNodeIndexes(randi(length(circleNodeIndexes)));
% Find all edge of A in formm of (nodea, nodeb)
edges = []; % Initialize empty list
for i = 1:N
for j = i+1:N % check only upper triangle
if A(i,j) ~= 0
edges = [edges; i, j];
end
end
end
% Chose a random edge
idx = randi(size(edges,1));
e = edges(idx, :);
a = e(1);
b = e(2);
% Find possible nodes C that have a path from a or b
neighborsA = find(A(a,:) == 1);
neighborsB = find(A(b,:) == 1);
possibleC = unique([neighborsA neighborsB]);
% Expand possibleC to include neighbors of neighbors
while true
[r, c] = find(A(possibleC, :) == 1); % r: row in submatrix, c: column in A
newNeighbors = unique(c); % column indices in A
newNeighbors = setdiff(newNeighbors, possibleC); % exclude already included nodes
if isempty(newNeighbors)
break;
end
possibleC = unique([possibleC(:); newNeighbors(:)]);
end
possibleC(possibleC == a | possibleC == b) = [];
if isempty(possibleC)
return; % cannot apply H2
end
c = possibleC(randi(length(possibleC)));
% Remove old edge
A(a,b) = 0; A(b,a) = 0;
% Add correct Henneberg II edges:
A(newNodeIndex, a) = 1; A(a, newNodeIndex) = 1;
A(newNodeIndex, b) = 1; A(b, newNodeIndex) = 1;
A(newNodeIndex, c) = 1; A(c, newNodeIndex) = 1;
nodes{newNodeIndex}.shape = 's';
end