G-6. Depth-First Search (DFS) | C++ and Java | Traversal Technique in Graphs

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ • 345

  • @takeUforward
    @takeUforward  2 роки тому +80

    Let's continue the habit of commenting “understood” if you got the entire video.
    Do follow me on Instagram: striver_79

  • @jeet-smokey
    @jeet-smokey 11 місяців тому +39

    Entire Semester you taught in these 56 videos. Kudos to your efforts...!!!!! Keep growing.

  • @Ace-ex9gg
    @Ace-ex9gg Рік тому +40

    I was 5.27 minutes into the video and i initially thought that dfs would use recursion when you started explaining about going back and front. The moment you told recursion i stopped the video and started to code my own recursion algorithm. And i figured it out and got output. Im complete honest here . I myself came up with my algorithm and it worked in first attemt itself. All thanks to strivers recursion playlist.
    my code
    public static void depthsearch(ArrayListal,int[] vis,Queue qu,int node)
    {
    for(int a:al.get(node))
    {
    if(vis[a]==1)continue;
    vis[a]=1;
    qu.add(a);
    depthsearch(al,vis,qu,a);
    }
    return;
    }

    • @SohailKhan-cx9gb
      @SohailKhan-cx9gb 8 місяців тому

      Bro i have a doubt whts the use of list and why we take it vectorls?

    • @jha.brajesh
      @jha.brajesh 7 місяців тому

      @@SohailKhan-cx9gb vector is for c++, and list is for Java

    • @rishabhranjan5162
      @rishabhranjan5162 5 місяців тому

      what's your codeforces rating?

    • @sahil1053
      @sahil1053 5 місяців тому

      Yhi cheej me saari visited nodes hoenge​@@SohailKhan-cx9gb

    • @AdityaMaurya-dw3od
      @AdityaMaurya-dw3od 2 місяці тому

      I did it too!! Striver's recursion playlist has indeed made me good at it!

  • @ProductivityPowerhouse0109
    @ProductivityPowerhouse0109 Рік тому +42

    Striver bhaiya ki jai...far far far better explaination than that of College professor ⚡🙌

  • @kroax9720
    @kroax9720 2 місяці тому +3

    I have an interview tomorrow for a good hefty package and I feel pretty confident . I really don't know what is going to happen tomorrow but I hope I come back with some thing good . Just putting this comment up to see if this would be a great memory or a don't know what 💕💕

    • @aishuladha6911
      @aishuladha6911 2 місяці тому

      How's your interview

    • @kroax9720
      @kroax9720 2 місяці тому

      @@aishuladha6911 did not go well . I was able to answer all questions almost right but not 100% right . In the end they ended up taking 2 students who I guess might were able to answer 100% right

  • @ShubhamKumar-km8pm
    @ShubhamKumar-km8pm Рік тому +12

    Thanks striver for such an amazing explanation of the recursive tree.

    • @kavyabanka4482
      @kavyabanka4482 Рік тому +1

      Can you tell me why here not mentioned base condition

    • @atulnath1474
      @atulnath1474 11 місяців тому +1

      ​@@kavyabanka4482 We are just visiting each node and adding it to an array list. And if for a particular node, there are no neighbours, then we are not calling the function again.
      So we just need to return the updated list back for each recursive call

  • @raghvendrakhatri5848
    @raghvendrakhatri5848 2 роки тому +17

    Understood each and every word♥. Kudos to your hard work and dedication, you are the motivation behind a lot of people. Your hardwork inspires a lot of people to work hard.
    Thankyou for providing such a beautiful graph series keep posting such content ♥, we all need this.

  • @c.ramanji2604
    @c.ramanji2604 Рік тому +5

    the best playList on graphs i ever see..Thank you so much 😊😍

  • @sumeetubale3923
    @sumeetubale3923 Рік тому +4

    Best video tuturials on the you tube absolutely!!!

  • @AkshatTambi
    @AkshatTambi 5 місяців тому +8

    if anyone wants an iterative approach here it is->
    class Solution
    {
    public:
    vector dfsOfGraph(int V, vector adj[])
    {
    stackstk;
    vectorvisited(V,0), dfs;
    stk.push(0);
    while(!stk.empty())
    {
    int temp=stk.top();
    stk.pop();
    if(!visited[temp])
    {
    visited[temp]=1;
    dfs.push_back(temp);
    //as the nodes in the adj list are given in order of left to right
    //so push right to left so that we can pop left to right, to maintain order
    for(int i=adj[temp].size()-1; i>=0; i--)
    {
    int t=adj[temp][i];
    if(!visited[t]) stk.push(t);
    }
    }
    }
    return dfs;
    }
    };

    • @vipuljha7305
      @vipuljha7305 5 місяців тому +1

      compilation error is there in ur code and it is for bfs, not dfs

    • @AkshatTambi-sd1qg
      @AkshatTambi-sd1qg 5 місяців тому

      It is ac on gfg and. It is indeed for dfs, notice the data structure used

    • @ashtonronald
      @ashtonronald 3 місяці тому

      i can confirm akshat's method is logical & correct

    • @VEDANSHSHARMA-o6k
      @VEDANSHSHARMA-o6k 3 місяці тому

      arre hello bhai

    • @AkshatTambi
      @AkshatTambi 3 місяці тому

      @@VEDANSHSHARMA-o6k are bhai hello

  • @parthib_deb23
    @parthib_deb23 Місяць тому

    I thought before coming to this video , you have explained topics like spanning forests and how its different from spanning tree and how peeps can relate tree-traversals with graph-traversals (relate doesn't means same !!). But you have just straight hopped on algorithms and code 🙂. Good , Carry on😀

  • @cinime
    @cinime 2 роки тому +10

    Understood! Amazing explanation as always, thank you very much!!

  • @DevanshuAugusty
    @DevanshuAugusty Рік тому +5

    instead of "int vis[v] = {0}" we can also use "vector vis(v)" right?

    • @ayushsenapati8420
      @ayushsenapati8420 Рік тому +6

      You can write "vector vis(v,0)"

    • @DevanshuAugusty
      @DevanshuAugusty Рік тому +4

      @@ayushsenapati8420 vector vis(v) would also work. as default values will all be 0

    • @gouravp.3851
      @gouravp.3851 11 днів тому

      it's just more intuitive to use normal array

  • @codewithvidhi
    @codewithvidhi 2 роки тому +21

    Pointing a mistake, at 12:36 you removed 3 from the final dfs traversal list and it stays like that till the end. Which is incorrect.

  • @sounaksaha1455
    @sounaksaha1455 2 роки тому +1

    striver op...explanation op....... graph series SUperrrrrrr OP

  • @1LineCode
    @1LineCode 23 дні тому +1

    I think for the undirected graph the time complexity should be TC-> O(N)*O(2E) i.e for each node visit you have to cheak all of its neibours so TC -> O(2*N*E)

    • @aryanagrawal9103
      @aryanagrawal9103 21 день тому

      2E is the total number of degrees, not the number of degrees of each node. So it will be O(N) + O (2E)

  • @vikasbagri1225
    @vikasbagri1225 2 роки тому +1

    understood each and every bit of your explanation...

  • @amansingh.h716
    @amansingh.h716 Рік тому +1

    lol mena 0.5 pa dekh lia puri video too funny .
    nice video bro

  • @umeshkaushik710
    @umeshkaushik710 2 роки тому +5

    Amazing explanation as always.

  • @mananarora141
    @mananarora141 Рік тому +1

    UNDERSTOOD!!!!!!!!!!!!!!!!!!!!!!!!!

  • @chitranshjain5075
    @chitranshjain5075 2 роки тому +1

    Completely UNDERSTOOD.

  • @sukhpreetsingh5200
    @sukhpreetsingh5200 2 роки тому +4

    Understood! Amazing explanation

  • @frekinshorts4237
    @frekinshorts4237 2 роки тому +4

    Please make a videos on number theory

  • @rahulsinghshekhawat2487
    @rahulsinghshekhawat2487 Рік тому +4

    Thank you very much. Great explanation.

  • @sankalpjain8192
    @sankalpjain8192 Рік тому +2

    i am using vectorvis(V,0) than only three test cases are passing

    • @himshikharsingh9010
      @himshikharsingh9010 3 місяці тому

      you need to pass it by reference, as you don't wanna send a copy of the vector every time dfs is called, and rather the original vis vector needs to be manipulated, hence, you need to pass it by reference

  • @supriyakeshri78
    @supriyakeshri78 3 місяці тому

    best series on Graph👍

  • @sagaravhad5198
    @sagaravhad5198 Рік тому

    your contribution is matchless. thank you.

  • @siddharthkardam321
    @siddharthkardam321 Рік тому

    you r god of coding

  • @psionl0
    @psionl0 Рік тому +3

    I would have liked to see the code for a DFS using a stack rather than recursion. It is more intuitive. It makes DFS and BFS almost identical except that DFS uses a stack and BFS uses a queue.

    • @Raju_Sharma852
      @Raju_Sharma852 11 місяців тому +2

      Hii bro
      vector dfsOfGraph(int V, vector adj[]) {
      vectorans, visited(V, 0);
      stacks;
      s.push(0);
      while(!s.empty()){
      int ele = s.top();
      s.pop();
      if(!visited[ele]){
      ans.push_back(ele);
      visited[ele] = 1;
      }
      for(int i = adj[ele].size() - 1; i >= 0; i--){
      if(!visited[adj[ele][i]]){
      s.push(adj[ele][i]);
      }
      }
      }
      return ans;

    • @psionl0
      @psionl0 11 місяців тому +1

      @@Raju_Sharma852 I know how to code it. I just thought it would be useful for other viewers to see it done in this video.

  • @ravikant8609
    @ravikant8609 2 роки тому +5

    great and amazing work! Loving it, thanks a lot❤

  • @hashcodez757
    @hashcodez757 3 місяці тому

    "UNDERSTOOD BHAIYA!!"

  • @sripriyapotnuru5839
    @sripriyapotnuru5839 2 роки тому +1

    Thank you, Striver 🙂

  • @Abcd-jt1qs
    @Abcd-jt1qs 5 місяців тому

    Understood Striver!! Thank you so so much for these amazing lectures :)

  • @oqant0424
    @oqant0424 2 роки тому +1

    Understood each and every word♥

  • @jatilyadav4000
    @jatilyadav4000 2 роки тому +1

    Great Explaination

  • @Satya-g5t
    @Satya-g5t Місяць тому

    Understood. Also, nice explanation.

  • @ramanpareek5218
    @ramanpareek5218 4 місяці тому

    Liked the video, notes taken, understood

  • @shaddyrogue9430
    @shaddyrogue9430 2 роки тому +10

    Should not we have a for loop in the top if we have unconnected components?

    • @takeUforward
      @takeUforward  2 роки тому +12

      Yes, you should. But this one was a single component graph. So not needed.

    • @shaddyrogue9430
      @shaddyrogue9430 2 роки тому

      @@takeUforward Ok got it thanks. Understood.

    • @amansinghal4663
      @amansinghal4663 Рік тому

      can you give the code which will work for unconnected components also ?
      Or can u just explain the logic ?

    • @amansinghal4663
      @amansinghal4663 Рік тому

      @@takeUforward Is the below code correct for unconnected components also ?
      void dfs(vector&visited, int node, vector&ans, vector adj[]){
      visited[node]=1;
      ans.push_back(node);
      for(auto it:adj[node]){
      if(visited[it]==0){
      dfs(visited,it,ans,adj);
      }
      }
      }
      vector dfsOfGraph(int V, vector adj[]) {
      // Code here
      vectorans;
      vectorvisited(10000);
      for(int i=0; i

  • @stith_pragya
    @stith_pragya 11 місяців тому

    Thank You So Much for this wonderful video...........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @adamrizk6735
    @adamrizk6735 2 роки тому +3

    Awesome work, thank you!

  • @jaykaantshikre751
    @jaykaantshikre751 Рік тому

    Understood and rewatched to revise

  • @kaichang8186
    @kaichang8186 3 місяці тому

    understood, perfect explanation

  • @raselhasan2433
    @raselhasan2433 Рік тому

    understood bro, love from bangladesh.

  • @vakhariyajay2224
    @vakhariyajay2224 Рік тому +1

    Thank you very much. You are a genius.

  • @rishabhagarwal8049
    @rishabhagarwal8049 2 роки тому +1

    Understood Sir, Thank you very much

  • @suheabkhan2546
    @suheabkhan2546 2 роки тому +1

    great lecture bhai

  • @Musicuvakavi1823
    @Musicuvakavi1823 3 місяці тому

    Thanks... I solved this by myself...

  • @bhavkushwaha
    @bhavkushwaha 4 місяці тому

    Thankyou Striver, Understood!

  • @paullater6230
    @paullater6230 6 місяців тому

    Understood beautifully!!!

  • @deepakbhallavi1612
    @deepakbhallavi1612 Рік тому +1

    Understood 😌

  • @AppaniMadhavi
    @AppaniMadhavi 6 місяців тому

    superb explanation

  • @UECAshutoshKumar
    @UECAshutoshKumar Рік тому +1

    Thank you sir

  • @sagarchauhan151
    @sagarchauhan151 Рік тому +1

    understood sir

  • @aryashjain7893
    @aryashjain7893 2 роки тому +8

    understood almost , but why is the vis array is not accessed by & sign as we need the values of it, or are arrays refrenced directly , and for vectors we need a '&'

    • @sumerrawat6947
      @sumerrawat6947 2 роки тому +17

      arrays are always passed by reference , when we pass an array as a parameter to a function , the pointer to the first element of the array is passed , so any changes in the array inside external function will reflect in the main function, or the changes made in the array remains !
      but for vector we need to specifically pass by reference to avoid making a copy of it

    • @jiteshjoshisde3154
      @jiteshjoshisde3154 2 роки тому +1

      @@sumerrawat6947 i have the same doubt about the reference . thanks to you .

    • @ASHUTOSHSHARMA-us6hd
      @ASHUTOSHSHARMA-us6hd 3 місяці тому

      @@sumerrawat6947 copy thodi banegi, reference bola haina, same memory address par hi saare changes hoge array mai

  • @LBK3
    @LBK3 Рік тому +1

    Understood ❤

  • @pratyushsahoo4948
    @pratyushsahoo4948 9 місяців тому

    Awesome work 👍

  • @Thescienceworld652
    @Thescienceworld652 Рік тому +2

    you have not discussed it for non connected components . btw nice explaination

    • @SlapB0X
      @SlapB0X Рік тому

      im not sure but can we traverse to a node if its not even connected to the parent node?

    • @The_Shubham_Soni
      @The_Shubham_Soni Рік тому

      Visited does that work

  • @Rieshu-l9i
    @Rieshu-l9i 7 місяців тому

    #Striver rocks🤟

  • @anandverma2714
    @anandverma2714 Місяць тому

    00:04 Learn about depth-first search (DFS) in graphs.
    02:23 Depth-First Search (DFS) is about traversing in depth
    04:26 DFS explores in depth, uses recursion.
    06:47 Explanation of Depth-First Search (DFS)
    09:04 DFS traversal explores graph depth-wise and avoids revisiting visited nodes.
    11:18 Depth-First Search (DFS) traversal technique in graphs.
    13:25 DFS technique explained using adjacency list in graphs
    15:35 Depth-First Search (DFS) traversal technique in graphs.
    17:44 DFS traversal iterates on node neighbors and calls function once per node.
    19:36 The time and space complexity depend on the number of edges.
    Crafted by Merlin AI.

  • @samuelfrank1369
    @samuelfrank1369 Рік тому

    Understood. Thanks a lot.

  • @ayah717
    @ayah717 Рік тому

    Understood bhaiya

  • @GoUrAvpandey-g7r
    @GoUrAvpandey-g7r 6 місяців тому

    thanks so much it was very helpful

  • @rohitshrirame2378
    @rohitshrirame2378 2 роки тому

    understood.. Depth!!!! Depth!!! Depth!!!!

  • @shyren_more
    @shyren_more 2 роки тому +1

    understood, thanks!

  • @khyatikakkar9259
    @khyatikakkar9259 7 місяців тому

    Understood sir.

  • @codeman3828
    @codeman3828 6 місяців тому

    Thanks. Understood.

  • @girishbhargava6367
    @girishbhargava6367 2 роки тому +6

    In the interviews, can we be sure, that we would be given adjacency lists, or we can be given adjacency matrix also?

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 7 місяців тому

    Thank you bhaiya

  • @palak443
    @palak443 Рік тому +2

    In java code why have we taken the size of visited array as v+1 when graph nodes are zero indexed

  • @priyamagarwal3469
    @priyamagarwal3469 Рік тому +3

    Can we not use stack method, if so please provide the code for it

  • @harshvardhansingh2272
    @harshvardhansingh2272 2 роки тому

    understood striver

  • @harshavardhan7074
    @harshavardhan7074 5 місяців тому

    understood sir👍👍

  • @shubhigupta5689
    @shubhigupta5689 2 роки тому

    Understood🌻

  • @kshitijmishra2716
    @kshitijmishra2716 2 роки тому +1

    understood!

  • @shreyxnsh.14
    @shreyxnsh.14 15 днів тому

    Use V (for vertex) instead of N to remember better, N is ambiguous

  • @poulamisaha1506
    @poulamisaha1506 2 роки тому +5

    Hi, the node 3 was traversed but not added to the visited array. Please correct me if I am wrong

    • @sachinkore974
      @sachinkore974 2 роки тому

      you are right, hi missed writing it by mistake

  • @sarcsi
    @sarcsi Рік тому

    Understood 🎉

  • @abhinavkumar6584
    @abhinavkumar6584 2 роки тому

    understood..and liked

  • @ANURAGSINGH-nl2ll
    @ANURAGSINGH-nl2ll Рік тому

    understood thank you

  • @oqant0424
    @oqant0424 2 роки тому

    6/56 DONE (3.12.22)

    • @jitinroy2246
      @jitinroy2246 Рік тому

      currently at which video or completed on which date graph series?

  • @sachinjain3833
    @sachinjain3833 5 місяців тому

    14:03 You forgot to mention 3 in DFS call. It should ve 1,2,5,6,3,7,8,4

  • @hrushi_borhade
    @hrushi_borhade Рік тому

    understood striver!!!

  • @arvindg553
    @arvindg553 2 роки тому

    Thanks striver

  • @hakunamatata-nl4js
    @hakunamatata-nl4js 5 місяців тому

    thank you

  • @swarajlahiri
    @swarajlahiri 7 місяців тому +2

    Can a stack based solution be made for tha same DFS?

  • @worthlessguy1621
    @worthlessguy1621 7 місяців тому

    Understood strvier sir ;)

  • @AryanGairola-th3qc
    @AryanGairola-th3qc 7 місяців тому

    great explanation bhaiya but you forget 3 to werite in ans

  • @DevashishJose
    @DevashishJose Рік тому

    Understood.

  • @kritagyaprasad7230
    @kritagyaprasad7230 2 роки тому

    great content

  • @priyagupta6999
    @priyagupta6999 Рік тому +13

    I am bit confused, on why the time complexity if added as O(n) + O(2*E) instead it should be multiplied right ? We are calling for all the nodes once and for each each we are calling all its neighbours no of time. i.e. O(n*2*E) ~ O(nE) . Please help me understand.

    • @suchetapal713
      @suchetapal713 Рік тому +1

      I too have the same doubt

    • @mdshaqlain3252
      @mdshaqlain3252 Рік тому

      @@suchetapal713 Did u understand??

    • @parinav10
      @parinav10 Рік тому +1

      The comment section in the BFS video contains a great explanation by @YeaOhYeahh. Should help.

    • @arpankoley4256
      @arpankoley4256 10 місяців тому

      that O(2*E) is kind of the summation over all those inner for loops , striver has explained that check out the bfs video

    • @Mr_X_FIFA
      @Mr_X_FIFA 4 місяці тому +2

      For each value from 0 -> n we are not visiting 2*E nodes.....we are visiting 2*E nodes in total for all n...not for each for i in 1 to n..... that's why time complexity is O(n + 2*E)

  • @maheshjindal2622
    @maheshjindal2622 Рік тому

    UNDERSTOOD

  • @neelulalchandani7429
    @neelulalchandani7429 3 місяці тому

    understoooooooooood

  • @KartikeyTT
    @KartikeyTT 4 місяці тому

    tysm sir

  • @3zam656
    @3zam656 Рік тому

  • @TharunMettala
    @TharunMettala 4 місяці тому

    understood..

  • @SidharthSharma99
    @SidharthSharma99 9 місяців тому

    understood.

  • @dreamyme543
    @dreamyme543 2 роки тому +1

    Understood:)

  • @Sakshi-nd2jz
    @Sakshi-nd2jz 2 роки тому

    Understood!

  • @swrajkantsharma630
    @swrajkantsharma630 2 роки тому

    Understood...

  • @kumarpawansahh
    @kumarpawansahh 2 роки тому

    superb