11# -*- coding: utf-8 -*-
22"""
3- Algorithms on Graphs.
4-
5- networkx does all the heavy lifting.
3+ Algorithms on Graphs
64"""
75
6+ from typing import Optional
7+
88from mathics .core .convert .expression import to_mathics_list
99from mathics .core .convert .python import from_python
10+ from mathics .core .evaluation import Evaluation
1011from mathics .core .expression import Expression
1112from mathics .core .list import ListExpression
1213from mathics .core .symbols import SymbolFalse
1314from mathics .core .systemsymbols import SymbolDirectedInfinity
1415
15- from pymathics .graph .__main__ import (
16+ from pymathics .graph .base import (
1617 DEFAULT_GRAPH_OPTIONS ,
1718 SymbolDirectedEdge ,
1819 SymbolUndirectedEdge ,
2526class ConnectedComponents (_NetworkXBuiltin ):
2627 """
2728 ## >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
28- ## = {{4, 3 }, {2}, {1}}
29+ ## = {{3, 4 }, {2}, {1}}
2930
3031 ## >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
3132 ## = {{1, 2, 3}}
@@ -34,7 +35,9 @@ class ConnectedComponents(_NetworkXBuiltin):
3435 ## = {{4, 5}, {1, 2, 3}}
3536 """
3637
37- def apply (self , graph , expression , evaluation , options ):
38+ def eval (
39+ self , graph , expression , evaluation : Evaluation , options : dict
40+ ) -> Optional [ListExpression ]:
3841 "ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
3942 graph = self ._build_graph (graph , evaluation , options , expression )
4043 if graph :
@@ -54,10 +57,10 @@ def apply(self, graph, expression, evaluation, options):
5457# <dd>returns a Hamiltonian path in the given tournament graph.
5558# </dl>
5659# """
57- # def apply_ (self, graph, expression, evaluation, options):
58- # "%(name)s [graph_, OptionsPattern[%(name)s ]]"
60+ # def eval_ (self, graph, expression, evaluation: Evaluation , options):
61+ # "FindHamiltonianPath [graph_, OptionsPattern[FindHamiltonPath ]]"
5962
60- # graph = self._build_graph(graph, evaluation, options, expression)
63+ # graph = self._build_graph(graph, evaluation: Evaluation , options, expression)
6164# if graph:
6265# # FIXME: for this to work we need to fill in all O(n^2) edges as an adjacency matrix?
6366# path = nx.algorithms.tournament.hamiltonian_path(graph.G)
@@ -106,17 +109,19 @@ class GraphDistance(_NetworkXBuiltin):
106109 = GraphDistance[{1 -> 2}, 3, 4]
107110 """
108111
109- def apply_s (self , graph , s , expression , evaluation , options ):
110- "%(name)s[graph_, s_, OptionsPattern[%(name)s]]"
112+ def eval_s (
113+ self , graph , s , expression , evaluation : Evaluation , options : dict
114+ ) -> Optional [ListExpression ]:
115+ "GraphDistance[graph_, s_, OptionsPattern[GraphDistance]]"
111116 graph = self ._build_graph (graph , evaluation , options , expression )
112117 if graph :
113118 weight = graph .update_weights (evaluation )
114119 d = nx .shortest_path_length (graph .G , source = s , weight = weight )
115120 inf = Expression (SymbolDirectedInfinity , 1 )
116121 return to_mathics_list (* [d .get (v , inf ) for v in graph .vertices ])
117122
118- def apply_s_t (self , graph , s , t , expression , evaluation , options ):
119- "%(name)s [graph_, s_, t_, OptionsPattern[%(name)s ]]"
123+ def eval_s_t (self , graph , s , t , expression , evaluation : Evaluation , options : dict ):
124+ "GraphDistance [graph_, s_, t_, OptionsPattern[GraphDistance ]]"
120125 graph = self ._build_graph (graph , evaluation , options , expression )
121126 if not graph :
122127 return
@@ -147,7 +152,7 @@ class FindSpanningTree(_NetworkXBuiltin):
147152
148153 options = DEFAULT_GRAPH_OPTIONS
149154
150- def apply (self , graph , expression , evaluation , options ):
155+ def eval (self , graph , expression , evaluation : Evaluation , options : dict ):
151156 "%(name)s[graph_, OptionsPattern[%(name)s]]"
152157 graph = self ._build_graph (graph , evaluation , options , expression )
153158 if graph :
@@ -182,8 +187,8 @@ class PlanarGraphQ(_NetworkXBuiltin):
182187
183188 options = DEFAULT_GRAPH_OPTIONS
184189
185- def apply (self , graph , expression , evaluation , options ):
186- "%(name)s [graph_, OptionsPattern[%(name)s ]]"
190+ def eval (self , graph , expression , evaluation : Evaluation , options : dict ):
191+ "PlanarGraphQ [graph_, OptionsPattern[PlanarGraphQ ]]"
187192 graph = self ._build_graph (graph , evaluation , options , expression )
188193 if not graph :
189194 return SymbolFalse
@@ -203,8 +208,8 @@ class WeaklyConnectedComponents(_NetworkXBuiltin):
203208 = {{1, 2, 3, 4, 5}, {6, 7, 8}}
204209 """
205210
206- def apply (self , graph , expression , evaluation , options ):
207- "WeaklyConnectedComponents[graph_, OptionsPattern[%(name)s ]]"
211+ def eval (self , graph , expression , evaluation : Evaluation , options ):
212+ "WeaklyConnectedComponents[graph_, OptionsPattern[WeaklyConnectedComponents ]]"
208213 graph = self ._build_graph (graph , evaluation , options , expression )
209214 if graph :
210215 components = nx .connected_components (graph .G .to_undirected ())
0 commit comments