Algorithm for finding diameter of a graph