the floyd algorithm is very similar but works with undirected graph