G-41. Bellman Ford Algorithm

Sdílet
Vložit
  • čas přidán 25. 06. 2024
  • GfG-Problem Link: bit.ly/3K7emug
    C++/Java/Codes and Notes Link: takeuforward.org/data-structu...
    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 • 304

  • @takeUforward
    @takeUforward  Před rokem +68

    Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞
    Do follow me on Instagram: striver_79

  • @akshaysoni3496
    @akshaysoni3496 Před rokem +44

    Improve Time Complexity by exponential with just minor observation:
    Put int count =0; After the first loop & increment the value of count by 1 when the dist array will get updated and at the end of the second loop, if the value of count is not increased then directly return dist array. if no update in dist array happened that means we already calculate dist array, no need to do further iteration, In the worst case it does not have any impact, but for rest, it decreases TC a lot. It Reduce the number of iteration in Best & Average cases.

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

      this should be pinned

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

      I did the same thing.

    • @shivangisehgal
      @shivangisehgal Před 8 měsíci

      exactly!

    • @nalingoyal881
      @nalingoyal881 Před 3 měsíci +1

      Is this case even possible? in a graph like this : a-> b-> c . if we have found smaller distance for a->b , we will surely find shorter distance for b->c in next iteration. Let me know if you think differently.

  • @mashfy6314
    @mashfy6314 Před rokem +168

    This guy got superpower. Can be cast as a Marvel hero "The Explainer" .

  • @harleenkaur7751
    @harleenkaur7751 Před rokem +65

    Thanks Striver. Trust me, even in my paid course, they just simply explained the working of Dijkstra and Bellman without going into such detail. U r the best teacher.

    • @dharmvir2330
      @dharmvir2330 Před rokem +9

      True , i am also here from a paid course , Someone believes it or not These explanations are way better than in a paid course.

  • @srinayan390
    @srinayan390 Před rokem +25

    when u said "yes u r correct", my confidence became infinity❤

  • @av21015
    @av21015 Před rokem +9

    You explained it really well, If I was to trace this myself I would have sat for an entire day.

  • @EliasEH89
    @EliasEH89 Před 4 měsíci +3

    Thank u.
    Note: The Dijkstra's algorithm implemented in G-32 can handle any negative edges graph EXCEPT the following cases:
    1- Directed graph having any negative cycle (cycle with sum < 0)
    2- Undirected graph having any negative edge because the edge in undirected graph is by itself a cycle

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

      What if graph is disconnected and negative cycle isnt reachable from source then your first point is false.

    • @shravani2922
      @shravani2922 Před 25 dny

      Your thought is right, my doubt is why not follow Dijkstras algorithm implemented in G-32 for directed graphs with no negative cycles. The time complexity is even less than Bellmanford algorithm. What is the use of Bellman ford algorithm?

    • @tasneemayham974
      @tasneemayham974 Před 24 dny

      @@Mercer80 I think we can apply a quick visited check? Like we did in the first lectures?

  • @mandarbhatye17
    @mandarbhatye17 Před rokem +32

    Thanks

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

    Have watched multiple videos, but got the understanding from this explaination. Thanks

  • @tanyacharanpahadi158
    @tanyacharanpahadi158 Před 29 dny +1

    OMG! too much hype about bellman ford algorithm and this is what it is? WOW! you made it so simple. Thanks a ton striver!

  • @cinime
    @cinime Před rokem +1

    Understood! Super amazing explanation as always, thank you very much!!

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

    One of the best playlist of Graph on youtube bhaia you deserve more

  • @AyushiPanday12
    @AyushiPanday12 Před rokem

    Thanks Striver for these wonderful lectures. Understood.

  • @dhanashreegodase4445
    @dhanashreegodase4445 Před 6 měsíci +1

    thanks striver, you are the real gem

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

    Best explaination of this algo till date !!

  • @virgarg9653
    @virgarg9653 Před rokem

    Beautiful Explanation . Loved your content keep going 100%

  • @ankitpandey7262
    @ankitpandey7262 Před rokem

    Understood !! Amazing as always

  • @newsbuster2194
    @newsbuster2194 Před 9 měsíci +1

    Thanks for putting such kind of effort for us.

  • @LokeshSharma-hm5jz
    @LokeshSharma-hm5jz Před 11 měsíci +1

    understood, I dont know why i was afraid of this algo in explaining. You made it a butter.

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

    thank you so much for the clean and crisp explanation.

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

    You got so much energy, bro!

  • @komalkrishna7836
    @komalkrishna7836 Před rokem

    Wow! very well explained, completely Understood

  • @soninirav
    @soninirav Před rokem

    Amazing , very well explained 🔥🔥

  • @doingsneakypeakylike
    @doingsneakypeakylike Před rokem

    Solid explanation man! Thanks!

  • @amankush2408
    @amankush2408 Před 6 dny

    Understood. Great explanation for the intuition.

  • @varunakrishnani7688
    @varunakrishnani7688 Před rokem +1

    Understood!! :)
    Thank you! 🙏🏻😊

  • @tasneemayham974
    @tasneemayham974 Před 24 dny +1

    SUBSCRIBED FROM FIRST RECURSION LIST VIDEO, SIRE!!!! UNDERRRSSTOOODDDD

  • @vakhariyajay2224
    @vakhariyajay2224 Před rokem +1

    Thank you very much. You are a genius.

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

    Question: why do we need N-1 iterations?
    Reason: Because we first of all set the source distance out ot all the N edges, now we have N-1 edges, to fill their distances w.r.t source, we need at max N-1 iterations for each Node.

  • @herculean6748
    @herculean6748 Před rokem

    lots of love and respect🙌

  • @g51661
    @g51661 Před 8 měsíci

    Thank you, Striver!
    Understood

  • @suyashjain3223
    @suyashjain3223 Před 4 měsíci

    Amazing Explanation!!🔥🔥

  • @Shivanai_h
    @Shivanai_h Před rokem

    👏 understood...very well explained..

  • @kr_ankit123
    @kr_ankit123 Před rokem +1

    Understood👍👍
    Thanks a lot

  • @kirankuyate6056
    @kirankuyate6056 Před rokem

    greate explaination and with great energy while explaining that make people more creative affracting getting more..💖

  • @smile8510
    @smile8510 Před rokem +2

    Master piece !

  • @stith_pragya
    @stith_pragya Před 6 měsíci +1

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

  • @manasranjanmahapatra3729

    Whatt an explanation amazing. understood.

  • @bhargavreddy5938
    @bhargavreddy5938 Před rokem

    Well explained man ❤️

  • @NeverGive-Up..26
    @NeverGive-Up..26 Před dnem +1

    Amazing explanation

  • @MojnoSardar
    @MojnoSardar Před rokem

    Very well explained.

  • @harshith4549
    @harshith4549 Před dnem

    We can also fit the negetive cycle check in the for loop with extending its range to V and checking if its the Vth iteration in relaxtion without writing repeated code. Also the best and worst cases can be improved by keeping a count of how many relaxations done in each iteration which signifies that if at any point no relaxation can be done then no further iteration is required bcoz there will be no change in distances further. Here's how its done:
    vector bellman_ford(int V, vector& edges, int S) {
    vector dist(V, 1e8);
    dist[S] = 0;
    for(int i = 1; i

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

    great explanation thank you so much and please continue😘😍

  • @nazimhussainmazumder4750

    understood very well!

  • @arnabdebnath2425
    @arnabdebnath2425 Před rokem +1

    Bhaiya nind se uth ke breakfast mai apka video khaneka Maza hi kuch Alag h…….
    Thank you so much bhaiya ….❤

  • @krishnashah6654
    @krishnashah6654 Před rokem

    nicely explained, thanks alot

  • @Kokowangmini
    @Kokowangmini Před rokem +1

    understood, It was so Awesome.

  • @alessandrocamilleri1239
    @alessandrocamilleri1239 Před rokem +1

    Top notch explanation as usual. I would have included an update flag to pre-empt unnecessary iterations.

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

    Things i figured out :
    1. If you know a path to reach a node you can figure out the paths to reach nodes adjacent to it.
    2. why repeat n-1 times, every time you run through the edges you find a path to reach the node and the cost to reach it, if its better you update, incase the cost doesnt update you have been able to exhaust the minimum cost path.

  • @bhaktisharma9681
    @bhaktisharma9681 Před 6 měsíci +1

    thanks buddy great vid

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

    Understood, thank you bhaiya

  • @vyomyadav6497
    @vyomyadav6497 Před 8 měsíci

    Good, well explained.

  • @jagannathan1014
    @jagannathan1014 Před 8 měsíci +2

    Shortly, if N nodes, the node at the farthest level will be at

  • @mdyousufgazi4030
    @mdyousufgazi4030 Před rokem

    the thought process is insane

  • @ashutoshpandey1639
    @ashutoshpandey1639 Před rokem +2

    one thing should also be mentioned that if a graph on N nodes have cycle, then their is path exist having more than N - 1 edges from first to last node.
    By the way best explanation on CZcams👌

  • @auroshisray9140
    @auroshisray9140 Před rokem

    Well explained bhai!

  • @naitikrathore3317
    @naitikrathore3317 Před rokem +2

    2 din baad DAA ki paper hai and Bellman Ford aayega exam me, soch hi rha tha ki striver agr next video yhi upload kr de fir to mjaa hi aa jaye and aaj subh dekha BOOM!!

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

    Habibi ye ek number bideo bana di tumne toh ...baut baut danyawaad

  • @Curiousdev01
    @Curiousdev01 Před rokem +3

    Amazing content sir !! ..... if I get a job will be because of you.🙂

  • @satyasantoshkumarpadavala4230

    Thank you so much

  • @Stickman-rz9nu
    @Stickman-rz9nu Před měsícem

    bhaiya , i want to thank you for this playlist, yesterday i attempted a codechef contest and there was a question related to graph and i solved it correctly using dijkstra, though i got a TLE 💀 but the code was correct.
    it was my first graph question on codechef. thank you so much 🥳🥳

  • @udaytewary3809
    @udaytewary3809 Před rokem

    Understood bhaiya 🙏❤️

  • @parshchoradia9909
    @parshchoradia9909 Před rokem

    Understood Sir!

  • @original_gangsta_
    @original_gangsta_ Před rokem +1

    Understood 🔥🔥

  • @sunilpanchal1498
    @sunilpanchal1498 Před rokem

    Understood as always 😃😃

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

    Just amazing

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

    Super explanation😀

  • @rajatjakhar3146
    @rajatjakhar3146 Před rokem +1

    great explanation 🔥🔥🔥🔥

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

    Imp when to apply - > when have -ve cycles, idea-> relax all the edges v-1 times , tc->(O(VE))

  • @aasifali9139
    @aasifali9139 Před rokem

    thx for this video..... Understood.

  • @cricketguru7596
    @cricketguru7596 Před rokem

    clear explanation

  • @decepticonWave
    @decepticonWave Před rokem +2

    The fact that you explained N-1 is why you are the GOAT. Please make a paid course and we will pay

    • @ashutoshpandey1639
      @ashutoshpandey1639 Před rokem

      But the one thing should also be mentioned that if a graph on N nodes have cycle then their is path exist having edges more than N - 1.

  • @user-zn3be9ik1x
    @user-zn3be9ik1x Před rokem

    start the relaxation loop from i=1 to i

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

    Thank you sir 🙏

  • @shashankgarg7476
    @shashankgarg7476 Před rokem

    understood and liked

  • @juniorboy1903
    @juniorboy1903 Před rokem +1

    Understood😉 bhaiya

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

    intution was just 🔥🔥🔥🔥🔥🔥🔥🔥

  • @psurya3053
    @psurya3053 Před rokem +1

    thank you bhaiyya , please can you make a playlist on examples on segment trees from codeforces

  • @pranshu_awasthi
    @pranshu_awasthi Před 2 měsíci +1

    Dijsktra's code which striver has taught works for negative edges , it just not works for Negative edge cycles. So all in all , it would work for DAG with positive / negative weights.

    • @tasneemayham974
      @tasneemayham974 Před 24 dny +1

      Of course, because Dijkstra doesn't work for negative edges in UNdirected grapsh: What if you have 0 - 1 with -1 weight? It will give TLE. But in directed, 0->1 with -1, 1 can never go back to 0 so the loop will not work.

    • @pranshu_awasthi
      @pranshu_awasthi Před dnem +1

      @@tasneemayham974 That's what I said. It works for DAG.

  • @-VLaharika
    @-VLaharika Před rokem

    Understood 👍

  • @user-lw9dj8we7k
    @user-lw9dj8we7k Před 7 měsíci

    Understood Sir

  • @rajeshj4066
    @rajeshj4066 Před rokem +1

    @ 16:00 : explained why it has n-1 iterations

  • @sambhavagarwal5871
    @sambhavagarwal5871 Před rokem

    understood striver

  • @Stickman-rz9nu
    @Stickman-rz9nu Před měsícem

    bhaiya , in the for loop terminating condition should be “ i < V “ for n-1 iterations

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

    Understood!

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

    Understood sir 🙂

  • @selvanambi9852
    @selvanambi9852 Před rokem

    Excellent

  • @RS-zh1vc
    @RS-zh1vc Před rokem

    Thank you...

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

    Had no idea it was this easy, damn. Obviously now that i know the logic, i don't even need to remember it.

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

    understood!!!!

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

    understood!

  • @user-kl3qv1sc8k
    @user-kl3qv1sc8k Před 20 dny

    amazing🤩😍😍

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

    understood ❤❤

  • @aryashjain7893
    @aryashjain7893 Před rokem

    watching it at 4 a.m. and when u say , I got a better guy, it really hurts :)🤣🤣

  • @monikagadewar4927
    @monikagadewar4927 Před rokem

    Just understood 😀

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

    undershood sir

  • @miglaniaakarsh8874
    @miglaniaakarsh8874 Před rokem

    really nice

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Před měsícem

    Thank you bhaiya

  • @ayushsharma-bw5ch
    @ayushsharma-bw5ch Před rokem +1

    we need to relax each edge for n - 1 times but in the code we are running loop for

    • @vishnubanjara5209
      @vishnubanjara5209 Před rokem +6

      if we assume it 0 index based then V-2 is right and if we take it 1 based than simply run it for 1 less as V-1

    • @shreyanshagrawal3115
      @shreyanshagrawal3115 Před rokem +3

      for(int i = 0; i < N; i++) : runs for N times ( 0 to N-1)
      for(int i = 0; i < N-1; i++) : runs for N-1 times ( 0 to N-2) - which is required

  • @p38_amankuldeep75
    @p38_amankuldeep75 Před rokem

    understood🔥🔥🔥