modify and accelerate graph building procedure