helper function to generate graph