Skip to content

Commit 9bce86d

Browse files
committed
Add FindSpanningTree
1 parent f88ac0b commit 9bce86d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

pymathics/graph/algorithms.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
networkx does all the heavy lifting.
66
"""
77

8-
from mathics.core.expression import Expression
8+
from mathics.core.expression import Expression, Symbol
99

1010
from pymathics.graph.__main__ import (
11+
DEFAULT_GRAPH_OPTIONS,
1112
_NetworkXBuiltin,
13+
_create_graph,
1214
nx,
1315
)
1416

@@ -87,3 +89,25 @@ def apply_s_t(self, graph, s, t, expression, evaluation, options):
8789
)
8890
except nx.exception.NetworkXNoPath:
8991
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

Comments
 (0)