|
22 | 22 | # SymbolFalse = Symbol("System`False") |
23 | 23 | # SymbolTrue = Symbol("System`True") |
24 | 24 |
|
| 25 | +class ConnectedComponents(_NetworkXBuiltin): |
| 26 | + """ |
| 27 | + >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; ConnectedComponents[g] |
| 28 | + = {{3, 4}, {2}, {1}} |
| 29 | +
|
| 30 | + >> g = Graph[{1 -> 2, 2 -> 3, 3 -> 1}]; ConnectedComponents[g] |
| 31 | + = {{1, 2, 3}} |
| 32 | +
|
| 33 | + >> g = Graph[{1 <-> 2, 2 <-> 3, 3 -> 4, 4 <-> 5}]; ConnectedComponents[g] |
| 34 | + = {{4, 5}, {1, 2, 3}} |
| 35 | + """ |
| 36 | + |
| 37 | + def apply(self, graph, expression, evaluation, options): |
| 38 | + "ConnectedComponents[graph_, OptionsPattern[%(name)s]]" |
| 39 | + graph = self._build_graph(graph, evaluation, options, expression) |
| 40 | + 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 | + ] |
| 45 | + return Expression("List", *components) |
| 46 | + |
| 47 | + |
| 48 | +# class FindHamiltonianPath(_NetworkXBuiltin): |
| 49 | +# """ |
| 50 | +# <dl> |
| 51 | +# <dt>'FindHamiltonianPath[$g$]' |
| 52 | +# <dd>returns a Hamiltonian path in the given tournament graph. |
| 53 | +# </dl> |
| 54 | +# """ |
| 55 | +# def apply_(self, graph, expression, evaluation, options): |
| 56 | +# "%(name)s[graph_, OptionsPattern[%(name)s]]" |
| 57 | + |
| 58 | +# graph = self._build_graph(graph, evaluation, options, expression) |
| 59 | +# if graph: |
| 60 | +# # FIXME: for this to work we need to fill in all O(n^2) edges as an adjacency matrix? |
| 61 | +# path = nx.algorithms.tournament.hamiltonian_path(graph.G) |
| 62 | +# if path: |
| 63 | +# # int_path = map(Integer, path) |
| 64 | +# return Expression("List", *path) |
| 65 | + |
25 | 66 | class GraphDistance(_NetworkXBuiltin): |
26 | 67 | """ |
27 | 68 | <dl> |
@@ -141,29 +182,6 @@ def apply(self, graph, expression, evaluation, options): |
141 | 182 | is_planar, _ = nx.check_planarity(graph.G) |
142 | 183 | return Symbol("System`True") if is_planar else Symbol("System`False") |
143 | 184 |
|
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 | 185 | class WeaklyConnectedComponents(_NetworkXBuiltin): |
168 | 186 | """ |
169 | 187 | >> g = Graph[{1 -> 2, 2 -> 3, 3 <-> 4}]; WeaklyConnectedComponents[g] |
|
0 commit comments