Skip to content

Add Functions for Finding Connected Dominating Sets - #7774

Merged
dschult merged 24 commits into
networkx:mainfrom
GalMichaeli:feat/cds
Apr 21, 2025
Merged

dschult merged 24 commits into
networkx:mainfrom
GalMichaeli:feat/cds

Conversation

@GalMichaeli

@GalMichaeli GalMichaeli commented Dec 20, 2024 •

Copy link
Copy Markdown
Contributor

Added Functionality

This PR extends the functionality of
networkx/networx/algorithms/dominating.py by adding two functions:

  1. connected_dominating_set - takes an undirected connected graph G
    and returns a connected dominating set (CDS) of G
  2. is_connected_dominating_set - takes an undirected graph G
    and an iterable nbunch and returns true if and only if nbunch
    is a CDS of G

Motivation

NetworkX is used extensively in the Networked Software Systems Lab at
the Technion - Israel Institute of Technology during reseach of various
networking topics. Finding a connected dominating set (CDS) in the
context of networks, especially in wireless or ad hoc networks, is
crucial for optimizing communication and resource usage. A CDS serves as
a virtual backbone for the network, meaning efficient routing, power
conservation and network coverage.

Implementation Details

The function connected_dominating_set is an implementation of
Algorithm I as described in this paper. Quoting from the paper,
"The idea behind the algorithm is the following: grow a tree T,
starting from the vertex of maximum degree. At each step we pick
a vertex v in T and 'scan it.' Scanning a vertex adds edges to T from v
to all its neighbors not in T. In the end we find a spanning tree T,
and pick the nonleaf nodes as the connected dominating set."

Testing Details

In order to test is_connected_dominating_set we create a path graph G
with four nodes: 0 <-> 1 <-> 2 <-> 3. We then assert that {1, 2} is a
CDS, {1, 3} is NOT a CDS and that {2, 3} indeed induces a connected
subgraph of G but is not a dominating set.
As for connected_dominating_set, we first test for the expected behavior
in edge cases, i.e., when G is the null graph, G has only one node and G
is disconnected. Next we test that for complete graphs the function
returns a CDS of size 1. Finally, we define a parametrized test, in which
we test connected Watts-Strogatz graphs with varying parameters and
parametrized seed for reproducibility.

 * Add the following functions to networkx/algorithms/dominating.py:
   - connected_dominating_set for finding a connected dominating set
     in undirected graphs
   - interval_graph_min_connected_dominating_set for finding a minimal
     connected dominating set in interval graphs
   - is_connected_dominating_set to check if a set of nodes is a
     connected dominating set of a graph

 * Add tests for the above functions in
   networkx/algorithms/tests/test_dominating.py
@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Hi,
The PR seems to fail due to a missing label.
How can I add the relevant label?

Thanks.

@dschult

dschult commented Dec 21, 2024

Copy link
Copy Markdown
Member

I have added the enhancement label -- The labels are assigned by the reviewer rather than the submitter.

Thanks for this! The choice of modules to add look good. I haven't looked at the content yet -- just wanted to get the label in place so you get the "green checkmarks"! :)

@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Thanks,
Looking forward for your feedback.

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

Sorry for the late review. This is great functionality, left comments!

Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Outdated
Comment on lines +245 to +248
if nx.is_dominating_set(G, nbunch) == False:
return False
SG = nx.subgraph(G, nbunch)
return nx.is_connected(SG)

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.

This can be rewritten as:

return nx.is_dominating_set(G, nbunch) and nx.is_connected(nx.subgraph(G, nbunch))

Which makes me think, do we really need this method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree that this function seems a bit redundant, but when developing other connected dominating set algorithms it was required quite often, and having a single function call with a declarative name was quite helpful so we included it as well. We can do without it but it makes assertions more concise.

Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
* Add detailed algorithm description and fix runtime complexity in
  connected_dominating_set
* Add use of itertools.count to avoid comparisons between nodes
* Rewrite is_dominating_set in a more concise manner
* Remove function for finding a minimal connected dominating set in
  interval graphs and its corresponding tests
* Add detailed algorithm description and fix runtime complexity in
  connected_dominating_set
* Add use of itertools.count to avoid comparisons between nodes
* Rewrite is_connected_dominating_set in a more concise manner
* Remove function for finding a minimal connected dominating set in
  interval graphs and its corresponding tests
@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Minor fix of commit message: the third bullet should be "Rewrite is_connected_dominating_set..."

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

Good stuff. Added minor comments!

Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
 - Change docstrings to follow an imperative mood and correct a typo
 - Change connected_dominating_set docstring output set name
 - Add failure logic when input graph is disconnected
 - Add test to test example in docstring

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

Amazing! Implementation looks good to me, no further comments there. I added other minor comments I missed for the unit tests.

I think we should also polish PR description a bit. Like having different sections:

  • What you are adding
  • Why you are adding it
  • Implementation details. You can also include a brief explanation of the algorithm and quote the paper.
  • How you tested it

You can use this one as an example #7945

@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Amazing! Implementation looks good to me, no further comments there. I added other minor comments I missed for the unit tests.

I think we should also polish PR description a bit. Like having different sections:

* What you are adding

* Why you are adding it

* Implementation details. You can also include a brief explanation of the algorithm and quote the paper.

* How you tested it

You can use this one as an example #7945

Should I edit the original description?

@amcandio

Copy link
Copy Markdown
Contributor

Amazing! Implementation looks good to me, no further comments there. I added other minor comments I missed for the unit tests.
I think we should also polish PR description a bit. Like having different sections:

* What you are adding

* Why you are adding it

* Implementation details. You can also include a brief explanation of the algorithm and quote the paper.

* How you tested it

You can use this one as an example #7945

Should I edit the original description?

Yeah, exactly

@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Please also see the updated PR description.

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

Excellent work, approving! @dschult we have a good PR here, can you take a look?

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

It looks to me like this finds a spanning tree by repeatedly adding the max-remaining-degree node from all nodes connected to the existing tree.

I would have named white/gray/black as unseen, seen, dominating or something similar which provides a description of what they represent. But I'll leave that up to you.

It often amazes me how heaps end up being so efficient. :) Here, we have outdated information on the heap -- and we still update it in a lazy way that is efficient in terms of number of updates performed and always correct. On first read, it seems like a lot of updates could be needed, but further thought indicates that this is indeed a really good way to do it. :)

Thanks for this!

Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
 - Change names of data structures from white_nodes, gray_nodes,
   black_nodes to unseen, seen, connected_dominating_set, respectively
 - Update paragraph in notes to reflect the new names from above
 - Replace set comprehension with built-in feature for initializing
   degree dict
 - Use original heappush and heappop instead of aliases push and pop
 - Inline `_update` subroutine
@GalMichaeli

GalMichaeli commented Apr 14, 2025 via email

Copy link
Copy Markdown
Contributor Author

@GalMichaeli
GalMichaeli requested a review from dschult April 14, 2025 14:28
@GalMichaeli

Copy link
Copy Markdown
Contributor Author

I’ve removed the change to imperative mood and the comment in the parentheses.

Please let me know if there’s anything else that needs changing.

Thanks.

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

I'm still working on the notes section... Can you see what you think of this suggestion?

Comment thread networkx/algorithms/dominating.py Resolved

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

One more very small change since we are in here with this code anyway.

Can you remove line 8 and line 10 (importing features from nx directly)
and replace where they are being used with nx.utils.arbitrary_element or nx.utils.not_implemented_for? That will help later when we clean up imports.

I can do that too if you prefer.

This all looks good to me!

@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Can you remove line 8 and line 10 (importing features from nx directly)
and replace where they are being used with nx.utils.arbitrary_element or nx.utils.not_implemented_for?
That will help later when we clean up imports.

Done!

@GalMichaeli
GalMichaeli requested a review from dschult April 19, 2025 09:59

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

This looks good. Just a few more suggestions.

And looking forward beyond this PR. Would it be helpful to have a function that yields all the connected dominating sets? We do have a SpanningTreeIterator that yields spanning trees in order of size, so we could remove the leaves from those to get CDSs. I don't know if it would be of interest -- or even if minimum CDS is a thing people care about. But it might not be hard to use existing NX tools to build it.

Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
Comment thread networkx/algorithms/dominating.py Resolved
 - Add a "Returns" section to is_dominating_set and
   is_connected_dominating_set docstring
 - Move "Raises" section from after "Examples" section to before it
 - Remove imperative mood from docstring of is_dominating_set
 - Add clarification regarding uniqueness to is_connected_dominating_set
@GalMichaeli

Copy link
Copy Markdown
Contributor Author

Would it be helpful to have a function that yields all the connected dominating sets? We do have a SpanningTreeIterator that yields spanning trees in order of size, so we could remove the leaves from those to get CDSs.

That's a great question. If the $i$ th spanning tree returned by the iterator is $ST_i$ then removing each one of its leaves yields a CDS. So I think the number of CDSs is in the order of
$$\sum_{i} \text{leaves}((ST_i)$$
I'm not sure how useful this is.

I don't know if it would be of interest -- or even if minimum CDS is a thing people care about.

Since finding a minimum CDS (MCDS) is NP-hard, this is an active research topic, especially for node/edge weighted graphs. Though here are algorithms to find an MCDS on certain types of graphs (permutation and interval graphs, and probably others).

@GalMichaeli
GalMichaeli requested a review from dschult April 20, 2025 15:18
@amcandio

amcandio commented Apr 20, 2025 •

Copy link
Copy Markdown
Contributor

It would be great to deeply understand the relationship between spanning trees and CDS. We could even leverage the random spanning tree functionality to derive a random minimal CDS.

What's not clear to me is how to guarantee that the dominating sets are drafted uniformly.

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

I can see why this could become exponential quickly if we start removing each leaf from a tree one at a time. And then all the pairs of leaves, etc. But to find a minimum CDS we would want to remove all the leaves. Do we get close to the minimum CDS by looking at all MSTs and removing all the leaves from them?

Note that the "minimum" for CDS is just the smallest number of nodes, while the "minimum" for MST is the smallest sum of the weighted edges in the tree. So there might not be any relation at all.

Most desirable: are we guaranteed to get a MCDS by removing all leaves from all MSTs? (I suspect not, especially if they are weighted MSTs)
Pretty good: are we likely in most cases to get close to a MCDS using this approach? How close? How do you even measure how close a CDS is from a MCDS?

Probably this PR is better than that approach -- this PR uses degree info to select the nodes in the tree. So that is at least working toward fewer nodes being non-leaf. :) Thanks for this discussion.

I believe this is ready to be merged.

@amcandio

Copy link
Copy Markdown
Contributor

Note that the "minimum" for CDS is just the smallest number of nodes, while the "minimum" for MST is the smallest sum of the weighted edges in the tree. So there might not be any relation at all.

Correct, that's why I said just "spanning tree". Each ST could induce a minimal (not minimum) CDS once you remove the leaves and some other nodes maybe?

Most desirable: are we guaranteed to get a MCDS by removing all leaves from all MSTs? (I suspect not, especially if they are weighted MSTs) Pretty good: are we likely in most cases to get close to a MCDS using this approach? How close? How do you even measure how close a CDS is from a MCDS?

Regarding "pretty good" there's this paper https://arxiv.org/abs/2303.03125#:~:text=For%20an%20m%2Dedge%20connected,dear%20friend%2C%20Professor%20Takao%20Nishizeki. If I understand that correctly, the algorithm finds a spanning tree with at most 2x more leaves than optimal solution. Meaning that you can probably use that algo to induce a CDSs of size S where |G|-|S| is at most 2x from optimum.

I believe this is ready to be merged.

+1 Good job @GalMichaeli!

@dschult
dschult merged commit 81df24c into networkx:main Apr 21, 2025
@jarrodmillman jarrodmillman added this to the 3.5 milestone May 29, 2025
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.

4 participants