Depth First Search Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ก.พ. 2013
  • This is one of the important Graph traversal technique. DFS is based on stack data structure.
    Analysis:
    The time complexity of DFS using Adjacency list is O(V + E) where V & E are the vertices and edges of the graph respectively.
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @danielxu647
    @danielxu647 10 ปีที่แล้ว +1205

    this 4 minute video explains better than my prof did in 1 hour

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

      same here

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

      same here

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

      One of the better professors I had barely spoke english and we could hardly understand him but all he did was teach what we needed to know and did examples, even though we barely understood what he said (like 50% of it we kinda got) the examples made sense as he did them along the way and we ended up having the last 2 and a half lectures be just show up, "do you have question" "no okay we can review but you can go if you want" so we all just left

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

      same here :v

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

      thats because professors can't common sense they only know logic thats why they will always suck compared to looking anything up on google lmfao

  • @Akashpagol
    @Akashpagol 9 ปีที่แล้ว +11

    I had trouble understanding the pseudo code given in class... your explanation is precise and to the point. I find it very easy to relate it to my pseudo code. Thank you very much!

  • @foolishmusic9430
    @foolishmusic9430 7 ปีที่แล้ว +152

    Difference between depth first search & breadth first search:
    BFS looks at each adjacent node and doesn't consider the children of those adjacent nodes.
    DFS looks at each adjacent node, and looks at all the children of the current adjacent nodes. It again, looks at the children of the next adjacent node (adjacent to the children of the prevoius).

    • @blasttrash
      @blasttrash 5 ปีที่แล้ว +7

      I just remember it like stack(dfs) vs queue(bfs) after watching these youtuber videos.

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

      So depth first search is a pedophile?

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

      @@jakeryker546 XD

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

      @@jakeryker546 this shit has me dyin rn 😂😅

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

      Best video for depth first search : th-cam.com/video/sxCp1lUxwtw/w-d-xo.html

  • @vishnuvishnu4306
    @vishnuvishnu4306 5 ปีที่แล้ว +66

    At the peak times to attend the exam it is very useful 😛😀😀😀

    • @Ahmad-ry3uf
      @Ahmad-ry3uf 2 ปีที่แล้ว

      Yes... watching this at very last minute before exam 😎

    • @Virus-lf2rp
      @Virus-lf2rp 2 ปีที่แล้ว

      Yeah

  • @PuthyMol
    @PuthyMol 10 ปีที่แล้ว +56

    Thank you very much for your video. Abut 4 minutes of your explaining is more effective than my teacher explain 1 hours

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

    this is a simple search strategy, but i have trouble handling with some misunderstanding about the stack. your video had solved it nice and tidy. i have no confusion now. thanks a lot.

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

    Best DFS animation ever watched. Thank you man!

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

    Hey, again, can you add the discovery (d) and finalization (f) times? thanks

  • @csandreas1
    @csandreas1 8 ปีที่แล้ว +65

    So the differences between depth first search and breadth first traversal are:
    a)Breadth traversal has a queue but depth search has a stack
    b) The other difference you will notice with node C at 2:13. In breadth traversal the result would be C D E F, in depth it is C D E H.
    other difference at 1:30 where in depth the result is S C D but in breadth it is S C G

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

      Someone down there added a little bit more: Breadth First traverses each level of the graph, irrespective of the children of each node. Depth first search traverses the children of a node, irrespective of the level of the graph.

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

      csandreas1 you are legend

  • @jspadesofficial5663
    @jspadesofficial5663 8 ปีที่แล้ว +290

    so we will pop it off

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

      And a poo poo purru booom
      Skiyaaa

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

      pop pop pop thats what ur brain is filled off.....niga

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

      @@nandkishorenangre8244 You are disgusting

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

      Which country?

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

      @@nandkishorenangre8244 could you not fucking speak

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

    Very clear, clean to the point. Thank you very much!

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

    this short video conveys the message very effectively..nice work

  • @coreyownz
    @coreyownz 10 ปีที่แล้ว +7

    Was a whole lot easier coding it from your example than reading pseudo code, thanks a bunch, great videos

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

    Very clear and easy to understand the concept of dfs - pls make more vids

  • @gogateiit
    @gogateiit  10 ปีที่แล้ว +7

    That was truly an appreciation to our hard work. Thanks Namitha.
    If you like the video, do subscribe for more such video. You can explore other videos too. I hope you'd like those too. Do share it across your friends.

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

    Thank you for sharing your knowledge with us! had an easier time understanding this way

  • @malharjajoo7393
    @malharjajoo7393 7 ปีที่แล้ว +13

    You have forgotten to mention the most imp point -
    1) This is iterative version of DFS ( there is a recursive version too )
    2) This DFS will only give you all connected components within the same SCC.
    Anythign disconnected is not seen

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

    Awesome videos. Simple explanation. This was exactly what I needed. Sometimes it's just nice to see how an algorithm works than to see the code only and struggle to guess what's going on in it.

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

      I know that this comment was made 8 years ago, however, I am curious to know if you have by now found a career for yourself following the field of computer science/engineering.

  • @NamithaLIAF
    @NamithaLIAF 10 ปีที่แล้ว +16

    I learnt DFS in less than three minutes, compared to 40 hours of a semester -_- Thank you so much, this made DFS so easy! :)

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

      true got exam on this at 5 pm

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

    good job mate...this video was real helpful for understanding..please next upload about the explanations to the algorithms of these searches.

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

    Hi. I have a question how can we look for cycles in the graph from this approach? Please let me know.

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

    Thank you! Very good explanation; simple and effective.

  • @26411bapul
    @26411bapul 7 ปีที่แล้ว +1

    Very specific explanation and great tutorial. Thanks

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

    If the ADT, Stack, is used, the implementation is non-recursive (iterative). If the code refers to the pseudocode of non-recursive DFS in Wikipedia and the neighbors are pushed onto the stack in alphabetical order, then the output sequence should be ASGHEFCDB; moreover, A should be popped immediately after it is pushed onto the stack.

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

    thanks, for sharing your knowledge with beginners like me. this video helped me a lot.

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

    thank you sir, it helps me so much to understand the bfs and dfs algorithm! :)

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

    in case of alphabets do we have to use of alphabetic order regardless of the structure ???
    better !!!

  • @sriramOV
    @sriramOV 3 วันที่ผ่านมา

    This is one of the best video for DFS in the whole internet

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

    Very nice - but D is added a bit late to the output sequence - when D is popped off, rather than when it is added.

  • @Dylan-ig9pg
    @Dylan-ig9pg 8 ปีที่แล้ว +2

    Please do a video just like this for Dijkstra's algorithm.

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

    Looking at S , the next Elements in Stack should be C ,G oder ?

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

    thanks for your feedback Bharat. If you like the video, do share it across your friends and subscribe for other upcoming videos too.

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

    Thanks for such a clear explanation.

  • @gogateiit
    @gogateiit  10 ปีที่แล้ว

    I would consider your comments as a Appreciation namitha. If you like the videos, then do subscribe and share it across your friends.

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

    This video helps me to do my report in Data structure and algorithm thank you so much! 💛

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

    Concise and precise. Thanks a lot!

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

    Thank you so much,
    All the best for everyone.
    Stay safe 💪

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

    could you post your hand written pseudo code on how to solve this problem

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

    we can choose any vertex as a starting point??

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

    Thanks man.. You are awesome!
    I have learned DFS in a versy short time.

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

    Thank you for the explanation! It helped me answer all the edge cases that I was thinking about!

  • @ForcesOfOdin
    @ForcesOfOdin 9 ปีที่แล้ว +8

    Thank you for your time and clear exposition. From watching this and the breadth first search video I have gathered the following: A depth first search is a breadth first search with a stack instead of a queue which causes a different traversal because of the LastInFirstOut property of a stack versus the FirstInFirstOut property of a queue. Is this in the ballpark?

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

      ForcesOfOdin more or less, you got it the wrong way around, the main difference between BFS and DFS are exactly what their name implies, breath vs depth. On one hand, bfs will search every node in the same distance from the origin first and move by layers, on the other hand, DFS will continue going deeper and deeper into the graph searching by depth first and only then coming back up! There is indeed a difference between the structures used in each but the difference in the way they work is what calls for the diferent structures, not the other way around!
      Hope i was clear :/

    • @kingkonark5203
      @kingkonark5203 9 ปีที่แล้ว

      Miguel Vera it helped me, thanks!!!

    • @ForcesOfOdin
      @ForcesOfOdin 9 ปีที่แล้ว

      Miguel Vera You are correct, of course, in your qualitative description of the two algorithms. However it's wrong to argue as you have. The only difference, between the two algorithms is the data structure used to store nodes which have been discovered until those nodes can be procesed and marked "visited". The first in first out property of Queue's causes the node visitation order to process every node at the current depth from the source node before any node at the next depth from the source. The LAST in first out property of the Stack data type is what causes the nodes children (which were added after the source node) to be explored in the next iteration rather than the nodes siblings.
      I'm not saying it's the data set that determines the algorithm , nor am I disagreeing with what you say, that the algorithm type creates the need for the specific data set, i'm actually saying something stronger.The causation can go either way. This implies, that you can discover new algorithms by replacing the data structure. If I use a priority Queue, for example, my implementation of the prirority sorting completely determines the node traversal order and thus the algorithm. Again, I'm saying that you can start with EITHER the data set or the algorithm , and you uniquely and consistently come up with the other. If you start with BFS you need a queue. if you use a queue you get BFS. Do you see ?
      This article I'm linking is much better written than my post (I do hope I wasn't to rambly /confusing but multiple sources cant hurt).
      jeremykun.com/2013/01/22/depth-and-breadth-first-search/

  • @y.yasemin
    @y.yasemin หลายเดือนก่อน

    Very helpful. Thanks for the video.

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

    Thanks a lot for this wonderful explanation.. DFS can't be explained any better than this..

  • @baqikenny
    @baqikenny 7 ปีที่แล้ว

    thanks, this clarification made implementations easier

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

    How am I supposed to know, which Children I choose first (S or B). You´ve said "we go alphabetically". But is there any rule for picking One out of two childrens coming up the same parent node?

  • @NguyenHung-bf8jw
    @NguyenHung-bf8jw 6 ปีที่แล้ว

    This video is helpful for me. Thank you!

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

    thank you so much sir. Well explained in just 3 minutes thank you.

  • @nushrakidunia..9534
    @nushrakidunia..9534 7 หลายเดือนก่อน

    when s is vistied we pop it from stack and at the place of s we add c or g

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

    Thank you! Thank you! I'm now halfway done with my assignment!

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

    I don't know u are reading my comment but this vedio is one of the best than the other you tube videos 😊

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

    bro thank you, so enjoyable to watch

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

    DFS gives spanning tree or forest ... where it is .... ?

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

    Your DFS stack is a functional stack, which is related with recursive function call.
    But if you mean the stack is a kind of memory type, (not recursive function call) 'G' may be pushed in the stack before pushing 'D'.
    So my suggestion is that you should give the information that your stack type is a recursive function not the stack array.

  • @JohnMtonga
    @JohnMtonga 9 ปีที่แล้ว

    Simple and to the point.. good... although add D to the output immediately you added it to the stack instead of after the pop...

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

    thanks bro for smoothly understanding us

  • @wajidnoorkhan8308
    @wajidnoorkhan8308 7 ปีที่แล้ว

    unique way of clear explaination..

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

    Great Video In A Very Short Amount Of Time.

  • @gavin8535
    @gavin8535 7 ปีที่แล้ว

    So that means you have to always push all the nodes into the stack first And then pop them one by one?

  • @Rohit-tz6gs
    @Rohit-tz6gs 3 ปีที่แล้ว

    So, finally, after watching 2-3 videos, I got it !!!

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

    Very well explained
    Love and support

  • @iamnero9
    @iamnero9 8 ปีที่แล้ว

    Thank you very much! It made a great help for me to understand this one in simpler way.

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

    Smooth af😎 understood in secs💯🙏🏻

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

    Excellent explanation!

  • @tony-silver
    @tony-silver 10 ปีที่แล้ว

    Short and simple explained, thanks!

  • @mahaalansi5659
    @mahaalansi5659 7 ปีที่แล้ว

    thank you for your effort .

  • @JoseSanchez-vv1zd
    @JoseSanchez-vv1zd 6 ปีที่แล้ว

    Great video. Thanks! :)

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

    Awesome. Nice and clear. UK Computer Science teacher.

  • @natesamedia4.02
    @natesamedia4.02 7 ปีที่แล้ว

    Well explained,Thank you boss

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

    Very well explained!

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

    Where we apply DFS & why ???

  • @amiMalimovka
    @amiMalimovka 7 ปีที่แล้ว

    I think the algorithm you describe will only work for a connected graph

  • @deepikak5868
    @deepikak5868 7 ปีที่แล้ว

    thank u fr ur clear explanation sir!!!...

  • @companymen42
    @companymen42 7 ปีที่แล้ว

    Great explanation!

  • @user-tx9jd4xh9i
    @user-tx9jd4xh9i 6 ปีที่แล้ว

    This is DFS WITH Huristic ! Right?

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

    very simple explanation good job

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

    Is it necessary to go alphabetical order

  • @linhyo2867
    @linhyo2867 11 ปีที่แล้ว

    thank you ! It's very useful for me to understand this algorithm ^^

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

    Months of shitty theoretical concepts and this video on only 4 minutes i understand more and better. Thank you very much for your video.

  • @humaunrashid3793
    @humaunrashid3793 9 ปีที่แล้ว

    Everything is seems to be good but I have one question????? For B and D we don't have any place to go. For B, when you have visited you add it in the output sequence and also stack before pop it from stack. Bur for D you first add it on stack then pop up it and then add it to the output sequence. Actually which one is correct?

    • @AkhilKumar
      @AkhilKumar 9 ปีที่แล้ว

      adding to output seq. while popping is correct(actually doesn't matter)...

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

    Oh, well my lecturer is referring to this video on her phone to teach us. That's great (sarcasm intended).

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

    Great video ! Even I understood

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

    Thanks a lot for this!!

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

    Thanks for uploading such a great content in quick time :)

  • @gregerz20
    @gregerz20 10 ปีที่แล้ว

    Nice video! thank you!

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

    Thanks for saving my time :)

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

    The output should be displayed in the reverse order in which you have it in the video, because you're popping from the top of the stack, meaning the most recent element pushed in should be the first element popped out and returned to the output AKA Last In First Out(LIFO).

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

    This is very helpful!

  • @derrickcheung919
    @derrickcheung919 11 ปีที่แล้ว

    thanks for your effort

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

    This seems to be wrong. You push to the stack all the adjacent nodes of the current node, then you pop out one of them and etc. en.wikipedia.org/wiki/Depth-first_search

  • @SChidawa
    @SChidawa 7 ปีที่แล้ว

    Very interested...Thanks a lot

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

    thanks for the short and sweet explaination bro

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

    great explanation!

  • @PriyaSingh-pe6fi
    @PriyaSingh-pe6fi 10 ปีที่แล้ว

    nice explanation ..thank u so much

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

    when you visit S, you need to add G then C, instead of just adding C to the stack

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

    Very good explanation

  • @ksumanth123
    @ksumanth123 8 ปีที่แล้ว +7

    Simple and clear..

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

    Very helpful!

  • @MohdSalman-ir8gr
    @MohdSalman-ir8gr 7 ปีที่แล้ว +1

    AWESOME DUDE

  • @yunusatl3452
    @yunusatl3452 9 ปีที่แล้ว

    Allah razı olsun :) çok yardımcı oldu.