Graph construction implementation in java