55
66from typing import Optional
77
8+ from mathics .core .atoms import Integer1 , Integer2 , Integer3
89from mathics .core .convert .expression import to_mathics_list
910from mathics .core .convert .python import from_python
1011from mathics .core .evaluation import Evaluation
2526
2627class ConnectedComponents (_NetworkXBuiltin ):
2728 """
28- ## >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
29- ## = {{3, 4}, {2}, {1}}
29+ <dl>
30+ <dt>'ConnectedComponents'[$g$]
31+ <dd> gives the connected components of the graph $g$
32+ </dl>
3033
31- ## >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
32- ## = {{1, 2, 3}}
34+ >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
35+ = {{3, 4}, {2}, {1}}
36+
37+ >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
38+ = {{1, 2, 3}}
3339
34- ## >> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
35- ## = {{4, 5}, {1, 2, 3}}
40+ >> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
41+ = {{4, 5}, {1, 2, 3}}
3642 """
3743
3844 def eval (
@@ -72,7 +78,7 @@ def eval(
7278class GraphDistance (_NetworkXBuiltin ):
7379 """
7480 <dl>
75- <dt>'GraphDistance[$g$, $s$, $t$]'
81+ <dt>'GraphDistance' [$g$, $s$, $t$]
7682 <dd>returns the distance from source vertex $s$ to target vertex $t$ in the graph $g$.
7783 </dl>
7884
@@ -117,7 +123,7 @@ def eval_s(
117123 if graph :
118124 weight = graph .update_weights (evaluation )
119125 d = nx .shortest_path_length (graph .G , source = s , weight = weight )
120- inf = Expression (SymbolDirectedInfinity , 1 )
126+ inf = Expression (SymbolDirectedInfinity , Integer1 )
121127 return to_mathics_list (* [d .get (v , inf ) for v in graph .vertices ])
122128
123129 def eval_s_t (self , graph , s , t , expression , evaluation : Evaluation , options : dict ):
@@ -127,27 +133,28 @@ def eval_s_t(self, graph, s, t, expression, evaluation: Evaluation, options: dic
127133 return
128134 G = graph .G
129135 if not G .has_node (s ):
130- self ._not_a_vertex (expression , 2 , evaluation )
136+ self ._not_a_vertex (expression , Integer2 , evaluation )
131137 elif not G .has_node (t ):
132- self ._not_a_vertex (expression , 3 , evaluation )
138+ self ._not_a_vertex (expression , Integer3 , evaluation )
133139 else :
134140 try :
135141 weight = graph .update_weights (evaluation )
136142 return from_python (
137143 nx .shortest_path_length (graph .G , source = s , target = t , weight = weight )
138144 )
139145 except nx .exception .NetworkXNoPath :
140- return Expression (SymbolDirectedInfinity , 1 )
146+ return Expression (SymbolDirectedInfinity , Integer1 )
141147
142148
143149class FindSpanningTree (_NetworkXBuiltin ):
144150 """
145151 <dl>
146- <dt>'FindSpanningTree[$g$]'
152+ <dt>'FindSpanningTree' [$g$]
147153 <dd>finds a spanning tree of the graph $g$.
148154 </dl>
149155
150156 >> FindSpanningTree[CycleGraph[4]]
157+ = -Graph-
151158 """
152159
153160 options = DEFAULT_GRAPH_OPTIONS
@@ -160,7 +167,7 @@ def eval(self, graph, expression, evaluation: Evaluation, options: dict):
160167 SymbolDirectedEdge if graph .G .is_directed () else SymbolUndirectedEdge
161168 # FIXME: put in edge to Graph conversion function?
162169 edges = [
163- Expression ("UndirectedEdge" , u , v )
170+ Expression (SymbolUndirectedEdge , from_python ( u ), from_python ( v ) )
164171 for u , v in nx .minimum_spanning_edges (graph .G , data = False )
165172 ]
166173 g = _create_graph (edges , [None ] * len (edges ), options )
@@ -174,30 +181,52 @@ def eval(self, graph, expression, evaluation: Evaluation, options: dict):
174181
175182class PlanarGraphQ (_NetworkXBuiltin ):
176183 """
184+ See <url>https://en.wikipedia.org/wiki/Planar_graph</url>
185+
177186 <dl>
178- <dd>PlanarGraphQ[g ]
179- <dd>Returns True if g is a planar graph and False otherwise.
187+ <dd>' PlanarGraphQ'[$g$ ]
188+ <dd>Returns True if $g$ is a planar graph and False otherwise.
180189 </dl>
181190
182191 >> PlanarGraphQ[CycleGraph[4]]
183192 = True
193+
184194 >> PlanarGraphQ[CompleteGraph[5]]
185195 = False
196+
197+ >> PlanarGraphQ[CompleteGraph[4]]
198+ = True
199+
200+ >> PlanarGraphQ[CompleteGraph[5]]
201+ = False
202+
203+ #> PlanarGraphQ[Graph[{}]]
204+ = False
205+
206+
207+ >> PlanarGraphQ["abc"]
208+ : Expected a graph at position 1 in PlanarGraphQ[abc].
209+ = False
186210 """
187211
188212 options = DEFAULT_GRAPH_OPTIONS
189213
190214 def eval (self , graph , expression , evaluation : Evaluation , options : dict ):
191- "PlanarGraphQ[graph_, OptionsPattern[PlanarGraphQ]]"
215+ "Pymathics` PlanarGraphQ[graph_, OptionsPattern[PlanarGraphQ]]"
192216 graph = self ._build_graph (graph , evaluation , options , expression )
193- if not graph :
217+ if not graph or graph . empty () :
194218 return SymbolFalse
195219 is_planar , _ = nx .check_planarity (graph .G )
196220 return from_python (is_planar )
197221
198222
199223class WeaklyConnectedComponents (_NetworkXBuiltin ):
200224 """
225+ <dl>
226+ <dt>'WeaklyConnectedComponents'[$g$]
227+ <dd> gives the weakly connected components of the graph $g$
228+ </dl>
229+
201230 >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
202231 = {{1, 2, 3, 4}}
203232
0 commit comments