Depth First Search Algorithm | Graph Theory

  Рет қаралды 437,872

WilliamFiset

WilliamFiset

Күн бұрын

Depth First Search (DFS) algorithm explanation
Source code:
github.com/williamfiset/algor...
Video Slides:
github.com/williamfiset/Algor...
0:00 Depth first search as an algorithm template
1:04 Simple DFS example
3:30 Depth first search pseudocode
5:10 Finding connected components with a DFS
7:32 connected components source code
9:27 What else is a DFS useful for?
===================================
Practicing for interviews? I have used, and recommend `Cracking the Coding Interview` which got me a job at Google. Link on Amazon: amzn.to/3cvMof5
A lot of the content on this channel is inspired by the book `Competitive Programming` by Steven Halim which I frequently use as a resource and reference. Link on Amazon: amzn.to/3wC2nix
Support me by purchasing the full graph theory course on Udemy which includes additional problems, exercises and quizzes not available on KZbin:
www.udemy.com/course/graph-th...

Пікірлер: 109
@anhmai81
@anhmai81 21 күн бұрын
this is way better than a 2-hour lecture from a CS professor with multiple PhD's explanation. Thank God I found your video.
@nmamano
@nmamano 4 жыл бұрын
4:55 It is pretty clear, but if anyone is wondering, "graph[at]" shuld be "g[at]" in the DFS pseudo-code. Great video!
@iSuperMC
@iSuperMC 2 жыл бұрын
Thank you, I was wondering what that was
@hasnaindev
@hasnaindev 2 жыл бұрын
What's the difference? g[at] seems more confusing to me as the letter "g" does not really mean anything and one has to think about what it is whereas "graph" clearly conveys what the variable is about.
@nmamano
@nmamano 2 жыл бұрын
@@hasnaindev the point is that the variable g is defined earlier in the video, whereas graph was not defined as a variable
@hasnaindev
@hasnaindev 2 жыл бұрын
@@nmamano Ooh.
@mathc
@mathc Жыл бұрын
it wasn't pretty clear for me, thanks
@iyadelwy1500
@iyadelwy1500 3 жыл бұрын
An explanation with the help of a good example > A CS professor with 2 PHD's explaining it in sheer theory
@J235304204
@J235304204 5 жыл бұрын
The best explanation a developer could ever ask for. You have cut out all the possible crap and got to the point.
@karthikrangaraju9421
@karthikrangaraju9421 4 жыл бұрын
So basically Google's notorious "Number of islands" problem is basic graph theory == Finding connected components using DFS! Thank you William
@protyaybanerjee5051
@protyaybanerjee5051 3 жыл бұрын
Finding 'disconnected components' . That's the number of island
@luisfelipe847
@luisfelipe847 3 жыл бұрын
That's such a great video! I loved the minimalist visual and explanation!
@roman_mf
@roman_mf 2 жыл бұрын
So happy I found your channel. Thanks for your efforts and quality content. Short, to the point, and the animation done just right. :-)
@miriyalajeevankumar5449
@miriyalajeevankumar5449 3 жыл бұрын
You are the best in the entire youtube for Algo and DS!
@TechItEasy0
@TechItEasy0 3 жыл бұрын
These videos are awesome so far... will watch them through, undoubtedly multiple times!
@sandeepkryadav1469
@sandeepkryadav1469 3 жыл бұрын
Thank you very much for such an amazing video series, the content of the video is so organized that whenever I think of some scenario that scenarios explanation comes to next. Amazing :)
@colegartner3339
@colegartner3339 2 жыл бұрын
Thank god for this man, the best explanation with visuals a comp sci major could ask for.
@wanqingli1254
@wanqingli1254 5 ай бұрын
this visualization is so clear and william is so good at explaining omg, thx for saving my algo exam
@varunvishwakarma2901
@varunvishwakarma2901 Жыл бұрын
I was scared of graph and you made it so easy.Thanks a lot.
@iacobibrasiliensium2139
@iacobibrasiliensium2139 2 жыл бұрын
Thank you sir, your explanations are always very clean and simple
@selvalooks
@selvalooks 3 жыл бұрын
Made it easy to understand and also the listing use cases, great !!! Thanks !!!
@iamakifislam
@iamakifislam 3 жыл бұрын
You made my concept crystal clear. :) Thanks
@vigneshsr1118
@vigneshsr1118 3 жыл бұрын
bought the course, thanks for all the help.
@kamalulazmim3820
@kamalulazmim3820 3 жыл бұрын
Very clear, thank you so much!
@myhimalayanchants
@myhimalayanchants 3 жыл бұрын
Excellent Clarity
@samtux762
@samtux762 5 жыл бұрын
Great video once again.
@rodituclashroyale1812
@rodituclashroyale1812 Жыл бұрын
You can reverse the order of calling the neighbours in this implementation, so the output will the same as You would use stack dfs algorithm - the one that uses stack instead of recursion. Because if You add neighbours to the stack the last one will be called first since it will be popped first.
@KAINOA104
@KAINOA104 3 жыл бұрын
Very helpful, thank you!
@delyart
@delyart Жыл бұрын
Awesome explanation. Thanks.
@isaacnewman9341
@isaacnewman9341 2 жыл бұрын
I understand the visual example for DFS, but I struggle with understanding the pseudo code. However, this is a great video!
@saulr.4481
@saulr.4481 2 жыл бұрын
Great explanation!
@JamesBrodski
@JamesBrodski 2 жыл бұрын
Great video. Thank you so much.
@anjaliyadav9360
@anjaliyadav9360 Жыл бұрын
Best Explanation ever!
@ElAntroDeDager
@ElAntroDeDager 3 жыл бұрын
Great job, TY!
@enriquelmx
@enriquelmx 4 жыл бұрын
Can you visit all the neighbors of 7 in any direction no matter the order (preorder, inorder, postorder)? or that only applies to trees?
@MrDayTwo
@MrDayTwo 2 жыл бұрын
Waaaay better than leetcode explanation. Thanx!
@gokulkurup1584
@gokulkurup1584 2 жыл бұрын
great video. jutst a gotacha for future learners, the dfs findComponents algo only works well for undirected graphs and will give weird results with directed graphs. ex. in directed graphs if you take the picture 7:25 the for loop first sees node#2. however there is no path from 2 to 3 even tho they are in the same component. so instead of the answer which is 5 you get 6 Excellent video tho and thanks for the explanation
@jshiwam
@jshiwam 2 жыл бұрын
I think you confuse reachability with connectedness. Two nodes can be connected but unreachable in directed graph. In order to ensure the connected components you need to loop over each node and see if a path exists which connects all the components and there can be a scenario where nodes would be connected but you wont have a path. In order to know the graph is connected or not, it need to be undirected.
@javiersorucolopez1502
@javiersorucolopez1502 3 жыл бұрын
Amazing video!
@frgaming6711
@frgaming6711 4 жыл бұрын
Thank you Sir
@christianrodier3381
@christianrodier3381 4 жыл бұрын
That you for using that visual aid.
@highinstitute2366
@highinstitute2366 4 ай бұрын
i love your channel, thanks soo much for these good videos🥰
@briannguyen5057
@briannguyen5057 3 жыл бұрын
very helpful!
@rajatbudania6181
@rajatbudania6181 3 жыл бұрын
very well explained )--
@amoghdadhich9318
@amoghdadhich9318 11 ай бұрын
Hey are you sure DFS can be used to find the minimum spanning tree? Pretty sure that it wont give a linear time solution. Prims or Kruskals will probably be better suited for this
@slavinastefanova4479
@slavinastefanova4479 4 жыл бұрын
In the pseudo-code for DFS, the variable g is initialized to be the adjacency list representing the graph. However, in the dfs function, this variable is called "graph". Or am I missing something? Also, n is initialized to be |V| but this variable is then never actually used, other than implicitly to initialize the "visited" list of booleans.
@evanwilliams2048
@evanwilliams2048 3 жыл бұрын
thats not an implict use tho - its very important
@senthilmuruganr234
@senthilmuruganr234 3 жыл бұрын
Excellent
@klevisimeri607
@klevisimeri607 Жыл бұрын
Thank you!
@yagzgazibaba857
@yagzgazibaba857 6 ай бұрын
If the graph is cyclic one then inside dfs method you also need to check if current node visited or not
@anhmai81
@anhmai81 21 күн бұрын
Thanks!
@chieesntra
@chieesntra 2 жыл бұрын
thank you!
@DT-ro9et
@DT-ro9et 3 жыл бұрын
You are awesome!!!
@josephhigh250
@josephhigh250 3 жыл бұрын
In your first example, you forgot to cover the subsequent call to DFS to explore the other component that contains exactly one node (e.g., node 12).
@AmanYadav-ry3xr
@AmanYadav-ry3xr 3 жыл бұрын
It will give 6 not 5 as the answer because in for loop we visit each node from 0 to n so when i=2 it will visit all the neighbours of 2 but not 3 because there is no outgoing edge to 3.so when i=3 it will also count it as a individual node. And hence the number of connected components would be 6 not 5. Correct me if i am wrong.
@ilikememes9052
@ilikememes9052 3 жыл бұрын
Is this Graph series helpful for competitive programming??
@shanthureddy4234
@shanthureddy4234 5 жыл бұрын
8:52 ?? Count++ ; // should be implemented later after implementing the DFS( i) ; right ?
@WilliamFiset-videos
@WilliamFiset-videos 5 жыл бұрын
It shouldn't matter. The idea is to give every component a unique id at the end of the day. However, labeling components starting at 1 instead of 0 is handy for debugging because by default the 'components' array has all values initialized to 0 so it's hard to tell whether a node was marked by the DFS method or simply initialized that way.
@shanthureddy4234
@shanthureddy4234 4 жыл бұрын
@@WilliamFiset-videos cool , gotcha, thanks man !!
@miguelbarajas9892
@miguelbarajas9892 2 жыл бұрын
thank you, now to find those islands 🏝 🏝 🏝
@jeffreycuraming3861
@jeffreycuraming3861 20 күн бұрын
How to create that visual representation?
@sambitdash4163
@sambitdash4163 4 жыл бұрын
Just to clarify this is the python code I wrote using the graph example given in video. Is it okay? g = {0:[1,9],9:[0,8],1:[0,8],8:[1,9,7],7:[8,10,3],10:[7,11],11:[7,10],6:[5,7],5:[6,3],3:[2,4,5],2:[3],4:[3]} n = 12 visited = n*[False] def dfs(at): at = at[0] if visited[at]: return visited[at] = True neighbours = g[at] for nex in neighbours: dfs([nex]) start_node = g[0] dfs(start_node)
@lindsaymahusay9769
@lindsaymahusay9769 19 күн бұрын
can someone help me how to write a complete code for this? using c language
@MrMacAwsome
@MrMacAwsome 5 жыл бұрын
If I wanted to find the number of paths between two nodes in a DAG, would I use a DFS or BFS? Nice video, thanks.
@WilliamFiset-videos
@WilliamFiset-videos 5 жыл бұрын
Probably a BFS with some DP? I haven't solved it yet but you might want to try: open.kattis.com/problems/walkforest
@MrMacAwsome
@MrMacAwsome 5 жыл бұрын
WilliamFiset thanks
@iqbalibrahim4713
@iqbalibrahim4713 6 жыл бұрын
Can I ask something, I know you entered icpc, and I really want to get ready when I'm entering it, so can you tell what I need to know about number theories for competitive programming, thank you very much
@abdelrahman2348
@abdelrahman2348 5 жыл бұрын
if you got any new or path to study for acm please tell me
@dumm005
@dumm005 4 жыл бұрын
DId you slowmo your presentation?
@marcftw
@marcftw 2 жыл бұрын
Where does the backtracking occur in the pseudo code snippet?
@zawette
@zawette 2 жыл бұрын
When you return from a recursive call
@hessamzadeh1453
@hessamzadeh1453 3 жыл бұрын
can we use this for nba fan duel? what the sharks use
@marialaurabisogno9951
@marialaurabisogno9951 3 жыл бұрын
9:24 aren't count and components global?
@JR-mk6ow
@JR-mk6ow 4 жыл бұрын
"the nice thing about dfs is that is really easy to code". THE AMOUNT OF ITERATORS, POINTERS AND VECTORS INSIDE THE GRAPH, VERTEX AND EDGE CLASS SAY OTHERWISE!! Sometimes I really hate c++
@MrKingoverall
@MrKingoverall 3 жыл бұрын
I love you man !!!!!!!!
@utilizator500
@utilizator500 Жыл бұрын
For the pseudo code: for next in neighbors should have an additional line below it that goes like this: if visited[next] = false In Python he did write it correctly though.
@spongsquad
@spongsquad Жыл бұрын
the exclamation mark "!" means the same thing as if visited[next] = false
@tomaskosta
@tomaskosta 4 жыл бұрын
in the beginning you explain that after the first traversal the dfs is over but you didn't visit the #12 node, dfs beside of coloring from white to gray to black also uses a counter to mark in each node when was that node firs found and when did the algorithm leave the node, you finished the traversal on the big graph but dfs start another traversal , you should have visit 12 also and mark it as a second component of the graph meaning the number of nils in the graph is 2
@WilliamFiset-videos
@WilliamFiset-videos 4 жыл бұрын
The animation doesn't visit node 12 because it's trivial, but for completeness sake it should. When I said the dfs was finished I was referring to the large component.
@videofountain
@videofountain 2 жыл бұрын
Why is there no return value in depth first search?
@poli2730
@poli2730 2 жыл бұрын
visitad[at], obviusly everybody miss the part on how to associate the node with his corresponding visited value... probably because nobody knows
@sumant9120
@sumant9120 3 жыл бұрын
3:30 Is this a DP implementation?
@sanathreddy7792
@sanathreddy7792 2 жыл бұрын
No
@sidekick3rida
@sidekick3rida 2 жыл бұрын
3:35 is `neighbours = graph[at]` supposed to be `neighbours = g[at]`?
@Kingofqueers1
@Kingofqueers1 5 жыл бұрын
What are edges & vertices in computer science?
@sharathkumar8422
@sharathkumar8422 5 жыл бұрын
Vertices could be computers, websites or programs. Edges then could be network connections, website links etc etc. I'm not a computer scientist but I do know that edges and vertices can be anything as long as they are connected or linked in some way in a system.
@Machinerium
@Machinerium 5 жыл бұрын
Think of edges as routes and vertices as cities. Check this awesome playlist about Graph Theory from the beginning kzbin.info/aero/PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P
@chinaguy101
@chinaguy101 3 жыл бұрын
cool
@louisham6998
@louisham6998 3 жыл бұрын
Why zero doesn't go to node 1
@jackmaxwell7342
@jackmaxwell7342 3 жыл бұрын
No goal node???
@SuchingYan
@SuchingYan 2 жыл бұрын
This is how my brain works
@Sairam30722
@Sairam30722 3 жыл бұрын
3;59 [0, n) not understand
@DeadalusX
@DeadalusX 2 жыл бұрын
Pro tip: set playback speed to 1.5
@bananaboyTS
@bananaboyTS 2 жыл бұрын
thanks a lot
@TheGianaJinx
@TheGianaJinx 2 жыл бұрын
This is the least confused I have ever felt hearing about DFS.
@shashankkumar3138
@shashankkumar3138 5 жыл бұрын
function findComponents(): invalid syntax is coming sir
@IgorSantarek
@IgorSantarek 2 жыл бұрын
@TheGugustar
@TheGugustar 2 жыл бұрын
You kinda sound like the guy from Brackeys
@svsrkpraveen
@svsrkpraveen 3 жыл бұрын
This guy sounds like Brackeys
@bezelyesevenordek
@bezelyesevenordek 2 жыл бұрын
what
@pcelis19
@pcelis19 4 жыл бұрын
even at 2x speed you talk slow, good tut
@Ammar23217
@Ammar23217 2 жыл бұрын
not useful. no complete example. many variables not declared. cannot understand.
@alanlinto9660
@alanlinto9660 10 ай бұрын
Dude sound like @ penguinz
@GodOfReality
@GodOfReality 3 жыл бұрын
You randomly switch between saying "dep" "defp" "def" and "defth" when you are trying to say "depth" lol.
@samirelzein1095
@samirelzein1095 2 жыл бұрын
dude, use less my imagination, show me, aka 3b1b
@bestwishes5553
@bestwishes5553 Жыл бұрын
siimiii.trii/catholicc'ica'mois'pentaocsiacal/o //nd.D
Breadth First Search Algorithm | Shortest Path | Graph Theory
7:23
WilliamFiset
Рет қаралды 658 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН
Omega Boy Past 3 #funny #viral #comedy
00:22
CRAZY GREAPA
Рет қаралды 34 МЛН
1🥺🎉 #thankyou
00:29
はじめしゃちょー(hajime)
Рет қаралды 78 МЛН
🍟Best French Fries Homemade #cooking #shorts
00:42
BANKII
Рет қаралды 39 МЛН
How Dijkstra's Algorithm Works
8:31
Spanning Tree
Рет қаралды 1,3 МЛН
Topological Sort Algorithm | Graph Theory
14:09
WilliamFiset
Рет қаралды 436 М.
Prim's Minimum Spanning Tree Algorithm | Graph Theory
14:53
WilliamFiset
Рет қаралды 112 М.
Depth-first search in 4 minutes
4:01
Michael Sambol
Рет қаралды 214 М.
A* (A Star) Search Algorithm - Computerphile
14:04
Computerphile
Рет қаралды 1,1 МЛН
Depth First Search
7:16
John Levine
Рет қаралды 92 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН