basic graph algorithm