adds some error checking to graph and raises ValueError on invalid input