https://www.geeksforgeeks.org/minimum-number-of-edges-between-two-vertices-of-a-graph/
You are given a undirected graph G(V, E) with N vertices and M edges. We need to find the minimum number of edges between a given pair of vertices (u, v).
static
int
minEdgeBFS(Vector <Integer> edges[],
int
u,
int
v,
int
n)
{
Vector<Boolean> visited =
new
Vector<Boolean>(n);
for
(
int
i =
0
; i < n; i++) {
visited.addElement(
false
);
}
Vector<Integer> distance =
new
Vector<Integer>(n);
for
(
int
i =
0
; i < n; i++) {
distance.addElement(
0
);
}
Queue<Integer> Q =
new
LinkedList<>();
distance.setElementAt(
0
, u);
Q.add(u);
visited.setElementAt(
true
, u);
while
(!Q.isEmpty())
{
int
x = Q.peek();
Q.poll();
for
(
int
i=
0
; i<edges[x].size(); i++)
{
if
(visited.elementAt(edges[x].get(i)))
continue
;
distance.setElementAt(distance.get(x) +
1
,edges[x].get(i));
Q.add(edges[x].get(i));
visited.setElementAt(
true
,edges[x].get(i));
}
}
return
distance.get(v);
}