An algebra of hierarchical graphs