forked from Qiskit/rustworkx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
union.rs
195 lines (178 loc) · 7.44 KB
/
union.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 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 crate::{digraph, find_node_by_weight, graph, StablePyGraph};
use petgraph::stable_graph::NodeIndex;
use petgraph::visit::{EdgeRef, IntoEdgeReferences, NodeIndexable};
use petgraph::{algo, EdgeType};
use pyo3::prelude::*;
use pyo3::Python;
#[derive(Copy, Clone)]
enum Entry<T> {
Merged(T),
Added(T),
None,
}
fn extract<T>(x: Entry<T>) -> T {
match x {
Entry::Merged(val) => val,
Entry::Added(val) => val,
Entry::None => panic!("Unexpected internal error: called `Entry::extract()` on a `None` value. Please file an issue at https://github.com/Qiskit/rustworkx/issues/new/choose with the details on how you encountered this."),
}
}
fn union<Ty: EdgeType>(
py: Python,
first: &StablePyGraph<Ty>,
second: &StablePyGraph<Ty>,
merge_nodes: bool,
merge_edges: bool,
) -> PyResult<StablePyGraph<Ty>> {
let mut out_graph = first.clone();
let mut node_map: Vec<Entry<NodeIndex>> = vec![Entry::None; second.node_bound()];
for node in second.node_indices() {
let weight = &second[node];
if merge_nodes {
if let Some(index) = find_node_by_weight(py, first, weight)? {
node_map[node.index()] = Entry::Merged(index);
continue;
}
}
let index = out_graph.add_node(weight.clone_ref(py));
node_map[node.index()] = Entry::Added(index);
}
let weights_equal = |a: &PyObject, b: &PyObject| -> PyResult<bool> {
a.as_ref(py)
.rich_compare(b, pyo3::basic::CompareOp::Eq)?
.is_true()
};
for edge in second.edge_references() {
let source = edge.source().index();
let target = edge.target().index();
let new_weight = edge.weight();
let mut found = false;
if merge_edges {
// if both endpoints were merged,
// check if need to skip the edge as well.
if let (Entry::Merged(new_source), Entry::Merged(new_target)) =
(node_map[source], node_map[target])
{
for edge in first.edges(new_source) {
if edge.target() == new_target && weights_equal(new_weight, edge.weight())? {
found = true;
break;
}
}
}
}
if !found {
let new_source = extract(node_map[source]);
let new_target = extract(node_map[target]);
out_graph.add_edge(new_source, new_target, new_weight.clone_ref(py));
}
}
Ok(out_graph)
}
/// Return a new PyGraph by forming a union from two input PyGraph objects
///
/// The algorithm in this function operates in three phases:
///
/// 1. Add all the nodes from ``second`` into ``first``. operates in
/// :math:`\mathcal{O}(n_2)`, with :math:`n_2` being number of nodes in
/// ``second``.
/// 2. Merge nodes from ``second`` over ``first`` given that:
///
/// - The ``merge_nodes`` is ``True``. operates in :math:`\mathcal{O}(n_1 n_2)`,
/// with :math:`n_1` being the number of nodes in ``first`` and :math:`n_2`
/// the number of nodes in ``second``
/// - The respective node in ``second`` and ``first`` share the same
/// weight/data payload.
///
/// 3. Adds all the edges from ``second`` to ``first``. If the ``merge_edges``
/// parameter is ``True`` and the respective edge in ``second`` and
/// ``first`` share the same weight/data payload they will be merged together.
///
/// :param PyGraph first: The first undirected graph object
/// :param PyGraph second: The second undirected graph object
/// :param bool merge_nodes: If set to ``True`` nodes will be merged between
/// ``second`` and ``first`` if the weights are equal. Default: ``False``.
/// :param bool merge_edges: If set to ``True`` edges will be merged between
/// ``second`` and ``first`` if the weights are equal. Default: ``False``.
///
/// :returns: A new PyGraph object that is the union of ``second`` and
/// ``first``. It's worth noting the weight/data payload objects are
/// passed by reference from ``first`` and ``second`` to this new object.
/// :rtype: PyGraph
#[pyfunction]
#[pyo3(signature=(first, second, merge_nodes=false, merge_edges=false), text_signature = "(first, second, /, merge_nodes=False, merge_edges=False)")]
pub fn graph_union(
py: Python,
first: &graph::PyGraph,
second: &graph::PyGraph,
merge_nodes: bool,
merge_edges: bool,
) -> PyResult<graph::PyGraph> {
let out_graph = union(py, &first.graph, &second.graph, merge_nodes, merge_edges)?;
Ok(graph::PyGraph {
graph: out_graph,
node_removed: first.node_removed,
multigraph: true,
attrs: py.None(),
})
}
/// Return a new PyDiGraph by forming a union from two input PyDiGraph objects
///
/// The algorithm in this function operates in three phases:
///
/// 1. Add all the nodes from ``second`` into ``first``. operates in
/// :math:`\mathcal{O}(n_2)`, with :math:`n_2` being number of nodes in
/// ``second``.
/// 2. Merge nodes from ``second`` over ``first`` given that:
///
/// - The ``merge_nodes`` is ``True``. operates in :math:`\mathcal{O}(n_1 n_2)`,
/// with :math:`n_1` being the number of nodes in ``first`` and :math:`n_2`
/// the number of nodes in ``second``
/// - The respective node in ``second`` and ``first`` share the same
/// weight/data payload.
///
/// 3. Adds all the edges from ``second`` to ``first``. If the ``merge_edges``
/// parameter is ``True`` and the respective edge in ``second`` and
/// ``first`` share the same weight/data payload they will be merged together.
///
/// :param PyDiGraph first: The first directed graph object
/// :param PyDiGraph second: The second directed graph object
/// :param bool merge_nodes: If set to ``True`` nodes will be merged between
/// ``second`` and ``first`` if the weights are equal. Default: ``False``.
/// :param bool merge_edges: If set to ``True`` edges will be merged between
/// ``second`` and ``first`` if the weights are equal. Default: ``False``.
///
/// :returns: A new PyDiGraph object that is the union of ``second`` and
/// ``first``. It's worth noting the weight/data payload objects are
/// passed by reference from ``first`` and ``second`` to this new object.
/// :rtype: PyDiGraph
#[pyfunction]
#[pyo3(signature=(first, second, merge_nodes=false, merge_edges=false), text_signature = "(first, second, /, merge_nodes=False, merge_edges=False)")]
pub fn digraph_union(
py: Python,
first: &digraph::PyDiGraph,
second: &digraph::PyDiGraph,
merge_nodes: bool,
merge_edges: bool,
) -> PyResult<digraph::PyDiGraph> {
let out_graph = union(py, &first.graph, &second.graph, merge_nodes, merge_edges)?;
Ok(digraph::PyDiGraph {
graph: out_graph,
cycle_state: algo::DfsSpace::default(),
check_cycle: false,
node_removed: first.node_removed,
multigraph: true,
attrs: py.None(),
})
}