Definitely that can be done. You can use an array of vectors.
vectoradj(100);
It will work exactly the same.
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[U].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
please provide vector implementation im unable to understand role of map
Ok, I will update that.
Check these links again, I have updated it.
BFS : https://github.com/Shraeyas/General-Codes/blob/master/bfs.cpp
DFS : https://github.com/Shraeyas/General-Codes/blob/master/dfs.cpp
one query-> s = q.front();
q.pop();
it should be
q.pop(); then
q.front();
am i right?
Yes, you are right! I fixed it. Thanks
vector adj[10]; it same like vector<vector> adj(10)⌠?
code is not working fine for input
5 3
1 2
1 3
2 4
it gives output 2 3 1 4
vectoradj[10] means we are taking an array of vectors and vector<vector >adj(10) means we are taking a vector of vector and we have already put 10 elements in the vector to initialise it.
Yes, both will do same work.
In first vector adj[10] :
we are making array of vector(vector containing integer) therefore,
adj[0] -> vector
adj[1] -> vector and so on.
In second we are making vector of size 10 whose each element will be vector which is equivalent to array of vector as above therefore here also,
adj[0] -> vector
adj[1] -> vector and so on
what should be prefer? arrays of vector or vectors of vectors?
You need to mark the starting vertex as visited initially. Rest seems fine
niceâŚthanks a lot for amazing help.
how can i deal with this bfs problem- TRATCK1 Problem - CodeChef
You can use any of them