undirect graph algorithm