finalised graphs, improved algorithm in code again