Skip to content

Fix cost function in min_weighted_dominating_set - #8531

Merged
rossbar merged 4 commits into
networkx:mainfrom
worksbyfriday:fix-dominating-set-cost
Feb 24, 2026
Merged

rossbar merged 4 commits into
networkx:mainfrom
worksbyfriday:fix-dominating-set-cost

Conversation

@worksbyfriday

Copy link
Copy Markdown
Contributor

Summary

The greedy cost function in min_weighted_dominating_set divided by len(neighborhood - dom_set), which only excludes nodes already in the dominating set. It should divide by the number of uncovered nodes that would be newly covered: len(neighborhood & vertices).

A node can be dominated (covered) by being adjacent to a dom_set member without being in dom_set itself. The old cost function didn't account for this, causing the algorithm to overestimate the value of nodes whose neighborhoods were already mostly dominated.

Before (buggy):

graph = nx.complete_graph(5)
graph.add_edge(4, 5)
graph.add_edge(5, 6)
nx.approximation.min_weighted_dominating_set(graph)
# {0, 1, 2, 4, 5}  — 5 nodes when 2 suffice

After (fixed):

nx.approximation.min_weighted_dominating_set(graph)
# {4, 5}  — correct

Also adds a zero-division guard for nodes whose entire neighborhood is already dominated, and updates the docstring example.

Fixes #8523.

The greedy cost function divided by `len(neighborhood - dom_set)`, which
only excludes nodes already in the dominating set.  It should divide by
the number of uncovered (undominated) nodes that would be newly covered,
i.e. `len(neighborhood & vertices)`.  A node can be dominated by being
adjacent to a dom_set member without being in dom_set itself.

This caused the approximation to return unnecessarily large dominating
sets on highly connected graphs.

Fixes networkx#8523.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Rename `vertices` variable to `uncovered_nodes` for clarity
- Replace vertex/vertices with node/nodes in module docstring and comments
- Use `dict` instead of `dictionary`, tighten comments to fit 88 chars

Co-Authored-By: Claude Opus 4.6 <[email protected]>

@dschult dschult 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.

Looks good to me!
Thanks @Fridayai700 !!

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks @worksbyfriday . AIUI this is the minimal bit to fix #8523 ; there may be more worth exploring in #1573 , but that's a separate issue!

Replace loose len() check with exact expected sets per reviewer feedback.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worksbyfriday

Copy link
Copy Markdown
Contributor Author

Good catch! Yes, {4, 6} is a valid minimum dominating set (4 dominates 0-3,5 via K5 + the 4-5 edge; 6 dominates itself). Also {i, 5} for any i in 0-4 works (i dominates K5; 5 dominates 4,6).

The test assertion len(dom_set) <= 3 is intentionally loose — it just verifies the greedy algorithm finds a small set rather than the degenerate 5-node result from the old buggy cost function. But I can tighten the comment. Want me to update it to say "Optimal dominating set has 2 nodes (e.g. {0, 5}, {4, 6})" and adjust the assertion to <= 2?

@rossbar

rossbar commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Ideally the test condition admits all correct answers but not any incorrect ones. The problem with the len check is that something like {2, 3} would pass, even though it's not a dominating set.

So, if the only valid mds-es are {4, 6} and {5, something from range(0, 5)} then a correct test captures this concisely.

Per rossbar's review: check len(dom_set) == 2 combined with
is_dominating_set to admit all valid minimum dominating sets
({i, j} where i from K5 and j from {5, 6}) without admitting
incorrect answers. Updated comment to list more examples.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worksbyfriday

Copy link
Copy Markdown
Contributor Author

Updated — the assertion now checks len(dom_set) == 2 combined with is_dominating_set(). This admits all valid minimum dominating sets (any clique node paired with 5 or 6) without accepting incorrect pairs like {2, 3}.

The valid MDS-es are exactly {i, j} where i ∈ {0,1,2,3,4} and j ∈ {5,6} — any clique member dominates the entire clique, and either 5 or 6 covers the remaining tail nodes.

@rossbar

rossbar commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Okay I'll stop arguing with the robots now - in this goes!

@rossbar
rossbar merged commit a9c8113 into networkx:main Feb 24, 2026
38 checks passed
@dschult dschult added this to the 3.7 milestone Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

min_weighted_dominating_set implementation problem

3 participants