-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteSuperfluousNodes_Fxn.m
424 lines (372 loc) · 27.1 KB
/
deleteSuperfluousNodes_Fxn.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
function[gEdgesMatrix,weightMatrix,numBondsMatrix] = ...
deleteSuperfluousNodes_Fxn(gEdgesMatrix,weightMatrix,numBondsMatrix)
%if a node is connected only by two single bonds (as long as they're not
%connected to the same node which I can't deal with as easily), I can delete
%it for the purposes of determining which net the graph is, because
%it means that the node was connected to another double bond before
%which got deleted. So I delete all nodes with weight and degree equal to 2
%(one by one, so as to not over-delete).
%Then, I need to make sure to transfer the edges from the nodes
%I delete to the nodes on either side.
%first we consider an edge case -- nodes which have four single bonds
%attached. There are two cases with these nodes -- 1) either all their
%neighbors are connected to one another (without them); or, 2) two of their
%neighbors are connected to each other, and the other two are as well. This
%is because if three of their neighbors are connected to each other and the
%other is not connected, then the bond between that neighbor and the node
%in question would have already been broken in order to make a disconnected
%graph. We are interested in case 2: in this case, we can delete the node,
%and make the graph disconnected.
%%
possibleNodesToDelete = find(sum(weightMatrix)==4 & sum(numBondsMatrix)==4)';
while ~isempty(possibleNodesToDelete)
for i = 1:length(possibleNodesToDelete)
node = possibleNodesToDelete(i);
hWeightMatrix = weightMatrix;
hWeightMatrix(node,:) = [];
hWeightMatrix(:,node) = [];
if max(myConnComp2(hWeightMatrix)) > max(myConnComp2(weightMatrix)) %then deleting that node disconnects the graph
%Delete this node. If the node is connected to nodes
%a, b, c, d, and when it's deleted the graph splits into a,b and c,d, then
%make a connection between a and b whose length is the sum of the lengths
%of the connections between a and node, and b and node (and same for c,d).
allConnectedNodes = find(weightMatrix(node,:)~=0)';
if length(allConnectedNodes)<3 %don't need to consider these cases because they're already handled by the
%``index'' part of the function in the for loop preceding
%the call of deleteSuperfluousNodes()
possibleNodesToDelete(i) = [];
break
end
if length(allConnectedNodes) == 3
%then it must be connected to one node twice (if it was
%connected to one node only once, and by removing it you
%disconnect the graph, then the removal of that edge would
%have disconnected the graph and we would have gotten rid
%of it previously)
%we first find which node that is, then take care of it,
%then take care of the other two.
for j = 1:3 %to find which node is connected twice
edgesOfNode = [allConnectedNodes(j) node];
%this line (below) is equivalent to: [~,index1] = ismember(edgesOfNode(1,:),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(1,:))'));
if ~isempty(index1) && gEdgesMatrix(index1,7) ~= 0 %singleBond2Length %then this is the node that's connected twice
break
end
%this line (below) is equivalent to: [~,index1] = ismember(fliplr(edgesOfNode(1,:)),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(1,:)))'));
if ~isempty(index1) && gEdgesMatrix(index1,7) ~= 0 %singleBond2Length %then this is the node that's connected twice
break
end
if j == 3 %if we've reached this point at j ==3 then our assumptions are wrong
disp('our assumptions are wrong')
end
end
connectedNodes = allConnectedNodes(j); %this is the node that's connected twice
edgeToAddLength = gEdgesMatrix(index1,6) + gEdgesMatrix(index1,7)... %singleBond1Length + singleBond2Length
+ gEdgesMatrix(index1,8) + gEdgesMatrix(index1,9); %singleBond3Length + singleBond4Length
if ~any(all((gEdgesMatrix(:,1:2) == [connectedNodes connectedNodes])'))
%this line (above) is equivalent to: ~ismember([connectedNodes connectedNodes], gEdgesMatrix(:,1:2),'rows')
edgeToAdd = [connectedNodes, connectedNodes, 1, 1, 0, edgeToAddLength, 0, 0, 0];
gEdgesMatrix = sortrows([gEdgesMatrix;edgeToAdd]);
weightMatrix(connectedNodes,connectedNodes) = 1;
numBondsMatrix(connectedNodes,connectedNodes) = 1;
else
%this line (below) is equivalent to: [~,index1] = ismember([connectedNodes connectedNodes],gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == [connectedNodes connectedNodes])'));
gEdgesMatrix(index1,3) = gEdgesMatrix(index1,3)+1; %update weight by adding 1
gEdgesMatrix(index1,4) = gEdgesMatrix(index1,4)+1; %update numBonds by adding 1
weightMatrix(connectedNodes,connectedNodes) = weightMatrix(connectedNodes,connectedNodes) + 1;
numBondsMatrix(connectedNodes,connectedNodes) = numBondsMatrix(connectedNodes,connectedNodes) + 1;
if gEdgesMatrix(index1,6) == 0 %singleBond1Length
gEdgesMatrix(index1,6) = edgeToAddLength;
elseif gEdgesMatrix(index1,7) == 0 %singleBond2Length
gEdgesMatrix(index1,7) = edgeToAddLength;
elseif gEdgesMatrix(index1,8) == 0 %singleBond3Length
gEdgesMatrix(index1,8) = edgeToAddLength;
elseif gEdgesMatrix(index1,9) == 0 %singleBond4Length
gEdgesMatrix(index1,9) = edgeToAddLength;
else
disp('something went strangely wrong')
end
end
allConnectedNodes(j) = []; %so now allConnectedNodes is the list of the other two singly connected nodes.
connectedNodes = allConnectedNodes;
edgesOfNode = [connectedNodes, [node; node]];
%figure out how long (how many ntds) the bond we're adding is.
%this line (below) is equivalent to: [~,index1] = ismember(edgesOfNode(1,:),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(1,:))'));
if isempty(index1) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index1] = ismember(fliplr(edgesOfNode(1,:)),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(1,:)))'));
end
%this line (below) is equivalent to: [~,index2] = ismember(edgesOfNode(2,:),gEdgesMatrix(:,1:2),'rows');
index2 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(2,:))'));
if isempty(index2) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index2] = ismember(fliplr(edgesOfNode(2,:)),gEdgesMatrix(:,1:2),'rows');
index2 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(2,:)))'));
end
edgeToAddLength = gEdgesMatrix(index1,6) + gEdgesMatrix(index2,6); %singleBond1Length + singleBond1Length
%make edge between connectedNodes, if an edge doesn't already exist
%this line (below) is equivalent to: [edgeExists,edgeIndex] = ismember(connectedNodes',gEdgesMatrix(:,1:2),'rows');
edgeIndex = find(all((gEdgesMatrix(:,1:2) == connectedNodes')'));
edgeExists = ~isempty(edgeIndex);
if ~edgeExists %make sure edge doesn't already exist or else program will produce an error message
edgeToAdd = [connectedNodes', 1, 1, 0, edgeToAddLength, 0, 0, 0];
gEdgesMatrix = sortrows([gEdgesMatrix;edgeToAdd]);
weightMatrix(connectedNodes(1),connectedNodes(2)) = weightMatrix(connectedNodes(1),connectedNodes(2)) + 1;
weightMatrix(connectedNodes(2),connectedNodes(1)) = weightMatrix(connectedNodes(2),connectedNodes(1)) + 1;
numBondsMatrix(connectedNodes(1),connectedNodes(2)) = numBondsMatrix(connectedNodes(1),connectedNodes(2)) + 1;
numBondsMatrix(connectedNodes(2),connectedNodes(1)) = numBondsMatrix(connectedNodes(2),connectedNodes(1)) + 1;
else %if edge does already exist, then modify it so numBonds = numBonds + 1, and weight = weight+1, and add a single bond with the proper length.
gEdgesMatrix(edgeIndex,3) = gEdgesMatrix(edgeIndex,3)+1; %update weight by adding 1
gEdgesMatrix(edgeIndex,4) = gEdgesMatrix(edgeIndex,4)+1; %update numBonds by adding 1
weightMatrix(connectedNodes(1),connectedNodes(2)) = weightMatrix(connectedNodes(1),connectedNodes(2)) + 1;
weightMatrix(connectedNodes(2),connectedNodes(1)) = weightMatrix(connectedNodes(2),connectedNodes(1)) + 1;
numBondsMatrix(connectedNodes(1),connectedNodes(2)) = numBondsMatrix(connectedNodes(1),connectedNodes(2)) + 1;
numBondsMatrix(connectedNodes(2),connectedNodes(1)) = numBondsMatrix(connectedNodes(2),connectedNodes(1)) + 1;
if gEdgesMatrix(edgeIndex,6) == 0 %singleBond1Length
gEdgesMatrix(edgeIndex,6) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,7) == 0 %singleBond2Length
gEdgesMatrix(edgeIndex,7) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,8) == 0 %singleBond3Length
gEdgesMatrix(edgeIndex,8) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,9) == 0 %singleBond4Length
gEdgesMatrix(edgeIndex,9) = edgeToAddLength;
else
disp('something is going on')
end
end
elseif length(allConnectedNodes) == 4
%find indices of connectedNodes in the graph h, which doesn't
%have the node ``node''. We'll use that to find out how the
%graph disconnects with the removal of ``node''.
connectedNodesH = allConnectedNodes;
connectedNodesH(connectedNodesH>node) = connectedNodesH(connectedNodesH>node) - 1;
conncompH = myConnComp2(hWeightMatrix);
aa = conncompH(connectedNodesH(1));
bb = conncompH(connectedNodesH(2));
cc = conncompH(connectedNodesH(3));
dd = conncompH(connectedNodesH(4));
%rearrange order in connectedNodes (which are the real values of the nodes) so that aa == bb
if aa == cc
allConnectedNodes = [allConnectedNodes(1), allConnectedNodes(3), allConnectedNodes(2), allConnectedNodes(4)]';
elseif aa == dd
allConnectedNodes = [allConnectedNodes(1), allConnectedNodes(4), allConnectedNodes(2), allConnectedNodes(3)]';
end
for j = [1,3]
connectedNodes = allConnectedNodes(j:j+1);
edgesOfNode = [connectedNodes, [node; node]];
%figure out how long (how many ntds) the bond we're adding is.
%this line (below) is equivalent to: [~,index1] = ismember(edgesOfNode(1,:),gEdgesMatrix(:,1:2),'rows'); %EndNodes
index1 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(1,:))'));
if isempty(index1) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index1] = ismember(fliplr(edgesOfNode(1,:)),gEdgesMatrix(:,1:2),'rows'); %EndNodes
index1 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(1,:)))'));
end
%this line (below) is equivalent to: [~,index2] = ismember(edgesOfNode(2,:),gEdgesMatrix(:,1:2),'rows'); %EndNodes
index2 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(2,:))'));
if isempty(index2) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index2] = ismember(fliplr(edgesOfNode(2,:)),gEdgesMatrix(:,1:2),'rows');
index2 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(2,:)))'));
end
edgeToAddLength = gEdgesMatrix(index1,6) + gEdgesMatrix(index2,6); %singleBond1Length + singleBond1Length
%make edge between connectedNodes, if an edge doesn't already exist
%this line (below) is equivalent to: [edgeExists,edgeIndex] = ismember(connectedNodes',gEdgesMatrix(:,1:2),'rows');
edgeIndex = find(all((gEdgesMatrix(:,1:2) == connectedNodes')'));
edgeExists = ~isempty(edgeIndex);
if ~edgeExists %make sure edge doesn't already exist or else program will produce an error message
edgeToAdd = [connectedNodes', 1, 1, 0, edgeToAddLength, 0, 0, 0];
gEdgesMatrix = sortrows([gEdgesMatrix;edgeToAdd]);
weightMatrix(connectedNodes(1),connectedNodes(2)) = weightMatrix(connectedNodes(1),connectedNodes(2)) + 1;
weightMatrix(connectedNodes(2),connectedNodes(1)) = weightMatrix(connectedNodes(2),connectedNodes(1)) + 1;
numBondsMatrix(connectedNodes(1),connectedNodes(2)) = numBondsMatrix(connectedNodes(1),connectedNodes(2)) + 1;
numBondsMatrix(connectedNodes(2),connectedNodes(1)) = numBondsMatrix(connectedNodes(2),connectedNodes(1)) + 1;
else %if edge does already exist, then modify it so numBonds = numBonds + 1, and weight = weight+1, and add a single bond with the proper length.
gEdgesMatrix(edgeIndex,3) = gEdgesMatrix(edgeIndex,3)+1; %update weight by adding 1
gEdgesMatrix(edgeIndex,4) = gEdgesMatrix(edgeIndex,4)+1; %update numBonds by adding 1
weightMatrix(connectedNodes(1),connectedNodes(2)) = weightMatrix(connectedNodes(1),connectedNodes(2)) + 1;
weightMatrix(connectedNodes(2),connectedNodes(1)) = weightMatrix(connectedNodes(2),connectedNodes(1)) + 1;
numBondsMatrix(connectedNodes(1),connectedNodes(2)) = numBondsMatrix(connectedNodes(1),connectedNodes(2)) + 1;
numBondsMatrix(connectedNodes(2),connectedNodes(1)) = numBondsMatrix(connectedNodes(2),connectedNodes(1)) + 1;
if gEdgesMatrix(edgeIndex,6) == 0 %singleBond1Length
gEdgesMatrix(edgeIndex,6) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,7) == 0 %singleBond2Length
gEdgesMatrix(edgeIndex,7) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,8) == 0 %singleBond3Length
gEdgesMatrix(edgeIndex,8) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,9) == 0 %singleBond4Length
gEdgesMatrix(edgeIndex,9) = edgeToAddLength;
else
disp('something is going on')
end
end
end
else
disp('huh thats a mistake')
end
%find edges that include node node and delete them.
gEdgesMatrix(gEdgesMatrix(:,1)==node | gEdgesMatrix(:,2)==node ,:) = [];
%After deleting these edges, we need to update the names of the
%nodes.
gEdgesMatrix(gEdgesMatrix(:,1)>node,1) = ...
gEdgesMatrix(gEdgesMatrix(:,1)>node,1) - 1;
gEdgesMatrix(gEdgesMatrix(:,2)>node,2) = ...
gEdgesMatrix(gEdgesMatrix(:,2)>node,2) - 1;
weightMatrix(:,node) = [];
weightMatrix(node,:) = [];
numBondsMatrix(:,node) = [];
numBondsMatrix(node,:) = [];
possibleNodesToDelete = find(sum(weightMatrix)==4 & sum(numBondsMatrix)==4)';
break %don't delete any more nodes before checking if we still need to after having deleted this one.
else
possibleNodesToDelete(i) = [];
break %because the index i needs to be changed.
end
end
end
%We don't need to consider the other edge case -- nodes which have three
%single bonds attached. There are two cases with these nodes -- 1) either
%all their neighbors are connected to one another (without them) (in which
%case we don't want to delete them); or, 2) by deleting the node, we can
%make the graph disconnected. There are three possibilities for case 2:
%Either the node in question is connected to two nodes, and to one of them
%twice -- in that case, the free single edge would have already been deleted;
%or it's connected to three nodes, two of which are connected to one another
%-- also in this case the free single edge would have been deleted; or it's
%connected to one node with three single bonds, in which case, we don't want to delete it.
%%
%now do the main thing which is deleting nodes whose only connections are
%two single bonds (but which aren't connected to themselves because those are
%closed nets 0).
nodesToDelete = find(sum(weightMatrix)==2 & sum(numBondsMatrix)==2)';
%note that the indicies of these will change as we delete nodes.
i = 1; %don't include nodes that are connected to themselves.
%If we don't include this, nodes with two single bonds connecting them to themselves will be in nodesToDelete
while i <= length(nodesToDelete)
if weightMatrix(nodesToDelete(i),nodesToDelete(i))~=0
nodesToDelete(i) = [];
else
i = i+1;
end
end
while ~isempty(nodesToDelete) %delete nodes one by one so that you don't accidentally delete all three nodes in a cycle, for example.
node = nodesToDelete(1); %make sure not to call it nodes to distinguish it from the vector of nodes
connectedNodes = find(weightMatrix(node,:)~=0)';
%find all nodes within a distance 1 of the given node,
%treating all weights as 1 (so we don't have to separately deal with the times when numBonds = Weight = 2)
if isempty(connectedNodes) %then we have a node connected to itself
%we know that the node is a closed net zero so we might as well
%compute its entropy and delete it
nodesToDelete(1)=[];
elseif length(connectedNodes)==1 %then we have two nodes connected to each other with two single bonds
edgesOfNode = [node, connectedNodes];
%this line (below) is equivalent to: [~,index1] = ismember(edgesOfNode(1,:),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(1,:))'));
if isempty(index1) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index1] = ismember(fliplr(edgesOfNode(1,:)),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(1,:)))'));
end
edgeToAddLength = gEdgesMatrix(index1,6) + gEdgesMatrix(index1,7)... %singleBond1Length + singleBond2Length
+ gEdgesMatrix(index1,8) + gEdgesMatrix(index1,9); %singleBond3Length + singleBond4Length
if ~any(all((gEdgesMatrix(:,1:2) == [connectedNodes connectedNodes])'))
%this line (above) is equivalent to: ~ismember([connectedNodes connectedNodes], gEdgesMatrix(:,1:2),'rows')
edgeToAdd = [connectedNodes, connectedNodes, 1, 1, 0, edgeToAddLength, 0, 0, 0];%note that connectedNodes has length 1
gEdgesMatrix = sortrows([gEdgesMatrix;edgeToAdd]);
weightMatrix(connectedNodes,connectedNodes) = weightMatrix(connectedNodes,connectedNodes) + 1;
numBondsMatrix(connectedNodes,connectedNodes) = numBondsMatrix(connectedNodes,connectedNodes) + 1;
else
%this line (below) is equivalent to: [~,index1] = ismember([connectedNodes connectedNodes],gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == [connectedNodes connectedNodes])'));
gEdgesMatrix(index1,3) = gEdgesMatrix(index1,3)+1; %update weight by adding 1
gEdgesMatrix(index1,4) = gEdgesMatrix(index1,4)+1; %update numBonds by adding 1
weightMatrix(connectedNodes,connectedNodes) = weightMatrix(connectedNodes,connectedNodes) + 1;
numBondsMatrix(connectedNodes,connectedNodes) = numBondsMatrix(connectedNodes,connectedNodes) + 1;
if gEdgesMatrix(index1,6) == 0 %singleBond1Length
gEdgesMatrix(index1,6) = edgeToAddLength;
elseif gEdgesMatrix(index1,7) == 0 %singleBond2Length
gEdgesMatrix(index1,7) = edgeToAddLength;
elseif gEdgesMatrix(index1,8) == 0 %singleBond3Length
gEdgesMatrix(index1,8) = edgeToAddLength;
elseif gEdgesMatrix(index1,9) == 0 %singleBond4Length
gEdgesMatrix(index1,9) = edgeToAddLength;
else
disp('something went strangely wrong')
end
end
%delete edges which include node node
gEdgesMatrix(gEdgesMatrix(:,1)==node | gEdgesMatrix(:,2)==node ,:) = [];
%After deleting these edges, we need to update the names of the
%nodes.
gEdgesMatrix(gEdgesMatrix(:,1)>node,1) = gEdgesMatrix(gEdgesMatrix(:,1)>node,1) - 1;
gEdgesMatrix(gEdgesMatrix(:,2)>node,2) = gEdgesMatrix(gEdgesMatrix(:,2)>node,2) - 1;
weightMatrix(:,node) = [];
weightMatrix(node,:) = [];
numBondsMatrix(:,node) = [];
numBondsMatrix(node,:) = [];
%take connectedNodes off of list of nodesToDelete
nodesToDelete(nodesToDelete==connectedNodes)=[];
nodesToDelete = nodesToDelete-1;
nodesToDelete(1)=[];
elseif length(connectedNodes) == 2
edgesOfNode = [connectedNodes, [node; node]];
%figure out how long (how many ntds) the bond we're adding is.
%this line (below) is equivalent to: [~,index1] = ismember(edgesOfNode(1,:),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(1,:))'));
if isempty(index1) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index1] = ismember(fliplr(edgesOfNode(1,:)),gEdgesMatrix(:,1:2),'rows');
index1 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(1,:)))'));
end
%this line (below) is equivalent to: [~,index2] = ismember(edgesOfNode(2,:),gEdgesMatrix(:,1:2),'rows');
index2 = find(all((gEdgesMatrix(:,1:2) == edgesOfNode(2,:))'));
if isempty(index2) %it's because we need to switch the order of the edges in edgesOfNode(:,1)
%this line (below) is equivalent to: [~,index2] = ismember(fliplr(edgesOfNode(2,:)),gEdgesMatrix(:,1:2),'rows');
index2 = find(all((gEdgesMatrix(:,1:2) == fliplr(edgesOfNode(2,:)))'));
end
edgeToAddLength = gEdgesMatrix(index1,6) + gEdgesMatrix(index2,6); %singleBond1Length + singleBond1Length
%make edge between connectedNodes, if an edge doesn't
%already exist
%this line (below) is equivalent to: [edgeExists,edgeIndex] = ismember(connectedNodes',gEdgesMatrix(:,1:2),'rows');
edgeIndex = find(all((gEdgesMatrix(:,1:2) == connectedNodes')'));
edgeExists = ~isempty(edgeIndex);
if ~edgeExists %make sure edge doesn't already exist or else program will produce an error message
edgeToAdd = [connectedNodes', 1, 1, 0, edgeToAddLength, 0, 0, 0];
gEdgesMatrix = sortrows([gEdgesMatrix;edgeToAdd]);
weightMatrix(connectedNodes(1),connectedNodes(2)) = weightMatrix(connectedNodes(1),connectedNodes(2)) + 1;
weightMatrix(connectedNodes(2),connectedNodes(1)) = weightMatrix(connectedNodes(2),connectedNodes(1)) + 1;
numBondsMatrix(connectedNodes(1),connectedNodes(2)) = numBondsMatrix(connectedNodes(1),connectedNodes(2)) + 1;
numBondsMatrix(connectedNodes(2),connectedNodes(1)) = numBondsMatrix(connectedNodes(2),connectedNodes(1)) + 1;
else %if edge does already exist, then modify it so numBonds = numBonds + 1, and weight = weight+1, and add a single bond with the proper length.
gEdgesMatrix(edgeIndex,3) = gEdgesMatrix(edgeIndex,3)+1; %update weight by adding 1
gEdgesMatrix(edgeIndex,4) = gEdgesMatrix(edgeIndex,4)+1; %update numBonds by adding 1
weightMatrix(connectedNodes(1),connectedNodes(2)) = weightMatrix(connectedNodes(1),connectedNodes(2)) + 1;
weightMatrix(connectedNodes(2),connectedNodes(1)) = weightMatrix(connectedNodes(2),connectedNodes(1)) + 1;
numBondsMatrix(connectedNodes(1),connectedNodes(2)) = numBondsMatrix(connectedNodes(1),connectedNodes(2)) + 1;
numBondsMatrix(connectedNodes(2),connectedNodes(1)) = numBondsMatrix(connectedNodes(2),connectedNodes(1)) + 1;
if gEdgesMatrix(edgeIndex,6) == 0 %singleBond1Length
gEdgesMatrix(edgeIndex,6) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,7) == 0 %singleBond2Length
gEdgesMatrix(edgeIndex,7) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,8) == 0 %singleBond3Length
gEdgesMatrix(edgeIndex,8) = edgeToAddLength;
elseif gEdgesMatrix(edgeIndex,9) == 0 %singleBond4Length
gEdgesMatrix(edgeIndex,9) = edgeToAddLength;
else
disp('something is going on')
end
end
%delete edges which include node node
gEdgesMatrix(gEdgesMatrix(:,1)==node | gEdgesMatrix(:,2)==node ,:) = [];
%After deleting these edges, we need to update the names of the
%nodes.
gEdgesMatrix(gEdgesMatrix(:,1)>node,1) = gEdgesMatrix(gEdgesMatrix(:,1)>node,1) - 1;
gEdgesMatrix(gEdgesMatrix(:,2)>node,2) = gEdgesMatrix(gEdgesMatrix(:,2)>node,2) - 1;
weightMatrix(:,node) = [];
weightMatrix(node,:) = [];
numBondsMatrix(:,node) = [];
numBondsMatrix(node,:) = [];
nodesToDelete = nodesToDelete-1;
nodesToDelete(1)=[];
else
disp('not sure why we have more than two connected nodes')
end
end