-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplyRule1.m
More file actions
44 lines (34 loc) · 895 Bytes
/
applyRule1.m
File metadata and controls
44 lines (34 loc) · 895 Bytes
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
function [A, nodes] = applyRule1(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 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 form 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);
A(newNodeIndex, a) = 1;
A(newNodeIndex, b) = 1;
A(a, newNodeIndex) = 1;
A(b, newNodeIndex) = 1;
nodes{newNodeIndex}.shape = 's'; % Update the shape of the new node
end