G-41. Bellman Ford Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ส.ค. 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
    ---------------------------------------------------------------------------------------------------------------------------

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

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

    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

  • @mashfy6314
    @mashfy6314 ปีที่แล้ว +199

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

    • @samarthagarwal6965
      @samarthagarwal6965 ปีที่แล้ว +18

      And we all guys as watchers 😂

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

      nice one

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

      @@samarthagarwal6965 already taken by the "Watcher"

    • @blutoo1363
      @blutoo1363 ปีที่แล้ว +9

      I think Striver is already a good enough superhero name

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

      Wahi yr his teachings skills with so much of patiencee

  • @harleenkaur7751
    @harleenkaur7751 ปีที่แล้ว +68

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

    • @harshitrautela6585
      @harshitrautela6585 2 วันที่ผ่านมา

      @@dharmvir2330 So why did you take the paid course? Is there any advantage you can think of that the paid course is giving better than Striver(just curious to know)?

  • @akshaysoni3496
    @akshaysoni3496 ปีที่แล้ว +50

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

      this should be pinned

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

      I did the same thing.

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

      exactly!

    • @nalingoyal881
      @nalingoyal881 5 หลายเดือนก่อน +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.

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

      @@nalingoyal881 Bhai you would find smoller distance for c in the same iteration.

  • @mandarbhatye17
    @mandarbhatye17 ปีที่แล้ว +43

    Thanks

  • @av21015
    @av21015 ปีที่แล้ว +9

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

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

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

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

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

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

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

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

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

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

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

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

    Thanks Striver for these wonderful lectures. Understood.

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

    thank you so much for the clean and crisp explanation.

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

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

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

    Beautiful Explanation . Loved your content keep going 100%

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

    Best explaination of this algo till date !!

  • @newsbuster2194
    @newsbuster2194 11 หลายเดือนก่อน +1

    Thanks for putting such kind of effort for us.

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

    thanks striver, you are the real gem

  • @LokeshSharma-hm5jz
    @LokeshSharma-hm5jz ปีที่แล้ว +1

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

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

    You got so much energy, bro!

  • @tanyacharanpahadi158
    @tanyacharanpahadi158 2 หลายเดือนก่อน +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!

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

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

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

    Wow! very well explained, completely Understood

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

    Amazing , very well explained 🔥🔥

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

    Solid explanation man! Thanks!

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

    Understood. Great explanation for the intuition.

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

    Understood !! Amazing as always

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

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

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

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

    Thank you very much. You are a genius.

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

    Amazing Explanation!!🔥🔥

  • @srinayan390
    @srinayan390 ปีที่แล้ว +26

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

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

    Understood!! :)
    Thank you! 🙏🏻😊

  • @NeverGive-Up..26
    @NeverGive-Up..26 หลายเดือนก่อน +1

    Amazing explanation

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

    👏 understood...very well explained..

  • @stith_pragya
    @stith_pragya 7 หลายเดือนก่อน +1

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

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

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

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

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

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

    Very well explained.

  • @Kparkhade
    @Kparkhade 9 วันที่ผ่านมา

    Maja aa gaya
    Itna khatarnak explanation bro 🎉

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

    Well explained man ❤️

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

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

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

    Thank you, Striver!
    Understood

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

    Whatt an explanation amazing. understood.

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

    lots of love and respect🙌

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

    Understood👍👍
    Thanks a lot

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

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

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

    nicely explained, thanks alot

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

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

    understood very well!

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

    Good, well explained.

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

    understood, It was so Awesome.

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

    thanks buddy great vid

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

    great explanation thank you so much and please continue😘😍

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

    Well explained bhai!

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

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

  • @pranshu_awasthi
    @pranshu_awasthi 4 หลายเดือนก่อน +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 2 หลายเดือนก่อน +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 หลายเดือนก่อน +1

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

  • @chitranshkulshrestha485
    @chitranshkulshrestha485 9 วันที่ผ่านมา

    Thanks for the intution

  • @user-fs8km9qc8f
    @user-fs8km9qc8f หลายเดือนก่อน

    very nice explanation

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

    Understood, thank you bhaiya

  • @ashutoshpandey1639
    @ashutoshpandey1639 ปีที่แล้ว +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 TH-cam👌

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

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

    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

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

    great explanation 🔥🔥🔥🔥

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

    Master piece !

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

      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.

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

    the thought process is insane

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

    Super explanation😀

  • @rithikraj4316
    @rithikraj4316 9 วันที่ผ่านมา

    Simple in every cycle worst can be 1 updation, so we need to update n - 1 updation as we already have 0 or src updated manually

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

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

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

    Understood bhaiya 🙏❤️

  • @user-zn3be9ik1x
    @user-zn3be9ik1x ปีที่แล้ว

    start the relaxation loop from i=1 to i

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

    thx for this video..... Understood.

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

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

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

    clear explanation

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

    Thank you so much

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

    understood and liked

  • @UECAshutoshKumar
    @UECAshutoshKumar 7 หลายเดือนก่อน +1

    Thank you sir 🙏

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

    Understood Sir!

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

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

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

    intution was just 🔥🔥🔥🔥🔥🔥🔥🔥

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

    Understood😉 bhaiya

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

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

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

    Understood as always 😃😃

  • @Stickman-rz9nu
    @Stickman-rz9nu 2 หลายเดือนก่อน

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

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

    thanks, understood

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

    understood striver

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

    Just amazing

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

    Understood 🔥🔥

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

    Understood sir 🙂

  • @ayushsharma-bw5ch
    @ayushsharma-bw5ch ปีที่แล้ว +1

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

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

  • @RS-zh1vc
    @RS-zh1vc ปีที่แล้ว

    Thank you...

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

    Just understood 😀

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

    Excellent

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

    really nice

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

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

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 2 หลายเดือนก่อน

    Thank you bhaiya

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

    Understood!

  • @user-lw9dj8we7k
    @user-lw9dj8we7k 8 หลายเดือนก่อน

    Understood Sir

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

    understood!!!

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

    understood🔥🔥🔥

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

    understood ❤❤