Function Repository Resource:

DepthLeafCountSort

Source Notebook

Sort expressions by depth and leaf count

Contributed by: Jan Mangaldan (original work by Stephen Wolfram)

ResourceFunction["DepthLeafCountSort"][list]

sort a List of expressions list by depth and leaf count.

Details and Options

ResourceFunction["DepthLeafCountSort"] effectively sorts based on the global structure of expressions, while Sort sorts "lexicographically," looking only at the "early parts" of expressions.
This is the method of sorting used to order theorems in A New Kind of Science, page 817.

Examples

Basic Examples (2) 

Sort a List of expressions by depth and leaf count:

In[1]:=
ResourceFunction["DepthLeafCountSort"][{f[g[a]], f[a, b, c], f[a, b]}]
Out[1]=

Generate a List of expressions:

In[2]:=
gr = Groupings[{a, b, c, d, e}, CenterDot -> 2]
Out[2]=

Show the tree forms of the expressions:

In[3]:=
TreeForm /@ gr
Out[3]=

Sort by depth and leaf count:

In[4]:=
TreeForm /@ ResourceFunction["DepthLeafCountSort"][gr]
Out[4]=

Properties and Relations (2) 

Sort sorts first by length, then based on lexicographic ordering:

In[5]:=
Sort[{f[g[a]], f[a, b, c], f[c, d], f[a, b]}]
Out[5]=

DepthLeafCountSort sorts first by Depth then LeafCount:

In[6]:=
ResourceFunction[
 "DepthLeafCountSort"][{f[g[a]], f[a, b, c], f[c, d], f[a, b]}]
Out[6]=

Generate a List of expressions:

In[7]:=
gr = Groupings[{a, b, c, d}, CirclePlus -> 2]
Out[7]=

Sort uses a lexicographic ordering:

In[8]:=
TreeForm /@ Sort[gr]
Out[8]=

DepthLeafCountSort orders expressions by their depth and then by their leaf count:

In[9]:=
TreeForm /@ ResourceFunction["DepthLeafCountSort"][gr]
Out[9]=

An equivalent ordering using SortBy:

In[10]:=
TreeForm /@ SortBy[gr, {Depth, LeafCount}]
Out[10]=

Version History

  • 1.0.0 – 01 October 2020

Related Resources

License Information