forked from jonalv/cdk
/
AllShortestPaths.java
553 lines (406 loc) · 14.9 KB
/
AllShortestPaths.java
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright (C) 2012 John May <jwmay@users.sf.net>
*
* Contact: cdk-devel@lists.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* All we ask is that proper credit is given for our work, which includes
* - but is not limited to - adding the above copyright notice to the beginning
* of your source code files, and to any copyright notice that you may distribute
* with programs based on this work.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.graph;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IChemObjectChangeEvent;
import org.openscience.cdk.interfaces.IChemObjectListener;
import org.openscience.cdk.interfaces.IElectronContainer;
import org.openscience.cdk.interfaces.ILonePair;
import org.openscience.cdk.interfaces.ISingleElectron;
import org.openscience.cdk.interfaces.IStereoElement;
import java.util.List;
import java.util.Map;
/**
* Determine the shortest paths from all atoms in a molecule.
*
*
* <pre>{@code
* IAtomContainer benzene = MoleculeFactory.makeBenzene();
* AllShortestPaths shortestPaths = new AllShortestPaths(benzene);
*
* for (int i = 0; i < benzene.getAtomCount(); i++) {
*
* // only to half the comparisons, we can reverse the
* // path[] to get all j to i
* for (int j = i + 1; j < benzene.getAtomCount(); j++) {
*
* // compute shortest path from i to j
* int[] path = shortestPaths.from(i).pathTo(j);
*
* // compute all shortest paths from i to j
* int[][] paths = shortestPaths.from(i).pathsTo(j);
*
* // compute the atoms in the path from i to j
* IAtom[] atoms = shortestPaths.from(i).atomsTo(j);
*
* // access the number of paths from i to j
* int nPaths = shortestPaths.from(i).nPathsTo(j);
*
* // access the distance from i to j
* int distance = shortestPaths.from(i).nPathsTo(j);
*
* }
* }
* }</pre>
*
* @author John May
* @cdk.module core
* @see ShortestPaths
*/
@TestClass("org.openscience.cdk.graph.AllShortestPathsTest")
public final class AllShortestPaths {
private final IAtomContainer container;
private final ShortestPaths[] shortestPaths;
@TestMethod("testConstruction_Null,testConstruction_Empty")
public AllShortestPaths(IAtomContainer container) {
// toAdjList performs null check
int[][] adjacent = ShortestPaths.toAdjList(container);
int n = container.getAtomCount();
this.container = container;
this.shortestPaths = new ShortestPaths[n];
// for each atom construct the ShortestPaths object
for (int i = 0; i < n; i++) {
shortestPaths[i] = new ShortestPaths(adjacent, container, i);
}
}
/**
* Access the shortest paths object for provided start vertex.
*
* <pre>{@code
* AllShortestPaths asp = ...;
*
* // access explicitly
* ShortestPaths sp = asp.from(0);
*
* // or chain method calls
* int[] path = asp.from(0).pathTo(5);
* }</pre>
*
* @param start the start vertex of the path
* @return The shortest paths from the given state vertex
* @see ShortestPaths
*/
@TestMethod("testFrom_Int_Benzene")
public ShortestPaths from(int start) {
return (start < 0 || start >= shortestPaths.length) ? EMPTY_SHORTEST_PATHS : shortestPaths[start];
}
/**
* Access the shortest paths object for provided start atom.
*
* <pre>{@code
* AllShortestPaths asp = ...;
* IAtom start, end = ...;
*
* // access explicitly
* ShortestPaths sp = asp.from(start);
*
* // or chain the method calls together
*
* // first path from start to end atom
* int[] path = asp.from(start).pathTo(end);
*
* // first atom path from start to end atom
* IAtom[] atoms = asp.from(start).atomTo(end);
*
* }</pre>
*
* @param start the start atom of the path
* @return The shortest paths from the given state vertex
* @see ShortestPaths
*/
@TestMethod("testFrom_Atom_Benzene")
public ShortestPaths from(IAtom start) {
// currently container.getAtomNumber() return -1 when null
return from(container.getAtomNumber(start));
}
/**
* an empty atom container so we can handle invalid vertices/atoms better. Note
* very pretty but we can't access the domain model from cdk-core.
*/
private static final IAtomContainer EMPTY_CONTAINER = new IAtomContainer() {
public void addStereoElement(IStereoElement element) {
}
public void setStereoElements(List<IStereoElement> elements) {
}
public Iterable<IStereoElement> stereoElements() {
throw new UnsupportedOperationException("not supported");
}
public void setAtoms(IAtom[] atoms) {
}
public void setBonds(IBond[] bonds) {
}
public void setAtom(int number, IAtom atom) {
}
public IAtom getAtom(int number) {
throw new UnsupportedOperationException("not supported");
}
public IBond getBond(int number) {
throw new UnsupportedOperationException("not supported");
}
public ILonePair getLonePair(int number) {
throw new UnsupportedOperationException("not supported");
}
public ISingleElectron getSingleElectron(int number) {
throw new UnsupportedOperationException("not supported");
}
public Iterable<IAtom> atoms() {
throw new UnsupportedOperationException("not supported");
}
public Iterable<IBond> bonds() {
throw new UnsupportedOperationException("not supported");
}
public Iterable<ILonePair> lonePairs() {
throw new UnsupportedOperationException("not supported");
}
public Iterable<ISingleElectron> singleElectrons() {
throw new UnsupportedOperationException("not supported");
}
public Iterable<IElectronContainer> electronContainers() {
throw new UnsupportedOperationException("not supported");
}
public IAtom getFirstAtom() {
throw new UnsupportedOperationException("not supported");
}
public IAtom getLastAtom() {
throw new UnsupportedOperationException("not supported");
}
public int getAtomNumber(IAtom atom) {
return -1;
}
public int getBondNumber(IAtom atom1, IAtom atom2) {
return -1;
}
public int getBondNumber(IBond bond) {
return -1;
}
public int getLonePairNumber(ILonePair lonePair) {
return -1;
}
public int getSingleElectronNumber(ISingleElectron singleElectron) {
return -1;
}
public IElectronContainer getElectronContainer(int number) {
throw new UnsupportedOperationException("not supported");
}
public IBond getBond(IAtom atom1, IAtom atom2) {
throw new UnsupportedOperationException("not supported");
}
public int getAtomCount() {
return 0;
}
public int getBondCount() {
return 0;
}
public int getLonePairCount() {
return 0;
}
public int getSingleElectronCount() {
return 0;
}
public int getElectronContainerCount() {
return 0;
}
public List<IAtom> getConnectedAtomsList(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public List<IBond> getConnectedBondsList(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public List<ILonePair> getConnectedLonePairsList(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public List<ISingleElectron> getConnectedSingleElectronsList(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public List<IElectronContainer> getConnectedElectronContainersList(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public int getConnectedAtomsCount(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public int getConnectedBondsCount(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public int getConnectedBondsCount(int atomnumber) {
return 0;
}
public int getConnectedLonePairsCount(IAtom atom) {
return 0;
}
public int getConnectedSingleElectronsCount(IAtom atom) {
return 0;
}
public double getBondOrderSum(IAtom atom) {
return 0;
}
public IBond.Order getMaximumBondOrder(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public IBond.Order getMinimumBondOrder(IAtom atom) {
throw new UnsupportedOperationException("not supported");
}
public void add(IAtomContainer atomContainer) {
}
public void addAtom(IAtom atom) {
}
public void addBond(IBond bond) {
}
public void addLonePair(ILonePair lonePair) {
}
public void addSingleElectron(ISingleElectron singleElectron) {
}
public void addElectronContainer(IElectronContainer electronContainer) {
}
public void remove(IAtomContainer atomContainer) {
}
public void removeAtom(int position) {
}
public void removeAtom(IAtom atom) {
}
public IBond removeBond(int position) {
throw new UnsupportedOperationException("not supported");
}
public IBond removeBond(IAtom atom1, IAtom atom2) {
throw new UnsupportedOperationException("not supported");
}
public void removeBond(IBond bond) {
}
public ILonePair removeLonePair(int position) {
throw new UnsupportedOperationException("not supported");
}
public void removeLonePair(ILonePair lonePair) {
}
public ISingleElectron removeSingleElectron(int position) {
throw new UnsupportedOperationException("not supported");
}
public void removeSingleElectron(ISingleElectron singleElectron) {
}
public IElectronContainer removeElectronContainer(int position) {
throw new UnsupportedOperationException("not supported");
}
public void removeElectronContainer(IElectronContainer electronContainer) {
}
public void removeAtomAndConnectedElectronContainers(IAtom atom) {
}
public void removeAllElements() {
}
public void removeAllElectronContainers() {
}
public void removeAllBonds() {
}
public void addBond(int atom1, int atom2, IBond.Order order, IBond.Stereo stereo) {
}
public void addBond(int atom1, int atom2, IBond.Order order) {
}
public void addLonePair(int atomID) {
}
public void addSingleElectron(int atomID) {
}
public boolean contains(IAtom atom) {
return false;
}
public boolean contains(IBond bond) {
return false;
}
public boolean contains(ILonePair lonePair) {
return false;
}
public boolean contains(ISingleElectron singleElectron) {
return false;
}
public boolean contains(IElectronContainer electronContainer) {
return false;
}
public boolean isEmpty() {
return true;
}
public IAtomContainer clone() throws CloneNotSupportedException {
throw new UnsupportedOperationException("not supported");
}
public void addListener(IChemObjectListener col) {
}
public int getListenerCount() {
return 0;
}
public void removeListener(IChemObjectListener col) {
}
public void setNotification(boolean bool) {
}
public boolean getNotification() {
return false;
}
public void notifyChanged() {
}
public void notifyChanged(IChemObjectChangeEvent evt) {
}
public void setProperty(Object description, Object property) {
}
public void removeProperty(Object description) {
}
public <T> T getProperty(Object description) {
throw new UnsupportedOperationException("not supported");
}
public <T> T getProperty(Object description, Class<T> c) {
throw new UnsupportedOperationException("not supported");
}
public Map<Object, Object> getProperties() {
return null;
}
public String getID() {
throw new UnsupportedOperationException("not supported");
}
public void setID(String identifier) {
}
public void setFlag(int mask, boolean value) {
}
public boolean getFlag(int mask) {
throw new UnsupportedOperationException("not supported");
}
public void setProperties(Map<Object, Object> properties) {
}
public void setFlags(boolean[] newFlags) {
}
public boolean[] getFlags() {
return new boolean[0];
}
public Number getFlagValue() {
return 0;
}
public IChemObjectBuilder getBuilder() {
throw new UnsupportedOperationException("not supported");
}
public void stateChanged(IChemObjectChangeEvent event) {
}
};
/**
* pseudo shortest-paths - when an invalid atom is given. this will always
* return 0 length paths and distances.
*/
private static final ShortestPaths EMPTY_SHORTEST_PATHS = new ShortestPaths(new int[0][0], EMPTY_CONTAINER, 0);
}