Added graph building algorithm