Python implementation of graph