Breadth-First Search (BFS) | Traversal Technique in Graphs

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

КОМЕНТАРІ • 236

  • @krishnabhagat2716
    @krishnabhagat2716 3 роки тому +49

    Your teaching is really awesome bhai, I have taken GFG dsa course but i unable to understand graphs concepts property but after watching your graph's video series now i can say i can any basic graph, Thank you for your quality content.

  • @j3ck390
    @j3ck390 2 роки тому +9

    TC & SC: 13:26
    Java code: 14:16
    C++ solution: 17:12

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

    Striver, your explanation is always so good that after watching just 3-4 mins of your explanation I'm able to code and think the rest part on my own. Gives me a lot of confidence .Crystal clear concepts.

  • @dhivyashri6554
    @dhivyashri6554 3 роки тому +3

    Out of all the videos online, yours is the only one that made me understand the algorithm. Thank you! Commenting for the algorithm!

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

    CLEAR CLEAN QUALITY CONTENT.
    U are providing a strong foundation for graph.
    Thank You

  • @archanakumari1482
    @archanakumari1482 3 роки тому +13

    Is it your teaching or my understanding level have raised...haha....Thanks bro, you made it simple.

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

      Anyone explain this please!!
      in the beginning we put 1 in the queue and mark it as visited , then inside that for loop we also mark adjacency of node 1 , that is node 2 as visited , now in the second iteration of first for loop it will check if the node 2 is visited or not and found that 2 is already visited during first iteration where we checked adjacency nodes of 1 and found 2 their and marked it as visited ,then it will ignore the if condition and it will check for 3rd iteration.... so how do we find adjancency nodes of node 2 ?
      please anyone clear my doubt.

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

      @@madhabkafle8898 check on 2's visit wont be performed immediately after 1's adjacent nodes are put inside the queue

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

    This is the best channel for dsa cp on youtube!

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

    This is nice explanation and really helped me to grasp the template to write a BFS traversal code for a give graph. I've a suggestion though, whenever you are referring using 'Queue', try to draw it with a horizontal rectangular box, this vertical box would typically used to represent 'Stack'

  • @omkarlavangad1434
    @omkarlavangad1434 2 роки тому +27

    On GFG, their test cases have disconnected graphs but they expect the answer only to be the bfs traversal of component containing node numbered 0. Don't waste time. Just run your code without the loop. It will pass the test cases

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

      Yes exactly i was doing the same using loop but then i dry run and got to know that they only want for vertex 0

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

      Exactly 💯💯 thanks man....

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

      Thanks buddy

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

      Can you please tell me, what will be the difference between the code of the BFS of directed and undirected graph? They are same or not?

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

      @@tanishsharma5440 yes directed or undirected doesn't matter for a normal bfs traversal

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

    for visited array, we can use visited arary of type bool instead of type int, this won't change the TC of SC at all, but it will use some lesser space.

  • @DharmveerSingh-ct1um
    @DharmveerSingh-ct1um 3 роки тому +14

    0 Dislikes. Power of Honesty.

    • @harpic949
      @harpic949 3 роки тому

      Now there are 9🙂

  • @lakshitajoshi9181
    @lakshitajoshi9181 3 роки тому +8

    Can't believe that this topic was hard for me before i watched the video....but after watching the video it's like the most easy thing ..thank u so much sir ....this video was very helpful 🤩😇

  • @arka6302
    @arka6302 3 роки тому +14

    Quality at its peak as usual ! ♥️

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

    this is the best playlist to learn graphs

  • @rakeshcharpe4478
    @rakeshcharpe4478 3 роки тому +10

    This graph is so useful bhaiya Thank you , please make more series like this of different topics too, it's really helpful ...👍🏻

  • @SDE_FACT
    @SDE_FACT 3 роки тому +21

    Please please please make a series on tree🙏🙏🙏
    The way you explain the topics are fantastic... Please help us in trees also

  • @degavathanandnayak9692
    @degavathanandnayak9692 3 роки тому +4

    Great explanation! It is easily understandable with your video. Thank you so much!

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

    Amazing explanation! This topic always went above my head before this video.

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

    Before watching this video I was confused why we used that visited array
    The concept is crystal clear now

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

    you are really doing a great work sir ... thank you so much for this playlist !

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

    I will be posting a video every Sunday on the other channel, so bell DABAAAAA dena: ua-cam.com/channels/vEKHATlVq84hm1jduTYm8g.html

  • @pranjwalsingh7615
    @pranjwalsingh7615 3 роки тому +5

    What a video series just before internship drive in my college

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

    Very helpful. Thanks for your efforts!

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

    These are some really good lectures 👍, one suggestion/request put timestamp in videos.

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

    TAKE A BOW MAN🙇‍♀🙇‍♀🙇‍♀🙇‍♀🙇‍♀

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

    bhai itna sahi padhate ho aapke video dekhta main hu place mera padosi ho gaya hai!

  • @PrashantSingh-jy6zp
    @PrashantSingh-jy6zp 3 роки тому +2

    for skip promotion go to 4:22

  • @akashkanni4622
    @akashkanni4622 3 роки тому +1

    The outer for loop should iterate from 1 to n ..(not from 1 to v) , and the size of vis (visited) vector should also be equal to n ( not v)
    Correct me if i am wrong .........much needed series striver bhaiyya ...thank you :-)

    • @forgotten_menace
      @forgotten_menace 3 роки тому

      n and v are the same thing(no. of nodes) here lmao

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

    Best explanation. understood , hope this channel reaches more people

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

      Anyone explain this please!!
      in the beginning we put 1 in the queue and mark it as visited , then inside that for loop we also mark adjacency of node 1 , that is node 2 as visited , now in the second iteration of first for loop it will check if the node 2 is visited or not and found that 2 is already visited during first iteration where we checked adjacency nodes of 1 and found 2 their and marked it as visited ,then it will ignore the if condition and it will check for 3rd iteration.... so how do we find adjancency nodes of node 2 ?
      please anyone clear my doubt.

  • @RAJENDRASHARMA-xt8uu
    @RAJENDRASHARMA-xt8uu 2 роки тому +1

    very great explanation

  • @shahzebkhan1770
    @shahzebkhan1770 3 роки тому +1

    You are doing Great,Good explanation.

  • @pratikthorat2314
    @pratikthorat2314 3 роки тому +1

    Great Explanation.

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

    I wish I could have watched this video before my Google STEP Intern interview😖. Thankyou bhaiya for amazing explanation.

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

    Thanks for the series bhaiya🙌

  • @chandanbanerjee3721
    @chandanbanerjee3721 3 роки тому +3

    this is excellent...can you do the same series for binary tree and linked lists

  • @karanudayandas1608
    @karanudayandas1608 3 роки тому +3

    19:25 the c++ code

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

    Understood! Thank you so much as always!!

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

    nice explanation , thanks

  • @arjunkurariya2235
    @arjunkurariya2235 3 роки тому +7

    1st comment. Bhaiya U are great ❤️.pls make video on CHEAT sheet on CP for Coding Round pls🙏🙏

    • @takeUforward
      @takeUforward  3 роки тому +9

      Cp sheet striver youtube me search kro

    • @arjunkurariya2235
      @arjunkurariya2235 3 роки тому +3

      @@takeUforward Mill gya Bhaiya video. Now I will follow it. Thanks bhaiya❤️❤️

  • @pasitopasito1967
    @pasitopasito1967 3 роки тому +1

    Nice explanation

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

    Mauj kardi Bhai 😁

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

    Crystal Clear

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

    Thank you, Striver!!!

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

    Well explained!!

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

    amazing brother

  • @muktadirkhan6674
    @muktadirkhan6674 3 роки тому +1

    C++ code at 19:26

  • @prakharagarwal4933
    @prakharagarwal4933 3 роки тому +6

    bhaiya after completeing this can you suggest problems we should try ??

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

    Heree it goes

  • @sayantaniguha8519
    @sayantaniguha8519 3 роки тому +4

    For *visited* array,
    Why take 'V+1' nodes and start from index 1?
    Why not 'V' nodes and start from index 0?

    • @yadneshkhode3091
      @yadneshkhode3091 3 роки тому +1

      V will also suffice if starting node is 0 else we will have to use V+1

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

    Thank you

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

    understood Thanks

  • @r.apuroop4034
    @r.apuroop4034 2 роки тому

    Understood Brother!

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

    Great video. 1 thing, I was checking out your java solution, it doesn't cover disconnected components.

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

    What changes do we need to make if the source node is not 1 but some other node?

  • @ggk656
    @ggk656 3 роки тому

    Thanks a lot bhaiya ♥️✨

  • @rashmi_gaur
    @rashmi_gaur 3 роки тому

    Really great explanation!👍👏👏

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

    Great Explanation btw for loop logic is not working on gfg why is it so ????

  • @ram_pande8039
    @ram_pande8039 3 роки тому

    thanks a lot lot lot... bhaiya.

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

    code explained and provided in description box is different , I understood but it creates confusion. Anyway loving watch and learn

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

      Code provided is for single component and code explained in video is for multiple component

  • @hemrajjeure2210
    @hemrajjeure2210 3 роки тому +3

    What changes should we need to make if the graph starts from 0, In for loop if we write i=0;i

    • @takeUforward
      @takeUforward  3 роки тому +1

      The code works at gfg.

    • @glint695
      @glint695 3 роки тому

      Check the code given in description as well

    • @aditya14-02
      @aditya14-02 3 роки тому +1

      you are crrt it doesnt work with for loop

    • @aditya14-02
      @aditya14-02 3 роки тому +2

      because the for loop is for multiple components where as in gfg the test case is for single component only

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

      @@aditya14-02 yup you are right

  • @rnjnmhta.catomato
    @rnjnmhta.catomato 2 роки тому

    19:27 c++ solution

  • @yadneshkhode3091
    @yadneshkhode3091 3 роки тому

    thanks mate

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

    Concepts are crystal clear but would have been better if a BFS traversal for adjacency matrix was also covered

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

      I think we can apply similar by creating a adjacency list from the adjacency matrix

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

    Thank you Raj bro. However could u pl help me understand why you are telling time complixty is O(n) ? you have outer for loop then while loop and then for loop inside. so time complicity is l*m*n of not n^3 please let me know

  • @gauravshukla6675
    @gauravshukla6675 3 роки тому

    itna achha bfs kisi ne nahi samjhaya aaj tak

  • @sans.creates
    @sans.creates 3 роки тому

    Thank you!!

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

    Can we initialise the queue before for loop??

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

    adjacency list just doesn't print edges in increasing order. WILL IT AFFEct the PATTERN OF TESTCASES IN ANY PROBLEM OF GRAPHS?? Pls answer...

  • @anshumansharma4580
    @anshumansharma4580 3 роки тому +1

    Can anyone please help me in this: when instead of auto, I wrote- vector:: iterator it, then I need to replace 'it' in the loop with *it, so I want to ask that if we use auto, does that mean that we can simply use the variable as index? that is 'it' is no longer pointing to the cells of the vector, rather is working as indices?

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

      Yes..it is the value itself..

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

      when iterator is used, you need *it to access value because iterator is just like pointers in C++
      for example: suppose we need to print values from key-value pairs in a map
      //auto can be changed to map::iterator
      for (auto it = mp.begin(), it!= mp.end(), it++)
      cout

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

      @@chetanraghavv It is very informative. Thanks for sharing.

  • @Manish_Sahu
    @Manish_Sahu 3 роки тому

    superb

  • @vaibhavtiwari6540
    @vaibhavtiwari6540 3 роки тому

    Constructive criticism: Striver if you explained how the frontier kind of ripples out I would have made sense quicker to me.

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

    whoa !!🤩🤩

  • @eshagusain9718
    @eshagusain9718 3 роки тому

    thank you sir

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

    Why the time complexity is 0(N+E) but not 0(N+NE) because for reaching 1 node it will take 0(1) and in the worst case every node is connected with every node so accessing all adjacent nodes for 1 node will take 0(E) if we repeat the task for N node dont we get 0(1)+0(1)+.......n => 0(N) for all N nodes and for all adjacent nodes it will be N *0(E) so total will be 0(N+NE) ?

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

      No because here we used visited array if adjacent node is already visited then we dont visit it twice so i guess t.c should be o(n) but also don't know why t.c=o(n+e)

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

    understood

  • @sayantaniguha8519
    @sayantaniguha8519 3 роки тому

    The value of 'i' is a constant incase the starting point is mentioned from earlier.
    Right?

  • @hitansupatra8383
    @hitansupatra8383 3 роки тому

    plz make more series on other data structures like tree & linkedlist

  • @Believer...
    @Believer... 3 роки тому +9

    You're voice became so sweet when you speak Hindi please try some time.

  • @NikhilKumar-mt6fb
    @NikhilKumar-mt6fb 2 роки тому +1

    For directed graph just remove the for loop it worked fine!!

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

    The size of the visited array should be (the value of the vertice having maximum value in the graph among all vertices+1) not (number of vertices+1) in the code

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

    It makes sense to use the for loop.
    Why is that gfg accepts the solution without the for loop?

    • @takeUforward
      @takeUforward  3 роки тому +3

      Does not have a test case with multiple components

    • @lavishgarg5090
      @lavishgarg5090 3 роки тому

      @@takeUforward bhaiya after completeing this can you suggest problems we should try ??

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

      @@takeUforward No! It has a Note: One can move from node u to node v only if there's an edge from u to v and find the BFS traversal of the graph starting from the 0th vertex, from left to right according to the graph. Also, you should only take nodes directly or indirectly connected from Node 0 in consideration.

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

      so we will just do it without for loop

  • @snehilmishra755
    @snehilmishra755 3 роки тому +1

    I am taking the input in adjacency list in main function and also making the vis array in main function but when i am passing them in my find bfs function ,it is giving an error.Should I pass the reference??

    • @krishnabhagat2716
      @krishnabhagat2716 3 роки тому

      I think you need to use arr.get(i).get (j) to access the every elements of array list.

  • @neetisaharan1340
    @neetisaharan1340 3 роки тому

    understood!!!

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

    Is there any problem in taking the queue just before the start of the first for loop?

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

    you made it so simple vaeya :)

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

    What Happens if the values of nodes in graph doesnt start from 1,what if it starts from 10 or 20??

    • @varsha9094
      @varsha9094 3 роки тому +1

      Even I got the same doubt...you got the answer??

    • @photoshopscreen3011
      @photoshopscreen3011 3 роки тому +1

      if it starts from 10 then you make vector adj[N+11] and now just traverse from 10 to N+10 like this for(int i=10;i

    • @saipavanpurella9253
      @saipavanpurella9253 3 роки тому

      @@varsha9094 It is fixed that values in graph starts from either 0 or 1!! or else we can subtract the value for example if we want to put the values like a,b,c by traversing the graph we get( Integer)(currchar-'a').

    • @sasikumarvm5172
      @sasikumarvm5172 3 роки тому +1

      Instead of using array we could use map and insert all the vertices to map and iterate the map and when we vist the vertices make it true in map, so that we can acces it in constant time and TC and SC also will not exceed, also it will work if graph contains character

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

    Bhaiya your code is not working actually you take while loop inside if condition .
    /*correct code*/
    vector bfsOfGraph(int V, vector adj[]) {
    vectorv;
    vectorvisited(V+1,0);
    queueq;
    q.push(0);
    visited[0]=1;
    while(!q.empty()){
    int node=q.front();
    q.pop();
    v.push_back(node);
    for(auto it:adj[node]){
    if(visited[it]==0){
    q.push(it);
    visited[it]=1;
    }
    }
    }
    return v;
    }

  • @yadneshkhode3091
    @yadneshkhode3091 3 роки тому

    sir how to prevent stack overflow for huge data in recursion ?

  • @sumitmukharjee5816
    @sumitmukharjee5816 3 роки тому +1

    bhaiayya, I have a question weather implementation with Adjacency list is good or with vector matrix??

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

      Analyse complexity u will understand

    • @jackwalker2947
      @jackwalker2947 3 роки тому

      See the 2nd video man ....don't skip any otherwise the base will not build up.

    • @sumitmukharjee5816
      @sumitmukharjee5816 3 роки тому

      @@jackwalker2947 yeah it happened exactly I did not se sequence of the videos

    • @hitansupatra8383
      @hitansupatra8383 3 роки тому

      @@sumitmukharjee5816 Adjacency list

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

    I'm a beginner can some one clear my doubt, I feel this bfs in not universal😅
    What if the starting index is 0
    What if we want another starting vertex

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

    i implemented this on gfg, they are not considering multiple components of the graph

  • @piyushkumarsingh7628
    @piyushkumarsingh7628 3 роки тому

    Start a series on LLD System Design

  • @nrlw-77777
    @nrlw-77777 2 роки тому

    20:14 why take array of size v+1???

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

      Because the vertex no.
      is starting from 1 and not 0

  • @ketandoshi5641
    @ketandoshi5641 3 роки тому

    cannot open your course where u teach pls give me the link

  • @ketandoshi5641
    @ketandoshi5641 3 роки тому

    what is that tc in cpp code .what does it mean MULTIPLE GRAPH.

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

    If i complete this series does that mean that i have covered all the topics for graphs that are required for Placements and stuff?

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

    Plz help me with this doubt. I've always found it difficult to understand why Time and Space complexity is O(V + E) in both BFS as well as DFS? Why not O(V*E) or O(Max(V, E)), etc?

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

      I am confused why not only o(n) why subtitle showed o(n+e),clearly it should be o(n) na

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

      @@dillitimalsina6003 You need to specify what's 'n' when describing time and space complexity. Are you taking about number of vertices or edges?

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

      @@letsinevolve6230 ​ @Let's InEvolve no of vertices, but you too didn't mentioned what Vand E are(just kidding), oviously it is vertices yr bcz we are travelling all vertices via edges and also visited array makes sure we won't travel same vertices twice,isn't it>

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

      @@dillitimalsina6003 My bad😅. I got more confused now! You're right, we're not re-visiting any vertex.. so the complexity could be O(V). But I guess cuz we're looping within a recursion, the complexity certainly can't be just O(V) ...right?

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

    for(auto it: adj[node]) could not understand this line.....can anyone help me out

    • @takeUforward
      @takeUforward  3 роки тому

      Its for each loop. Since adj[node] means a vector hence it will iterate in it, and it will be iterating on every integer

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

    didn't get this line i.e auto it:adj[x]. what is the another way of writing this ?

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

    Bhaiya, I have tried to implement bfs recursively:
    vector adj[n + 1];
    int vis[n + 1] = {0};
    queue que;
    void bfs(int i) {
    vis[i] = 1;
    cout