Skip to content

Commit 746cae7

Browse files
Handle smaller than k components
1 parent c6d9980 commit 746cae7

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

alpha/alpha-algo/src/main/java/org/neo4j/gds/impl/spanningtree/KSpanningTree.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ private boolean moveMakesSense(double cost1, double cost2, DoubleUnaryOperator m
369369
}
370370

371371
private SpanningTree combineApproach(SpanningTree tree) {
372-
372+
if (tree.effectiveNodeCount() < k) {
373+
return tree;
374+
}
373375
var spanningTree1 = cutLeafApproach(tree);
374376
var spanningTree2 = growApproach(tree);
375377

alpha/alpha-algo/src/test/java/org/neo4j/gds/impl/spanningtree/KSpanningTreeTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,35 @@ void worstCaseForPruningLeaves() {
292292

293293
}
294294

295+
@Test
296+
void shouldWorkForComponentSmallerThanK() {
297+
var factory = GdlFactory.of("CREATE" +
298+
" (a:Node)" +
299+
", (b:Node)" +
300+
", (c:Node)" +
301+
", (d:Node)" +
302+
", (e:Node)" +
303+
", (f:Node)" +
304+
", (g:Node)" +
305+
", (a)-[:TYPE {cost: 1.0}]->(b)" +
306+
", (b)-[:TYPE {cost: 1.0}]->(c)" +
307+
", (c)-[:TYPE {cost: 1.0}]->(d)");
308+
309+
var graph = factory.build().getUnion();
310+
var startNode = factory.nodeId("a");
311+
312+
var spanningTree = new KSpanningTree(
313+
graph,
314+
Prim.MIN_OPERATOR,
315+
startNode,
316+
5,
317+
ProgressTracker.NULL_TRACKER
318+
).compute();
319+
320+
assertThat(spanningTree.effectiveNodeCount()).isEqualTo(4);
321+
322+
}
323+
295324
@Test
296325
void shouldLogProgress() {
297326
var config = KSpanningTreeBaseConfigImpl.builder().sourceNode(idFunction.of("a")).k(2).build();

0 commit comments

Comments
 (0)