Skip to content

Preserve edge weights in minimum spanning forests - #1543

Merged
angriman merged 3 commits into
networkit:masterfrom
vera-rubin:fix/preserve-msf-weights
Nov 4, 2026
Merged

angriman merged 3 commits into
networkit:masterfrom
vera-rubin:fix/preserve-msf-weights

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

On master 5f03e17cec09, for one edge of weight 2.5, PrimMSF and KruskalMSF return a forest edge with weight 1.0 while getTotalWeight() returns 2.5. The documented minimum spanning forest should preserve the selected edge's weight.

import networkit as nk
g = nk.Graph(2, weighted=True)
g.addEdge(0, 1, 2.5)
for cls in (nk.graph.PrimMSF, nk.graph.KruskalMSF):
    a = cls(g).run()
    print(cls.__name__, a.getForest().weight(0, 1), a.getTotalWeight())

We now pass the selected weight to addEdge() in both algorithms. We've added one regression test for fractional, negative and zero weights across disconnected components. The regression fails for both algorithms before the fix and passes after rebuilding, along with the existing graph tests.

— ChipLab

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 38997178961

Coverage increased (+0.02%) to 80.673%

Details

  • Coverage increased (+0.02%) from the base build.
  • Patch coverage: 2 of 2 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 38248
Covered Lines: 30856
Line Coverage: 80.67%
Coverage Strength: 2272507.14 hits per line

💛 - Coveralls

@angriman angriman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but could you add C++ tests too? The ones in SpanningGTest.cpp never check the forest's edge weights, and the C++-only CI jobs don't run the Python test. Something like:

template <class MSF>
void expectForestKeepsEdgeWeights() {
    Graph g(6, true);
    g.addEdge(0, 1, 2.5);
    g.addEdge(1, 2, -1.25);
    g.addEdge(0, 2, 4.);
    g.addEdge(3, 4, 0.);

    MSF msf(g);
    msf.run();
    const Graph T = msf.getForest();

    EXPECT_TRUE(T.isWeighted());
    EXPECT_EQ(T.numberOfEdges(), 3u);
    EXPECT_TRUE(T.hasEdge(3, 4));
    T.forEdges([&](node u, node v, edgeweight w) -> void { EXPECT_EQ(w, g.weight(u, v)); });
    EXPECT_EQ(T.totalEdgeWeight(), msf.getTotalWeight());
    EXPECT_EQ(msf.getTotalWeight(), 1.25);
}

TEST_F(SpanningGTest, testKruskalMinimumSpanningForestKeepsEdgeWeights) {
    expectForestKeepsEdgeWeights<KruskalMSF>();
}

TEST_F(SpanningGTest, testPrimMinimumSpanningForestKeepsEdgeWeights) {
    expectForestKeepsEdgeWeights<PrimMSF>();
}

Comment thread networkit/test/test_graph.py Resolved
@angriman
angriman merged commit 69abc70 into networkit:master Nov 4, 2026
@vera-rubin

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the test sketch!

@vera-rubin
vera-rubin deleted the fix/preserve-msf-weights branch November 5, 2026 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants