55networkx does all the heavy lifting.
66"""
77
8- from mathics .core .expression import Expression , Symbol
8+ from mathics .core .convert .python import from_python
9+ from mathics .core .expression import Expression
10+ from mathics .core .list import ListExpression
11+ from mathics .core .symbols import SymbolFalse
912
1013from pymathics .graph .__main__ import (
1114 DEFAULT_GRAPH_OPTIONS ,
12- _NetworkXBuiltin ,
15+ SymbolDirectedEdge ,
16+ SymbolUndirectedEdge ,
1317 _create_graph ,
18+ _NetworkXBuiltin ,
1419 nx ,
1520)
1621
17- from mathics .core .expression import (
18- from_python ,
19- )
20-
21- # FIXME: Add to Mathics Expression
22- # SymbolFalse = Symbol("System`False")
23- # SymbolTrue = Symbol("System`True")
24-
2522
2623class ConnectedComponents (_NetworkXBuiltin ):
2724 """
@@ -152,8 +149,8 @@ def apply(self, graph, expression, evaluation, options):
152149 "%(name)s[graph_, OptionsPattern[%(name)s]]"
153150 graph = self ._build_graph (graph , evaluation , options , expression )
154151 if graph :
155- weight = graph .update_weights (evaluation )
156- edge_type = "DirectedEdge" if graph .G .is_directed () else "UndirectedEdge"
152+ graph .update_weights (evaluation )
153+ SymbolDirectedEdge if graph .G .is_directed () else SymbolUndirectedEdge
157154 # FIXME: put in edge to Graph conversion function?
158155 edges = [
159156 Expression ("UndirectedEdge" , u , v )
@@ -187,9 +184,9 @@ def apply(self, graph, expression, evaluation, options):
187184 "%(name)s[graph_, OptionsPattern[%(name)s]]"
188185 graph = self ._build_graph (graph , evaluation , options , expression )
189186 if not graph :
190- return Symbol ( "System`False" )
187+ return SymbolFalse
191188 is_planar , _ = nx .check_planarity (graph .G )
192- return Symbol ( "System`True" ) if is_planar else Symbol ( "System`False" )
189+ return from_python ( is_planar )
193190
194191
195192class WeaklyConnectedComponents (_NetworkXBuiltin ):
@@ -214,6 +211,6 @@ def apply(self, graph, expression, evaluation, options):
214211 components = sorted (components , key = lambda c : index [next (iter (c ))])
215212
216213 vertices_sorted = graph .vertices .get_sorted ()
217- result = [Expression ( "List" , * vertices_sorted (c )) for c in components ]
214+ result = [ListExpression ( * vertices_sorted (c )) for c in components ]
218215
219- return Expression ( "List" , * result )
216+ return ListExpression ( * result )
0 commit comments