now creating the graph with O(n+m)