Iterative Depth First Search in Data Structure | DFS (Iterative) | C++ Java Python

preview_player
Показать описание
Lesson 6: Depth First Search Traversal (Iterative | Stacks)
--------------------------

#Graphs #GraphTraversal #DFS #DepthFirstSearch
Рекомендации по теме
Комментарии
Автор

I honestly never understood dfs and backtracking mechanisms but now that I watched your videos about dfs I'm finally getting how it works and I'm able to solve dfs questions that I could not solve before.

MrSun
Автор

The iterative code for DFS is wrong.
Note: It will explore all the node but it will not follow depth first search.
While Popping the element we should mark the element as visited not during push. This way same node can be pushed in the stack multiple times. but while popping we will check, the element is visited or not . if not visited then only print the node.

AmitKhoth
Автор

What's the use of for loop in dfs

noushadkhan
Автор

Please try to add some problems according to the topic so that after learning we can practice.

israkulhaquetusher
Автор

The code is wrong. If you mark all the adjacent nodes visited, then the child nodes will not be able to go visit some node in a depth-first way because it is already marked as visited.

while (!stack.empty())
{
int u = S.top();
S.pop();

if (!visited[u])
{
cout << u << " ";
visited[u] = true;
}

for (int v : m_adj[u])
if (!visited[v])
S.push(v);
}

swaw
welcome to shbcf.ru