When to apply bfs and dfs? what type of problems are handled by bfs & dfs?

any simple practice problems? I dont want to learn pointers in detail thats why i am interested in stl implementation.Please help me.

Here’s the code to Breadth first Search : https://github.com/Shraeyas/General-Codes/blob/master/bfs.cpp

Here’s the code to Depth first Search : https://github.com/Shraeyas/General-Codes/blob/master/dfs.cpp

For practise you can check this problem on SPOJ : www.spoj.com/problems/PT07Z

It can be done either using BFS or DFS.

can u please explain the code little bit.can i able to solve any problem with dfs or bfs at codechef

One question before that… are you completely new to BFS and DFS and want me to explain those algorithms also or you just want the explanation for the code?

yes completely new to dfs and bfs,i just want to increase my level

Then I would suggest you to watch video tutorials on youtube and then try to understand the code. This way it would be better.

If you understand the algorithm properly then you will surely understand the code. It’s very straight forward

But If you insist I can explain the codes but I guess it would be better the other way, i.e. first the algo and then the code.

i already knows about the algorithm,but plz explain me code because for full clearity purpose

OK, give me a few minutes

sure…it will be okay to use dfs,bfs through stl in contests?

I have taken the number of vertices as n and the number of edges as m.

And the adjacency list is being denoted by adj.

Visited array is used to check if our current vertex is visited or not.

For DFS :

Since I have taken a map for visited array, all the vertices will be initially marked 0 i.e. unvisited.

Now we will take some starting vertex s (which I have by default to be 1 but you can change it to anything).

Now inside the DFS function :

for(int i=0 ; i< adj[s].size();i++)

```
if(!vis[adj[s][i]])
{
dfs(adj[s][i]);
}
```

If our current edge leads to a vertex which is already visited then we do nothing.

Otherwise if that vertex is unvisited then we will start processing from this vertex, i.e. we will make it our current vertex i.e. further move into recursion making it our starting vertex.

```
if(!vis[adj[s][i]])
{
dfs(adj[s][i]);
}
```

When all the vertices for current vertex are visited then we will start backtracking.

Now this same process will be carried out again and again until all the vertices are visited.

For BFS :

Similar to DFS, initially all the vertices are marked 0 i.e. unvisited.

Just similar to how we used recursion in DFS, here we will make use of a queue.

BFS processes a graph level by level.

First we take a starting vertex s, and push it onto queue and mark it as visited.

Now we will process the graph until the queue is empty.

We will take all the vertices which are directly attached to the vertex on top of the queue.

for(int i=0;i< adj[s].size();i++)

`if(!vis[adj[s][i]])`

```
{
cout<<adj[s][i]<<" ";
vis[adj[s][i]] = 1;
q.push(adj[s][i]);
}
```

Now if the vertices are unvisited they are pushed into the queue.

Now we will perform pop operation on the queue and remove the topmost element of the queue and repeat the above two steps again again until all the vertices are visited.

Thats it!

You could refer these links:

Simple STL Explanation from TopCoder

For questions, you could just sort down on Codechef, Codeforces I guess based on BFS/ DFS tags.

Yes, STL will definitely be good to use for contests, it saves you a lot of time.

For example you want to implement dijkstra somewhere and if you go for implementing priority queue by yourself then it will eat up a lot of time and if any error creep in there you might also have to debug that part too. So it’s better to go with STL

what if i take arrays of vector,since im unable to understand some lines(meaning).please help me

but i am unable to figure out how to apply,please help me

Definitely that can be done. You can use an array of vectors.

vectoradj(100);

It will work exactly the same.

Firstly, vector adj[V]; defines a List container which shall keep the stored nodes and the list stored shall be of the nodes which are directly connected to it. Secondly, another approach for weighted graph is to Store weight using a vector adj[V]; where suppose for a node U you can have (edge, weight) stored all along. Then adj.push_back(V); stores V as a node connected to U with a direction from U --> V for undirected do the same pushback swapping U, V again. Simply, use a list iterator to traverse through the connected nodes for DFS which should suffice.

Using Lists in DFS/BFS reduces Time/ Space complexity by a huge efficiency. you can’t even store weights for graphs with vertices over 10^5 because you’d have to create COST[100000][100000] which shall result in StackOverFlow error. But using PAIR as i mentioned above solves all the problem. Refer through the links I mentioned above, they’ll provide you an optimum approach to make you understand.

then role of map? please dont mind…But nice explanation

map is more useful when the limits on inputs are too high. For example you can’t take an array of size 10^9 but with map you can do it directly.

So it’s just the size, which is why I used map. But you can replace map with vector of array here. It doesn’t really matter