1,779 views
1 1 vote
Does BFS and DFS traversal sequence exist for disconnected graphs? Is yes can you please give a dry run of the algorithm on a disconnected graph?

According to me it should not exist as there is no edge to visit certain vertices of a different graph component from the source vertex in a disconnected graph. There is no absolute answer I could find online to this question.

1 Answer

Best answer
1 1 vote

Does BFS and DFS traversal sequence exist for disconnected graphs? Is yes can you please give a dry run of the algorithm on a disconnected graph?

Yes.

We start BFS or DFS on any random vertex (s) . Now BFS or DFS will print all the vertices which are reachable from s. 

What about other vertices? The graph can be disconnected.

To handle this situation we can do a little bit of modification in our algorithm. We will use an array of size V(number of vertices). Whenever we will visit a vertex we will mark it as visited.

Our code will be like this:

For all the vertices v of G

{

    if(visited[v] == false)

     BFS(v) or DFS(v) // count++;

}

the count will give us the number of connected components.

• selected by
Position:
Show:

Related questions

1 1 vote
1 1 answer
2.0k
2.0k views
Sandy Sharma asked Apr 24, 2018
1,957 views
Is this statement correct?? and why?.If there are negative weight cycles than dijkstra will surely fail but if there are negative weight edges(need not be cycle) then dij...
1 1 vote
1 1 answer
139
139 views
Shubham Sharma 2 asked Apr 19
139 views
Which of the following statements about DFS are correct?It can detect cycles in a graphIt can be used to find connected components.It works for both directed and undirect...
0 0 votes
1 answers 1 answer
342
342 views
admin asked Oct 10, 2024
342 views
Let $G=(V, E)$ be an undirected simple graph, and $s$ be a designated vertex in $G$. For each $v \in V$, let $d(v)$ be the length of a shortest path between $s$ and $v$. ...
0 0 votes
0 0 answers
925
925 views
Miku221 asked Aug 31, 2024
925 views
Hi everyone.I have a question for everyone as follows:Given a connected undirected graph with N vertices and N edges and an integer K.Print the number of ways to remove e...