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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
use super::*;
static MERMAID_REPR_COUNTER: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
/// Represents a part of an edge that connects to one vertex. It can be directed or undirected.
#[cfg_attr(feature = "python_stubgen", gen_stub_pyclass)]
#[pyclass(from_py_object, name = "HalfEdge", module = "symbolica.core")]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct PythonHalfEdge {
half_edge: HalfEdge<Atom>,
}
#[cfg_attr(feature = "python_stubgen", gen_stub_pymethods)]
#[cfg_attr(not(feature = "python_stubgen"), remove_gen_stub)]
#[pymethods]
impl PythonHalfEdge {
/// Create a new half-edge. The `data` can be any expression, and the `direction` can be `True` (outgoing),
/// `False` (incoming) or `None` (undirected).
#[new]
#[pyo3(signature = (data, direction = None))]
fn new(data: ConvertibleToExpression, direction: Option<bool>) -> Self {
Self {
half_edge: match direction {
None => HalfEdge::undirected(data.to_expression().expr),
Some(false) => HalfEdge::incoming(data.to_expression().expr),
Some(true) => HalfEdge::outgoing(data.to_expression().expr),
},
}
}
/// Return a new half-edge with the direction flipped. Undirected edges remain undirected.
fn flip(&self) -> Self {
Self {
half_edge: self.half_edge.flip(),
}
}
/// Get the direction of the half-edge. `True` means outgoing, `False` means incoming, and `None` means undirected.
fn direction(&self) -> Option<bool> {
self.half_edge.direction
}
/// Get the data associated with the half-edge.
fn data(&self) -> PythonExpression {
self.half_edge.data.clone().into()
}
}
/// A graph that supported directional edges, parallel edges, self-edges and custom data on the nodes and edges.
///
/// Warning: modifying the graph if it is contained in a `dict` or `set` will invalidate the hash.
#[cfg_attr(feature = "python_stubgen", gen_stub_pyclass)]
#[pyclass(from_py_object, name = "Graph", module = "symbolica.core")]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct PythonGraph {
graph: Graph<Atom, Atom>,
}
#[cfg_attr(feature = "python_stubgen", gen_stub_pymethods)]
#[cfg_attr(not(feature = "python_stubgen"), remove_gen_stub)]
#[pymethods]
impl PythonGraph {
/// Create an empty graph.
#[new]
fn new() -> Self {
Self {
graph: Graph::new(),
}
}
/// Convert the graph into a portable string.
pub fn __repr__(&self) -> PyResult<String> {
Ok(format!("{}", self.graph))
}
/// Print the graph in a human-readable format.
fn __str__(&self) -> String {
format!("{}", self.graph)
}
/// Convert the graph into an HTML Mermaid representation.
fn _repr_html_(&self) -> String {
let diagram = crate::printer::AnsiHtmlFormatter::escape_html(&self.graph.to_mermaid());
let id = MERMAID_REPR_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let id_attr = format!("symbolica-mermaid-{id}");
format!(
r##"<div id="{id_attr}-container" class="symbolica-mermaid-container">
<style>
#{id_attr}-container .mermaid {{
background: transparent;
}}
@media (prefers-color-scheme: dark) {{
#{id_attr}-container svg {{
background: transparent !important;
}}
#{id_attr}-container .node rect,
#{id_attr}-container .node circle,
#{id_attr}-container .node ellipse,
#{id_attr}-container .node polygon,
#{id_attr}-container .node path {{
fill: #f8fafc !important;
stroke: #a78bfa !important;
stroke-width: 1.5px !important;
}}
#{id_attr}-container .nodeLabel,
#{id_attr}-container .nodeLabel p,
#{id_attr}-container .node text {{
color: #111827 !important;
fill: #111827 !important;
}}
#{id_attr}-container .edgePath path,
#{id_attr}-container .flowchart-link {{
stroke: #cbd5e1 !important;
stroke-width: 1.8px !important;
}}
#{id_attr}-container marker path,
#{id_attr}-container .arrowheadPath {{
fill: #cbd5e1 !important;
stroke: #cbd5e1 !important;
}}
#{id_attr}-container .edgeLabel,
#{id_attr}-container .edgeLabel p {{
color: #f8fafc !important;
background-color: #111827 !important;
}}
#{id_attr}-container .edgeLabel rect,
#{id_attr}-container .labelBkg {{
fill: #111827 !important;
background-color: #111827 !important;
}}
}}
</style>
<div id="{id_attr}" class="mermaid">
{diagram}</div>
</div>
<script type="module">
import mermaid from "https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs";
mermaid.initialize({{
startOnLoad: false,
theme: "base",
themeVariables: {{
background: "transparent",
mainBkg: "#f8fafc",
primaryColor: "#f8fafc",
primaryBorderColor: "#8b5cf6",
primaryTextColor: "#111827",
lineColor: "#64748b",
edgeLabelBackground: "#f8fafc"
}}
}});
mermaid.run({{ querySelector: "#{id_attr}" }}).catch(() => {{}});
</script>"##
)
}
/// Hash the graph.
fn __hash__(&self) -> u64 {
let mut hasher = ahash::AHasher::default();
self.graph.hash(&mut hasher);
hasher.finish()
}
/// Copy the graph.
fn __copy__(&self) -> PythonGraph {
Self {
graph: self.graph.clone(),
}
}
/// Get the number of nodes.
fn __len__(&self) -> usize {
self.graph.nodes().len()
}
/// Compare two graphs.
fn __richcmp__(&self, other: &Self, op: CompareOp) -> PyResult<bool> {
match op {
CompareOp::Eq => Ok(self.graph == other.graph),
CompareOp::Ne => Ok(self.graph != other.graph),
_ => Err(exceptions::PyTypeError::new_err(
"Inequalities between graphs are not allowed".to_string(),
)),
}
}
/// Generate all connected graphs with `external_edges` half-edges and the given allowed list
/// of vertex connections. The vertex signatures are given in terms of an edge direction (or `None` if
/// there is no direction) and edge data.
///
/// Returns the canonical form of the graph and the size of its automorphism group (including edge permutations).
/// If `KeyboardInterrupt` is triggered during the generation, the generation will stop and will yield the currently generated
/// graphs.
///
/// Examples
/// --------
/// >>> from symbolica import *
/// >>> g, q, gh = HalfEdge(S("g")), HalfEdge(S("q"), True), HalfEdge(S("gh"), True)
/// >>> graphs = Graph.generate(
/// >>> external_edges=[(1, g), (2, g)],
/// >>> vertex_signatures=[[g, g, g], [g, g, g, g],
/// >>> [q.flip(), q, g], [gh.flip(), gh, g]],
/// >>> max_loops=2,
/// >>> )
/// >>> for (g, sym) in graphs.items():
/// >>> print(f'Symmetry factor = 1/{sym}:')
/// >>> print(g.to_dot())
///
/// generates all connected graphs up to 2 loops with the specified vertices.
///
/// Parameters
/// ----------
/// external_edges: Sequence[tuple[Expression | int, HalfEdge]]
/// The external edges, consisting of a tuple of the node data and a tuple of the edge direction and edge data.
/// If the node data is the same, flip symmetries will be recognized.
/// vertex_signatures: Sequence[Sequence[HalfEdge]]
/// The allowed connections for each vertex.
/// max_vertices: int, optional
/// The maximum number of vertices in the graph.
/// max_loops: int, optional
/// The maximum number of loops in the graph.
/// max_bridges: int, optional
/// The maximum number of bridges in the graph.
/// allow_self_loops: bool, optional
/// Whether self-edges are allowed.
/// allow_zero_flow_edges: bool, optional
/// Whether bridges that do not need to be crossed to connect external vertices are allowed.
/// filter_fn: Optional[Callable[[Graph, int], bool]], optional
/// Set a filter function that is called during the graph generation.
/// The first argument is the graph `g` and the second argument the vertex count `n`
/// that specifies that the first `n` vertices are completed (no new edges will) be
/// assigned to them. The filter function should return `true` if the current
/// incomplete graph is allowed, else it should return `false` and the graph is discarded.
/// progress_fn: Optional[Callable[[Graph, bool]], optional
/// Set a progress function that is called every time a new unique graph is created.
/// The argument is the newly created graph.
/// If the function returns `false`, the generation is aborted and the currently
/// generated graphs are returned.
#[pyo3(signature = (external_edges, vertex_signatures, max_vertices = None, max_loops = None,
max_bridges = None, allow_self_loops = None, allow_zero_flow_edges = None, filter_fn = None, progress_fn = None))]
#[classmethod]
fn generate(
_cls: &Bound<'_, PyType>,
external_edges: Vec<(ConvertibleToExpression, PythonHalfEdge)>,
vertex_signatures: Vec<Vec<PythonHalfEdge>>,
max_vertices: Option<usize>,
max_loops: Option<usize>,
max_bridges: Option<usize>,
allow_self_loops: Option<bool>,
allow_zero_flow_edges: Option<bool>,
#[gen_stub(override_type(
type_repr = "typing.Optional[typing.Callable[[Graph, int], bool]]"
))]
filter_fn: Option<Py<PyAny>>,
#[gen_stub(override_type(type_repr = "typing.Optional[typing.Callable[[Graph], bool]]"))]
progress_fn: Option<Py<PyAny>>,
py: Python<'_>,
) -> PyResult<HashMap<PythonGraph, PythonExpression>> {
if max_vertices.is_none() && max_loops.is_none() {
return Err(exceptions::PyValueError::new_err(
"At least one of max_vertices or max_loop must be set",
));
}
let external_edges: Vec<_> = external_edges
.into_iter()
.map(|(a, b)| (a.to_expression().expr, b.half_edge))
.collect();
let vertex_signatures: Vec<_> = vertex_signatures
.into_iter()
.map(|v| v.into_iter().map(|x| x.half_edge).collect())
.collect();
let mut settings = GenerationSettings::new();
if let Some(max_vertices) = max_vertices {
settings = settings.max_vertices(max_vertices);
}
if let Some(max_loops) = max_loops {
settings = settings.max_loops(max_loops);
}
if let Some(max_bridges) = max_bridges {
settings = settings.max_bridges(max_bridges);
}
if let Some(allow_self_loops) = allow_self_loops {
settings = settings.allow_self_loops(allow_self_loops);
}
if let Some(allow_zero_flow_edge) = allow_zero_flow_edges {
settings = settings.allow_zero_flow_edges(allow_zero_flow_edge);
}
let abort = Arc::new(std::sync::atomic::AtomicBool::new(false));
if let Some(filter_fn) = filter_fn {
let abort = abort.clone();
settings = settings.filter_fn(Box::new(move |g, v| {
Python::attach(|py| {
match filter_fn.call(py, (Self { graph: g.clone() }, v), None) {
Ok(r) => r
.is_truthy(py)
.expect("Match map does not return a boolean"),
Err(e) => {
if e.is_instance_of::<exceptions::PyKeyboardInterrupt>(py) {
abort.store(true, std::sync::atomic::Ordering::Relaxed);
false
} else {
panic!("Bad callback function: {}", e);
}
}
}
})
}));
}
if let Some(progress_fn) = progress_fn {
settings = settings.progress_fn(Box::new(move |g| {
Python::attach(|py| {
match progress_fn.call(py, (Self { graph: g.clone() },), None) {
Ok(r) => r.is_truthy(py).unwrap_or(true),
Err(e) => {
error!("Bad callback function: {}", e);
false
}
}
})
}));
}
settings = settings.abort_check(Box::new(move || {
if abort.load(std::sync::atomic::Ordering::Relaxed) {
true
} else {
Python::attach(|py| py.check_signals())
.map(|_| false)
.unwrap_or(true)
}
}));
Ok(py.detach(move || {
Graph::generate(&external_edges, &vertex_signatures, settings)
.unwrap_or_else(|e| e)
.into_iter()
.map(|(k, v)| (Self { graph: k }, Atom::num(v).into()))
.collect()
}))
}
/// Convert the graph to a graphviz dot string.
fn to_dot(&self) -> String {
self.graph.to_dot()
}
/// Convert the graph to a mermaid string.
fn to_mermaid(&self) -> String {
self.graph.to_mermaid()
}
/// Add a node with data `data` to the graph, returning the index of the node.
/// The default data is the number 0.
#[pyo3(signature = (data = None))]
fn add_node(&mut self, data: Option<ConvertibleToExpression>) -> usize {
self.graph
.add_node(data.map(|x| x.to_expression().expr).unwrap_or_default())
}
/// Add an edge between the `source` and `target` nodes, returning the index of the edge.
/// Optionally, the edge can be set as directed. The default data is the number 0.
#[pyo3(signature = (source, target, directed = false, data = None))]
fn add_edge(
&mut self,
source: usize,
target: usize,
directed: bool,
data: Option<ConvertibleToExpression>,
) -> PyResult<usize> {
self.graph
.add_edge(
source,
target,
directed,
data.map(|x| x.to_expression().expr).unwrap_or_default(),
)
.map_err(|e| exceptions::PyValueError::new_err(e.to_string()))
}
/// Set the data of the node at index `index`, returning the old data.
pub fn set_node_data(
&mut self,
index: isize,
data: PythonExpression,
) -> PyResult<PythonExpression> {
if index.unsigned_abs() < self.graph.nodes().len() {
let n = if index < 0 {
self.graph.nodes().len() - index.unsigned_abs()
} else {
index as usize
};
Ok(self.graph.set_node_data(n, data.expr).into())
} else {
Err(PyIndexError::new_err(format!(
"Index {} out of bounds: the graph only has {} nodes.",
index,
self.graph.nodes().len(),
)))
}
}
/// Set the data of the edge at index `index`, returning the old data.
pub fn set_edge_data(
&mut self,
index: isize,
data: PythonExpression,
) -> PyResult<PythonExpression> {
if index.unsigned_abs() < self.graph.edges().len() {
let e = if index < 0 {
self.graph.edges().len() - index.unsigned_abs()
} else {
index as usize
};
Ok(self.graph.set_edge_data(e, data.expr).into())
} else {
Err(PyIndexError::new_err(format!(
"Index {} out of bounds: the graph only has {} edges.",
index,
self.graph.edges().len(),
)))
}
}
/// Set the directed status of the edge at index `index`, returning the old value.
pub fn set_directed(&mut self, index: isize, directed: bool) -> PyResult<bool> {
if index.unsigned_abs() < self.graph.edges().len() {
let e = if index < 0 {
self.graph.edges().len() - index.unsigned_abs()
} else {
index as usize
};
Ok(self.graph.set_directed(e, directed))
} else {
Err(PyIndexError::new_err(format!(
"Index {} out of bounds: the graph only has {} edges.",
index,
self.graph.edges().len(),
)))
}
}
/// Get the `idx`th node.
fn __getitem__(&self, idx: isize) -> PyResult<(Vec<usize>, PythonExpression)> {
self.node(idx)
}
/// Get the number of nodes.
fn num_nodes(&self) -> usize {
self.graph.nodes().len()
}
/// Get the number of edges.
fn num_edges(&self) -> usize {
self.graph.edges().len()
}
/// Get the number of loops.
fn num_loops(&self) -> usize {
self.graph.num_loops()
}
/// Get the `idx`th node, consisting of the edge indices and the data.
fn node(&self, idx: isize) -> PyResult<(Vec<usize>, PythonExpression)> {
if idx.unsigned_abs() < self.graph.nodes().len() {
let n = if idx < 0 {
self.graph
.node(self.graph.nodes().len() - idx.unsigned_abs())
} else {
self.graph.node(idx as usize)
};
Ok((n.edges.clone(), n.data.clone().into()))
} else {
Err(PyIndexError::new_err(format!(
"Index {} out of bounds: the graph only has {} nodes.",
idx,
self.graph.nodes().len(),
)))
}
}
/// Get all nodes, consisting of the edge indices and the data.
fn nodes(&self) -> Vec<(Vec<usize>, PythonExpression)> {
self.graph
.nodes()
.iter()
.map(|n| (n.edges.clone(), n.data.clone().into()))
.collect()
}
/// Get the `idx`th edge, consisting of the the source vertex, target vertex, whether the edge is directed, and the data.
fn edge(&self, idx: isize) -> PyResult<(usize, usize, bool, PythonExpression)> {
if idx.unsigned_abs() < self.graph.edges().len() {
let e = if idx < 0 {
self.graph
.edge(self.graph.edges().len() - idx.unsigned_abs())
} else {
self.graph.edge(idx as usize)
};
Ok((
e.vertices.0,
e.vertices.1,
e.directed,
e.data.clone().into(),
))
} else {
Err(PyIndexError::new_err(format!(
"Index {} out of bounds: the graph only has {} edges.",
idx,
self.graph.edges().len(),
)))
}
}
/// Get all edges, consisting of the the source vertex, target vertex, whether the edge is directed, and the data.
fn edges(&self) -> Vec<(usize, usize, bool, PythonExpression)> {
self.graph
.edges()
.iter()
.map(|e| {
(
e.vertices.0,
e.vertices.1,
e.directed,
e.data.clone().into(),
)
})
.collect()
}
/// Write the graph in a canonical form.
/// Returns the canonicalized graph, the vertex map, the automorphism group size, and the orbit.
fn canonize(&self) -> (PythonGraph, Vec<usize>, PythonExpression, Vec<usize>) {
let c = self.graph.canonize();
(
Self { graph: c.graph },
c.vertex_map,
Atom::num(c.automorphism_group_size).into(),
c.orbit,
)
}
/// Sort and relabel the edges of the graph, keeping the vertices fixed.
pub fn canonize_edges(&mut self) {
self.graph.canonize_edges();
}
/// Return true `iff` the graph is isomorphic to `other`.
fn is_isomorphic(&self, other: &PythonGraph) -> bool {
self.graph.is_isomorphic(&other.graph)
}
}