Тёмный

Bellman-Ford in 4 minutes - Theory 

Michael Sambol
Подписаться 126 тыс.
Просмотров 321 тыс.
50% 1

The theory behind the Bellman-Ford algorithm and how it differs from Dijkstra's algorithm.
Bellman-Ford in 5 minutes - Step by step example: • Bellman-Ford in 5 minu...
Code: github.com/msa...
Source: Algorithms by S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani [www.amazon.com...]
LinkedIn: / michael-sambol

Опубликовано:

 

2 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 94   
@yangmyfly
@yangmyfly 5 лет назад
It's basically a dp, you iterate v-1 times, first time 1 length edge, second time 2 length edge path, until v-1 length edge path, this is the most important part. You should mention this
@trungngngng
@trungngngng 3 года назад
Yeah that's exactly the nature of this algorithm.
@DrTryloByte
@DrTryloByte 7 лет назад
Its so hard to find concise but useful videos like this. Extremely useful for last minute revision.
@ryancross7345
@ryancross7345 8 лет назад
Please keep making videos, they're excellent.
@MichaelSambol
@MichaelSambol 8 лет назад
+Ryan Cross Glad you like them!
@SonuSonu-tk5pk
@SonuSonu-tk5pk 7 лет назад
more no of edges also means parallel paths
@DVZM.
@DVZM. 5 лет назад
Unfortunately, he stopped uploading short after this video.
@owenmajor1314
@owenmajor1314 2 года назад
got an exam on algorithms in two days. Your channel has helped me out a bunch throughout the semester, thank you!
@DeepanshuThakuriamgsa2k14
@DeepanshuThakuriamgsa2k14 9 лет назад
Thumbs up for showing difference between Dijkstra and Bellman-Ford in case of negative edge! :D
@walterclementsjr.5947
@walterclementsjr.5947 4 года назад
1:34 if we Dijkstra normaly, wouldn't we get the same result as Bellman-Ford? ? I mean you can't just close it after only 1 iteration bc Dijkstra visit all vertices, not stop when just only reach vertex A.
@luizfelipels7
@luizfelipels7 3 года назад
Thank you; I agree.
@professorpoke
@professorpoke Год назад
03:06 "This may be a little confusing, so lets look at the pseudo code." * even more confused * 😂😂😂😂😂😂😂😂😂😂😂
@MichaelSambol
@MichaelSambol Год назад
Ah, I'm sorry! Hopefully this code clears it up: github.com/msambol/youtube/blob/master/shortest_path/bellman_ford.py
@skateboarddude8260
@skateboarddude8260 2 года назад
At ~1:22, you say using Dijkstra would make it so no more updates could be done on the path to A. But if you ignore the input constraint saying no edge has a negative weight and you apply Dijkstra's anyways, it would actually work in this example. Distance to A is 3 after updating from S. When we start checking the edges of B, we check its edge to A and see it is -2. Distance S -> B = 4. And 4+(-2)=2, which is less than the distance S -> A = 3. So now the shortest distance to A is correctly labeled as 2.
@sempitjikufarm8810
@sempitjikufarm8810 Год назад
I agree I still caonfuse why Dijkstra can not do it
@QmdVJ4KrCjUk888
@QmdVJ4KrCjUk888 7 лет назад
In 1:35, don't we get the same result if we use bellman? A is still 2 after v-1 iteration. What is the difference?
@Super21Nash
@Super21Nash 8 лет назад
Your videos are lifesavers !! Thanks a lot !
@yuanzhibao7078
@yuanzhibao7078 9 лет назад
great video! wonderful explanation! thank you so much.
@marcuschiu8615
@marcuschiu8615 6 лет назад
2.35 can't we have a directed edge from node S to node B and not have a cycle?
@thesvodnik
@thesvodnik 4 года назад
Great videos :D. One correction @2:38. A Simple path is a path that has no repeating vertices (this condition includes no repeating edges as well). The statement that all paths are simple still stands.
@ChukwumaAkunyili
@ChukwumaAkunyili 5 месяцев назад
But, Bellman Ford, algorithm works correctly with graphs containing negative cycles. Your video says otherwise.
@MichaelSambol
@MichaelSambol 5 месяцев назад
Negative edges are allowed but not negative cycles. Otherwise you can relax the nodes indefinitely. See code here: github.com/msambol/dsa/blob/master/shortest_path/bellman_ford.py
@UmairAkhtar
@UmairAkhtar 7 лет назад
You are very articulate and the videos are well prepared.
@gagepollard8566
@gagepollard8566 3 года назад
At 0:45, you say the difference between Dijkstra and Bellman-Ford is that Bellman-Ford "works" on negative edges. What does this mean? In actual coding, I don't see a difference in the way I would code to find the shortest path from one node to all other nodes. Your "pseudo-code" is far more confusing than the rest of the video and should have its own video explaining it in detail.
@mathiasbertorelli8362
@mathiasbertorelli8362 7 лет назад
Thank you, this and your other video really helped me a lot!
@MrsConito
@MrsConito 8 лет назад
your explanations are really good and precise! and also your examples video are awesome!! Thank you, you made my finals much easier! :)
@ayusumardi
@ayusumardi 8 лет назад
At 2:39, if you can look at this link www.edmath.org/MATtours/discrete/concepts/cwalk.html, it said that path is no repeating vertices, not edge. But I may be right, may be wrong as I seek for explanation. Thanks
@withyuva
@withyuva 7 лет назад
Simply Excellent. I am gonna nail my exam tomorrow.
@alexlazea9978
@alexlazea9978 5 лет назад
did u ?
@Leo-io4bq
@Leo-io4bq 9 месяцев назад
did u ?
@withyuva
@withyuva 9 месяцев назад
@@Leo-io4bq yupppp
@B0XMATTER
@B0XMATTER 2 года назад
You're a fooking legend Michael.
@efficaciousuave
@efficaciousuave 2 года назад
when does a bellman ford stabilise?
@youreyesarebleeding1368
@youreyesarebleeding1368 Месяц назад
Does prev not get updated in the pseudocode?
@MichaelSambol
@MichaelSambol Месяц назад
See here for working code: github.com/msambol/dsa/blob/master/shortest_path/bellman_ford.py
@benyaminyakobi3652
@benyaminyakobi3652 4 года назад
Thank you very much! Simple explanation :)
@sreesub
@sreesub 2 года назад
outstanding videos. Subbed to the channel.
@Leon-pn6rb
@Leon-pn6rb 8 лет назад
How subjects should be taught!!
@danielyang6826
@danielyang6826 8 лет назад
Better explained than what I can pick up from a class! Nice explanation on why we need a max of V-1 iterations
@jamesperry4470
@jamesperry4470 7 лет назад
Also keep in mind guys the property of trees, that the number of edges in a tree cannot exceed V - 1
@chicagopark9679
@chicagopark9679 Год назад
You are amazing 😊
@vam8775
@vam8775 Год назад
very concise and straight to the point... this is what fits into Gen-Z's mind...
@danieldaniil566
@danieldaniil566 6 лет назад
Very good, but I noticed you used the wrong definition of simple path. It is a path with no repeated vertices, but instead you defined it as a path with no repeated edges.
@kvelez
@kvelez 9 месяцев назад
Cool algorithm.
@gregmakov2680
@gregmakov2680 4 года назад
nhu cut :D
@localhostechoEro
@localhostechoEro 6 лет назад
I wanted to subscribe. But I see I already did so. EDIT: 64 comments. YaY.
@VishiVish01
@VishiVish01 7 лет назад
Where did you get your pseudo-code from
@aritramullick3559
@aritramullick3559 3 года назад
You are literally a God
@MarcosCastroSouza
@MarcosCastroSouza 8 лет назад
excellent explanation!
@debralegorreta1375
@debralegorreta1375 5 лет назад
Love your videos Michael! Thank you.
@catarinapedreira3231
@catarinapedreira3231 6 лет назад
Actual lifesaver. Awesome videos, keep it up :)
@mary__jane
@mary__jane 4 года назад
You've helped me pass sooooo many exams.
@draganostojic6297
@draganostojic6297 7 лет назад
thanks, very useful
@rizwansworld
@rizwansworld 5 лет назад
SWEET! Short and the most efficient 4 minutes of my life :P
@rafaelcheng6221
@rafaelcheng6221 8 лет назад
Very helpful! Thank you so much~
@daiictian1
@daiictian1 8 лет назад
simple and great explanation :)
@alexandrosmourtziapis2055
@alexandrosmourtziapis2055 7 лет назад
thanks man. helped me a lot ;)
@farmangaribov8288
@farmangaribov8288 4 года назад
GOD BLESS YOU
@mKumaranVeera
@mKumaranVeera 9 лет назад
great explanation, thanks.
@Ash-fb5kz
@Ash-fb5kz 8 лет назад
This is so great! Thanks, man! :)
@negatifzeo5709
@negatifzeo5709 8 лет назад
Great work.
@acur665
@acur665 7 лет назад
you're dope thank you for these videos, short and to the point.
@DVZM.
@DVZM. 5 лет назад
Unfortunately, he stopped uploading short after this video.
@vinitkumar06
@vinitkumar06 6 лет назад
Excellent Michael.
@peiranwang3949
@peiranwang3949 6 лет назад
helps a lot, thank you!!!
@CreeperHaunterDavid
@CreeperHaunterDavid 6 лет назад
Great video!
@supdawg7811
@supdawg7811 9 лет назад
I'm confused about the |V| - 1 iterations. I get why there are at most |V| - 1 edges in a shortest path, but how does that number of iterations guarantee that we'll get the shortest path? You say that it is because we'd have a loop in the graph if we had more than |V| - 1 edges in the shortest path, but what do the iterations have to do with it?
@MichaelSambol
@MichaelSambol 9 лет назад
supdawg Revisit the example where they're all in a straight line. Because we do |V| - 1 iterations, we pass through every node on our way to D. If we did |V| iterations, we'd have to pass through a node twice, aka a cycle. If the cycle was positive we wouldn't include it on our path anyway. Remember we can't have negative cycles. Hopefully that helps. Let me know if you're still having trouble.
@MichaelSambol
@MichaelSambol 9 лет назад
supdawg I think you understand that a path with |V| edges is impossible. So if we iterate |V| - 1 times, we would find the path that has |V| - 1 edges, if that was indeed the shortest path.
@supdawg7811
@supdawg7811 9 лет назад
Michael Sambol I guess a way to rephrase my question would be, why wouldn't the algorithm just compute the shortest path after the first iteration? What do the iterations after the first do for the algorithm? I understand that the graph may not be completely sorted out after only the first iteration, but why is that the case?
@MichaelSambol
@MichaelSambol 9 лет назад
supdawg Hmm, maybe watch this video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-obWXjtg0L64.html. You'll see we're not done after the first iteration.
@supdawg7811
@supdawg7811 9 лет назад
Michael Sambol Everything up to about 2:40 makes perfect sense, but I get lost after when you jump to saying "we know if we iterate |V| - 1 times, we're guaranteed to find the shortest path from s to d". I think what doesn't make sense to me is we aren't iterating over one edge per iteration, we are iterating over all the edges per iteration in |V| - 1, so why wouldn't one iteration work? As in, per iteration, we are updating every edge, so if we have n iterations, we are updating each edge n times. Why do we update each edge n times? Why can't we update each edge just once?
@GuRke5100
@GuRke5100 5 лет назад
ehrenmann
@zekekace6882
@zekekace6882 8 лет назад
Your Videos are awesome and time saviour! Do continue making more and more videos.
@MichaelSambol
@MichaelSambol 8 лет назад
Glad I could help. Thanks for watching. :)
@DVZM.
@DVZM. 5 лет назад
Unfortunately, he stopped uploading short after this video.
@hsb0818able
@hsb0818able 8 лет назад
thanks
@MichaelSambol
@MichaelSambol 8 лет назад
Welcome!
@mercadoalvin14beqoy
@mercadoalvin14beqoy 8 лет назад
Hi just want to ask your opinion if it is possible to combine bellman ford with dijkstra's algorithm? If so will the resulting hybrid algorithm be more effective? Thanks
@Cupcakes77
@Cupcakes77 4 года назад
don advertise it takes 4 min, you are mentioning so many other stuffs to know before. wasted my 4 min !
@damayantighosh8854
@damayantighosh8854 7 лет назад
waste of 5 mins. no detailed explanation.
@soumilssinha5010
@soumilssinha5010 7 лет назад
are Bellman-Ford and Dijkstra's algorithm same??
Далее
Bellman-Ford in 5 minutes - Step by step example
5:10
Depth-first search in 4 minutes
4:01
Просмотров 259 тыс.
Dijkstra's Algorithm - Computerphile
10:43
Просмотров 1,3 млн
God-Tier Developer Roadmap
16:42
Просмотров 7 млн
LeetCode was HARD until I Learned these 15 Patterns
13:00
Big-O notation in 5 minutes
5:13
Просмотров 1,1 млн
Heap sort in 4 minutes
4:13
Просмотров 1 млн
The hidden beauty of the A* algorithm
19:22
Просмотров 865 тыс.