Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edge_subgraph graph method #391

Merged
merged 5 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions releasenotes/notes/add-edge-subgraph-eba30930f679895b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
features:
- |
Added a new method, :meth:`~retworkx.PyDiGraph.edge_subgraph`, to the
:class:`~retworkx.PyDiGraph` and :class:`~retworkx.PyGraph`
(:meth:`~retworkx.PyGraph.edge_subgraph`) to get an edge induced subgraph
from a given graph object.
59 changes: 59 additions & 0 deletions src/digraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,65 @@ impl PyDiGraph {
}
}

/// Return a new PyDiGraph object for an edge induced subgraph of this graph
///
/// The induced subgraph contains each edge in `edges` and each node
/// incident to any of those edges.
///
/// :param list edges: A list of edge tuples (2-tuples with the source and
/// target node) to generate the subgraph from. In cases of parallel
/// edges for a multigraph all edges between the specified node. In case
/// of an edge specified that doesn't exist in the graph it will be
/// silently ignored.
///
/// :returns: The edge subgraph
/// :rtype: PyDiGraph
///
#[pyo3(text_signature = "(self, edges, /)")]
pub fn edge_subgraph(&self, edge_list: Vec<[usize; 2]>) -> PyDiGraph {
// Filter non-existent edges
let edges: Vec<[usize; 2]> = edge_list
.into_iter()
.filter(|x| {
let source = NodeIndex::new(x[0]);
let target = NodeIndex::new(x[1]);
self.graph.find_edge(source, target).is_some()
})
.collect();

let nodes: HashSet<NodeIndex> = edges
.iter()
.map(|x| x.iter())
.flatten()
.copied()
.map(NodeIndex::new)
.collect();
let mut edge_set: HashSet<[NodeIndex; 2]> =
HashSet::with_capacity(edges.len());
for edge in edges {
let source_index = NodeIndex::new(edge[0]);
let target_index = NodeIndex::new(edge[1]);
edge_set.insert([source_index, target_index]);
}
let mut out_graph = self.clone();
for node in self
.graph
.node_indices()
.filter(|node| !nodes.contains(node))
{
out_graph.graph.remove_node(node);
out_graph.node_removed = true;
}
for edge in self
.graph
.edge_references()
.filter(|edge| !edge_set.contains(&[edge.source(), edge.target()]))
{
out_graph.graph.remove_edge(edge.id());
}
out_graph
}

/// Check if the graph is symmetric
///
/// :returns: True if the graph is symmetric
Expand Down
58 changes: 58 additions & 0 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,64 @@ impl PyGraph {
}
}

/// Return a new PyGraph object for an edge induced subgrapph of this graph
///
/// The induced subgraph contains each edge in `edges` and each node
/// incident to any of those edges.
///
/// :param list edge_list: A list of edge tuples (2-tuples with the source
/// and target node) to generate the subgraph from. In cases of parallel
/// edges for a multigraph all edges between the specified node. In case
/// of an edge specified that doesn't exist in the graph it will be
/// silently ignored.
///
/// :returns: The edge subgraph
/// :rtype: PyDiGraph
///
#[pyo3(text_signature = "(self, edges, /)")]
pub fn edge_subgraph(&self, edge_list: Vec<[usize; 2]>) -> PyGraph {
// Filter non-existent edges
let edges: Vec<[usize; 2]> = edge_list
.into_iter()
.filter(|x| {
let source = NodeIndex::new(x[0]);
let target = NodeIndex::new(x[1]);
self.graph.find_edge(source, target).is_some()
})
.collect();

let nodes: HashSet<NodeIndex> = edges
.iter()
.map(|x| x.iter())
.flatten()
.copied()
.map(NodeIndex::new)
.collect();
let mut edge_set: HashSet<[NodeIndex; 2]> =
HashSet::with_capacity(edges.len());
for edge in edges {
let source_index = NodeIndex::new(edge[0]);
let target_index = NodeIndex::new(edge[1]);
edge_set.insert([source_index, target_index]);
}
let mut out_graph = self.clone();
for node in self
.graph
.node_indices()
.filter(|node| !nodes.contains(node))
{
out_graph.graph.remove_node(node);
out_graph.node_removed = true;
}
for edge in self.graph.edge_references().filter(|edge| {
!edge_set.contains(&[edge.source(), edge.target()])
&& !edge_set.contains(&[edge.target(), edge.source()])
}) {
out_graph.graph.remove_edge(edge.id());
}
out_graph
}

/// Return a shallow copy of the graph
///
/// All node and edge weight/data payloads in the copy will have a
Expand Down
66 changes: 66 additions & 0 deletions tests/digraph/test_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,69 @@ def test_subgraph_replace_weight_no_reference(self):
self.assertEqual([{"a": 0}, "b", "d"], subgraph.nodes())
graph[0] = 4
self.assertEqual(subgraph[0]["a"], 0)

def test_edge_subgraph(self):
graph = retworkx.PyDiGraph()
graph.add_node("a")
graph.add_node("b")
graph.add_node("c")
graph.add_node("d")
graph.add_edges_from([(0, 1, 1), (0, 2, 2), (0, 3, 3), (1, 3, 4)])
subgraph = graph.edge_subgraph([(0, 1), (1, 3)])
self.assertEqual(["a", "b", "d"], subgraph.nodes())
self.assertEqual([(0, 1, 1), (1, 3, 4)], subgraph.weighted_edge_list())

def test_edge_subgraph_parallel_edge(self):
graph = retworkx.PyDiGraph()
graph.add_nodes_from(list(range(4)))
graph.extend_from_weighted_edge_list(
[
(0, 1, 2),
(0, 1, 3),
(0, 2, 2),
(1, 2, 4),
(0, 3, 5),
(2, 3, 6),
]
)
subgraph = graph.edge_subgraph([(0, 1), (1, 2)])
self.assertEqual([0, 1, 2], subgraph.nodes())
self.assertEqual(
[(0, 1, 2), (0, 1, 3), (1, 2, 4)], subgraph.weighted_edge_list()
)

def test_edge_subgraph_empty_list(self):
graph = retworkx.PyDiGraph()
graph.add_nodes_from(list(range(4)))
graph.extend_from_weighted_edge_list(
[
(0, 1, 2),
(0, 1, 3),
(0, 2, 2),
(1, 2, 4),
(0, 3, 5),
(2, 3, 6),
]
)
subgraph = graph.edge_subgraph([])
self.assertEqual([], subgraph.nodes())

def test_edge_subgraph_non_edge(self):
graph = retworkx.PyDiGraph()
graph.add_nodes_from(list(range(4)))
graph.extend_from_weighted_edge_list(
[
(0, 1, 2),
(0, 1, 3),
(0, 2, 2),
(1, 2, 4),
(0, 3, 5),
(2, 3, 6),
]
)
# 1->3 isn't an edge in graph
subgraph = graph.edge_subgraph([(0, 1), (1, 2), (1, 3)])
self.assertEqual([0, 1, 2], subgraph.nodes())
self.assertEqual(
[(0, 1, 2), (0, 1, 3), (1, 2, 4)], subgraph.weighted_edge_list()
)
66 changes: 66 additions & 0 deletions tests/graph/test_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,69 @@ def test_subgraph_replace_weight_no_reference(self):
self.assertEqual([{"a": 0}, "b", "d"], subgraph.nodes())
graph[0] = 4
self.assertEqual(subgraph[0]["a"], 0)

def test_edge_subgraph(self):
graph = retworkx.PyGraph()
graph.add_node("a")
graph.add_node("b")
graph.add_node("c")
graph.add_node("d")
graph.add_edges_from([(0, 1, 1), (0, 2, 2), (0, 3, 3), (1, 3, 4)])
subgraph = graph.edge_subgraph([(0, 1), (1, 3)])
self.assertEqual(["a", "b", "d"], subgraph.nodes())
self.assertEqual([(0, 1, 1), (1, 3, 4)], subgraph.weighted_edge_list())

def test_edge_subgraph_parallel_edge(self):
graph = retworkx.PyGraph()
graph.add_nodes_from(list(range(4)))
graph.extend_from_weighted_edge_list(
[
(0, 1, 2),
(0, 1, 3),
(0, 2, 2),
(1, 2, 4),
(0, 3, 5),
(2, 3, 6),
]
)
subgraph = graph.edge_subgraph([(0, 1), (1, 2)])
self.assertEqual([0, 1, 2], subgraph.nodes())
self.assertEqual(
[(0, 1, 2), (0, 1, 3), (1, 2, 4)], subgraph.weighted_edge_list()
)

def test_edge_subgraph_empty_list(self):
graph = retworkx.PyGraph()
graph.add_nodes_from(list(range(4)))
graph.extend_from_weighted_edge_list(
[
(0, 1, 2),
(0, 1, 3),
(0, 2, 2),
(1, 2, 4),
(0, 3, 5),
(2, 3, 6),
]
)
subgraph = graph.edge_subgraph([])
self.assertEqual([], subgraph.nodes())

def test_edge_subgraph_non_edge(self):
graph = retworkx.PyGraph()
graph.add_nodes_from(list(range(4)))
graph.extend_from_weighted_edge_list(
[
(0, 1, 2),
(0, 1, 3),
(0, 2, 2),
(1, 2, 4),
(0, 3, 5),
(2, 3, 6),
]
)
# 1->3 isn't an edge in graph
subgraph = graph.edge_subgraph([(0, 1), (1, 2), (1, 3)])
self.assertEqual([0, 1, 2], subgraph.nodes())
self.assertEqual(
[(0, 1, 2), (0, 1, 3), (1, 2, 4)], subgraph.weighted_edge_list()
)