build a graph as an adjacency list