rewrote algorithm for graph generation