55networkx does all the heavy lifting.
66"""
77
8+ from typing import Optional
9+
810from mathics .core .convert .expression import to_mathics_list
911from mathics .core .convert .python import from_python
12+ from mathics .core .evaluation import Evaluation
1013from mathics .core .expression import Expression
1114from mathics .core .list import ListExpression
1215from mathics .core .symbols import SymbolFalse
2528class ConnectedComponents (_NetworkXBuiltin ):
2629 """
2730 ## >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
28- ## = {{4, 3 }, {2}, {1}}
31+ ## = {{3, 4 }, {2}, {1}}
2932
3033 ## >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
3134 ## = {{1, 2, 3}}
@@ -34,7 +37,9 @@ class ConnectedComponents(_NetworkXBuiltin):
3437 ## = {{4, 5}, {1, 2, 3}}
3538 """
3639
37- def eval (self , graph , expression , evaluation , options ):
40+ def eval (
41+ self , graph , expression , evaluation : Evaluation , options : dict
42+ ) -> Optional [ListExpression ]:
3843 "ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
3944 graph = self ._build_graph (graph , evaluation , options , expression )
4045 if graph :
@@ -54,10 +59,10 @@ def eval(self, graph, expression, evaluation, options):
5459# <dd>returns a Hamiltonian path in the given tournament graph.
5560# </dl>
5661# """
57- # def eval_(self, graph, expression, evaluation, options):
58- # "%(name)s [graph_, OptionsPattern[%(name)s ]]"
62+ # def eval_(self, graph, expression, evaluation: Evaluation , options):
63+ # "FindHamiltonianPath [graph_, OptionsPattern[FindHamiltonPath ]]"
5964
60- # graph = self._build_graph(graph, evaluation, options, expression)
65+ # graph = self._build_graph(graph, evaluation: Evaluation , options, expression)
6166# if graph:
6267# # FIXME: for this to work we need to fill in all O(n^2) edges as an adjacency matrix?
6368# path = nx.algorithms.tournament.hamiltonian_path(graph.G)
@@ -106,17 +111,19 @@ class GraphDistance(_NetworkXBuiltin):
106111 = GraphDistance[{1 -> 2}, 3, 4]
107112 """
108113
109- def eval_s (self , graph , s , expression , evaluation , options ):
110- "%(name)s[graph_, s_, OptionsPattern[%(name)s]]"
114+ def eval_s (
115+ self , graph , s , expression , evaluation : Evaluation , options : dict
116+ ) -> Optional [ListExpression ]:
117+ "GraphDistance[graph_, s_, OptionsPattern[GraphDistance]]"
111118 graph = self ._build_graph (graph , evaluation , options , expression )
112119 if graph :
113120 weight = graph .update_weights (evaluation )
114121 d = nx .shortest_path_length (graph .G , source = s , weight = weight )
115122 inf = Expression (SymbolDirectedInfinity , 1 )
116123 return to_mathics_list (* [d .get (v , inf ) for v in graph .vertices ])
117124
118- def eval_s_t (self , graph , s , t , expression , evaluation , options ):
119- "%(name)s [graph_, s_, t_, OptionsPattern[%(name)s ]]"
125+ def eval_s_t (self , graph , s , t , expression , evaluation : Evaluation , options : dict ):
126+ "GraphDistance [graph_, s_, t_, OptionsPattern[GraphDistance ]]"
120127 graph = self ._build_graph (graph , evaluation , options , expression )
121128 if not graph :
122129 return
@@ -147,7 +154,7 @@ class FindSpanningTree(_NetworkXBuiltin):
147154
148155 options = DEFAULT_GRAPH_OPTIONS
149156
150- def eval (self , graph , expression , evaluation , options ):
157+ def eval (self , graph , expression , evaluation : Evaluation , options : dict ):
151158 "%(name)s[graph_, OptionsPattern[%(name)s]]"
152159 graph = self ._build_graph (graph , evaluation , options , expression )
153160 if graph :
@@ -182,8 +189,8 @@ class PlanarGraphQ(_NetworkXBuiltin):
182189
183190 options = DEFAULT_GRAPH_OPTIONS
184191
185- def eval (self , graph , expression , evaluation , options ):
186- "%(name)s [graph_, OptionsPattern[%(name)s ]]"
192+ def eval (self , graph , expression , evaluation : Evaluation , options : dict ):
193+ "PlanarGraphQ [graph_, OptionsPattern[PlanarGraphQ ]]"
187194 graph = self ._build_graph (graph , evaluation , options , expression )
188195 if not graph :
189196 return SymbolFalse
@@ -203,8 +210,8 @@ class WeaklyConnectedComponents(_NetworkXBuiltin):
203210 = {{1, 2, 3, 4, 5}, {6, 7, 8}}
204211 """
205212
206- def eval (self , graph , expression , evaluation , options ):
207- "WeaklyConnectedComponents[graph_, OptionsPattern[%(name)s ]]"
213+ def eval (self , graph , expression , evaluation : Evaluation , options ):
214+ "WeaklyConnectedComponents[graph_, OptionsPattern[WeaklyConnectedComponents ]]"
208215 graph = self ._build_graph (graph , evaluation , options , expression )
209216 if graph :
210217 components = nx .connected_components (graph .G .to_undirected ())
0 commit comments