-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEdgeArraysGraph.java
2213 lines (1860 loc) · 80.5 KB
/
EdgeArraysGraph.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.github.mjwach.steiner_tree_algorithms;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Stack;
import org.jheaps.AddressableHeap.Handle;
import org.jheaps.tree.PairingHeap;
import gnu.trove.list.array.TFloatArrayList;
import gnu.trove.list.array.TIntArrayList;
public class EdgeArraysGraph implements Graph, Iterable<EdgeArraysGraph.Vertex>
{
private final ArrayList<Vertex> vertices;
public static interface EdgeFactory
{
Edge act(Vertex end0, Vertex end1);
}
public static final EdgeFactory defaultEdgeFactory = new EdgeFactory()
{
@Override
public Edge act(Vertex end0, Vertex end1)
{
return new EuclideanEdge((EuclideanVertex) end0, (EuclideanVertex) end1);
}
};
public static interface ActionOnVertexOrEdge
{
void perform(Vertex vertex);
void perform(Edge edge);
}
public static class Vertex implements GraphVertex, VirtualGraphVertex
{
public int index;
public final ArrayList<Edge> edges;
private int mark;
protected Vertex(int index)
{
this.index = index;
edges = new ArrayList<>();
}
@Override
public String toString()
{
return Integer.toString(index);
}
@Override
public GraphVertex asGraphVertex()
{
return this;
}
@Override
public int getIndex()
{
return index;
}
@Override
public void setMark(int markIndex)
{
mark |= 1 << markIndex;
}
@Override
public boolean readMark(int markIndex)
{
return (mark & 1 << markIndex) != 0;
}
public void clearMark(int markIndex)
{
mark &= ~(1 << markIndex);
}
public void clearAllMarks()
{
mark = 0;
}
@Override
public GraphEdge addEdge(GraphVertex farEnd)
{
return addEdge(farEnd, defaultEdgeFactory);
}
public GraphEdge addEdge(GraphVertex farEnd, EdgeFactory edgeFactory)
{
Vertex v = (Vertex) farEnd;
Edge edge = edgeFactory.act(this, v);
edges.add(edge);
v.edges.add(edge);
return edge;
}
@Override
public void doForEachEdge(Action1<GraphEdge> action)
{
for (Edge edge: edges)
action.perform(edge);
}
@Override
public GraphEdge findEdgeTo(GraphVertex peer)
{
for (Edge edge: edges)
if (edge.getDestination(this) == peer)
return edge;
return null;
}
public boolean hasEdgeTo(GraphVertex peer)
{
return findEdgeTo(peer) != null;
}
public int getEdgeCount()
{
return edges.size();
}
@Override
public void removeEdge(GraphEdge edge)
{
Edge e = (Edge) edge;
e.getDestination(this).edges.remove(e);
edges.remove(e);
}
@Override
public void removeAllEdges()
{
for (Edge edge: edges)
{
Vertex destination = edge.getDestination(this);
if (destination != this)
destination.edges.remove(edge);
}
edges.clear();
}
@Override
public void clearMarkFromVerticesInConnectedSubgraph(int markIndex)
{
if (readMark(markIndex))
{
clearMark(markIndex);
for (Edge edge: edges)
edge.getDestination(this).clearMarkFromVerticesInConnectedSubgraph(markIndex);
}
}
@Override
public void clearMarkFromVerticesAndEdgesInConnectedSubgraph(int markIndex)
{
if (readMark(markIndex))
{
clearMark(markIndex);
for (Edge edge: edges)
if (edge.readMark(markIndex))
{
edge.clearMarkInBothDirections(markIndex);
edge.getDestination(this).clearMarkFromVerticesAndEdgesInConnectedSubgraph(markIndex);
}
}
}
@Override
public void clearMarkFromEdgesInConnectedSubgraph(int markIndex)
{
for (Edge edge: edges)
if (edge.readMark(markIndex))
{
edge.clearMarkInBothDirections(markIndex);
edge.getDestination(this).clearMarkFromEdgesInConnectedSubgraph(markIndex);
}
}
public EdgeArraysGraph buildShortCutsGraph(InterestEvaluation interestEvaluation, MarkFilter markFilter, int graphwideVertexCount, Sleeve<ShortcutGraphVertex[]> oldVertexToNewVertexMap_out)
{
EdgeArraysGraph graph = new EdgeArraysGraph();
ShortcutGraphVertex[] vertexCache = new ShortcutGraphVertex[graphwideVertexCount];
buildShortCutsGraph(interestEvaluation, markFilter, null, null, graph, vertexCache);
if (oldVertexToNewVertexMap_out != null) oldVertexToNewVertexMap_out.setIdentity(vertexCache);
return graph;
}
private void buildShortCutsGraph(InterestEvaluation interestEvaluation, MarkFilter markFilter, ShortcutGraphVertex source, ArrayList<Edge> pathFromSource, EdgeArraysGraph graph, ShortcutGraphVertex[] vertexCache)
{
Edge sourceEdge = pathFromSource != null ? pathFromSource.get(pathFromSource.size() - 1) : null;
boolean cycleIsCompletedHere = false;
if (interestEvaluation.perform(this))
{
ShortcutGraphVertex destination = vertexCache[getIndex()];
if (destination == null)
destination = vertexCache[getIndex()] = new ShortcutGraphVertex(this, graph.vertices.size());
else
cycleIsCompletedHere = true;
graph.vertices.add(destination);
final ArrayList<Edge> pathFromSource_ = pathFromSource;
if (source != null)
source.addEdge(destination, new EdgeFactory()
{
@Override
public Edge act(Vertex end0, Vertex end1)
{
return new ShortcutGraphEdge(end0, end1, pathFromSource_);
}
});
source = destination;
pathFromSource = null;
}
if (!cycleIsCompletedHere)
for (Edge edge: edges)
if (edge != sourceEdge && markFilter.passes(edge))
{
Vertex destination = edge.getDestination(this);
if (markFilter.passes(destination))
{
ArrayList<Edge> pathThroughEdge = pathFromSource != null ? pathFromSource : new ArrayList<Edge>();
pathThroughEdge.add(edge);
destination.buildShortCutsGraph(interestEvaluation, markFilter, source, pathThroughEdge, graph, vertexCache);
}
}
}
public void asRootOfMarkedTreePruneUninterestingBranches(int markIndex, Predicate1<Vertex> isInteresting)
{
inMarkedTreePruneUninterestingBranchesAndReportOwnInterestingness(markIndex, null, isInteresting);
}
private boolean inMarkedTreePruneUninterestingBranchesAndReportOwnInterestingness(int markIndex, Edge source, Predicate1<Vertex> isInteresting)
{
boolean thisIsInteresting = isInteresting.isTrue(this);
for (Edge edge: edges)
if (edge != source && edge.readMark(markIndex))
thisIsInteresting = edge.getDestination(this).inMarkedTreePruneUninterestingBranchesAndReportOwnInterestingness(markIndex, edge, isInteresting) || thisIsInteresting;
if (!thisIsInteresting)
{
this.clearMark(markIndex);
source.clearMark(markIndex);
}
return thisIsInteresting;
}
public float getWeightOfMarkedTree(int markIndex)
{
return getWeightOfMarkedTree(markIndex, null);
}
public float getWeightOfMarkedTree(int markIndex, Edge source)
{
float weight = 0;
for (Edge edge: edges)
if (edge.readMark(markIndex) && edge != source)
weight += edge.getWeight() + edge.getDestination(this).getWeightOfMarkedTree(markIndex, edge);
return weight;
}
public void doForEachVertexOrEdgeInMarkedTree(int markIndex, ActionOnVertexOrEdge action)
{
doForEachVertexOrEdgeInMarkedTree(markIndex, action, null);
}
private void doForEachVertexOrEdgeInMarkedTree(int markIndex, ActionOnVertexOrEdge action, Edge source)
{
if (readMark(markIndex))
{
action.perform(this);
for (Edge edge: edges)
if (edge != source && edge.readMark(markIndex))
{
action.perform(edge);
edge.getDestination(this).doForEachVertexOrEdgeInMarkedTree(markIndex, action, edge);
}
}
}
}
public static class EuclideanVertex extends Vertex
{
private EuclideanVector location;
public EuclideanVertex(int index, float x, float y)
{
this(index, new PlaneVector(x, y));
}
public EuclideanVertex(int index, float x, float y, float z)
{
this(index, new SpaceVector(x, y, z));
}
public EuclideanVertex(int index, EuclideanVector location)
{
super(index);
this.location = location;
}
public void setLocation(EuclideanVector newValue)
{
location = newValue;
}
public EuclideanVector getLocation()
{
return location;
}
public float getX()
{
return location.getX();
}
public float getY()
{
return location.getY();
}
public PlaneVector getLocationAsPlaneVector()
{
return (PlaneVector) location;
}
public float getDistanceSquaredFrom(EuclideanVertex v)
{
return getDistanceSquaredFrom(v.location);
}
public float getDistanceSquaredFrom(EuclideanVector v)
{
return location.getDistanceSquaredFrom_(v);
}
}
public static class ShortcutGraphVertex extends Vertex
{
public final Vertex underlyingVertex;
public ShortcutGraphVertex(Vertex underlyingVertex, int index)
{
super(index);
this.underlyingVertex = underlyingVertex;
}
public void settleMarkedTreeIntoUnderlyingGraph(int markIndexInShortcutTree, int markIndexInUnderlyingGraph)
{
settleMarkedTreeIntoUnderlyingGraph(null, markIndexInShortcutTree, markIndexInUnderlyingGraph);
}
private void settleMarkedTreeIntoUnderlyingGraph(ShortcutGraphEdge source, int markIndexInShortcutTree, int markIndexInUnderlyingGraph)
{
underlyingVertex.setMark(markIndexInUnderlyingGraph);
for (Edge e: edges)
if (e != source && e.readMark(markIndexInShortcutTree))
{
ShortcutGraphEdge edge = (ShortcutGraphEdge) e;
ShortcutGraphVertex destination = (ShortcutGraphVertex) edge.getDestination(this);
for (Edge underlyingEdge: edge.underlyingPath)
underlyingEdge.setMarkFullyInSelfAndEnds(markIndexInUnderlyingGraph);
destination.settleMarkedTreeIntoUnderlyingGraph(edge, markIndexInShortcutTree, markIndexInUnderlyingGraph);
}
}
}
public static class ShortcutGraphEdge extends Edge
{
public final ArrayList<Edge> underlyingPath;
private float weight;
private boolean weightIsSet;
public ShortcutGraphEdge(Vertex end0, Vertex end1, ArrayList<Edge> underlyingPath)
{
super(end0, end1);
this.underlyingPath = underlyingPath;
}
@Override
public float getWeight()
{
if (!weightIsSet)
{
weight = calculateWeight();
weightIsSet = true;
}
return weight;
}
private float calculateWeight()
{
float w = 0;
for (Edge edge: underlyingPath)
w += edge.getWeight();
return w;
}
@Override
public float getWeightRelatedQuantityForComparison()
{
return getWeight();
}
@Override
public float getWeight(float weightRelatedQuantity)
{
return getWeight();
}
}
public static abstract class Edge implements GraphEdge
{
public Vertex end0;
public Vertex end1;
private int mark;
public static final Comparator<Edge> comparatorByWeight = new Comparator<Edge>()
{
@Override
public int compare(Edge edge1, Edge edge2)
{
return edge1.compareWeights(edge2);
}
};
public Edge(Vertex end0, Vertex end1)
{
this.end0 = end0;
this.end1 = end1;
}
public void setMarkFullyInSelfAndEnds(int markIndex)
{
setMarkInBothDirections(markIndex);
end0.setMark(markIndex);
end1.setMark(markIndex);
}
public void clearMarkFullyInSelfAndEnds(int markIndex)
{
clearMarkInBothDirections(markIndex);
end0.clearMark(markIndex);
end1.clearMark(markIndex);
}
@Override
public void setMarkInBothDirections(int markIndex)
{
mark |= 1 << markIndex;
}
@Override
public void clearMarkInBothDirections(int markIndex)
{
clearMark(markIndex);
}
@Override
public boolean readMark(int markIndex)
{
return (mark & 1 << markIndex) != 0;
}
public void clearMark(int markIndex)
{
mark &= ~(1 << markIndex);
}
public void clearAllMarks()
{
mark = 0;
}
@Override
public GraphVertex getEnd0()
{
return end0;
}
@Override
public GraphVertex getEnd1()
{
return end1;
}
@Override
public GraphVertex getEnd(GraphVertex otherEnd)
{
return otherEnd == end0 ? end1 : end0;
}
@Override
public boolean isNotNegativelyOriented()
{
return true;
}
public boolean endsAt(GraphVertex v)
{
return end0 == v || end1 == v;
}
public boolean destinationHasGreaterIndex(Vertex vertex)
{
return getDestination(vertex).getIndex() > vertex.getIndex();
}
public Vertex getDestination(Vertex source)
{
return end0 == source ? end1 : end0;
}
public abstract float getWeightRelatedQuantityForComparison();
public abstract float getWeight();
public abstract float getWeight(float weightRelatedQuantity);
private int compareWeights(Edge peer)
{
float w1 = this.getWeightRelatedQuantityForComparison();
float w2 = peer.getWeightRelatedQuantityForComparison();
return w1 < w2 ? -1 : w1 > w2 ? 1 : 0;
}
}
public static class EuclideanEdge extends Edge
{
public EuclideanEdge(EuclideanVertex end0, EuclideanVertex end1)
{
super(end0, end1);
}
public float getLengthSquared()
{
return ((EuclideanVertex) end0).getDistanceSquaredFrom((EuclideanVertex) end1);
}
@Override
public float getWeightRelatedQuantityForComparison()
{
return getLengthSquared();
}
@Override
public float getWeight()
{
return (float) Math.sqrt(getLengthSquared());
}
@Override
public float getWeight(float weightRelatedQuantity)
{
return (float) Math.sqrt(weightRelatedQuantity);
}
}
public EdgeArraysGraph(List<? extends Vertex> vertices)
{
this.vertices = new ArrayList<>(vertices);
}
public EdgeArraysGraph()
{
this(new ArrayList<Vertex>());
}
public int getVertexCount()
{
return vertices.size();
}
@Override
public Iterator<Vertex> iterator()
{
return vertices.iterator();
}
@Override
public GraphVertex getVertex(int index)
{
return vertices.get(index);
}
public Vertex getEdgeArrayVertex(int index)
{
return vertices.get(index);
}
public void addVertex(Vertex vertex)
{
vertices.add(vertex);
}
@Override
public void clearAllMarks()
{
for (Vertex vertex: vertices)
{
vertex.clearAllMarks();
for (Edge edge: vertex.edges)
edge.clearAllMarks();
}
}
@Override
public void markMinimalSpanningTree_Kruskal(int markIndex)
{
markMinimalSpanningTree_Kruskal(markIndex, false);
}
public void markMinimalSpanningTree_Kruskal(int markIndex, boolean markEdgelessVerticesToo)
{
ArrayList<Edge> edges = gatherAllEdges();
Collections.sort(edges, Edge.comparatorByWeight);
DisjointSetRegistry sets = new DisjointSetRegistry(vertices.size());
int edgeCount = 0;
for (Edge edge: edges)
{
int i1 = edge.end0.index;
int i2 = edge.end1.index;
if (sets.find(i1) != sets.find(i2))
{
sets.union(i1, i2);
edge.setMarkFullyInSelfAndEnds(markIndex);
if (++edgeCount == vertices.size() - 1)
break;
}
}
if (markEdgelessVerticesToo)
for (Vertex vertex: vertices)
vertex.setMark(markIndex);
}
@Override
public ShortestPathsTable findShortestPathsForAllVertexPairs(InterruptionSignal interruptionSignal) throws ShortestPathsSearchInterruption
{
ShortestPathsTable table = new ShortestPathsTable(vertices.size());
for (Vertex vertex: vertices)
{
ShortestPathsSearch search = new DijkstraShortestPathsSearch(vertices, vertex, table, interruptionSignal);
while (!search.isFinished())
search.advance();
}
return table;
}
public ShortestPathsTable findShortestPathsForAllVertexPairs_lazily(InterruptionSignal interruptionSignal)
{
DijkstraShortestPathsSearch[] searches = new DijkstraShortestPathsSearch[vertices.size()];
ShortestPathsTable table = new ShortestPathsTable(searches);
for (int i = 0; i < searches.length; ++i)
searches[i] = new DijkstraShortestPathsSearch(vertices, vertices.get(i), table, interruptionSignal);
return table;
}
private static class DijkstraShortestPathsSearch implements ShortestPathsSearch
{
private final Vertex root;
private final ShortestPathsTable table;
private final InterruptionSignal interruptionSignal;
private final ArrayList<Vertex> vertices;
private PairingHeap<Double, Vertex> heap;
private ArrayList<Handle<Double, Vertex>> entries;
private ArrayList<Vertex> precedingVertices;
private int i;
public DijkstraShortestPathsSearch(ArrayList<Vertex> vertices, Vertex root, ShortestPathsTable table, InterruptionSignal interruptionSignal)
{
this.vertices = vertices;
this.root = root;
this.table = table;
this.interruptionSignal = interruptionSignal;
}
@Override
public void advance() throws ShortestPathsSearchInterruption
{
if (heap == null)
{
heap = new PairingHeap<Double, EdgeArraysGraph.Vertex>();
entries = new ArrayList<>();
precedingVertices = new ArrayList<>();
for (int i = 0; i < vertices.size(); ++i) entries.add(null);
for (Vertex v: vertices)
{
entries.set(v.index, heap.insert(v == root ? 0 : Double.POSITIVE_INFINITY, v));
precedingVertices.add(null);
if (interruptionSignal.isActive()) throw new ShortestPathsSearchInterruption();
}
}
if (isFinished())
throw new UnsupportedOperationException("no more advancement is possible");
Handle<Double, Vertex> entry = heap.deleteMin();
Vertex v = entry.getValue();
double distance = entry.getKey();
table.setCellValue(root, v, (float) distance, precedingVertices.get(v.index), i);
for (Edge edge: v.edges)
{
if (interruptionSignal.isActive()) throw new ShortestPathsSearchInterruption();
double distanceToDestination = distance + edge.getWeight();
Vertex destination = edge.getDestination(v);
Handle<Double, Vertex> destinationEntry = entries.get(destination.getIndex());
if (distanceToDestination < destinationEntry.getKey())
{
destinationEntry.decreaseKey(distanceToDestination);
precedingVertices.set(destination.getIndex(), v);
}
}
++i;
}
@Override
public boolean isFinished()
{
return heap != null && heap.isEmpty();
}
}
private ArrayList<Edge> gatherAllEdges()
{
ArrayList<Edge> edges = new ArrayList<>();
for (Vertex vertex: vertices)
for (Edge edge: vertex.edges)
if (edge.destinationHasGreaterIndex(vertex))
edges.add(edge);
return edges;
}
@Override
public float[] getVerticesAsExesMadeFromLineSegments_xyxy(float exHalfWidth, Boolean requiredMark, int markIndex)
{
TFloatArrayList result = new TFloatArrayList();
for (int i = 0; i < vertices.size(); ++i)
{
EuclideanVertex vertex = (EuclideanVertex) vertices.get(i);
if (requiredMark == null || vertex.readMark(markIndex) == requiredMark)
{
float x = vertex.getX();
float y = vertex.getY();
result.add(x - exHalfWidth);
result.add(y - exHalfWidth);
result.add(x + exHalfWidth);
result.add(y + exHalfWidth);
result.add(x - exHalfWidth);
result.add(y + exHalfWidth);
result.add(x + exHalfWidth);
result.add(y - exHalfWidth);
}
}
return result.toArray();
}
@Override
public float[] getEdgesAsLineSegments_xyxy(Boolean requiredMark, int markIndex)
{
TFloatArrayList result = new TFloatArrayList();
for (Vertex vertex: vertices)
for (Edge edge: vertex.edges)
if (edge.destinationHasGreaterIndex(vertex) && (requiredMark == null || edge.readMark(markIndex) == requiredMark))
{
result.add(((EuclideanVertex) vertex).getX());
result.add(((EuclideanVertex) vertex).getY());
result.add(((EuclideanVertex) edge.getDestination(vertex)).getX());
result.add(((EuclideanVertex) edge.getDestination(vertex)).getY());
}
return result.toArray();
}
@Override
public void addDelaunayTriangulationEdges()
{
// an intermediate DCEL is created here... that's a bit silly; it'd probably be better to just do the Delaunay algorithm within this graph (but that'd require
// a second version of the triangulation algorithm to be written) - TODO, handle that then
DCELGraph triangulation = new DCELGraph(vertices.size());
for (int i = 0; i < vertices.size(); ++i)
{
EuclideanVertex v = (EuclideanVertex) vertices.get(i);
triangulation.vertices[i] = new DCELGraph.Vertex(v.getIndex(), v.getLocationAsPlaneVector());
}
triangulation.addDelaunayTriangulationEdges();
for (DCELGraph.Vertex v: triangulation.vertices)
v.doForEachEdge(new Action1<GraphEdge>()
{
@Override
public void perform(GraphEdge edge)
{
if (edge.isNotNegativelyOriented())
addEdgeIfNotPresent(edge.getEnd0().getIndex(), edge.getEnd1().getIndex());
}
});
}
public void addEdgeIfNotPresent(int end0Index, int end1Index)
{
Vertex v0 = vertices.get(end0Index);
Vertex v1 = vertices.get(end1Index);
if (v0.findEdgeTo(v1) == null)
v0.addEdge(v1);
}
private static class SteinerSubgraphVertex
{
public Vertex v;
public final ArrayList<SteinerSubgraphVertex> neighbors;
public final Boolean isSteinerVertex;
public boolean mark;
public SteinerSubgraphVertex(Vertex v, Boolean isSteinerVertex)
{
this.v = v;
this.isSteinerVertex = isSteinerVertex;
neighbors = new ArrayList<>();
}
@Override
public String toString()
{
return v.toString();
}
public void addNeighbor(Vertex neighbor, Boolean neighborIsSteinerVertex)
{
SteinerSubgraphVertex n = new SteinerSubgraphVertex(neighbor, neighborIsSteinerVertex);
addNeighbor(n);
}
public void addNeighbor(SteinerSubgraphVertex neighbor)
{
neighbors.add(neighbor);
neighbor.neighbors.add(this);
}
@SuppressWarnings("unused")
public void doForEachEdgeInGraph(Action2<SteinerSubgraphVertex, SteinerSubgraphVertex> action)
{
doForEachEdgeInGraph(action, null);
}
private void doForEachEdgeInGraph(Action2<SteinerSubgraphVertex, SteinerSubgraphVertex> action, SteinerSubgraphVertex source)
{
for (SteinerSubgraphVertex neighbor: neighbors)
if (neighbor != source)
{
action.perform(this, neighbor);
neighbor.doForEachEdgeInGraph(action, this);
}
}
public <ThrownException extends Throwable> void doForEachEdgeInGraph(Action2Throwing<SteinerSubgraphVertex, SteinerSubgraphVertex, ThrownException> action) throws ThrownException
{
doForEachEdgeInGraph(action, null);
}
private <ThrownException extends Throwable> void doForEachEdgeInGraph(Action2Throwing<SteinerSubgraphVertex, SteinerSubgraphVertex, ThrownException> action, SteinerSubgraphVertex source) throws ThrownException
{
for (SteinerSubgraphVertex neighbor: neighbors)
if (neighbor != source)
{
action.perform(this, neighbor);
neighbor.doForEachEdgeInGraph(action, this);
}
}
public SteinerNetStats gatherHiLoPrerequisiteData(SteinerSubgraphVertex neighborInNet, Vertex newTerminal, int maximalNetNeighborDepth, SubgraphSteinerVertexCache cache, ShortestPathsTable paths) throws ShortestPathsSearchInterruption
{
SteinerNetStats stats1 = gatherHiLoDataInBranch(neighborInNet, newTerminal, true, maximalNetNeighborDepth, cache, paths);
SteinerNetStats stats2 = neighborInNet.gatherHiLoDataInBranch(this, newTerminal, false, maximalNetNeighborDepth, cache, paths);
return stats1.plus(stats2);
}
private SteinerNetStats gatherHiLoDataInBranch(SteinerSubgraphVertex neighborInNet, Vertex newTerminal, boolean includeThisEdge, int maximalNetNeighborDepth, SubgraphSteinerVertexCache cache, ShortestPathsTable paths) throws ShortestPathsSearchInterruption
{
SteinerNetStats stats = new SteinerNetStats(neighborInNet, includeThisEdge ? this : null, newTerminal, paths);
if (neighborInNet.isSteinerVertex)
{
for (SteinerSubgraphVertex neighborBeyond: neighborInNet.neighbors)
if (neighborBeyond != this)
stats = stats.plus(neighborInNet.gatherHiLoDataInBranch(neighborBeyond, newTerminal, true, maximalNetNeighborDepth, cache, paths));
}
else if (maximalNetNeighborDepth > 0)
{
SteinerSubgraphVertex representativeOfNearestEdge = neighborInNet.getRepresentativeOfNearestEdge(newTerminal, this, cache, paths);
if (representativeOfNearestEdge != null)
stats = stats.plus(neighborInNet.gatherHiLoDataInBranch(representativeOfNearestEdge, newTerminal, true, maximalNetNeighborDepth - 1, cache, paths));
}
return stats;
}
public SteinerSubgraphVertex getRepresentativeOfNearestEdge(Vertex referencePoint, SteinerSubgraphVertex source, SubgraphSteinerVertexCache cache, ShortestPathsTable paths) throws ShortestPathsSearchInterruption
{
float minimalDistance = Float.MAX_VALUE;
SteinerSubgraphVertex representativeOfNearestEdge = null;
for (SteinerSubgraphVertex neighbor: neighbors)
if (neighbor != source)
{
float distanceToEdgeMoreOrLess = cache.getDistanceFromVertexToNearestVertexOnPathThatIsNotTheStartOfThePath(referencePoint, v, neighbor.v, paths);
if (distanceToEdgeMoreOrLess < minimalDistance)
{
minimalDistance = distanceToEdgeMoreOrLess;
representativeOfNearestEdge = neighbor;
}
}
return representativeOfNearestEdge;
}
public boolean settleIntoMainGraph(int markIndex, ArrayList<Edge> markedEdgeRepository, ShortestPathsTable paths, Graph graph) throws ShortestPathsSearchInterruption
{
v.setMark(markIndex);
return settleIntoNewGraph(null, markIndex, markedEdgeRepository, paths, graph);
}
private boolean settleIntoNewGraph(SteinerSubgraphVertex source, int markIndex, ArrayList<Edge> markedEdgeRepository, ShortestPathsTable paths, Graph graph) throws ShortestPathsSearchInterruption
{
boolean cycleEncountered = false;
for (SteinerSubgraphVertex neighbor: neighbors)
if (neighbor != source)
{
boolean breakingNewGround = false;
for (Vertex lastVertex = null, vertex = v; vertex != null; lastVertex = vertex, vertex = (Vertex) paths.getNextVertexOnShortestPath(graph, vertex, neighbor.v))
if (lastVertex != null)
{
Edge edge = (Edge) vertex.findEdgeTo(lastVertex);
boolean edgeMark = edge.readMark(markIndex);
if (!edgeMark)
{
breakingNewGround = true;
markedEdgeRepository.add(edge);
}
if (breakingNewGround && (edgeMark || vertex.readMark(markIndex)))
cycleEncountered = true;