2222# SymbolFalse = Symbol("System`False")
2323# SymbolTrue = Symbol("System`True")
2424
25+
2526class ConnectedComponents (_NetworkXBuiltin ):
2627 """
2728 >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
@@ -38,10 +39,12 @@ def apply(self, graph, expression, evaluation, options):
3839 "ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
3940 graph = self ._build_graph (graph , evaluation , options , expression )
4041 if graph :
41- connect_fn = nx .strongly_connected_components if graph .G .is_directed () else nx .connected_components
42- components = [
43- Expression ("List" , * c ) for c in connect_fn (graph .G )
44- ]
42+ connect_fn = (
43+ nx .strongly_connected_components
44+ if graph .G .is_directed ()
45+ else nx .connected_components
46+ )
47+ components = [Expression ("List" , * c ) for c in connect_fn (graph .G )]
4548 return Expression ("List" , * components )
4649
4750
@@ -63,6 +66,7 @@ def apply(self, graph, expression, evaluation, options):
6366# # int_path = map(Integer, path)
6467# return Expression("List", *path)
6568
69+
6670class GraphDistance (_NetworkXBuiltin ):
6771 """
6872 <dl>
@@ -110,9 +114,7 @@ def apply_s(self, graph, s, expression, evaluation, options):
110114 weight = graph .update_weights (evaluation )
111115 d = nx .shortest_path_length (graph .G , source = s , weight = weight )
112116 inf = Expression ("DirectedInfinity" , 1 )
113- return Expression (
114- "List" , * [d .get (v , inf ) for v in graph .vertices ]
115- )
117+ return Expression ("List" , * [d .get (v , inf ) for v in graph .vertices ])
116118
117119 def apply_s_t (self , graph , s , t , expression , evaluation , options ):
118120 "%(name)s[graph_, s_, t_, OptionsPattern[%(name)s]]"
@@ -133,6 +135,7 @@ def apply_s_t(self, graph, s, t, expression, evaluation, options):
133135 except nx .exception .NetworkXNoPath :
134136 return Expression ("DirectedInfinity" , 1 )
135137
138+
136139class FindSpanningTree (_NetworkXBuiltin ):
137140 """
138141 <dl>
@@ -144,14 +147,18 @@ class FindSpanningTree(_NetworkXBuiltin):
144147 """
145148
146149 options = DEFAULT_GRAPH_OPTIONS
150+
147151 def apply (self , graph , expression , evaluation , options ):
148152 "%(name)s[graph_, OptionsPattern[%(name)s]]"
149153 graph = self ._build_graph (graph , evaluation , options , expression )
150154 if graph :
151155 weight = graph .update_weights (evaluation )
152156 edge_type = "DirectedEdge" if graph .G .is_directed () else "UndirectedEdge"
153157 # FIXME: put in edge to Graph conversion function?
154- edges = [Expression ("UndirectedEdge" , u , v ) for u , v in nx .minimum_spanning_edges (graph .G , data = False )]
158+ edges = [
159+ Expression ("UndirectedEdge" , u , v )
160+ for u , v in nx .minimum_spanning_edges (graph .G , data = False )
161+ ]
155162 g = _create_graph (edges , [None ] * len (edges ), options )
156163 if not hasattr (g .G , "graph_layout" ):
157164 if hasattr (graph .G , "graph_layout" ):
@@ -160,6 +167,7 @@ def apply(self, graph, expression, evaluation, options):
160167 g .G .graph_layout = "tree"
161168 return g
162169
170+
163171class PlanarGraphQ (_NetworkXBuiltin ):
164172 """
165173 <dl>
@@ -174,6 +182,7 @@ class PlanarGraphQ(_NetworkXBuiltin):
174182 """
175183
176184 options = DEFAULT_GRAPH_OPTIONS
185+
177186 def apply (self , graph , expression , evaluation , options ):
178187 "%(name)s[graph_, OptionsPattern[%(name)s]]"
179188 graph = self ._build_graph (graph , evaluation , options , expression )
@@ -182,6 +191,7 @@ def apply(self, graph, expression, evaluation, options):
182191 is_planar , _ = nx .check_planarity (graph .G )
183192 return Symbol ("System`True" ) if is_planar else Symbol ("System`False" )
184193
194+
185195class WeaklyConnectedComponents (_NetworkXBuiltin ):
186196 """
187197 >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
0 commit comments