|
5 | 5 | networkx does all the heavy lifting. |
6 | 6 | """ |
7 | 7 |
|
8 | | -from mathics.core.expression import Expression |
| 8 | +from mathics.core.expression import Expression, Symbol |
9 | 9 |
|
10 | 10 | from pymathics.graph.__main__ import ( |
| 11 | + DEFAULT_GRAPH_OPTIONS, |
11 | 12 | _NetworkXBuiltin, |
| 13 | + _create_graph, |
12 | 14 | nx, |
13 | 15 | ) |
14 | 16 |
|
@@ -87,3 +89,25 @@ def apply_s_t(self, graph, s, t, expression, evaluation, options): |
87 | 89 | ) |
88 | 90 | except nx.exception.NetworkXNoPath: |
89 | 91 | return Expression("DirectedInfinity", 1) |
| 92 | + |
| 93 | +class FindSpanningTree(_NetworkXBuiltin): |
| 94 | + """ |
| 95 | + <dl> |
| 96 | + <dt>'FindSpanningTree[$g$]' |
| 97 | + <dd>finds a spanning tree of the graph $g$. |
| 98 | + </dl> |
| 99 | +
|
| 100 | + >> FindSpanningTree[CycleGraph[4]] |
| 101 | + """ |
| 102 | + |
| 103 | + options = DEFAULT_GRAPH_OPTIONS |
| 104 | + def apply(self, graph, expression, evaluation, options): |
| 105 | + "%(name)s[graph_, OptionsPattern[%(name)s]]" |
| 106 | + graph = self._build_graph(graph, evaluation, options, expression) |
| 107 | + if graph: |
| 108 | + weight = graph.update_weights(evaluation) |
| 109 | + edge_type = "DirectedEdge" if graph.G.is_directed() else "UndirectedEdge" |
| 110 | + # FIXME: put in edge to Graph conversion function? |
| 111 | + edges = [Expression("UndirectedEdge", u, v) for u, v in nx.minimum_spanning_edges(graph.G, data=False)] |
| 112 | + g = _create_graph(edges, [None] * len(edges), options) |
| 113 | + return g |
0 commit comments