Python implementation of directed acyclic graph