free web page hit counter
🛡️
Copyright Notice: This video is officially sourced and embedded from YouTube. For all copyright inquiries, reports, or removals, please contact YouTube's legal team here.
Reducible

Reducible

334,000 subscribers

👁 526,338 views

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 :(

— @ElectricalEngineerGenius

0:00 Intro and Preview

0:50 Graph Traversal

So you can create mazes using DFS and solve then using BFS ?

— @JuanAdonisNúñezCollado-c6j5y

1:20 DFS Walkthrough and Examples

6:26 Recursive Implementation

hey budy, which tool you used for visualization

— @Sukhnaazkiduniya

11:08 Iterative Implementation

15:06 Preorder vs Postorder DFS

Good video

— @УлугбекНуриддинов-ф8ж

17:01 DFS Applications

Support: https://www.patreon.com/reducible

w

— @jelorivera

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.

— @RJamshidbek

More User Perspectives

@

you made it amazingly simple to understand.

@itsmemansoor
@

For the dfs implementation 2 (stack version), do you need the "if not marked" check twice? Wouldn't just the second be enough?

@jacobthorn2800
@

20:29 There is no solution for the maze.

@null-0x
@

Hi

@VladGheorghe-f9c
@

my goat thank you

@strive4441
@

recursive solution is very infrequently used outside of academia. very infrequently.

@jonassteinberg3779
@

When I was studying game making I'd wonder how the hell pathing in strategy games work. This feels like it has answered my question

@thepraetorian2368
@

There's no way out

@La_these
@

wouldn'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?

@surzp
@

Petition to change the name to the "f*** around and find out" algorithm

@EigenRovak
@

best

@BardiyaBasafa
@

I'm currently preparing for a coding interview at a big tech company, and this video helped me a lot. Many thanks!

@adminator
@

That maze is not solvable.

@ElGoog2
@

what about weighted undirected graphs?

@_greysama_
@

what a labour you put on this video. Thanks a lot.

@ahmetlacin5748
@

why there is not inorder

@longvan7487
@

Hey, 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

@HiddenNameUnfortunately
@

Song?

@mx24v41
@

The recursive solution is so elegant because it uses the native stack that most programming languages have - the call stack.

@andrewsologor2696
@

The only video i wanted to watch to understand dfs

@ShirmilWelgama
@

Nice, but what is that visit() function didn't get it?

@terciofelipeoliveirafrance2228
@

Why did we not go to vertex 4 but to 3 first?

@supratimroy9771
@

Thanks a lot

@parsasamandi569
@

Tysm!!!

@user-fh9wm2re2j
@

Amazing video! A variant of the iterative approach which also builds a post order would also be cool to see!

@AthenaXOfficial
@

very nice animation

@Antonio-kv4lj
@

What do you mean by the neighbors function?

@j2dagriffs
@

i have another task is to find a path from a maze using DFS

@nissanskyline7151
@

Thank 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!

@artyomkarapetyan08
@

Topological 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-c4d
@

this is art.

@whatthesigma-c4d
@

The maze at the end has no solution, I almost lost my mind trying to solve it

@christopherpoulsenfernande1624
@

This video sucks, hes just farming view time for ads

@getcrackedapp
@

Same algorithm i use when exploring games

@ErickandSomeAnimals
@

I'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?

@RobertShane
@

I love this video! I'm glad I found out about your channel from 3B1B's FAQ section on animation

@rickyc46
@

My 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?

@asalrjz2006
@

4:58 Thank you for simplifying the DFS order

@bonietdube
@

best explanation. I have spent a long time trying to learn this. Your visualization has made a HUGE difference.

@SirFancyPantsMcee
@

I 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

@harshnj
@

Thank you so much

@fatalsg92
@

Great explanation of intuition behind graphs!!!

@joelngige5776
@

This is absolutely brilliant! Just what I needed! Thank you so much for this! Keep ut the excellent work!

@fredericoamigo