Building the graph and initializing edges