Depth First Search Algorithm | Graph Theory

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ธ.ค. 2024

ความคิดเห็น •

  • @nmamano
    @nmamano 4 ปีที่แล้ว +193

    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 3 ปีที่แล้ว +3

      Thank you, I was wondering what that was

    • @hasnaindev
      @hasnaindev 3 ปีที่แล้ว +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 3 ปีที่แล้ว +12

      @@hasnaindev the point is that the variable g is defined earlier in the video, whereas graph was not defined as a variable

    • @hasnaindev
      @hasnaindev 3 ปีที่แล้ว

      @@nmamano Ooh.

    • @mathc
      @mathc 2 ปีที่แล้ว +2

      it wasn't pretty clear for me, thanks

  • @anhmai81
    @anhmai81 7 หลายเดือนก่อน +25

    this is way better than a 2-hour lecture from a CS professor with multiple PhD's explanation. Thank God I found your video.

    • @bullymaguire2335
      @bullymaguire2335 2 หลายเดือนก่อน

      Guess what ? My prof literally took screenshots of SCC from this channel and used it as his ppt lol

  • @J235304204
    @J235304204 5 ปีที่แล้ว +117

    The best explanation a developer could ever ask for. You have cut out all the possible crap and got to the point.

  • @iyadelwy1500
    @iyadelwy1500 4 ปีที่แล้ว +109

    An explanation with the help of a good example > A CS professor with 2 PHD's explaining it in sheer theory

  • @karthikrangaraju9421
    @karthikrangaraju9421 4 ปีที่แล้ว +44

    So basically Google's notorious "Number of islands" problem is basic graph theory == Finding connected components using DFS! Thank you William

    • @protyaybanerjee5051
      @protyaybanerjee5051 3 ปีที่แล้ว +3

      Finding 'disconnected components' . That's the number of island

  • @wanqingli1254
    @wanqingli1254 ปีที่แล้ว +1

    this visualization is so clear and william is so good at explaining omg, thx for saving my algo exam

  • @miriyalajeevankumar5449
    @miriyalajeevankumar5449 4 ปีที่แล้ว +6

    You are the best in the entire youtube for Algo and DS!

  • @VarunVishwakarma1
    @VarunVishwakarma1 ปีที่แล้ว +1

    I was scared of graph and you made it so easy.Thanks a lot.

  • @colegartner3339
    @colegartner3339 3 ปีที่แล้ว +2

    Thank god for this man, the best explanation with visuals a comp sci major could ask for.

  • @luisfelipe847
    @luisfelipe847 4 ปีที่แล้ว +11

    That's such a great video! I loved the minimalist visual and explanation!

  • @gokulkurup1584
    @gokulkurup1584 3 ปีที่แล้ว +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.

  • @TechItEasy0
    @TechItEasy0 4 ปีที่แล้ว

    These videos are awesome so far... will watch them through, undoubtedly multiple times!

  • @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. :-)

  • @sandeepkryadav1469
    @sandeepkryadav1469 4 ปีที่แล้ว

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

  • @MrDayTwo
    @MrDayTwo 2 ปีที่แล้ว

    Waaaay better than leetcode explanation. Thanx!

  • @rodituclashroyale1812
    @rodituclashroyale1812 2 ปีที่แล้ว

    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.

  • @isaacnewman9341
    @isaacnewman9341 2 ปีที่แล้ว +1

    I understand the visual example for DFS, but I struggle with understanding the pseudo code. However, this is a great video!

  • @anjaliyadav9360
    @anjaliyadav9360 2 ปีที่แล้ว

    Best Explanation ever!

  • @vigneshsr1118
    @vigneshsr1118 4 ปีที่แล้ว

    bought the course, thanks for all the help.

  • @IagoSB__0.0
    @IagoSB__0.0 3 ปีที่แล้ว

    Thank you sir, your explanations are always very clean and simple

  • @delyart
    @delyart 2 ปีที่แล้ว

    Awesome explanation. Thanks.

  • @iamakifislam
    @iamakifislam 3 ปีที่แล้ว

    You made my concept crystal clear. :) Thanks

  • @selvalooks
    @selvalooks 4 ปีที่แล้ว

    Made it easy to understand and also the listing use cases, great !!! Thanks !!!

  • @myhimalayanchants
    @myhimalayanchants 4 ปีที่แล้ว

    Excellent Clarity

  • @saulr.4481
    @saulr.4481 2 ปีที่แล้ว

    Great explanation!

  • @yagzgazibaba857
    @yagzgazibaba857 ปีที่แล้ว

    If the graph is cyclic one then inside dfs method you also need to check if current node visited or not

  • @highinstitute2366
    @highinstitute2366 11 หลายเดือนก่อน

    i love your channel, thanks soo much for these good videos🥰

  • @kamalulazmim3820
    @kamalulazmim3820 3 ปีที่แล้ว

    Very clear, thank you so much!

  • @KAINOA104
    @KAINOA104 4 ปีที่แล้ว

    Very helpful, thank you!

  • @samtux762
    @samtux762 5 ปีที่แล้ว

    Great video once again.

  • @javiersorucolopez1502
    @javiersorucolopez1502 3 ปีที่แล้ว

    Amazing video!

  • @ahmedamr1124
    @ahmedamr1124 หลายเดือนก่อน

    thank god you exist

  • @frgaming6711
    @frgaming6711 4 ปีที่แล้ว +1

    Thank you Sir

  • @josephhigh250
    @josephhigh250 3 ปีที่แล้ว +2

    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).

  • @miguelbarajas9892
    @miguelbarajas9892 3 ปีที่แล้ว

    thank you, now to find those islands 🏝 🏝 🏝

  • @shanthureddy4234
    @shanthureddy4234 5 ปีที่แล้ว +1

    8:52 ??
    Count++ ; // should be implemented later after implementing the DFS( i) ; right ?

    • @WilliamFiset-videos
      @WilliamFiset-videos  5 ปีที่แล้ว +2

      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 5 ปีที่แล้ว

      @@WilliamFiset-videos cool , gotcha, thanks man !!

  • @utilizator500
    @utilizator500 2 ปีที่แล้ว

    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 2 ปีที่แล้ว +2

      the exclamation mark "!" means the same thing as if visited[next] = false

  • @AmanYadav-ry3xr
    @AmanYadav-ry3xr 4 ปีที่แล้ว

    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.

  • @marcftw
    @marcftw 3 ปีที่แล้ว +1

    Where does the backtracking occur in the pseudo code snippet?

    • @zawette
      @zawette 3 ปีที่แล้ว +1

      When you return from a recursive call

  • @sidekick3rida
    @sidekick3rida 2 ปีที่แล้ว

    3:35 is `neighbours = graph[at]` supposed to be `neighbours = g[at]`?

  • @amoghdadhich9318
    @amoghdadhich9318 ปีที่แล้ว

    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

  • @klevisimeri607
    @klevisimeri607 ปีที่แล้ว

    Thank you!

  • @rajatbudania6181
    @rajatbudania6181 4 ปีที่แล้ว

    very well explained )--

  • @ElAntroDeDager
    @ElAntroDeDager 4 ปีที่แล้ว

    Great job, TY!

  • @senthilmuruganr234
    @senthilmuruganr234 3 ปีที่แล้ว

    Excellent

  • @christianrodier3381
    @christianrodier3381 4 ปีที่แล้ว +1

    That you for using that visual aid.

  • @briannguyen5057
    @briannguyen5057 3 ปีที่แล้ว

    very helpful!

  • @chieesntra
    @chieesntra 3 ปีที่แล้ว

    thank you!

  • @sumant9120
    @sumant9120 4 ปีที่แล้ว +1

    3:30 Is this a DP implementation?

  • @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?

  • @slavinastefanova4479
    @slavinastefanova4479 5 ปีที่แล้ว +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

  • @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)

  • @marialaurabisogno9951
    @marialaurabisogno9951 4 ปีที่แล้ว

    9:24 aren't count and components global?

  • @tomaskosta
    @tomaskosta 5 ปีที่แล้ว

    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  5 ปีที่แล้ว +3

      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.

  • @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

  • @JR-mk6ow
    @JR-mk6ow 4 ปีที่แล้ว +10

    "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++

  • @jeffreycuraming3861
    @jeffreycuraming3861 7 หลายเดือนก่อน

    How to create that visual representation?

  • @ilikememes9052
    @ilikememes9052 4 ปีที่แล้ว

    Is this Graph series helpful for competitive programming??

  • @lindsaymahusay
    @lindsaymahusay 7 หลายเดือนก่อน

    can someone help me how to write a complete code for this? using c language

  • @MrMacAwsome
    @MrMacAwsome 6 ปีที่แล้ว

    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  6 ปีที่แล้ว +1

      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 6 ปีที่แล้ว

      WilliamFiset thanks

  • @videofountain
    @videofountain 2 ปีที่แล้ว

    Why is there no return value in depth first search?

  • @DT-ro9et
    @DT-ro9et 4 ปีที่แล้ว

    You are awesome!!!

  • @dumm005
    @dumm005 4 ปีที่แล้ว

    DId you slowmo your presentation?

  • @hessamzadeh1453
    @hessamzadeh1453 3 ปีที่แล้ว

    can we use this for nba fan duel? what the sharks use

  • @MrKingoverall
    @MrKingoverall 3 ปีที่แล้ว

    I love you man !!!!!!!!

  • @Kingofqueers1
    @Kingofqueers1 6 ปีที่แล้ว

    What are edges & vertices in computer science?

    • @sharathkumar8422
      @sharathkumar8422 6 ปีที่แล้ว

      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 th-cam.com/play/PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P.html

  • @SuchingYan
    @SuchingYan 3 ปีที่แล้ว

    This is how my brain works

  • @jack7342-x5m
    @jack7342-x5m 4 ปีที่แล้ว

    No goal node???

  • @louisham6998
    @louisham6998 3 ปีที่แล้ว

    Why zero doesn't go to node 1

  • @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 6 ปีที่แล้ว

      if you got any new or path to study for acm please tell me

  • @DeadalusX
    @DeadalusX 3 ปีที่แล้ว +2

    Pro tip: set playback speed to 1.5

  • @chinaguy101
    @chinaguy101 3 ปีที่แล้ว

    cool

  • @TheGianaJinx
    @TheGianaJinx 2 ปีที่แล้ว

    This is the least confused I have ever felt hearing about DFS.

  • @Sairam30722
    @Sairam30722 3 ปีที่แล้ว

    3;59 [0, n) not understand

  • @sshmm92
    @sshmm92 15 วันที่ผ่านมา

    here for advent of code 2024 day 10

  • @TheGugustar
    @TheGugustar 3 ปีที่แล้ว

    You kinda sound like the guy from Brackeys

  • @LawsonAlger
    @LawsonAlger 2 หลายเดือนก่อน

    104 Isidro Road

  • @svsrkpraveen
    @svsrkpraveen 3 ปีที่แล้ว

    This guy sounds like Brackeys

  • @shashankkumar3138
    @shashankkumar3138 5 ปีที่แล้ว

    function findComponents():
    invalid syntax is coming sir

  • @IgorSantarek
    @IgorSantarek 3 ปีที่แล้ว

  • @beypazariofficial
    @beypazariofficial 3 ปีที่แล้ว

    what

  • @alanlinto9660
    @alanlinto9660 ปีที่แล้ว

    Dude sound like @ penguinz

  • @pcelis19
    @pcelis19 5 ปีที่แล้ว +1

    even at 2x speed you talk slow, good tut

  • @Ammar23217
    @Ammar23217 2 ปีที่แล้ว

    not useful. no complete example. many variables not declared. cannot understand.

  • @samirelzein1095
    @samirelzein1095 3 ปีที่แล้ว

    dude, use less my imagination, show me, aka 3b1b

  • @GodOfReality
    @GodOfReality 3 ปีที่แล้ว

    You randomly switch between saying "dep" "defp" "def" and "defth" when you are trying to say "depth" lol.

  • @JanetLashley
    @JanetLashley 3 หลายเดือนก่อน

    6674 Swift Via

  • @bestwishes5553
    @bestwishes5553 2 ปีที่แล้ว +1

    siimiii.trii/catholicc'ica'mois'pentaocsiacal/o //nd.D

  • @anhmai81
    @anhmai81 7 หลายเดือนก่อน +1

    Thanks!

  • @JamesBrodski
    @JamesBrodski 3 ปีที่แล้ว

    Great video. Thank you so much.