Reputation: 11
For cyclic graphs with a number of vertices larger than 5, running BFS in each node and picking a maximum out of those lengths stops working.
For example:
Number each vertex from 1 to 6 in a cyclic manner.
Now, using BFS: -from node 1:
Length is already equal to 4, which is more than the diameter.
Upvotes: 1
Views: 2589
Reputation: 1654
BFS does find the correct diameter in your example.
It takes two separate paths: <1, 2, 3, 4>
and <1, 6, 5>
.
You take the longest path which is <1, 2, 3, 4>
and you take the distance between the first and the last node which is the number of nodes in the path minus 1. It gives 4-1=3
which is the diameter of the cycle graph.
Upvotes: 0