Python code for the graph model