Search code examples
pythongraphgraph-algorithm

Is there any algorithm to detect the two furthest node in a graph? Sorry if the question might be trivial


Hi all I am searching an algorithm to determine which are the 2 furthest node in an undirected unweighted graph. So I mean only in terms of edges. For example in the photo should be the two marked in red with a distance of 4 edges.

enter image description here


Solution

  • A more complete result can be given thanks to the Dijkstra algorithm, which will give you the distance between each points of the graph. In this simple implementation, the graph is represented as a matrix of neighborhood, and you get all the distances. You can adapt the code to not print the result in the shell but to return a matrix of distances or a dictionnary, and take the max.