Skip to content

Commit

Permalink
Cache lazily the main connected component (#537)
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Dupuy <florian.dupuy@rte-france.com>
  • Loading branch information
flo-dup authored May 30, 2022
1 parent 478d19c commit 0ad740c
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class EvenShiloachGraphDecrementalConnectivity<V, E> implements GraphDecr

private final List<Set<V>> newConnectedComponents;
private final Map<V, LevelNeighbours> levelNeighboursMap;
private Set<V> mainConnectedComponent;

private final List<Triple<V, E, V>> cutEdges;
private final List<E> edgesToCut;
Expand Down Expand Up @@ -83,7 +84,7 @@ private void invalidateInit() {
cutEdges.clear();
edgesToCut.clear();
newConnectedComponents.clear();
invalidateVertexMapCache();
invalidateConnectedComponentCache();
}

@Override
Expand All @@ -94,12 +95,13 @@ public void cut(E edge) {
if (edgesToCut.contains(edge)) {
throw new PowsyblException("Edge already cut: " + edge);
}
invalidateVertexMapCache();
invalidateConnectedComponentCache();

edgesToCut.add(edge);
}

private void invalidateVertexMapCache() {
private void invalidateConnectedComponentCache() {
mainConnectedComponent = null;
vertexMapCacheInvalidated = true;
vertexToConnectedComponent.clear();
}
Expand All @@ -120,7 +122,7 @@ public void initLevels() {
}

public void reset() {
invalidateVertexMapCache();
invalidateConnectedComponentCache();
newConnectedComponents.clear();
allSavedChangedLevels.descendingIterator().forEachRemaining(levelNeighboursMap::putAll);
allSavedChangedLevels.clear();
Expand Down Expand Up @@ -155,7 +157,10 @@ public Set<V> getConnectedComponent(V vertex) {
}

private Set<V> getMainConnectedComponent() {
return vertices.stream().filter(v -> newConnectedComponents.stream().noneMatch(cc -> cc.contains(v))).collect(Collectors.toSet());
if (mainConnectedComponent == null) {
mainConnectedComponent = vertices.stream().filter(v -> newConnectedComponents.stream().noneMatch(cc -> cc.contains(v))).collect(Collectors.toSet());
}
return mainConnectedComponent;
}

@Override
Expand Down

0 comments on commit 0ad740c

Please sign in to comment.