Depth First Search (DFS) Explained: Algorithm, Examples, and Code
Video Overview & Insights
In this video, I explain the fundamental ideas behind the Depth First Search (DFS) graph algorithm. We first introduce the concept of a graph traversal. We then go through several examples of DFS to provide intuition. Afterwards, we then go through both a recursive and iterative implementation with provided code. We discuss the differences between the implementation and also make a distinction between a preorder and post order DFS traversal. We then finish the video off with some practical and fun applications of depth first search in graph theory.
lovely video, but the music was a bit sad :(
0:00 Intro and Preview
0:50 Graph Traversal
So you can create mazes using DFS and solve then using BFS ?
1:20 DFS Walkthrough and Examples
6:26 Recursive Implementation
hey budy, which tool you used for visualization
11:08 Iterative Implementation
15:06 Preorder vs Postorder DFS
17:01 DFS Applications
Support: https://www.patreon.com/reducible
w
This video wouldn't be possible without the open source manim library created by 3blue1brown: https://github.com/3b1b/manim
Here is link to the repository that contains the code used to generate the animations in this video: https://github.com/nipunramk/Reducible
I really like this video. They are very helpful, that my teacher can't deliver it to me. I hope we will see more videos related to other algorithms too.
More User Perspectives
you made it amazingly simple to understand.
@itsmemansoorFor the dfs implementation 2 (stack version), do you need the "if not marked" check twice? Wouldn't just the second be enough?
@jacobthorn280020:29 There is no solution for the maze.
@null-0xHi
@VladGheorghe-f9cmy goat thank you
@strive4441recursive solution is very infrequently used outside of academia. very infrequently.
@jonassteinberg3779When I was studying game making I'd wonder how the hell pathing in strategy games work. This feels like it has answered my question
@thepraetorian2368There's no way out
@La_thesewouldn't it be safer to first mark the node visited and then perform the visit() action?
if there are things in the visit() action which rely on the state of the nodes, it is possible that we re-push the same node again.
for the context of this video, which is just plain printing, it may not make a difference. or is my understanding flawed somewhere?
Petition to change the name to the "f*** around and find out" algorithm
@EigenRovakbest
@BardiyaBasafaI'm currently preparing for a coding interview at a big tech company, and this video helped me a lot. Many thanks!
@adminatorThat maze is not solvable.
@ElGoog2what about weighted undirected graphs?
@_greysama_what a labour you put on this video. Thanks a lot.
@ahmetlacin5748why there is not inorder
@longvan7487Hey, quick recommendation to make my experience watching your content a tiny bit better: start including (and fix so all existing have) an index number in the title of any video that is part of a series. That way i still know which video is next if i am not in the playlist. It probably also would help engagement since if i watched the first only and then later got recommended the second one in my feed I’d be more likely to click, since YouTube does not promote content as a part of a series to the extent other sites do with their “Continue Watching” to pick up the next episode of a series you are watching.
Just a suggestion. Thanks for making valuable content
Song?
@mx24v41The recursive solution is so elegant because it uses the native stack that most programming languages have - the call stack.
@andrewsologor2696The only video i wanted to watch to understand dfs
@ShirmilWelgamaNice, but what is that visit() function didn't get it?
@terciofelipeoliveirafrance2228Why did we not go to vertex 4 but to 3 first?
@supratimroy9771Thanks a lot
@parsasamandi569Tysm!!!
@user-fh9wm2re2jAmazing video! A variant of the iterative approach which also builds a post order would also be cool to see!
@AthenaXOfficialvery nice animation
@Antonio-kv4ljWhat do you mean by the neighbors function?
@j2dagriffsThank you so much! Your DFS/BFS explanation videos are SO amazing and you explain everything so well whilst making the presentation beautiful. I can confidently say your videos are awesome, keep it up!
@artyomkarapetyan08Topological Sort kind of remains me how skill trees works in some games.If you need two requirements to unlock a skill,you shall unlock those first.But on the other hand,if you choose to upgrade a skill with no requirements first you may get more skills.Essentially,if you make a weight graph of how valuable each skill is you can calculate best skill tree to upgrade your skills.
@whatthesigma-c4dthis is art.
@whatthesigma-c4dThe maze at the end has no solution, I almost lost my mind trying to solve it
@christopherpoulsenfernande1624This video sucks, hes just farming view time for ads
@getcrackedappSame algorithm i use when exploring games
@ErickandSomeAnimalsI'm confused about the difference between visit(v) and marked[v] = true. Is visit(v) more like a comment about what we are going to do?
@RobertShaneI love this video! I'm glad I found out about your channel from 3B1B's FAQ section on animation
@rickyc46My question might sound silly, but I'm gonna ask it. So the only language I know is python and I use Spyder as my IDLE. Where should I run the code for dfs? which app?
@asalrjz20064:58 Thank you for simplifying the DFS order
@bonietdubebest explanation. I have spent a long time trying to learn this. Your visualization has made a HUGE difference.
@SirFancyPantsMceeI like the sombre music, reflects how I'm feeling about my midterm tomorrrow
@liyre4189"Intiution", "come up on your own", "invent yourself", these 3b1b words are just magical
@harshnjThank you so much
@fatalsg92Great explanation of intuition behind graphs!!!
@joelngige5776This is absolutely brilliant! Just what I needed! Thank you so much for this! Keep ut the excellent work!
@fredericoamigo