Skip to content

Commit 1143fc6

Browse files
committed
Add PlanarQ[]
1 parent ce2bde1 commit 1143fc6

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

pymathics/graph/algorithms.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
from_python,
1919
)
2020

21-
21+
# FIXME: Add to Mathics Expression
22+
# SymbolFalse = Symbol("System`False")
23+
# SymbolTrue = Symbol("System`True")
2224

2325
class GraphDistance(_NetworkXBuiltin):
2426
"""
@@ -111,3 +113,25 @@ def apply(self, graph, expression, evaluation, options):
111113
edges = [Expression("UndirectedEdge", u, v) for u, v in nx.minimum_spanning_edges(graph.G, data=False)]
112114
g = _create_graph(edges, [None] * len(edges), options)
113115
return g
116+
117+
class PlanarGraphQ(_NetworkXBuiltin):
118+
"""
119+
<dl>
120+
<dd>PlanarGraphQ[g]
121+
<dd>Returns True if g is a planar graph and False otherwise.
122+
</dl>
123+
124+
>> PlanarGraphQ[CycleGraph[4]]
125+
= True
126+
>> PlanarGraphQ[CompleteGraph[5]]
127+
= False
128+
"""
129+
130+
options = DEFAULT_GRAPH_OPTIONS
131+
def apply(self, graph, expression, evaluation, options):
132+
"%(name)s[graph_, OptionsPattern[%(name)s]]"
133+
graph = self._build_graph(graph, evaluation, options, expression)
134+
if not graph:
135+
return Symbol("System`False")
136+
is_planar, _ = nx.check_planarity(graph.G)
137+
return Symbol("System`True") if is_planar else Symbol("System`False")

0 commit comments

Comments
 (0)