diff --git a/releasenotes/notes/densest_subgraph-1b068f69f80facd4.yaml b/releasenotes/notes/densest_subgraph-1b068f69f80facd4.yaml new file mode 100644 index 000000000..a83383f55 --- /dev/null +++ b/releasenotes/notes/densest_subgraph-1b068f69f80facd4.yaml @@ -0,0 +1,31 @@ +--- +features: + - | + Added a new function, :func:`~.densest_subgraph_of_size`, which is used to return a + subgraph of given size that has the highest degree of connecitivity between the nodes. + For example, if you wanted to find the subgraph of 5 nodes in a 19 node heavy hexagon + graph: + + .. jupyter-execute:: + + import rustworkx as rx + from rustworkx.visualization import mpl_draw + + graph = rx.generators.hexagonal_lattice_graph(4, 5) + + subgraph, node_map = rx.densest_subgraph_of_size(graph, 5) + subgraph_edge_set = set(subgraph.edge_list()) + node_colors = [] + for node in graph.node_indices(): + if node in node_map: + node_colors.append('red') + else: + node_colors.append('blue') + graph[node] = node + edge_colors = [] + for edge in graph.edge_list(): + if edge[0] in node_map and edge[1] in node_map: + edge_colors.append('red') + else: + edge_colors.append('blue') + mpl_draw(graph, with_labels=True, node_color=node_colors, edge_color=edge_colors, labels=str) diff --git a/rustworkx-core/src/dense_subgraph.rs b/rustworkx-core/src/dense_subgraph.rs new file mode 100644 index 000000000..714fd7a9c --- /dev/null +++ b/rustworkx-core/src/dense_subgraph.rs @@ -0,0 +1,212 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +use hashbrown::{HashMap, HashSet}; +use std::hash::Hash; + +use petgraph::prelude::*; +use petgraph::visit::{ + EdgeCount, GraphProp, IntoEdgeReferences, IntoNeighbors, IntoNodeIdentifiers, NodeCount, + Visitable, +}; + +use rayon::prelude::*; + +struct SubsetResult { + pub count: usize, + pub error: f64, + pub map: Vec, +} + +/// Find the most densely connected k-subgraph +/// +/// This function will return the node indices of the subgraph of `num_nodes` that is the +/// most densely connected. +/// +/// This method does not provide any guarantees on the approximation as it +/// does a naive search using BFS traversal. +/// +/// # Arguments +/// +/// * `graph` - The graph to find densest subgraph in. +/// * `num_nodes` - The number of nodes in the subgraph to find +/// * `edge_weight_callback` - An optional callable that if specified will be +/// passed the node indices of each edge in the graph and it is expected to +/// return a float value. If specified the lowest avg weight for edges in +/// a found subgraph will be a criteria for selection in addition to the +/// connectivity of the subgraph. +/// * `node_weight_callback` - An optional callable that if specified will be +/// passed the node indices of each node in the graph and it is expected to +/// return a float value. If specified the lowest avg weight for node of +/// a found subgraph will be a criteria for selection in addition to the +/// connectivity of the subgraph. +/// +/// # Example: +/// +/// ```rust +/// use std::convert::Infallible; +/// use rustworkx_core::petgraph::stable_graph::{StableDiGraph, NodeIndex}; +/// use rustworkx_core::petgraph::visit::IntoEdgeReferences; +/// use rustworkx_core::generators::grid_graph; +/// use rustworkx_core::dense_subgraph::densest_subgraph; +/// +/// type EdgeWeightType = Box as IntoEdgeReferences>::EdgeRef) -> Result>; +/// type NodeWeightType = Box Result>; +/// +/// let graph: StableDiGraph<(), ()> = grid_graph( +/// Some(10), +/// Some(10), +/// None, +/// || {()}, +/// || {()}, +/// false +/// ).unwrap(); +/// let subgraph_nodes = densest_subgraph(&graph, 10, None::, None::).unwrap(); +/// +/// let expected = vec![ +/// NodeIndex::new(7), NodeIndex::new(8), NodeIndex::new(17), NodeIndex::new(9), +/// NodeIndex::new(18), NodeIndex::new(27), NodeIndex::new(19), NodeIndex::new(28), +/// NodeIndex::new(37), NodeIndex::new(29) +/// ]; +/// +/// assert_eq!(subgraph_nodes, expected); +/// ``` +pub fn densest_subgraph( + graph: G, + num_nodes: usize, + edge_weight_callback: Option, + node_weight_callback: Option, +) -> Result, E> +where + G: IntoNodeIdentifiers + + IntoEdgeReferences + + EdgeCount + + GraphProp + + NodeCount + + IntoNeighbors + + Visitable + + Sync, + G::NodeId: Eq + Hash + Send + Sync, + F: FnMut(G::NodeId) -> Result, + H: FnMut(G::EdgeRef) -> Result, +{ + let node_indices: Vec = graph.node_identifiers().collect(); + let mut edge_weight_map: Option> = None; + let mut node_weight_map: Option> = None; + + if edge_weight_callback.is_some() { + let mut inner_weight_map: HashMap<[G::NodeId; 2], f64> = + HashMap::with_capacity(graph.edge_count()); + let mut callback = edge_weight_callback.unwrap(); + for edge in graph.edge_references() { + let source = edge.source(); + let target = edge.target(); + let weight = callback(edge)?; + inner_weight_map.insert([source, target], weight); + if !graph.is_directed() { + inner_weight_map.insert([target, source], weight); + } + } + edge_weight_map = Some(inner_weight_map); + } + let mut avg_node_error: f64 = 0.; + if node_weight_callback.is_some() { + let mut callback = node_weight_callback.unwrap(); + let mut inner_weight_map: HashMap = + HashMap::with_capacity(graph.node_count()); + for node in graph.node_identifiers() { + let weight = callback(node)?; + avg_node_error += weight; + inner_weight_map.insert(node, weight); + } + avg_node_error /= graph.node_count() as f64; + node_weight_map = Some(inner_weight_map); + } + let reduce_identity_fn = || -> SubsetResult { + SubsetResult { + count: 0, + map: Vec::new(), + error: f64::INFINITY, + } + }; + + let reduce_fn = + |best: SubsetResult, curr: SubsetResult| -> SubsetResult { + if edge_weight_map.is_some() || node_weight_map.is_some() { + if curr.count >= best.count && curr.error <= best.error { + curr + } else { + best + } + } else if curr.count > best.count { + curr + } else { + best + } + }; + + let best_result = node_indices + .into_par_iter() + .filter_map(|index| { + let mut subgraph: Vec<[G::NodeId; 2]> = Vec::with_capacity(num_nodes); + let mut bfs = Bfs::new(&graph, index); + let mut bfs_vec: Vec = Vec::with_capacity(num_nodes); + let mut bfs_set: HashSet = HashSet::with_capacity(num_nodes); + + let mut count = 0; + while let Some(node) = bfs.next(&graph) { + bfs_vec.push(node); + bfs_set.insert(node); + count += 1; + if count >= num_nodes { + break; + } + } + if bfs_vec.len() < num_nodes { + return None; + } + let mut connection_count = 0; + for node in &bfs_vec { + for nbr in graph.neighbors(*node).filter(|j| bfs_set.contains(j)) { + connection_count += 1; + subgraph.push([*node, nbr]); + } + } + let mut error = match &edge_weight_map { + Some(map) => { + subgraph.iter().map(|edge| map[edge]).sum::() / subgraph.len() as f64 + } + None => 0., + }; + error *= match &node_weight_map { + Some(map) => { + let subgraph_node_error_avg = + bfs_vec.iter().map(|node| map[node]).sum::() / num_nodes as f64; + let node_error_diff = subgraph_node_error_avg - avg_node_error; + if node_error_diff > 0. { + num_nodes as f64 * node_error_diff + } else { + 1. + } + } + None => 1., + }; + + Some(SubsetResult { + count: connection_count, + error, + map: bfs_vec, + }) + }) + .reduce(reduce_identity_fn, reduce_fn); + Ok(best_result.map) +} diff --git a/rustworkx-core/src/lib.rs b/rustworkx-core/src/lib.rs index fc5d6f5df..e19895212 100644 --- a/rustworkx-core/src/lib.rs +++ b/rustworkx-core/src/lib.rs @@ -109,6 +109,7 @@ pub mod planar; pub mod shortest_path; pub mod traversal; // These modules define additional data structures +pub mod dense_subgraph; pub mod dictmap; pub mod distancemap; mod min_scored; diff --git a/rustworkx/__init__.py b/rustworkx/__init__.py index 2943017fc..2be66a7c1 100644 --- a/rustworkx/__init__.py +++ b/rustworkx/__init__.py @@ -1873,6 +1873,33 @@ def longest_simple_path(graph): found in the graph. If the graph is empty ``None`` will be returned instead. :rtype: NodeIndices """ + raise TypeError("Invalid Input Type %s for graph" % type(graph)) + + +@_rustworkx_dispatch +def densest_subgraph_of_size( + graph, num_nodes, /, edge_weight_callback=None, node_weight_callback=None +): + """Find a connected and dense subgraph of a given size in a graph. + + This method does not provide any guarantees on the approximation of the optimal solution as it + does a naive search using BFS traversal. + + :param graph: The graph to find the densest subgraph in. This can be a + :class:`~retworkx.PyGraph` or a :class:`~retworkx.PyDiGraph`. + :param int num_nodes: The number of nodes in the subgraph to find + :param func weight_callback: An optional callable that if specified will be + passed the node indices of each edge in the graph and it is expected to + return a float value. If specified the lowest avg weight for edges in + a found subgraph will be a criteria for selection in addition to the + connectivity of the subgraph. + :returns: A tuple of the subgraph found and a :class:`~.NodeMap` of the + mapping of node indices in the input ``graph`` to the index in the + output subgraph. + + :rtype: (subgraph, node_map) + """ + raise TypeError("Invalid Input Type %s for graph" % type(graph)) @_rustworkx_dispatch diff --git a/rustworkx/__init__.pyi b/rustworkx/__init__.pyi index 11edc5922..43210318f 100644 --- a/rustworkx/__init__.pyi +++ b/rustworkx/__init__.pyi @@ -232,6 +232,8 @@ from .rustworkx import steiner_tree as steiner_tree from .rustworkx import metric_closure as metric_closure from .rustworkx import digraph_union as digraph_union from .rustworkx import graph_union as graph_union +from .rustworkx import digraph_densest_subgraph_of_size as digraph_densest_subgraph_of_size +from .rustworkx import graph_densest_subgraph_of_size as graph_densest_subgraph_of_size from .rustworkx import NodeIndices as NodeIndices from .rustworkx import PathLengthMapping as PathLengthMapping from .rustworkx import PathMapping as PathMapping @@ -602,3 +604,10 @@ def longest_simple_path(graph: PyGraph[_S, _T] | PyDiGraph[_S, _T]) -> NodeIndic def isolates(graph: PyGraph[_S, _T] | PyDiGraph[_S, _T]) -> NodeIndices: ... def two_color(graph: PyGraph[_S, _T] | PyDiGraph[_S, _T]) -> dict[int, int]: ... def is_bipartite(graph: PyGraph[_S, _T] | PyDiGraph[_S, _T]) -> bool: ... +def densest_subgraph_of_size( + graph: PyGraph[_S, _T] | PyDiGraph[_S, _T], + num_nodes: int, + /, + edge_weight_callback: Callable[[_T], float] | None = ..., + node_weight_callback: Callable[[_S], float] | None = ..., +) -> tuple[PyGraph[_S, _T], NodeMap]: ... diff --git a/rustworkx/rustworkx.pyi b/rustworkx/rustworkx.pyi index 47c8e3673..6999eabb1 100644 --- a/rustworkx/rustworkx.pyi +++ b/rustworkx/rustworkx.pyi @@ -1012,6 +1012,23 @@ def graph_union( merge_edges: bool = ..., ) -> PyGraph[_S, _T]: ... +# Densest Subgraph + +def graph_densest_subgraph_of_size( + graph: PyGraph[_S, _T], + num_nodes: int, + /, + edge_weight_callback: Callable[[_T], float] | None = ..., + node_weight_callback: Callable[[_T], float] | None = ..., +) -> tuple[PyGraph[_S, _T], NodeMap]: ... +def digraph_densest_subgraph_of_size( + graph: PyDiGraph[_S, _T], + num_nodes: int, + /, + edge_weight_callback: Callable[[_T], float] | None = ..., + node_weight_callback: Callable[[_T], float] | None = ..., +) -> tuple[PyGraph[_S, _T], NodeMap]: ... + # Iterators _T_co = TypeVar("_T_co", covariant=True) diff --git a/src/dense_subgraph.rs b/src/dense_subgraph.rs new file mode 100644 index 000000000..73dc6e19a --- /dev/null +++ b/src/dense_subgraph.rs @@ -0,0 +1,187 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + +use hashbrown::HashSet; + +use petgraph::algo; +use petgraph::graph::NodeIndex; +use petgraph::prelude::*; +use petgraph::visit::{IntoEdgeReferences, NodeFiltered}; + +use pyo3::prelude::*; +use pyo3::Python; + +use rustworkx_core::dense_subgraph::densest_subgraph; +use rustworkx_core::dictmap::*; + +use crate::digraph; +use crate::graph; +use crate::iterators::NodeMap; +use crate::StablePyGraph; + +/// Find densest subgraph in a :class:`~.PyGraph` +/// +/// This method does not provide any guarantees on the approximation as it +/// does a naive search using BFS traversal. +/// +/// :param PyGraph graph: The graph to find densest subgraph in. +/// :param int num_nodes: The number of nodes in the subgraph to find +/// :param func edge_weight_callback: An optional callable that if specified will be +/// passed the node indices of each edge in the graph and it is expected to +/// return a float value. If specified the lowest avg weight for edges in +/// a found subgraph will be a criteria for selection in addition to the +/// connectivity of the subgraph. +/// :param func node_weight_callback: An optional callable that if specified will be +/// passed the node indices of each node in the graph and it is expected to +/// return a float value. If specified the lowest avg weight for node of +/// a found subgraph will be a criteria for selection in addition to the +/// connectivity of the subgraph.// +/// :returns: A tuple of the subgraph found and a :class:`~.NodeMap` of the +/// mapping of node indices in the input ``graph`` to the index in the +/// output subgraph. +/// :rtype: (PyGraph, NodeMap) +#[pyfunction] +#[pyo3( + signature=(graph, num_nodes, /, edge_weight_callback=None, node_weight_callback=None) +)] +pub fn graph_densest_subgraph_of_size( + py: Python, + graph: &graph::PyGraph, + num_nodes: usize, + edge_weight_callback: Option, + node_weight_callback: Option, +) -> PyResult<(graph::PyGraph, NodeMap)> { + let edge_callback = edge_weight_callback.map(|callback| { + move |edge: <&StablePyGraph as IntoEdgeReferences>::EdgeRef| -> PyResult { + let res = callback + .bind(py) + .call1((edge.source().index(), edge.target().index()))?; + res.extract() + } + }); + let node_callback = node_weight_callback.map(|callback| { + move |node_index: NodeIndex| -> PyResult { + let res = callback.bind(py).call1((node_index.index(),))?; + res.extract() + } + }); + + let subgraph_nodes = densest_subgraph(&graph.graph, num_nodes, edge_callback, node_callback)?; + let node_subset: HashSet = subgraph_nodes.iter().copied().collect(); + let node_filter = |node: NodeIndex| -> bool { node_subset.contains(&node) }; + let filtered = NodeFiltered(&graph.graph, node_filter); + let mut inner_graph: StablePyGraph = + StablePyGraph::with_capacity(subgraph_nodes.len(), 0); + let node_map: DictMap = subgraph_nodes + .into_iter() + .map(|node| { + ( + node.index(), + inner_graph + .add_node(graph.graph.node_weight(node).unwrap().clone_ref(py)) + .index(), + ) + }) + .collect(); + for edge in filtered.edge_references() { + let new_source = NodeIndex::new(*node_map.get(&edge.source().index()).unwrap()); + let new_target = NodeIndex::new(*node_map.get(&edge.target().index()).unwrap()); + inner_graph.add_edge(new_source, new_target, edge.weight().clone_ref(py)); + } + let out_graph = graph::PyGraph { + graph: inner_graph, + node_removed: false, + multigraph: graph.multigraph, + attrs: py.None(), + }; + Ok((out_graph, NodeMap { node_map })) +} + +/// Find densest subgraph in a :class:`~.PyDiGraph` +/// +/// This method does not provide any guarantees on the approximation as it +/// does a naive search using BFS traversal. +/// +/// :param PyDiGraph graph: The graph to find the densest subgraph in. +/// :param int num_nodes: The number of nodes in the subgraph to find +/// :param func edge_weight_callback: An optional callable that if specified will be +/// passed the node indices of each edge in the graph and it is expected to +/// return a float value. If specified the lowest avg weight for edges in +/// a found subgraph will be a criteria for selection in addition to the +/// connectivity of the subgraph. +/// :param func node_weight_callback: An optional callable that if specified will be +/// passed the node indices of each node in the graph and it is expected to +/// return a float value. If specified the lowest avg weight for node of +/// a found subgraph will be a criteria for selection in addition to the +/// connectivity of the subgraph. +/// :returns: A tuple of the subgraph found and a :class:`~.NodeMap` of the +/// mapping of node indices in the input ``graph`` to the index in the +/// output subgraph. +/// :rtype: (PyDiGraph, NodeMap) +#[pyfunction] +#[pyo3( + signature = (graph, num_nodes, /, edge_weight_callback=None, node_weight_callback=None) +)] +pub fn digraph_densest_subgraph_of_size( + py: Python, + graph: &digraph::PyDiGraph, + num_nodes: usize, + edge_weight_callback: Option, + node_weight_callback: Option, +) -> PyResult<(digraph::PyDiGraph, NodeMap)> { + let edge_callback = edge_weight_callback.map(|callback| { + move |edge: <&StablePyGraph as IntoEdgeReferences>::EdgeRef| -> PyResult { + let res = callback + .bind(py) + .call1((edge.source().index(), edge.target().index()))?; + res.extract() + } + }); + let node_callback = node_weight_callback.map(|callback| { + move |node_index: NodeIndex| -> PyResult { + let res = callback.bind(py).call1((node_index.index(),))?; + res.extract() + } + }); + + let subgraph_nodes = densest_subgraph(&graph.graph, num_nodes, edge_callback, node_callback)?; + let node_subset: HashSet = subgraph_nodes.iter().copied().collect(); + let node_filter = |node: NodeIndex| -> bool { node_subset.contains(&node) }; + let filtered = NodeFiltered(&graph.graph, node_filter); + let mut inner_graph: StablePyGraph = + StablePyGraph::with_capacity(subgraph_nodes.len(), 0); + let node_map: DictMap = subgraph_nodes + .into_iter() + .map(|node| { + ( + node.index(), + inner_graph + .add_node(graph.graph.node_weight(node).unwrap().clone_ref(py)) + .index(), + ) + }) + .collect(); + for edge in filtered.edge_references() { + let new_source = NodeIndex::new(*node_map.get(&edge.source().index()).unwrap()); + let new_target = NodeIndex::new(*node_map.get(&edge.target().index()).unwrap()); + inner_graph.add_edge(new_source, new_target, edge.weight().clone_ref(py)); + } + let out_graph = digraph::PyDiGraph { + graph: inner_graph, + node_removed: false, + cycle_state: algo::DfsSpace::default(), + check_cycle: graph.check_cycle, + multigraph: graph.multigraph, + attrs: py.None(), + }; + Ok((out_graph, NodeMap { node_map })) +} diff --git a/src/lib.rs b/src/lib.rs index cce7c9175..711079943 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ mod centrality; mod coloring; mod connectivity; mod dag_algo; +mod dense_subgraph; mod digraph; mod dot_utils; mod generators; @@ -45,6 +46,7 @@ use centrality::*; use coloring::*; use connectivity::*; use dag_algo::*; +use dense_subgraph::*; use graphml::*; use isomorphism::*; use json::*; @@ -582,6 +584,8 @@ fn rustworkx(py: Python<'_>, m: &Bound) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(connected_subgraphs))?; m.add_wrapped(wrap_pyfunction!(is_planar))?; m.add_wrapped(wrap_pyfunction!(read_graphml))?; + m.add_wrapped(wrap_pyfunction!(digraph_densest_subgraph_of_size))?; + m.add_wrapped(wrap_pyfunction!(graph_densest_subgraph_of_size))?; m.add_wrapped(wrap_pyfunction!(digraph_node_link_json))?; m.add_wrapped(wrap_pyfunction!(graph_node_link_json))?; m.add_wrapped(wrap_pyfunction!(pagerank))?; diff --git a/tests/digraph/test_densest_subgraph.py b/tests/digraph/test_densest_subgraph.py new file mode 100644 index 000000000..c6bc15481 --- /dev/null +++ b/tests/digraph/test_densest_subgraph.py @@ -0,0 +1,31 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import unittest + +import rustworkx as rx + + +class TestDensestSubgraph(unittest.TestCase): + def test_simple_grid_three_nodes(self): + graph = rx.generators.grid_graph(3, 3) + subgraph, node_map = rx.densest_subgraph_of_size(graph, 3) + expected_subgraph_edge_list = [(0, 2), (0, 1)] + self.assertEqual(expected_subgraph_edge_list, subgraph.edge_list()) + self.assertEqual(node_map, {0: 0, 1: 1, 3: 2}) + + def test_simple_grid_six_nodes(self): + graph = rx.generators.grid_graph(3, 3) + subgraph, node_map = rx.densest_subgraph_of_size(graph, 6) + expected_subgraph_edge_list = [(5, 2), (5, 3), (3, 0), (3, 4), (4, 1), (2, 0), (0, 1)] + self.assertEqual(expected_subgraph_edge_list, subgraph.edge_list()) + self.assertEqual(node_map, {7: 0, 8: 1, 6: 2, 4: 3, 5: 4, 3: 5})