retagged by
18,330 views
3 votes
3 votes

Consider two vertices a and b that are simultaneously on the FIFO queue at same point during the execution of breadth first search from s in an undirected graph.
Which of the following is true?
1. The number of edges on the shortest path between s and a is atmost one more than the number of edges on the shortest path between s and b.
2. The number of edges on the shortest path between s and a is atleast one less than the number of edges on the shortest path between s and b.
3. There is a path between a and b.

     a.1 only

     b.1 and 2 only

     c. 2 only

     d. 1, 2 and 3

retagged by

2 Answers

3 votes
3 votes

Here (i) is satisfying condition 1 and 3

and (ii) is satisfying condition 2 and 3

1 votes
1 votes

In BFS traversing we maintain a Queue to store nodes. Using the following loop we do bfs and push the nodes in the queue : 

while(!Q.empty()) {
        s = Q.front();
        //cout << s << " ";
        Q.pop_front();
        
        // while exploring the nodes of s we only push the adjacent nodes of s
        // explore adjacent loop
        for(i : i = adjacent nodes of s) {
            if(i is visited) {
                mark i as visited;
                // marking a node as visited does not mean fully explored
                Q.push(i);
            }
        }
    }

Queue makes it possible to push the nodes that are close to source earlier.

If we say that A is a node and it is at a distance d from the Source.While exploring (and pushing its adjacents in Queue) the adjacent nodes of A we touch a layer of nodes which are at distance of (d+1) from the source. (not more than that). And without completing (exploring) the layer of nodes at distance d from source we never explore a node which is at distance (d+1) from the source (how this even possible ? yes : because we are using queue not stack ) . This ensures that, whatever be the nodes in the queue in a particular time , the distance from source to any one of those nodes do not differ by more than 1.

conclusion:

distance[a] - distance[b] = 1,-1,0
// a and b are nodes in the queue in a particular time
// distance[a] = distance of a from source

Since a and b are in the queue, they are reachable from source S. There may or may not be direct path to go from a to b or b to a. We do not bother about that, because there is definitely a path via S :  a--to--S--to--b.

ALL ARE CORRECT.

edited by
Answer:

Related questions

365
views
1 answers
0 votes
Subbu. asked Jul 16, 2022
365 views
Please list the problems where BFS alone can do and DFS alone can do and both can do??
910
views
2 answers
1 votes
saptarshiDey asked Feb 1, 2019
910 views
What will be the path from A-H if BFS is used in the following graph?
1.5k
views
0 answers
1 votes
Markzuck asked Dec 30, 2018
1,455 views
Can someone please explain what are the types of edges possible in BFS and DFS for DIRECTED as well as UNDIRECTED graphs?Individual meaning of BACK, FRONT and CROSS edges...
1.5k
views
1 answers
0 votes
Sandy Sharma asked Dec 25, 2018
1,524 views
True or False , with reason.For a directed graph, the absence of back edges with respect to a BFS tree implies that the graph is acyclic?Answer is FalseExplanation:FALSE....