C++ Depth First Search (DFS) Implementation [closed]

落爺英雄遲暮 提交于 2019-12-06 08:46:20
for (int i = 0; i < (int)adj[i].size(); i++) {

into:

for (int i = 0; i < (int)adj[u].size(); i++) {
//                           ^ u, not i

Live demo link

The overall logic seems ok, but I see some weird things in the details:

  1. in line 17, the for-loop, should that be adj[u], rather than adj[i] ??

Other things I noticed:

  1. Not using single-letter variable names would greatly help us understand your code...means you'll get more replies.
  2. Your connected components will print in reverse order (3 2 1, rather than 1 2 3) because in dfs() you have the print after the recursive call
  3. I question why you're using a pair in lines 31 and 32 when the second int is 1, and you never use it.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!