Skip to content

Commit 4e21c1e

Browse files
committed
Redo connected component with nx routines
1 parent 659f66f commit 4e21c1e

File tree

2 files changed

+49
-50
lines changed

2 files changed

+49
-50
lines changed

pymathics/graph/__main__.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,56 +1134,6 @@ def apply(self, graph, expression, evaluation, options):
11341134
return Symbol("False")
11351135

11361136

1137-
class ConnectedComponents(_NetworkXBuiltin):
1138-
"""
1139-
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
1140-
= {{3, 4}, {2}, {1}}
1141-
1142-
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
1143-
= {{1, 2, 3}}
1144-
1145-
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
1146-
= {{4, 5}, {1, 2, 3}}
1147-
"""
1148-
1149-
def apply(self, graph, expression, evaluation, options):
1150-
"ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
1151-
graph = self._build_graph(graph, evaluation, options, expression)
1152-
if graph:
1153-
vertices_sorted = graph.vertices.get_sorted()
1154-
components = [
1155-
Expression("List", *vertices_sorted(c)) for c in _components(graph.G)
1156-
]
1157-
return Expression("List", *components)
1158-
1159-
1160-
class WeaklyConnectedComponents(_NetworkXBuiltin):
1161-
"""
1162-
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
1163-
= {{1, 2, 3, 4}}
1164-
1165-
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; WeaklyConnectedComponents[g]
1166-
= {{1, 2, 3}}
1167-
1168-
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5, 6 <-> 7, 7 <-> 8}]; WeaklyConnectedComponents[g]
1169-
= {{1, 2, 3, 4, 5}, {6, 7, 8}}
1170-
"""
1171-
1172-
def apply(self, graph, expression, evaluation, options):
1173-
"WeaklyConnectedComponents[graph_, OptionsPattern[%(name)s]]"
1174-
graph = self._build_graph(graph, evaluation, options, expression)
1175-
if graph:
1176-
components = nx.connected_components(graph.G.to_undirected())
1177-
1178-
index = graph.vertices.get_index()
1179-
components = sorted(components, key=lambda c: index[next(iter(c))])
1180-
1181-
vertices_sorted = graph.vertices.get_sorted()
1182-
result = [Expression("List", *vertices_sorted(c)) for c in components]
1183-
1184-
return Expression("List", *result)
1185-
1186-
11871137
class FindVertexCut(_NetworkXBuiltin):
11881138
"""
11891139
<dl>

pymathics/graph/algorithms.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,52 @@ def apply(self, graph, expression, evaluation, options):
140140
return Symbol("System`False")
141141
is_planar, _ = nx.check_planarity(graph.G)
142142
return Symbol("System`True") if is_planar else Symbol("System`False")
143+
144+
class ConnectedComponents(_NetworkXBuiltin):
145+
"""
146+
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g]
147+
= {{3, 4}, {2}, {1}}
148+
149+
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g]
150+
= {{1, 2, 3}}
151+
152+
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g]
153+
= {{4, 5}, {1, 2, 3}}
154+
"""
155+
156+
def apply(self, graph, expression, evaluation, options):
157+
"ConnectedComponents[graph_, OptionsPattern[%(name)s]]"
158+
graph = self._build_graph(graph, evaluation, options, expression)
159+
if graph:
160+
connect_fn = nx.strongly_connected_components if graph.G.is_directed() else nx.connected_components
161+
components = [
162+
Expression("List", *c) for c in connect_fn(graph.G)
163+
]
164+
return Expression("List", *components)
165+
166+
167+
class WeaklyConnectedComponents(_NetworkXBuiltin):
168+
"""
169+
>> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g]
170+
= {{1, 2, 3, 4}}
171+
172+
>> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; WeaklyConnectedComponents[g]
173+
= {{1, 2, 3}}
174+
175+
>> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5, 6 <-> 7, 7 <-> 8}]; WeaklyConnectedComponents[g]
176+
= {{1, 2, 3, 4, 5}, {6, 7, 8}}
177+
"""
178+
179+
def apply(self, graph, expression, evaluation, options):
180+
"WeaklyConnectedComponents[graph_, OptionsPattern[%(name)s]]"
181+
graph = self._build_graph(graph, evaluation, options, expression)
182+
if graph:
183+
components = nx.connected_components(graph.G.to_undirected())
184+
185+
index = graph.vertices.get_index()
186+
components = sorted(components, key=lambda c: index[next(iter(c))])
187+
188+
vertices_sorted = graph.vertices.get_sorted()
189+
result = [Expression("List", *vertices_sorted(c)) for c in components]
190+
191+
return Expression("List", *result)

0 commit comments

Comments
 (0)