forked from Qiskit/rustworkx
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a new function transitive_closure_dag() which is an optimized method for computing the transitive closure for DAGs. In support of this a new function descendants_at_distance() for finding the nodes a fixed distance from a given source to both rustworkx and rustworkx-core. Related to: Qiskit#704
- Loading branch information
Showing
9 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
releasenotes/notes/add-transitive-closure-dag-3fb45113d552f007.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new function ``descendants_at_distance`` to the rustworkx-core | ||
crate under the ``traversal`` module | ||
- | | ||
Added a new function, :func:`~.transitive_closure_dag`, which provides | ||
an optimize method for computing the transitive closure of an input | ||
DAG. | ||
- | | ||
Added a new function :func:`~.descendants_at_distance` which provides | ||
a method to find the nodes at a fixed distance from a source in | ||
a graph object. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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::visit::{IntoNeighborsDirected, NodeCount, Visitable}; | ||
|
||
/// Returns all nodes at a fixed `distance` from `source` in `G`. | ||
/// Args: | ||
/// `graph`: | ||
/// `source`: | ||
/// `distance`: | ||
pub fn descendants_at_distance<G>(graph: G, source: G::NodeId, distance: usize) -> Vec<G::NodeId> | ||
where | ||
G: Visitable + IntoNeighborsDirected + NodeCount, | ||
G::NodeId: std::cmp::Eq + std::hash::Hash, | ||
{ | ||
let mut current_layer: Vec<G::NodeId> = vec![source]; | ||
let mut layers: usize = 0; | ||
let mut visited: HashSet<G::NodeId> = HashSet::with_capacity(graph.node_count()); | ||
visited.insert(source); | ||
while !current_layer.is_empty() && layers < distance { | ||
let mut next_layer: Vec<G::NodeId> = Vec::new(); | ||
for node in current_layer { | ||
for child in graph.neighbors_directed(node, petgraph::Outgoing) { | ||
if !visited.contains(&child) { | ||
visited.insert(child); | ||
next_layer.push(child); | ||
} | ||
} | ||
} | ||
current_layer = next_layer; | ||
layers += 1; | ||
} | ||
current_layer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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 TestTransitivity(unittest.TestCase): | ||
def test_path_graph(self): | ||
graph = rx.generators.directed_path_graph(4) | ||
transitive_closure = rx.transitive_closure_dag(graph) | ||
expected_edge_list = [(0, 1), (1, 2), (2, 3), (1, 3), (0, 3), (0, 2)] | ||
self.assertEqual(transitive_closure.edge_list(), expected_edge_list) | ||
|
||
def test_invalid_type(self): | ||
with self.assertRaises(TypeError): | ||
rx.transitive_closure_dag(rx.PyGraph()) | ||
|
||
def test_cycle_error(self): | ||
graph = rx.PyDiGraph() | ||
graph.extend_from_edge_list([(0, 1), (1, 0)]) | ||
with self.assertRaises(rx.DAGHasCycle): | ||
rx.transitive_closure_dag(graph) |