refactor of buildgraph and new method of clustering nodes