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

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ก.พ. 2025

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

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

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

  • @UECSoumyaRay
    @UECSoumyaRay ปีที่แล้ว +465

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

    • @aniket6858
      @aniket6858 ปีที่แล้ว +31

      Because your placement season is here

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

      @@aniket6858 What is placement? Is this india

    • @joseph2073
      @joseph2073 ปีที่แล้ว +19

      @@Moch117 no, its pakistan.

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

      what is the result ?

    • @montynathan3318
      @montynathan3318 8 หลายเดือนก่อน +6

      Yoh Netfilix ke hovey?😶‍🌫️

  • @HARSHKAJALKHANEIIIIIIII
    @HARSHKAJALKHANEIIIIIIII 4 หลายเดือนก่อน +28

    THOSE WHO ARE WORDERING WHY IT IS O(N) + O(2E) NOT O(N*2E)
    For each node, the while loop runs multiple times based on the number of edges connected to that node. Here's how it works:
    In the first iteration, the loop runs for e1 edges, plus one extra operation for pushing and popping the node.
    In the second iteration, it runs for e2 edges, plus one extra operation for pushing and popping, and so on.
    Thus, the total time complexity is the sum of all iterations:
    (e1 + e2 + ... + en) + (1 + 1 + ... n times).
    The sum of all the edges connected to each node is equal to the total number of edges, which is 2E (since each edge is counted twice in an undirected graph). Adding the n push/pop operations gives the final complexity:
    O(V + 2E) because e1 + e2 + ... + en = 2E.
    So, the overall complexity is O(V + 2E), which simplifies to O(V + E).

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

      Was searching for this thanks ...
      Just to summerize ...
      the inner for loop runs in O(2E) in total and not for each iteration of while so its O(V) + o(2E)

    • @deepakdas4513
      @deepakdas4513 25 วันที่ผ่านมา

      @@moneshc6887 perfect

  • @creativearts6406
    @creativearts6406 10 หลายเดือนก่อน +4

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

  • @gourabbhattacharjee2128
    @gourabbhattacharjee2128 ปีที่แล้ว +15

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

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

    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 2 ปีที่แล้ว +6

      Very well explained !

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

      Awesome 👌👌

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

      Thank you

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

      @@mypowerlevelisover9000 🙂

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

      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

  • @chitrankusarkar7278
    @chitrankusarkar7278 ปีที่แล้ว +72

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

    • @nextnotification9857
      @nextnotification9857 10 หลายเดือนก่อน +4

      same bro

    • @poetrystation3178
      @poetrystation3178 6 หลายเดือนก่อน +1

      this code is not running editor is a pro for sure

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

      vis[V] = {0}

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

      same, didn't expect this from him

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

    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🙏🙏🙏

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

    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.

  • @asadeducation9513
    @asadeducation9513 11 หลายเดือนก่อน +3

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

  • @YCCMdigo
    @YCCMdigo 4 หลายเดือนก่อน +1

    Thank you man from the bottom of my heart for this video. You're one of the greatest instructors i've ever seen

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

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

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

    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  2 ปีที่แล้ว +138

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

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

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

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

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

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

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

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

    17:02 the code he wrote has errors , 17:06 the errors are gone .

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

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

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

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

  • @tharaniarumugam-zb9il
    @tharaniarumugam-zb9il 7 หลายเดือนก่อน

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

  • @raghvendrakhatri5848
    @raghvendrakhatri5848 2 ปีที่แล้ว +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.

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

    This guy teachers so fuckin good that if I try seeing DSA related video from any other utube channel it feels something is missing, he's not striver

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

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

  • @test-nature
    @test-nature 5 หลายเดือนก่อน

    I was happy to say that I will do and understand first graph problem.🎉

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

    Thank you so much stiver. Happy Teachers dayy

  • @shreyyyc
    @shreyyyc ปีที่แล้ว +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 ปีที่แล้ว +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 ปีที่แล้ว

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

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

      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 ปีที่แล้ว

      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 ปีที่แล้ว +6

      @@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

  • @siddharthpatil3046
    @siddharthpatil3046 8 หลายเดือนก่อน +1

    Time complexity of BFS may be different depending upon representation of graph.
    Adjacency List: O(V+E)
    Adjacency Matrix: O(V^2)
    where V=no of vertex and E = no of edges

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

    00:01 Breadth-First Search (BFS) is a traversal technique in graphs.
    02:16 Breadth-first search (BFS) traversal of the graph
    04:25 Breadth-First Search (BFS) is a traversal technique in graphs.
    06:37 Breadth-First Search (BFS) is a traversal technique in graphs.
    08:54 Performing BFS traversal on a graph using adjacency list representation
    11:35 BFS traversal of graph
    14:04 Implementing Breadth-First Search (BFS) in C++ and Java
    16:01 Breadth-First Search (BFS) is a traversal technique used in graphs.
    18:04 BFS algorithm runs on all the neighbors of each node
    Crafted by Merlin AI.

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

    BEST DSA TEACHER FOR ME

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

    how does adj[node] an int is iterable ? shouldnt it be vector adj in the function definition too?

  • @RanjeetKumar-dr5ds
    @RanjeetKumar-dr5ds 2 ปีที่แล้ว +1

    understood

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

    17:03 i see what you did there 🤣🤣

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

      no one commented on this :(

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

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

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

    Thankyou striver bhaiya! ❤️

  • @PCCOERCoder
    @PCCOERCoder 2 หลายเดือนก่อน +1

    Lecture successfully completed on 03/12/2024 🔥🔥

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

    Understood, Happy Learning🤗

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

    please also explain space and time complexities

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

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

  • @MischievousSoul
    @MischievousSoul ปีที่แล้ว +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... ❤

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

    14:30 program starts

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

    how can someone be so perfect in explianing concept

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

    Nice and crystal clear explanation !!

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

    You are amazing striver ❣️

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

    great content loving this after completing dp series💙💛💙

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

    "UNDERSTOOD BHAIYA!!"

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

    those who are doing on Codestudio we first have to create adjlist first
    so here the easy solution to it using set
    #include
    void prepareList(unordered_map &adjlist,vector &edges){
    for(int i=0;i

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

    a well explained and organised lecture !!!

  • @alt-f4gaming222
    @alt-f4gaming222 2 ปีที่แล้ว

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

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

    here we go !

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

    5/56 done(3.12.22)

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

    Liked the video, notes taken, understood

  • @AryanVerma-y3m
    @AryanVerma-y3m 7 หลายเดือนก่อน

    you are the best stiver

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

    i loved it sir , what a beautiful explanation

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

    Ohoo masthhh bhaiyaaaa Woohoo

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

    My favourite algorithm so far, Breast First Search

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

    understood!! Explained beautifully!!

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

    very well explained with all the minute details

  • @sourabhkushwah9711
    @sourabhkushwah9711 2 หลายเดือนก่อน +1

    Haha I see how you swithed the code between 17:02~17:05 but you should have told the audience rather than chaging the frame. I bet most of them were confused why thier code was not working until they saw you changed the vis[n] with vis[V] because initally I was confused how does your code worked when litrally N is not defined anywhere. and its push_back how does push worked for queue. then I saw the immidiate frame change😂😂 that was sneaky.

    • @Tbm4545
      @Tbm4545 2 หลายเดือนก่อน +1

      Yup u get confused if u pause the video in between, even i got confused then i just watched even i saw that frame where he changed it to V and ya its ohk. We understood it.

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

    hats off to ur hard work.

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

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

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

    crystal clear.. excellent explanantion

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

    Fantastic 🎉 Understood

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

    9:39 ASMR moments

  • @aniketshukla2426
    @aniketshukla2426 2 ปีที่แล้ว +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

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

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

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

    Thanks Bhaiya

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

    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..??

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

    What an amazing explanation! Understood! 🤩❤‍🔥

  • @BharatKumar-rc8vn
    @BharatKumar-rc8vn 6 หลายเดือนก่อน

    made it simple to understand

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

    Thank you, Striver

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

    Thank you sir

  • @syedfaheem1185
    @syedfaheem1185 4 หลายเดือนก่อน +3

    I am having a question regarding the TC. How can it be O(N+2E). Since, the outer loop runs as long as the queue isn't empty, which means it will run exactly N times and for each vertex or node, we check if the neighbours are visited and if not, we add them to the queue. Now, at each node, we're checking whether it's neighbours are visited or jot which is itself an O(1) operation. So, for N nodes, we are performing operations equal to the degree of a node. How can the TC not be O(n*avg. degree).

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

    love you from bangladesh

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

    Understood. Thanks a lot.

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

    Understood..next please ✌🏻

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

    Amazing explanation! I have a doubt regarding time complexity 18:34 . For loop is running inside while loop right? So shouldn't we multiply both as:
    O(n*(2E)) instead of adding both as: O(n) + O(2E). Thanks for the content.

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

      No,it would have been multiplied only when for each node total number of adjacent node is 2*e,but this is not the case,here ultimately we are overall visiting n nodes and for each node,visiting its adjacent nodes,which is total of 2e for total nodes.

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

      @@amanbhadani8840 2E or E, shouldn't be the number of edges, which was E here in this example?

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

      @@amanbhadani8840 very well addressed brother!!

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

      @@himanshupoddar1395 No, in the case of undirected graph you have to traverse 2*E edges.

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

    Thanks sir .... best solutions

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

    Interesting that you have to set 0 as visited missed that when I first tried it!

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

    There is a small correction between 16:56 and 17:05 , pls re-edit . It will help the beginners, remaining everything is cool stuff ., Striver.

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

    Understood sir❤🙏🙇‍♂

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

    Wonderful bhaiya.....

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

    Understood Sir, Thank you very much

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

    Thank you very much. You are a genius.

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

    Wont Time Complexity be O(n*2E)?? Since for each node n we are traversing 2E

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

      But once you have travelled for all nodes, all are visited, you won’t again traverse na. Overall you will travel everyone once only

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

      nooo!!!
      you don't know what E is!!
      E is total no of edges of whole graph in V + E
      in your n*E, E is no of neighbours of a particular node!!!
      see...
      for every node(while loop) you are running "for" loop for number of times==number of its neighbours
      so in that case you can say n*e
      but the thing is this -> for every node number of neighbours are different hence we don''t write complexity like you have written
      while loop n times run karega aur andar wala for loop jo hai, woh in total, sab while loop ka milake, E(total edges in whole graph) times run karega
      isliye toh add kiya N aur E ko
      ab clear ho jana chahiye
      me bhi atka tha ispe 3-4 dinse
      same concept dijkstra, prims me bhi apply hoga so make sure you understand it!

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

      @@CuriousAnonDev thanks for indepth explaination !

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

      @@CuriousAnonDev naam dekh kar mai bhi atak gaya

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

    Great explanation

  • @varunkumar-vs5wc
    @varunkumar-vs5wc ปีที่แล้ว

    all clear thank u bro

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

    Great series

  • @sreehithathatwadi5210
    @sreehithathatwadi5210 9 หลายเดือนก่อน +2

    Isn't the TC is O(N* 2E)? Because we run for loop on each node's degree.

    • @YupIam-f4t
      @YupIam-f4t 9 หลายเดือนก่อน

      yeah, I got the same doubt. Its multiplication only .

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

      @@YupIam-f4t not, it will be adding only because 2E is equal to degree of all nodes, not 1 node. so we can't think like for each node, it is taking 2E time and hence it will not be n*2E

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

    Woah nice explanation

  • @its_kundan
    @its_kundan 7 หลายเดือนก่อน +2

    what if the graph have different components?
    the solution seems to solve the connected components only.

  • @ronnie3357
    @ronnie3357 10 หลายเดือนก่อน +1

    16:56 if the start node is not 0 then the list corresponding to node 0 will be empty hence the queue will become empty after 0 is processed and since node 0 do not have neighbours so the for each loop will not be executed so queue will remain empty and bfs will only contain 0 please address this doubt sir

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

      Exactly my thoughts! The implementation seems wrong. It should be q.add(V) & visited[V] = true

  • @TON-108
    @TON-108 10 หลายเดือนก่อน

    Understood 🥳🥳

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

    Understood bhaiya!

  • @AbhishekGupta-kn3cz
    @AbhishekGupta-kn3cz 7 หลายเดือนก่อน

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

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

    just thank you 🙏

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

    I think bool data type will be better than int data type for visited array because bool data type takes 1 byte whereas int data type takes 4 bytes.
    And If I am wrong pls correct me :)

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

      ya no issue ... just taking int as it would help with weighted graphs as well and generalize the approach

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

    Thankyou striver!

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

    Great work. Thanks for doing this.

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

    Thank you so much.

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

    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 ปีที่แล้ว +12

      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 11 หลายเดือนก่อน

      ​@@IITiansgreat

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

    thx striver. Understood.

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

    Thank you, understood!