adding graph algorithm