Skip to content

Commit e2fb7b7

Browse files
committed
Add TreeQ[]
1 parent ad8f4c1 commit e2fb7b7

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

pymathics/graph/tree.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import networkx as nx
2-
from pymathics.graph.__main__ import Graph, _graph_from_list, DEFAULT_GRAPH_OPTIONS
3-
from mathics.core.expression import String
2+
from pymathics.graph.__main__ import Graph, _graph_from_list, DEFAULT_GRAPH_OPTIONS, _NetworkXBuiltin
3+
from mathics.core.expression import String, Symbol
44

55
DEFAULT_TREE_OPTIONS = {
66
**DEFAULT_GRAPH_OPTIONS,
@@ -54,3 +54,25 @@ class TreeGraph(Graph):
5454
def __init__(self, G, **kwargs):
5555
super(Graph, self).__init__()
5656
self.G = G
57+
58+
59+
class TreeGraphQ(_NetworkXBuiltin):
60+
"""
61+
<dl>
62+
<dt>'TreeGraphQ[$g$]'
63+
<dd>returns $True$ if the graph $g$ is a tree and $False$ otherwise.
64+
</dl>
65+
66+
>> TreeGraphQ[StarGraph[3]]
67+
= True
68+
>> TreeGraphQ[CompleteGraph[2]]
69+
= True
70+
>> TreeGraphQ[CompleteGraph[3]]
71+
= False
72+
"""
73+
74+
def apply(self, g, expression, evaluation, options):
75+
"TreeGraphQ[g_, OptionsPattern[%(name)s]]"
76+
if not isinstance(g, Graph):
77+
return Symbol("False")
78+
return Symbol("True" if nx.is_tree(g.G) else "False")

0 commit comments

Comments
 (0)