Bellman-Ford in 5 minutes - Step by step example

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 มิ.ย. 2015
  • Step by step instructions showing how to run Bellman-Ford on a graph.
    Bellman-Ford in 4 minutes - Theory: • Bellman-Ford in 4 minu...
    Code: github.com/msambol/dsa/blob/m...
    Source: Algorithms by S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani [www.amazon.com/Algorithms-San...]
    LinkedIn: / michael-sambol

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

  • @triandot
    @triandot ปีที่แล้ว +237

    7 years ago, better explanation than any modern source

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

      Still better than anything else from 2023 😂

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

      yeah for real

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

      now, 8 years ago

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

      because every mf decides to hide the algorithm behind opaque notation

  • @BharatSingh-zk8lx
    @BharatSingh-zk8lx 8 ปีที่แล้ว +721

    Tomorrow is my exam and this has saved my day . Thanks a lot man🙌

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

      +Bharat Singh Me either :p

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

      My exam is in two hours.

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

      I'm giving my exam.

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

      You take an exam and not 'give' an exam. :)

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

      what if he was at that moment handing in his exam?

  • @xxbighotshotxx
    @xxbighotshotxx 8 ปีที่แล้ว +127

    excellent work! I will be sure to pass this on to anyone else that needs help with this concept in my algorithms class

    • @MichaelSambol
      @MichaelSambol  8 ปีที่แล้ว +21

      +Cameron Ellis Thanks Cameron. Glad you enjoyed.

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

    it's unbelievable how well you explain the algoritms

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

    Thank you so much ! It's just pretty amazing how we spent 2 sessions of 2 hours in an amphitheatre trying to learn this and turn the algorithm by following the literal code, and here I got it in literally 5 minutes. I'm so eternally grateful.

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

      I love how universities teach computer science like social studies

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

    You have the lightest and best understandable pseudo-code i've seen !
    Thanks a lot !

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

    Very eloquently demonstrated, Michael!

  • @nightmareblocks
    @nightmareblocks 6 ปีที่แล้ว

    Bless. Thank you so much. This makes WAY more sense than the pitiful amount of explanations on it in my class. I have my final in an hour and a half and you might have just saved me a world of hurt.

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

    Hey Michael!
    Your videos are really time saving and awesome! It would be a great help if you made such videos on string matching algorithms like KMP, Rabin Karp, Finite Automata and Naïve as well!

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

    I LOVE YOUR VIDEOS!! I learn so much when you explain short and simple- THANK YOU!

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

    Thank you, relaxing edges seems now so easy when drawn instead of all these number and steps in algorighms!

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

    Thank you! Already shared your channel with my friends. We have design & analysis of algorithms exam tomorrow and your videos are short and precise.

  • @cyprianblunt1783
    @cyprianblunt1783 6 ปีที่แล้ว

    Thanks for the video. It'll help me get through my homework and (maybe) final. I'll show this to anyone else stuck on this problem.

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

    Thanks a lot man...I am going for exams and these 5-minute video will definitely add some marks to my paper...

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

    jeez if the professor at our uni could explain it this way. simple and straight forward to the point. thanks a lot

  • @Anushkumar-lq6hv
    @Anushkumar-lq6hv ปีที่แล้ว +1

    I finally understood Bellman-Ford. Thanks for the working example.

  • @deep.space.12
    @deep.space.12 2 ปีที่แล้ว +21

    Instead of only outputting the distance, it would be nice to add that, for each node one can keep track of the parent node where the current shortest distance is found, then traverse backwards from the destination to the source to obtain the shortest path.

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

      Very important, it is also missing in the pseudocode, the prev attribute is set to nil initially, but not changed in the update procedure

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

    Great videos, I encourage you to make a full library of these

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

    Wow my professor took about 20min to explain this and I didn't really get it. I thought it was hard, but after watching your video in 1/4th the time I am able to explain to others how to run the algorithm, and truly understand it. Turns out it isn't hard at all - just need someone good like you to explain it! Thanks a ton.

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

    Thank you so much for your fantastic work! I found learning these techniques by merely reading textbooks and listening to university lecturers pretty bland and counter-intuitive, but fortunately your visualized examples have given me a much clearer picture. in fact I started to understand the all the previously incomprehensible texts and pseudo-codes just after having seen your videos, and I would very much appreciate if you have any plans in the future to share further videos on NP-complete problems and approximation algorithms. Have a nice day!

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

      More on the way, thank you for watching.

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

    You must have spent a huge amount of time on drawing all these pictures. thank you so much!!!

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

    This helped me a lot with my exams. Thanks a lot brother. You are a savior 😅

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

    Straightforward and clear demonstration of the algorithm. Your video helped me a lot. Thanks :)

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

      You are welcome! Thanks for watching.

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

    Only video on Bellman-Ford that cemented my understanding.

  • @gazebo543
    @gazebo543 3 ปีที่แล้ว

    Thank you! much better and simpler than so many ppt slides

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

    thank you so much...!!! 😢😢 i had tears in my eyes....it cleared completely all my doubts...

  • @kellyharper753
    @kellyharper753 8 ปีที่แล้ว

    Thaaaaaaaaanks man............you saved my life...it's the best explanation of bellman-ford algo for me

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

    I've have exam in half an hour and here I am watching this video! Thanks Man!

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

      Hope you managed to pass 👍😀

  • @gargnakshatra
    @gargnakshatra 3 ปีที่แล้ว

    Thanks Very Much, I Really Appreciate It. I have watched many videos on this topic in the last hour but You explained the BEST. THANKS, Michael!

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

    I've never seen better explanation about Bellman-Ford than this video. Thanks a lot!

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

    New knew Bellman Ford algorithm was so easy. Great content man 🔥

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

    really good explanation! thank you, Michael.

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

    Thank u so much. It's the best Bellman Ford Descrption Video I've ever seen!

  • @mdellertson
    @mdellertson 6 ปีที่แล้ว

    Great explanation, very easy to understand. Thanks so much!

  • @estebancarrasco5823
    @estebancarrasco5823 6 ปีที่แล้ว

    I have a discrete mathematics test coming up, and thanks to you, now i understand better. Thanks¡

  • @Guiguilamster
    @Guiguilamster 6 ปีที่แล้ว

    You juste saved me one hour before my exam, thank you so much :D

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

    Nice!
    You may want to add how to check for negative-weight cycles.

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

      for sure something that's missing from this video. Especially when this is a big advantage for this algorithm

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

    Excellent explanation. Very concise.

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

    Cant be better. Thanks a lot.

  • @slahfaguira3138
    @slahfaguira3138 7 ปีที่แล้ว

    Tomorrow is my exam and this has saved my day . Thanks a lot man :)

  • @FaizanAli-nl5ll
    @FaizanAli-nl5ll 6 ปีที่แล้ว

    Great excercise I like it..it's very useful for us and everybody...everybody can see this and learn this excercise easily from this channel.

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

    If only lectures were as easy and straightforward as your tutorials

  • @michaliskaseris2467
    @michaliskaseris2467 7 ปีที่แล้ว

    Best tutorial out there! Good work mate!

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

    i was litterly in a middle of a half an hour exam and i didnot know what was this algorithm i opened youtube and found you and litterly in less than 2 mins i understood it and solved the exam thanks bro ❤❤

    • @Lircking
      @Lircking 17 วันที่ผ่านมา

      bruh

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

    how can I get the critical path using this method ?

  • @pawesosnowski253
    @pawesosnowski253 4 ปีที่แล้ว

    Awesome simple explanation, thanks!

  • @AdityaNand97
    @AdityaNand97 6 ปีที่แล้ว

    Great Illustration Sir. Thanks for the video...!

  • @armadonluvr
    @armadonluvr 6 ปีที่แล้ว

    very clear and concise, thanks a bunch man!

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

    This has very much saved my day for my final tomorrow. Thanks, Michael.

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

      Glad I could help! Good luck on your final, Brandon.

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

      @@MichaelSambol Fantastic Michael. This video's helpful for me and glad I watched this.

  • @lynam5464
    @lynam5464 5 ปีที่แล้ว

    Very very veryyyyy great for my prepare to exam tomorrow. Thank you!

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

    I am headed to my exam rightnow ...You saved my life

  • @-luca9982
    @-luca9982 5 ปีที่แล้ว

    You are a god for every cs student! All hail Michael!

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

    No negative cycle detection int this video, which is a key part of Bellman Ford!!!

  • @Mark-fc7tu
    @Mark-fc7tu 3 ปีที่แล้ว

    Very clear and informative. Thank you.

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

    A comprehensive short and sweet video
    Than you!!!

  • @marcorivera4776
    @marcorivera4776 6 ปีที่แล้ว

    Clear and concise video, thanks!

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

    efficient and direct. Thank you.

  • @sarveshpandey1125
    @sarveshpandey1125 5 ปีที่แล้ว

    It's such a perfect explanation. Please make a video on Master's Theorem.

  • @sanketsinha8933
    @sanketsinha8933 7 ปีที่แล้ว

    thanks so much your method of teaching is very detailed..thanks again for making this video!

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

    Big thumbs up. Very well explained!

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

    Love this! I've been stuck for hours

  • @stephenlasky9348
    @stephenlasky9348 8 ปีที่แล้ว

    great video, short and to the point

  • @tnvmadhav2442
    @tnvmadhav2442 6 ปีที่แล้ว

    It is really good and concise ! Thank you

  • @chadkrause6574
    @chadkrause6574 6 ปีที่แล้ว

    Good, easy, short explanation. These algorithms aren't hard, but everyone makes them really hard by making videos that are way too long and with unnecessary information. Show the algorithm first, then the detail. Good job!

  • @crazygeekman4068
    @crazygeekman4068 5 ปีที่แล้ว

    Vidéo de qualité avec des sous-titres en Français écrits avec soin. Merci :)

  • @william0377
    @william0377 5 ปีที่แล้ว

    genius, its clear and easy to understand, thanks man

  • @shando_tube
    @shando_tube 4 ปีที่แล้ว

    nothing will ever beat this vid

  • @muhammadmuzammil2140
    @muhammadmuzammil2140 8 ปีที่แล้ว

    Perfect explanation.. Thanks alot man

  • @lucaspenna6009
    @lucaspenna6009 6 ปีที่แล้ว

    You are awesome! Thank you for the explanation!

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

    wow your explanation is the best bro keep it up

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

    Great video for a refresher!

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

    Thank you very much. I couldn’t find anything useful on Russian, so I found this video and it help me

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

    Woow, You saved lot of time. Thank You very much.

  • @Alxsllrs
    @Alxsllrs 8 ปีที่แล้ว

    thanks, you helped me in my time of need

  • @avinashsingh4989
    @avinashsingh4989 7 ปีที่แล้ว

    It's an awesome video mate. Thank you very much..!

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

    Thanks! Great and brief video :)

  • @DALIProductions
    @DALIProductions 6 ปีที่แล้ว

    Man, you're amazing, thank you

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

    It helped me so much! Thank you :)

  • @ubaidurrehman4377
    @ubaidurrehman4377 6 ปีที่แล้ว

    Thanks a lot man, Tomorrow is my exam you have saved my day ......

  • @toufiqulislam5637
    @toufiqulislam5637 8 ปีที่แล้ว

    Very nice explanation. Thanks a lot. Thumbs UP.

  • @kotsos131
    @kotsos131 7 ปีที่แล้ว

    excellent explanation! thank you!

  • @debojyotisaha
    @debojyotisaha 7 ปีที่แล้ว

    Thanks sir for giving this great explanation .

  • @Noah-jz3gt
    @Noah-jz3gt 2 ปีที่แล้ว

    this video helps a lot for my understanding.. finally...

  • @folksgames7047
    @folksgames7047 8 ปีที่แล้ว

    Fast accurate videos. Thank you!

    • @MichaelSambol
      @MichaelSambol  8 ปีที่แล้ว

      +FolksGames Glad you enjoyed. Thanks for watching.

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

    Hi, you have an awesome video here and easy to understand. Can I request for you to make a video on Floyd-Warshall's Algorithm? Truly apprecited.

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

    Thank u. u r the god of teaching algorithm.

  • @MsDingens
    @MsDingens 8 ปีที่แล้ว

    nice, that was some good explaining man!

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

    This is real eduation. Today's over 1k students' CS class with low pass rate can only be called a filter, it is not a class!

  • @harshdhaundiyal2241
    @harshdhaundiyal2241 6 ปีที่แล้ว

    Great Video 🙌🙌🙌 Thanks It's been really helpful

  • @TuNguyen-ox5lt
    @TuNguyen-ox5lt 7 ปีที่แล้ว

    Great video . It helps me a lot . tk you pretty much

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

    you have started from the node S then you go to A node but what if i visit E 1st ! does it matter if i choose E node over A node! or the choosing of nodes depends no how i create my adjacency list ? or does it depends on the weights of the edges ? I am a bit confused in which node i should 1st A or E !

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

      This is a long answer, just because that I wanted to summarize it for myself, and if I am wrong about anything below, please write a comment.
      It doesn't matter which edge you choose first but you must update all of them. In any case, first, you will at least reach to E and A and in the second iteration D and C and so on.
      Why doesn't it matter: Let P be the shortest simple path from A to B for example, then its length is less than |V| and at i-th iteration, you have the shortest simple path from S to all nodes which are reachable in i many steps (or maybe more). So, after |V|-1 steps, you will eventually observe P, maybe a little bit early, depending on your choice.
      For example, you started the iteration i and updated an edge (X,Y), then you choose another edge. You could use the old information about the edge (X,Y), I mean you can use dist(X,Y) at iteration i-1, you don't need to hurry.
      Of course, I am assuming that there is no negative cycles. We are actually looking for the shortest SIMPLE paths but the algorithm is looking for shortest paths and if after |V|-1 many iterations, an edge is still "updatable", we understand that there exists a negative cycle in our graph, so we could add this "check" step and search for negative cycles and the algorithm would be more complete.
      Also Moore-Bellman-Ford can accept edges with negative weights, contrary to the Dijkstra's algorithm, because Dijkstra's algorithm prefers minimal edges and we could actually end up with a longer simple path even tough any other path started with a bigger weight than the weight of all the whole path using the initial minimal edge. I couldn't write this in a clear way, so here an example:
      V(G) = {s,a,b,t}, E(G) = {(s,a),(s,b),(a,b),(b,t)} with weights respectively 3, 1, -3, 1.
      Dijkstra's algorithm yields 2 because after reaching t with 2, it doesn't even bother to look at (s,a). But MBF-algorithm check all of them, so this is not a
      problem:
      Initialization: dist(s) = 0, all others are infinity, all prev(s)'s are empty.
      1st iteration: dist(a) is 3 / prev(a) is {s}, dist(b) is 1 / prev(b) is {s}, others are infinity / empty.
      2nd iteration: dist(a) is 3 / prev(a) is {s}, dist(b) is 1 / prev(b) is {s} but dist(b) is updated to 0 / prev(b) = {a} and lastly dist(t) is 2 / prev(t) is {b}.
      3rd iteration: (s,a), (s,b) and (a,b) doesn't update anything but (b,t) does and as an answer we have:
      dist(s) = 0, prev(s) = empty // path is empty, s = s
      dist(a) = 3 , prev(a) = s // path is s, a
      dist(b) = 1, prev(b) = s // path is s, b
      dist(t) = 1, prev(t) = b (and we can deduce the path via looking at prev(b) and prev(s)) // path is path(b), t = path(s), b, t = s, b, t.

    • @S3Kglitches
      @S3Kglitches 7 ปีที่แล้ว

      you should go from lowest according to alphabet to have a selection rule which is indeed needed in the algorithm.

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

    Kinda forgot the cycle detection there lad. If the algorithm still finds a better path after n-1 iterations a negative cycle must exist. Which is one of the great powers of Bellman-ford's.

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

      No algo can find shortest distance if there exists a negative cycle

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

      @@it41tanmayaron26 Yes but it can detect one and return a message stating that one exists and that the shortest path is -∞

  • @AgPower1000
    @AgPower1000 8 ปีที่แล้ว +12

    In the algorithm you have a prev() table that you don't update next neither you use it on the animation. Other than that great job

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

    Great video Michael!
    Someone please tell me if bellman ford and distance routing algorithm are both same.

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

      @LiGht YaGami 2 years later, your answer is still saving grades. Thank you!

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

      @@Scondoro What answer? :'(

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

    thanku sir much simpler explanation and exact point

  • @SuperBodet
    @SuperBodet 7 ปีที่แล้ว

    Thank you from Algeria ! :D

  • @andreialeksanian7334
    @andreialeksanian7334 3 ปีที่แล้ว

    better than my uni lecture. Thanks!

  • @PriyanshiSharma0308
    @PriyanshiSharma0308 8 ปีที่แล้ว

    Awesome video. Loved it.

  • @paulorugal
    @paulorugal 6 ปีที่แล้ว

    @Michael Samol
    Why do you declare a prev(u) = nil, if you don't use it in the code?

  • @esraamohamed5601
    @esraamohamed5601 5 ปีที่แล้ว

    Thank you very much ...for all you have done