G-5. Breadth-First Search (BFS) | C++ and Java | Traversal Technique in Graphs

Sdílet
Vložit
  • čas přidán 6. 08. 2022
  • GfG-Problem Link: bit.ly/3bn84K8
    C++/Java/Codes and Notes Link: takeuforward.org/graph/breadt...
    DP Series: • Striver's Dynamic Prog...
    SDE Sheet: takeuforward.org/interviews/s...
    Check out our Website for curated resources:
    Our Second Channel: / @striver_79
    In case you are thinking to buy courses, please check below:
    Code "takeuforward" for 15% off at GFG: practice.geeksforgeeks.org/co...
    Code "takeuforward" for 20% off on sys-design: get.interviewready.io?_aff=takeuforward
    Crypto, I use the Wazirx app: wazirx.com/invite/xexnpc4u
    Take 750 rs free Amazon Stock from me: indmoney.onelink.me/RmHC/idje...
    Earn 100 rs by making a Grow Account for investing: app.groww.in/v3cO/8hu879t0
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    ---------------------------------------------------------------------------------------------------------------------------

Komentáře • 418

  • @takeUforward
    @takeUforward  Před rokem +83

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

  • @UECSoumyaRay
    @UECSoumyaRay Před rokem +296

    I can proudly say that this summer I watched more tUf lectures than Netflix episodes.

    • @aniket6858
      @aniket6858 Před rokem +17

      Because your placement season is here

    • @Moch117
      @Moch117 Před 11 měsíci +5

      @@aniket6858 What is placement? Is this india

    • @joseph2073
      @joseph2073 Před 11 měsíci +13

      @@Moch117 no, its pakistan.

    • @congdatt
      @congdatt Před 2 měsíci

      what is the result ?

    • @montynathan3318
      @montynathan3318 Před měsícem

      Yoh Netfilix ke hovey?😶‍🌫️

  • @somz21ad
    @somz21ad Před rokem +9

    Hey Striver! Thank you for creating outstanding content and helping people interested in coding problems worldwide! Please don’t stress yourself out, take a break after this one. It’s not easy to work full time and dedicate time for this.

  • @nkgautam6161
    @nkgautam6161 Před rokem +11

    you are great striver.
    Explain such a complex topic in very easy manner.
    Your method of teaching is unique, a unique lecture by a unique teacher🙏🙏🙏

  • @arthurdark3945
    @arthurdark3945 Před rokem +89

    Are you going to teach leetcode questions just like you did in DP series? It would be very helpful if you can teach commonly asked good questions covering different graph patterns and not just the basic ones.

    • @takeUforward
      @takeUforward  Před rokem +122

      Yups, this one is going to be 50+ videos.

    • @pranavsharma7479
      @pranavsharma7479 Před rokem +13

      @@takeUforward bro try to cover as max you can till 15 aug, thnks for helping

  • @gourabbhattacharjee2128
    @gourabbhattacharjee2128 Před rokem +3

    Just some simple words! No one can beat your DSA teaching style!!

  • @thealgorithm7535
    @thealgorithm7535 Před rokem +2

    after seeing your post on LinkedIn that you are launching graph series 2.0 i used to see your CZcams playlist daily now I am very happy thank you very much 💗

  • @chitrankusarkar7278
    @chitrankusarkar7278 Před rokem +26

    17:04 i was still wondering where the heck vis[n] came from. edited like a pro

  • @creativearts6406
    @creativearts6406 Před 3 měsíci +2

    I like the way you explain time and space complexities, which actually helps me to analyze my code complexities. Thanks for the explanation.

  • @raghvendrakhatri5848
    @raghvendrakhatri5848 Před rokem +4

    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.

  • @asadeducation9513
    @asadeducation9513 Před 4 měsíci +2

    understood..awesome..most of the youtuber's don't explain a topic in depth... great video

  • @priyadarsinipaikaray4998
    @priyadarsinipaikaray4998 Před 11 měsíci +3

    You are amazing 🤩
    Guru wo hota h jo muskil si cheez ko v Asan karde tushi great ho ji striver ❤

  • @cinime
    @cinime Před rokem +1

    Understood! Awesome explanation as always, thank you so much!

  • @rajsekhardutta8891
    @rajsekhardutta8891 Před rokem

    What an amazing explanation! Understood! 🤩❤‍🔥

  • @anuraggoswami3534
    @anuraggoswami3534 Před rokem

    57 +videos trurly 🇮🇳 biggest graph series Ironically GOAT 🐐 is teaching GRAPH 🤩

  • @farazjawaid2982
    @farazjawaid2982 Před 11 měsíci +1

    a well explained and organised lecture !!!

  • @p38_amankuldeep75
    @p38_amankuldeep75 Před rokem

    great content loving this after completing dp series💙💛💙

  • @tanaysingh5348
    @tanaysingh5348 Před rokem

    very well explained with all the minute details

  • @aniketshukla2426
    @aniketshukla2426 Před rokem +3

    What about Directed Graphs, It applies for them too?
    I think yes, because the adjacency list will only have those edges so we only traverse those edges

  • @kulkarnisoham
    @kulkarnisoham Před rokem +1

    Awesome Space & Time Analysis 🔥🔥🔥🔥🔥🔥🔥🔥

  • @tharaniarumugam-zb9il
    @tharaniarumugam-zb9il Před 14 dny

    Your videos never fail to save us anytime :) Undhan rasigaiyee naaum unaken puriyavillai...

  • @umeshkaushik710
    @umeshkaushik710 Před rokem

    Great work. Thanks for doing this.

  • @satishsingh8297
    @satishsingh8297 Před rokem +1

    Thankyou striver bhaiya! ❤️

  • @supriyamanna715
    @supriyamanna715 Před rokem +12

    coded on my own! Got an error, resolved the issue, all TC passed! Note taken

    • @futurev14
      @futurev14 Před rokem

      Toh tujhe kya lg rha bada jhanda gaad diya tune saale itne chappal maruga

  • @raiusamaakram
    @raiusamaakram Před rokem

    brilliantly explain thanks sir and neeed complete playlist of DSA from you for cracking google like companies

  • @Maunil2k
    @Maunil2k Před 3 měsíci

    Nice and crystal clear explanation !!

  • @akshaikumar7966
    @akshaikumar7966 Před rokem

    i loved it sir , what a beautiful explanation

  • @aniketshukla2426
    @aniketshukla2426 Před rokem +5

    I'm confused on the Time complexity,
    If we know the while loop runs N times and the size of the adjacency list is 2E, It is alright to add them to get the time complexity?
    like, the while loop runs N times and the for loop overall runs 2E times..??

  • @paullater6230
    @paullater6230 Před 2 měsíci

    understood!! Explained beautifully!!

  • @alt-f4gaming222
    @alt-f4gaming222 Před rokem

    congrats bhaiya for 300k
    ek din apan sath me 1m jayenge

  • @simmi641
    @simmi641 Před 10 měsíci

    Thank you so much stiver. Happy Teachers dayy

  • @mahaprasadm9770
    @mahaprasadm9770 Před rokem

    brilliant explanation!

  • @ramanpareek5218
    @ramanpareek5218 Před 9 dny

    Liked the video, notes taken, understood

  • @RajeevCanDev
    @RajeevCanDev Před rokem

    outstanding explanation!!

  • @Sillysmiles76
    @Sillysmiles76 Před rokem

    Understood, Happy Learning🤗

  • @ancycoding
    @ancycoding Před 9 měsíci

    BEST DSA TEACHER FOR ME

  • @ayat_middya
    @ayat_middya Před rokem

    Wonderful bhaiya.....

  • @shashankdesai8650
    @shashankdesai8650 Před rokem

    Ohoo masthhh bhaiyaaaa Woohoo

  • @rishabhagarwal8049
    @rishabhagarwal8049 Před rokem

    Understood Sir, Thank you very much

  • @aditya_raj7827
    @aditya_raj7827 Před 6 měsíci

    You are amazing striver ❣️

  • @abhichi
    @abhichi Před rokem

    Understood..next please ✌🏻

  • @jatilyadav4000
    @jatilyadav4000 Před rokem

    Great video Loved it

  • @YeaOhYeahh
    @YeaOhYeahh Před rokem +176

    If u r confused about time complexity part, then see the following dry run of the while loop of the qs. he has solved..
    This is how nodes are connected(assuming undirected graph) :
    0 -> 1 ,2, 3
    1 -> 0
    2 -> 0, 4
    3 -> 0
    4 -> 2
    So, total no. of edges = E = 4
    For first while loop ,
    node = 0, edges = 3
    Now, before going to the for loop part, u see a constant time operation O(1) --> q.pop( )
    This step will be executed every time we enter into while loop.
    So, for first while loop how many times for loop will execute ??
    It will be equal to the no. of edges , here it will be 3.
    Therefore, total = ( 1 + 3 )
    Similarly for all other nodes, this is how it will look :
    ( 1 + 3 ) + ( 1 + 1 ) + ( 1 + 2 ) + ( 1 + 1 ) + ( 1 + 1 )
    = 13
    = O ( V + 2 * E )
    = O ( 5 + 2 * 4 )

    • @sumerrawat6947
      @sumerrawat6947 Před rokem +6

      Very well explained !

    • @Saurav_Kumar514
      @Saurav_Kumar514 Před rokem +3

      Awesome 👌👌

    • @mypowerlevelisover9000
      @mypowerlevelisover9000 Před rokem +1

      Thank you

    • @YeaOhYeahh
      @YeaOhYeahh Před rokem +1

      @@mypowerlevelisover9000 🙂

    • @shaikhfaisal2423
      @shaikhfaisal2423 Před rokem +6

      but at the worst case it will be O(n^2) right? since a complete graph have all the vertex with (n-1) edges. which will lead [(1+(n-1))=n] at each while and for loop. since after n times it will become n square. Please confirm this. BTW thanks for the explaination

  • @vakhariyajay2224
    @vakhariyajay2224 Před rokem

    Thank you very much. You are a genius.

  • @sripriyapotnuru5839
    @sripriyapotnuru5839 Před rokem

    Thank you, Striver

  • @shreyyyc
    @shreyyyc Před rokem +1

    Thanks a lot stiver for putting all these playlists out. i can't imagine getting a job if you were not on youtube. i have a little doubt that in "bfsOfGraph" function the syntax of adj[ ] should be this "Vector> adj [ ]" but it is "vector adj[ ]" instead and this is a 1D vector not a vector of vector.

    • @iitbhuvictim
      @iitbhuvictim Před rokem +1

      Here you're creating an array of vector.... basically number of vector is fixed....that is the way to create array of vectors

    • @lakshsinghania
      @lakshsinghania Před rokem

      there is a similar comment in the video number G-2 check that out

    • @ayushmishra9758
      @ayushmishra9758 Před rokem

      Hence, vectoradj[n]
      n is no of vertices.(0 based) . adj creates an array where each adj[i] is a vector itself. Array of VECTORS.

    • @prachi1112
      @prachi1112 Před rokem

      Same doubt. if we're storing an array at each index of the vector, shouldn't it be vector adj instead of vectoradj??

    • @ShivamTh405
      @ShivamTh405 Před rokem +5

      @@prachi1112 We are storing vector in array index, i.e array of vectors. Every node of array denotes an array . Eg -> if we write int arr[] , here data type is int , so it is array of integers, but if we write vector arr[] , here data type is vector , so it is array of vector

  • @yashpadiyar4952
    @yashpadiyar4952 Před rokem +1

    Thankuu sooo muchhh broooo🤗🤗🤗❤❤❤❤❤

  • @likhitbhogadi
    @likhitbhogadi Před 3 měsíci

    hats off to ur hard work.

  • @stith_pragya
    @stith_pragya Před 7 měsíci

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

  • @morganyu3391
    @morganyu3391 Před rokem

    Understood bhaiya!

  • @MischievousSoul
    @MischievousSoul Před 10 měsíci +1

    There was some sound glitch for 30 sec.. Was weird but
    Loved the way you teach and i am here after conpleting your trees playliat... ❤

  • @vikasbagri1225
    @vikasbagri1225 Před rokem

    understood very well...

  • @addictedtocricket8827

    how can someone be so perfect in explianing concept

  • @tanvirhasanmonir1627
    @tanvirhasanmonir1627 Před rokem

    Thank you, understood!

  • @subhamoybera6456
    @subhamoybera6456 Před rokem

    Great explanation

  • @user-mt4jk5gq7g
    @user-mt4jk5gq7g Před 21 dnem

    you are the best stiver

  • @udaypratapsingh8923
    @udaypratapsingh8923 Před rokem +1

    here we go !

  • @samuelfrank1369
    @samuelfrank1369 Před 9 měsíci

    Understood. Thanks a lot.

  • @AbhishekGupta-kn3cz
    @AbhishekGupta-kn3cz Před 19 dny

    This was great, wandering about traversing Graph with starting nodes somewhere in middle. Should that be traversed wrt to levels or some-other way?

  • @gunahawk6893
    @gunahawk6893 Před rokem

    Woah nice explanation

  • @rameshbabuy9254
    @rameshbabuy9254 Před rokem +3

    please also explain space and time complexities

  • @kritagyaprasad7230
    @kritagyaprasad7230 Před rokem

    Great Content

  • @shyren_more
    @shyren_more Před rokem

    understood, thanks!

  • @nandini2783
    @nandini2783 Před rokem

    Thankyou striver!

  • @ankitz007
    @ankitz007 Před rokem

    Understood, Sire!

  • @paulangelp7099
    @paulangelp7099 Před 5 měsíci

    Fantastic 🎉 Understood

  • @ashish2736
    @ashish2736 Před 9 měsíci +4

    Hi @TakeUforward, I had a doubt, when the for loop is inside the while loop, shouldn't we multiply the time complexities instead of adding them?

    • @IITians
      @IITians Před 9 měsíci +10

      understand with the following explanation (just copy-pasted)
      This is how nodes are connected(assuming undirected graph) :
      0 -> 1 ,2, 3
      1 -> 0
      2 -> 0, 4
      3 -> 0
      4 -> 2
      So, total no. of edges = E = 4
      For first while loop ,
      node = 0, edges = 3
      Now, before going to the for loop part, u see a constant time operation O(1) --> q.pop( )
      This step will be executed every time we enter into while loop.
      So, for first while loop how many times for loop will execute ??
      It will be equal to the no. of edges , here it will be 3.
      Therefore, total = ( 1 + 3 )
      Similarly for all other nodes, this is how it will look :
      ( 1 + 3 ) + ( 1 + 1 ) + ( 1 + 2 ) + ( 1 + 1 ) + ( 1 + 1 )
      = 13
      = O ( V + 2 * E )
      = O ( 5 + 2 * 4 )

    • @vip-qg1zl
      @vip-qg1zl Před 4 měsíci

      ​@@IITiansgreat

  • @kartikshukla5018
    @kartikshukla5018 Před 9 měsíci

    Thanks sir .... best solutions

  • @codeman3828
    @codeman3828 Před 2 měsíci

    Great series

  • @hrushi_borhade
    @hrushi_borhade Před rokem

    understood striver!!

  • @adityaroychowdhury3709

    understood
    thankyou very much

  • @tasneemayham974
    @tasneemayham974 Před 6 měsíci

    bestttt!! understoodddd

  • @aasifali9139
    @aasifali9139 Před rokem

    thx striver. Understood.

  • @Highlights_Point
    @Highlights_Point Před rokem +1

    Thanks Bhaiya

  • @varunkumar-vs5wc
    @varunkumar-vs5wc Před 8 měsíci

    all clear thank u bro

  • @_hulk748
    @_hulk748 Před rokem

    Understood sir❤🙏🙇‍♂

  • @BharatKumar-rc8vn
    @BharatKumar-rc8vn Před dnem

    made it simple to understand

  • @unmeshkadam4876
    @unmeshkadam4876 Před rokem

    Which software and device you are using for your illustration?

  • @shivasai7707
    @shivasai7707 Před rokem

    striver by bfs we wont visit unconnected componenets?
    thank you

  • @sagaravhad5198
    @sagaravhad5198 Před 11 měsíci

    Thank you so much.

  • @RitikKumar-bk6pj
    @RitikKumar-bk6pj Před rokem

    Sir I have a question how many time we have spent to complete graph ?

  • @mriduljain6809
    @mriduljain6809 Před rokem

    Understood Bhaiya

  • @ANURAGSINGH-nl2ll
    @ANURAGSINGH-nl2ll Před 10 měsíci

    understood thank you

  • @farheenkhan3248
    @farheenkhan3248 Před 11 měsíci

    just thank you 🙏

  • @hemachel175
    @hemachel175 Před měsícem

    Thanks for the video buddy. I understood everything except the point where you sum the O(N) and O(sum of degrees of nodes) to find the time complexity. why the sum is there? I think the inner loop will be running 2E times which should be the complexity only. can you please elaborate on why the sum of O(N) is also needed here?

  • @tanmaysinghal8370
    @tanmaysinghal8370 Před rokem

    You are great Striver, how's it going in Warsaw.?

  • @chitranjansingh8432
    @chitranjansingh8432 Před rokem +1

    Brother you forgot to tell why was that code working for directed graph as well? Does anybody here know?

  • @UECAshutoshKumar
    @UECAshutoshKumar Před 11 měsíci +1

    Thank you sir

  • @shubhigupta5689
    @shubhigupta5689 Před rokem

    Understood🌻

  • @hakunamatata-nl4js
    @hakunamatata-nl4js Před 28 dny

    Thank you

  • @parthivsarkar6835
    @parthivsarkar6835 Před rokem

    Understood💯

  • @phlox22
    @phlox22 Před rokem +2

    helllo , i am confused in this line "vis[n]" when i run the program it's saying "n" not declared but when i corrected it with vis[V-1] V being no. of vertices of graph it worked fine , so my question is , what is n here exactly ? number of edges or vertices used for declaring visited array?

    • @phlox22
      @phlox22 Před rokem +2

      i got the same error as well , he did a mistake on declaring visited array first time ,it should be "Visited[V]" not "visited[n]" ,so what you did was correct

  • @siddheshborse3536
    @siddheshborse3536 Před rokem

    Understood.
    😊

  • @RanjeetKumar-dr5ds
    @RanjeetKumar-dr5ds Před rokem +1

    understood

  • @radharamamohanakunnattur3035

    A Big Thanks!!

  • @udayrajvadeghar8555
    @udayrajvadeghar8555 Před 2 měsíci

    UNDERSTOOD!

  • @Saurabh-fe2bg
    @Saurabh-fe2bg Před rokem

    Hey can someone please help me with this thing --> while initialising queue we are mentioning linkedlist and also the node part

  • @oqant0424
    @oqant0424 Před rokem +1

    5/56 done(3.12.22)

  • @Shivi32590
    @Shivi32590 Před 10 dny

    thank you

  • @amangopalgoyal371
    @amangopalgoyal371 Před 11 měsíci

    can anyone please tell that why same code works for directed and undirected graph for bfs?