1.2k views

Consider an undirected unweighted graph $G$. Let a breadth-first traversal of $G$ be done starting from a node $r$. Let $d(r,u)$ and $d(r,v)$ be the lengths of the shortest paths from $r$ to $u$ and $v$ respectively in $G$. if $u$ visited before $v$ during the breadth-first traversal, which of the following statements is correct?

1. $d(r,u) < d(r,v)$
2. $d(r,u) > d(r,v)$
3. $d(r,u) \leq d(r,v)$
4. None of the above
edited | 1.2k views
(C) is the correct option!

As, BFS traverses level by level, so either v is at the same level (sibling) or v is at just the next level from u.

Ans : C.

BFS is used to count shortest path from source (If all path costs are 1 !)

now if $u$ is visited before $v$ it means $2$ things.

1. Either $u$ is closer to $v$.
2. if $u$ & $v$ are same distance from $r$, then our BFS algo chose to visit $u$ before $v$.
edited by
does BFS always take the shortest path to every node?
@Arjun Sir

First of BFS doesnot calculate according to shorest distance. After root we can take node any order. But if we take any parent first, it's children should be select first. So, u is first taken than doesnot mean u have shortest distance than v. But there is also a possibility that distance of (r,v) is less than distance of (r,u) . So, according to me d) will be answer.
@srestha, if we choose a node $x$  to visit in BFS then children of this node will not be visited until all the siblings of this node(which are at the same distance from root ) will be explored.  henece if $u$ is visited first then only two cases are possible , either $u$ is nearest to root or $u$ is at the same distance as $v$ is.
sir can you elaborate  the proper meaning of option c,

Considering all given constraints :-

u is visited before q means 2-cases can only occur:-

Hence option C is Ans.

First of BFS doesnot calculate according to shorest distance. After root we can take node any order. But if we take any parent first, it's children should be select first. So, u is first taken than doesnot mean u have shortest distance than v. But there is also a possibility that distance of (r,v) is less than distance of (r,u) . So, according to me d) will be answer.
@shreshta as question is on BFS and we know that bfs works on unweighted graph..what you are saying is that vertex v can also have small distance but its not possible if distance would have been small than it would have been more closer to root..so according to bfs as its level order we need to go by that theory...vertex v can only have same distance as u or greater than v..in these two cases only vertex u can be visited before u..!

But still there are chances that even if the vertex v being traversed later has lesser distance than u.
Then I guess it should be not related as per option C and answer should be D.

+1 vote
ans is C.

here if the distances d(r,u)=d(r,v) then also path from r will not be updated because if the distance is shorter then only path is updated.
In BFS can't we visit any adjacent vertex  of vertex r( in any order.) ..what is the concept of min distance here...?Please explain
u is visted before v.  therefore d[u] < d[v]

but u and v can be on same level.  therefore d[u] = d[v]

therefore d[u] <= d[v]