Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit d0f2439

Browse files
perf: hoist base spread computation out of inner candidate loop in findTopKSeeds
Fixes #26. The monteCarlo(selected) call was being recomputed for every candidate node in the inner loop, but selected doesn't change during candidate evaluation. This wasted V * numTrials simulations per outer iteration. Now computed once before the candidate loop.
1 parent cb9ccf7 commit d0f2439

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

Gvisual/src/gvisual/InfluenceSpreadSimulator.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,16 @@ public List<SeedCandidate> findTopKSeeds(int k, Model model,
313313
String bestNode = null;
314314
double bestGain = -1;
315315

316+
// Hoist base spread computation outside the inner loop:
317+
// selected doesn't change while we evaluate candidates,
318+
// so recomputing it per-candidate wastes V * numTrials simulations.
319+
double baseSpread = 0;
320+
if (!selected.isEmpty()) {
321+
MonteCarloResult baseResult = monteCarlo(selected, model,
322+
probability, 0.3, 0, numTrials);
323+
baseSpread = baseResult.getAverageSpread();
324+
}
325+
316326
for (String candidate : graph.getVertices()) {
317327
if (selected.contains(candidate)) continue;
318328

@@ -321,14 +331,7 @@ public List<SeedCandidate> findTopKSeeds(int k, Model model,
321331

322332
MonteCarloResult mcResult = monteCarlo(trial, model,
323333
probability, 0.3, 0, numTrials);
324-
double avgSpread = mcResult.getAverageSpread();
325-
326-
double gain = avgSpread;
327-
if (!selected.isEmpty()) {
328-
MonteCarloResult baseResult = monteCarlo(selected, model,
329-
probability, 0.3, 0, numTrials);
330-
gain = avgSpread - baseResult.getAverageSpread();
331-
}
334+
double gain = mcResult.getAverageSpread() - baseSpread;
332335

333336
if (gain > bestGain) {
334337
bestGain = gain;

0 commit comments

Comments
 (0)