Depth-first search in 4 minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 พ.ค. 2024
  • Depth-first search in 4 minutes.
    Code: github.com/msambol/dsa/blob/m...
    Sources:
    1. Introduction To Algorithms, Third Edition (CLRS) [www.amazon.com/Introduction-A...]
    2. en.wikipedia.org/wiki/Depth-f...
    LinkedIn: / michael-sambol

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

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

    You explained in 4 minutes what my data structures professor failed to do in 1 hour. Thank you!

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

      ayifdi hele deme

    • @sachinnn3452
      @sachinnn3452 5 หลายเดือนก่อน +3

      In one semester bro

    • @danieldeda3188
      @danieldeda3188 13 วันที่ผ่านมา

      @@sachinnn3452 in one lifetime bro

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

    I had already seen your search and sorting videos , they were concise and helped me first understanding how it works and figuring out everything else in the process , helped the cogs of my brain move a lot !

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

    Just stumbled upon your channel and all your videos are so short yet informative. Thank you!

  • @user-fq2jt7lv8l
    @user-fq2jt7lv8l 2 ปีที่แล้ว +5

    Consice, straight-to-the-point and very easy to understand! Great video!

  • @ahamedshathelegend1409
    @ahamedshathelegend1409 ปีที่แล้ว +25

    My professor was great at teaching DSA but I missed classes due to sickness and various reasons today I have a test I am don't know what I am going to do but thanks to you , your videos are short sweet and minimalistic ❤

  • @Jordan-pm6zx
    @Jordan-pm6zx 2 ปีที่แล้ว +1

    Thanks a lot! Currently working on Cracking the code interview and found this. Short and precise enough:D Keep it up man.

  • @antoniooliveira6022
    @antoniooliveira6022 ปีที่แล้ว +12

    if you could also explain uniform-cost search, depth-limited, iterative deepening and bidirectional would be amazing ! great vids, learning a lot from you.

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

    I'm so happy that you start to post videos again.

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

    Thanks a lot for the new videos!! Hope you are back definitely!

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

    Thanks a Ton! I have my data structure exam today 😁.
    Welcome back too 🥳🥳

  • @francescoercole5592
    @francescoercole5592 11 หลายเดือนก่อน +1

    Ehy Michael, I only watch your videos because your explanations are clear (many slides) and straight to the point. Thank you

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

      Appreciate it, Francesco!

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

    Thanks man. Perfect explanation and understandable code!

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

    So clear and conscise, thank you!

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

    the right video to be free from confusion

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

    OG Michael back at it again 🎉🎉🎉

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

    Thank you lots, your channel is super informative.

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

    RETURN OF THE KING

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

    This channel is perfect!

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

    Welcome back man :D

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

    The Legend is back

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

    finally you updated!

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

    yooo he's back. lesgooo

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

    Welcome back :)

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

    Hello, great video.
    I would like to ask you some additional question. List of Stack and list of Visited will be on the evening like this?
    Stack: A, B, G, C, D, E, F, H, I
    Visited: A, B, C, D, E, F, G, H, I
    I am not sure if the List of Stack should preserve all previous values or it is changed continuously.

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

    brother said im going to teach you DFS in 4 min and went on to teach DFS in 4min. kudos

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

    Back with a Bang!!

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

    Awesome!

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

    Welcome back

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

    would it possible to link references on how the distance matrix is populated with BFS and DFS? and merits of using a stack vs a queue for DFS ?

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

    Very Intuitive, thank you

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

    Great video! A have a question (probably stupid, but anyway) about mapping your explanation to your code. So this video says: stack is a list of nodes to be visited;
    1) 'A' is a first node to be visited
    2) Add it to stack (to be visited)
    3) Pop it from stack
    4) Mark as visited
    5) Add adjacent nodes (to be visited) in stack
    ...
    Now according to your code for dfs:
    1) 'A' is a first node to be visited
    2) You add right away 'A' node to visited array ( visited.append(node)) before popping, so it's marked as visited?
    3) You add 'A' node into a stack (to be visited) but 'A' is already been visited according to visited array
    4) 'A' node is popped
    5) Then you loop through 'A's adjacent nodes (G first) (for n in reversed(graph[s])) marking 'G' as visited ( visited.append(n)); pushed into visited array
    6) Then you put 'G' into stack to be visited (stack.append(n)). But 'G' is already in visited, isn't it?
    7) Same as point 6) happens with 'B'
    8) Pop 'B' from the stack
    ...
    Then algorithm proceeds with other nodes pushing into visited before popping them
    So the question is: am I getting something wrong? What is the indicator of nodes to be marked as visited: being popped from the stack or being pushed into visited?
    Again in short:
    -The video states: Add node to be visited in the stack -> pop it -> mark as visited -> add adjacent nodes to the stack -> repeat
    -And according to code: Mark 'A' as visited(push to visited array) -> add 'A' to stack(to be visited) -> pop 'A' from stack -> loop through 'A's adjacent nodes (mark 'G' as visited, add 'G' to stack, mark 'B' as visited, add 'B' to stack) ->pop 'B' -> repeat
    Hope I explained my confusion well. Trying hard to get DFS right so I'll be waiting for your response, thanks!

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

      i guess this is not useful for you anymore, since it has been a year, but I caught the same error.
      In the code, nodes should be added to visited just as they are popped from the stack and not while considering the neighboring nodes.

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

    A stack only has two operations, push and pop. They do not let you add the 3 elements C,D and E before G as you did in the 3rd step.

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

      He pushed three items onto the stack, forcing G to the bottom.

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

    great video

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

    I never thought about dfs as the "opposite" of bfs... thank you

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

    King!!

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

    Nice !

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

    Thanks💞💓

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

    Thank you I have subscribed to you

  • @SaatvikPandey-lq9qz
    @SaatvikPandey-lq9qz 19 วันที่ผ่านมา

    so, is dfs in tree same as its preorder traversal?

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

    the "in" operation has a time complexity of O(n) though, in this case wouldnt it be O(n!) because you check it for 1,2,3,...n elements when you do "not in visited"?

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

      Not really, the "in" operation is a lookup in a hash table, so it's constant time O(1), not O(n).

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

    you really save my life !!!!!

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

    Thanks

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

    you said that the graph is stored in an "adjacency list" but isn't that an adjacency map?

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

    Why add both visited and stack? Why just one i don't understand 😢

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

    🇧🇷 thankssss

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

    So the difference between BFS and DFS is simply whether the queue is FIFO or FILO?

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

      BFS = queue / FIFO ... DFS = stack / FILO.
      Note: I chose to teach the iterative approach. You can also do this recursively, and I have examples on my GitHub [1]. DFS (pre/in/post in the code below) is easier to do recursively than BFS (level).
      [1] github.com/msambol/youtube/blob/master/tree_traversal/traversal.py

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

    please do graph data structure code implementation in python

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

    Is this a pre order traversal?

  • @mat-on-go8644
    @mat-on-go8644 ปีที่แล้ว

    I have a question why are are you popping a whilst we are still items in a thats wrong kaa

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

    Shout out to the Computer Science majors in the comment section .

  • @abhishekshivgan1884
    @abhishekshivgan1884 5 หลายเดือนก่อน +1

    Isn't the algorithm works best, if we continue to add vertices till we reach leaf node and in the process of backtracking (popping out of the stack) marking it as visited. While backtracking if any node has children, same process will be applied (adding descendant vertices in the stack till leaf node and backtracking it.

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

    Why is graph[s] reversed?

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

      This is just so the output matches the recursive version (shown is the iterative code).

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

    Lex Fridman ?

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

    kod yok

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

    fyi git hub link is broken

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

    The code seems wrong.

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

    A for loop in a while loop for dfs smh ? Just learn recursion and no need to impprt anything from collections module.

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

      Thanks for the feedback. Yes, can also do it recursively! See examples below [1]. deque is O(1) for append and pop [2], but I did change it to an array so there is no import [3].
      [1] github.com/msambol/youtube/blob/master/tree_traversal/traversal.py
      [2] wiki.python.org/moin/TimeComplexity
      [3] github.com/msambol/youtube/blob/master/search/depth_first_search.py#L15

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

    Check this sample and give me feedback:
    from queue import deque
    def depth_first_search(graph, node):
    visited = []
    stack = deque()
    visited.append(node)
    stack.append(node)
    while stack:
    s = stack.pop()
    print(s, end=" ")
    for n in reversed(graph[s]):
    if n not in visited:
    visited.append(n)
    stack.append(n)
    graph = {
    'A': ['B', 'C'],
    'B': ['D', 'E'],
    'C': [],
    'D': [],
    'E': ['F'],
    'F': [],
    'G': ['H'],
    'H': ['I'],
    'I': [],
    }
    depth_first_search(graph, 'A')