forked from Qiskit/rustworkx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Transitive Reduction Function (Qiskit#923)
* Implementing filter_nodes and filter_edges funcs * Running fmt and clippy * Fixed issue where errors were not being propagated up to Python. Created tests for filter_edges and filter_nodes for both PyGraph and PyDiGraph. Created release notes for the functions. * Ran fmt, clippy, and tox * Fixing release notes * Fixing release notes again * Fixing release notes again again * Fixed release notes * Fixed release notes. Changed Vec allocation. Expanded on documentation. * ran cargo fmt and clippy * working on adding different parallel edge behavior * Fixing docs for filter functions * Working on graph_adjacency_matrix * Implementing changes to graph_adjacency_matrix and digraph_adjacency_matrix * working on release notes * Fixed release notes and docs * Ran cargo fmt * Ran cargo clippy * Fixed digraph_adjacency_matrix, passes tests * Removed mpl_draw from r elease notes * Changed if-else blocks in adjacency_matrix functions to match blocks. Wrote tests. * Fixed tests to pass lint * Added transitive reduction function to dag algo module * Fixed issue with graph that have nodes removed. Function now returns index_map for cases where there were nodes removed. Added tests. * Changing graph.nodes_removed to be false again. Return graph does not have removed nodes * Adding requested changes: - Fixing Docs - Fixing Maps to only have capacity of node_count - Fixing tests * Adding function to DAG algorithsm index
- Loading branch information
1 parent
f6666dd
commit fdc17eb
Showing
5 changed files
with
206 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
36 changes: 36 additions & 0 deletions
36
releasenotes/notes/transitive-reduction-6db2b80351c15887.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,36 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new function, :func:`~.transitive_reduction` which returns the transtive reduction | ||
of a given :class:`~rustworkx.PyDiGraph` and a dictionary with the mapping of indices from the given graph to the returned graph. | ||
The given graph must be a Directed Acyclic Graph (DAG). | ||
For example: | ||
.. jupyter-execute:: | ||
from rustworkx import PyDiGraph | ||
from rustworkx import transitive_reduction | ||
graph = PyDiGraph() | ||
a = graph.add_node("a") | ||
b = graph.add_node("b") | ||
c = graph.add_node("c") | ||
d = graph.add_node("d") | ||
e = graph.add_node("e") | ||
graph.add_edges_from([ | ||
(a, b, 1), | ||
(a, d, 1), | ||
(a, c, 1), | ||
(a, e, 1), | ||
(b, d, 1), | ||
(c, d, 1), | ||
(c, e, 1), | ||
(d, e, 1) | ||
]) | ||
tr, _ = transitive_reduction(graph) | ||
list(tr.edge_list()) | ||
Ref: https://en.wikipedia.org/wiki/Transitive_reduction | ||
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
76 changes: 76 additions & 0 deletions
76
tests/rustworkx_tests/digraph/test_transitive_reduction.py
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,76 @@ | ||
# 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 | ||
|
||
|
||
class TestTransitiveReduction(unittest.TestCase): | ||
def test_tr1(self): | ||
graph = rustworkx.PyDiGraph() | ||
a = graph.add_node("a") | ||
b = graph.add_node("b") | ||
c = graph.add_node("c") | ||
d = graph.add_node("d") | ||
e = graph.add_node("e") | ||
graph.add_edges_from( | ||
[(a, b, 1), (a, d, 1), (a, c, 1), (a, e, 1), (b, d, 1), (c, d, 1), (c, e, 1), (d, e, 1)] | ||
) | ||
tr, _ = rustworkx.transitive_reduction(graph) | ||
self.assertCountEqual(list(tr.edge_list()), [(0, 2), (0, 1), (1, 3), (2, 3), (3, 4)]) | ||
|
||
def test_tr2(self): | ||
graph2 = rustworkx.PyDiGraph() | ||
a = graph2.add_node("a") | ||
b = graph2.add_node("b") | ||
c = graph2.add_node("c") | ||
graph2.add_edges_from( | ||
[ | ||
(a, b, 1), | ||
(b, c, 1), | ||
(a, c, 1), | ||
] | ||
) | ||
tr2, _ = rustworkx.transitive_reduction(graph2) | ||
self.assertCountEqual(list(tr2.edge_list()), [(0, 1), (1, 2)]) | ||
|
||
def test_tr3(self): | ||
graph3 = rustworkx.PyDiGraph() | ||
graph3.add_nodes_from([0, 1, 2, 3]) | ||
graph3.add_edges_from([(0, 1, 1), (0, 2, 1), (0, 3, 1), (1, 2, 1), (1, 3, 1)]) | ||
tr3, _ = rustworkx.transitive_reduction(graph3) | ||
self.assertCountEqual(list(tr3.edge_list()), [(0, 1), (1, 2), (1, 3)]) | ||
|
||
def test_tr_with_deletion(self): | ||
graph = rustworkx.PyDiGraph() | ||
a = graph.add_node("a") | ||
b = graph.add_node("b") | ||
c = graph.add_node("c") | ||
d = graph.add_node("d") | ||
e = graph.add_node("e") | ||
|
||
graph.add_edges_from( | ||
[(a, b, 1), (a, d, 1), (a, c, 1), (a, e, 1), (b, d, 1), (c, d, 1), (c, e, 1), (d, e, 1)] | ||
) | ||
|
||
graph.remove_node(3) | ||
|
||
tr, index_map = rustworkx.transitive_reduction(graph) | ||
|
||
self.assertCountEqual(list(tr.edge_list()), [(0, 1), (0, 2), (2, 3)]) | ||
self.assertEqual(index_map[4], 3) | ||
|
||
def test_tr_error(self): | ||
digraph = rustworkx.generators.directed_cycle_graph(1000) | ||
with self.assertRaises(ValueError): | ||
rustworkx.transitive_reduction(digraph) |