G-42. Floyd Warshall Algorithm

  Рет қаралды 189,167

take U forward

take U forward

Жыл бұрын

GfG-Problem Link: bit.ly/3JZlk4a
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
---------------------------------------------------------------------------------------------------------------------------

Пікірлер: 249
@takeUforward
@takeUforward Жыл бұрын
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
@namratam1522
@namratam1522 Жыл бұрын
Thank you soooooo much!!! Understood!!!!!❤❤
@sairam3351
@sairam3351 Жыл бұрын
di[i][[j] = Min(d[i][k]+d[k][j]) right ?,u said d[j][k] dont know why
@AJ-xc3ks
@AJ-xc3ks 8 ай бұрын
@@sairam3351 min(d[i][k] + d[k][j]) is right..
@shikharshiromani2728
@shikharshiromani2728 Жыл бұрын
At 5:39, I think it should be min(d[i][k] + d[k][j])
@takeUforward
@takeUforward Жыл бұрын
Yes, a small typo..
@saranghae3720
@saranghae3720 Жыл бұрын
Hey, Striver, Pin this comment or write this in ur already pinned comment.
@AbhrajyotiKundu00
@AbhrajyotiKundu00 Жыл бұрын
Yes, this is how addiction is built over Algorithms! Thank you Striver :)
@anonymousanonymous7507
@anonymousanonymous7507 7 ай бұрын
Job lgi?
@ITACHIUCHIHA-dr8sz
@ITACHIUCHIHA-dr8sz 11 күн бұрын
@@anonymousanonymous7507 savage!!
@mohanjha4397
@mohanjha4397 4 ай бұрын
People say Graph is a difficult Topic, but after going through striver's i haven't faced any problem.so you are a genius.😊
@saurabhsangam2737
@saurabhsangam2737 11 ай бұрын
5:46 The equation should be minimum of (d [I] [k] + d [k] [j] ) for calculating for path I => k => j
@andresfgalvis2010
@andresfgalvis2010 Жыл бұрын
I can´t say enough, how much i love your videos
@The_Shubham_Soni
@The_Shubham_Soni 10 ай бұрын
People say Graph is a difficult Topic, but after going through striver's series i haven't faced any problem. Moreover i find it Super Easy🤩
@darshika8808
@darshika8808 Жыл бұрын
understood! thanks Striver, the explanation is 🔥
@lavanyam3224
@lavanyam3224 4 ай бұрын
I feel like sitting in a maths class. Amazing explanation! You made a complex algorithm look very simple. Thanks a ton Striver!
@mannumichel1925
@mannumichel1925 Жыл бұрын
Understood and I too understood your effort which is incredible.
@technologicalvivek7510
@technologicalvivek7510 8 күн бұрын
Best graph playlist on youtube. You are the great sir.
@shikharsrivastava8600
@shikharsrivastava8600 9 ай бұрын
Hey striver, the way you explained was quite commendable. Just at 26:58 , we should add extra if(matrix[i][k] != 1e9 && matrix[k][j] != 1e9) condition to make sure we don’t go via unreachable nodes.
@user-kl3qv1sc8k
@user-kl3qv1sc8k 2 ай бұрын
No need for it since we are taking min of both so even if all of them are 1e9 the result will still be 1e9
@ashurajput6916
@ashurajput6916 20 күн бұрын
@@user-kl3qv1sc8k in case of negative edges followed by an 1e9 edge. this will pose problem. so i think if condition should be proposed
@king-akash1479
@king-akash1479 11 ай бұрын
I really like the prompt that comes in which a message comes explaining what we should focus on. :)
@priyanshkumar_iitd
@priyanshkumar_iitd 2 ай бұрын
Yeah, very helpful
@visase2036
@visase2036 Жыл бұрын
Amazing teaching as usual Striver. If you continue to teach this depth in Graph , you will soon device your own algorithm one day for sure.
@PrabhatSingh-8533
@PrabhatSingh-8533 Жыл бұрын
devise*
@cinime
@cinime Жыл бұрын
Understood! Such a fantastic explanation as always, thank you very much!
@MusaafirSonu
@MusaafirSonu 3 ай бұрын
After watching lots of video , I can only understand this video . Thank you Striver .❤
@chidam333
@chidam333 4 ай бұрын
thank you so much ! i was panicking while revising as i couldn't get the intuition properly when revising!! Your 22:48 footnote was really helpful
@Purvilicious
@Purvilicious 10 ай бұрын
Can't thank u enough! Striver 🔥
@mayankkumar6997
@mayankkumar6997 Жыл бұрын
A small doubt at the last, if we apply dijkstra algorithm for finding all pair shortest path, then time complexity is = V E logV. But if it is complete graph then E approximate V^2. So final time complexity becomes V^3 logV, then how its better from floyd warshall algorithm!
@takeUforward
@takeUforward Жыл бұрын
Usually you don't get complete graphs always..
@srijankhatri4689
@srijankhatri4689 Ай бұрын
I agree with mayank here. If you try to solve the ‘Find the city with smallest neighbours in a threshold’ leetcode problem using Dijkstra’s using set, the time taken is much higher than a simple Floyd warshal solution. Solving it through PQ gives a TLE.
@user-jc5vo9sz3l
@user-jc5vo9sz3l Жыл бұрын
A very fantastic explanation ❤
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
understood just awesome explanation
@stith_pragya
@stith_pragya 7 ай бұрын
Thank You So Much for this wonderful video..............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@CodeMode9313
@CodeMode9313 11 ай бұрын
Habibi katai bawaal macha rakhe ho ...1 number.chiz hai ye toh
@arghyadeepchakraborty1977
@arghyadeepchakraborty1977 Жыл бұрын
Great explanation!
@user-yi4gx5zj9q
@user-yi4gx5zj9q 11 ай бұрын
Amazing explanation!
@AYUSHKUMAR-xj4wc
@AYUSHKUMAR-xj4wc Жыл бұрын
Awesome Explanation❤❤❤
@IK-xk7ex
@IK-xk7ex 11 ай бұрын
Awesome. thank you for explanation
@rohinianekar61
@rohinianekar61 Жыл бұрын
understood, nice video, well explained
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@user-qz7tx5ce7e
@user-qz7tx5ce7e 8 ай бұрын
Hey Striver, i have an doubt the graph you use here,ends with infinity(in the final matrix) so does that mean there is no shortest path for such nodes???
@herculean6748
@herculean6748 Жыл бұрын
Thank you striver!
@user-gc2fy1jb6p
@user-gc2fy1jb6p Жыл бұрын
striver understood!!!! thankyou!!!👍
@Chandraprakash-kx4ic
@Chandraprakash-kx4ic Жыл бұрын
love u striver...Thanku sooo much
@preetkatiyar969
@preetkatiyar969 Жыл бұрын
THIS IS REAL THAT SOMETHING WE WANT AND I GURENTEED NO ONE CAN TEACH BETTER THAN THIS EVEN IN PAID COURSES OF LAKHS.
@princepal8523
@princepal8523 Жыл бұрын
amazing explanation *understood*
@fatmayasser8188
@fatmayasser8188 10 күн бұрын
well explained thank you!
@srinayan390
@srinayan390 Жыл бұрын
u r the true gem🤩
@TarunKumar-jg4jz
@TarunKumar-jg4jz Жыл бұрын
Is it not possible to apply "bellman ford" for all the vertices and store in 2d array for result. ??
@GyaanGrave
@GyaanGrave Жыл бұрын
The voice editing is OP💥
@venkateshvenky2880
@venkateshvenky2880 Жыл бұрын
Good explanation
@vaibhavagarwal1479
@vaibhavagarwal1479 7 ай бұрын
Hi! need help with the Space complexity Here striver mentioned that space complexity is n3(n*n*n) meaning we consider the space we use in solving the problem which here is the original matrix,lets take an example of bubble sort there also we use the same array to sort it, no other array is considered to solve but there the time complexity is not n but o(1) ? why is that am i missing onto something.. please reply if you have any clue.. Thankyou
@jritzeku
@jritzeku 4 ай бұрын
INTUITION(correct me if im wrong): We can think of 'via' as an intermediate node we must hop thru to get to destination( kind of like connecting flight(the second plane)). Therefore, after performing via 'nth' node, we have found shortest paths for node 'nth-1' as source where it touches nth node as intermediate node. Ex:After processing via node1, we were able to update shortest path from node0 to node2 because node1 as intermediate offered shorter path. See @17:10
@UECAshutoshKumar
@UECAshutoshKumar 7 ай бұрын
Thank you sir 🙏
@original_gangsta_
@original_gangsta_ Жыл бұрын
Understood 🔥🔥
@udaytewary3809
@udaytewary3809 Жыл бұрын
Understood bhaiya 🙏❤️
@ShivamKendre-fc3su
@ShivamKendre-fc3su 7 ай бұрын
Thank you so much
@ayushrai6699
@ayushrai6699 Жыл бұрын
(V*E * logV) > (V^3)... right? As in worst case E is V*(V-1). So Floyd Warshall is better than Dijkstras for shortest path to all the nodes from multiple source in contrast to what striver said at 29:08
@takeUforward
@takeUforward Жыл бұрын
E is V x V-1 is a very very very rare and dense graph..
@ayushrai6699
@ayushrai6699 Жыл бұрын
@@takeUforward Agreed but in interview we are supposed to give worst case time complexity... right? Or am I missing something?
@ravisingh-el8np
@ravisingh-el8np 11 ай бұрын
​@@ayushrai6699yes you're right don't worry
@abhishek__anand__
@abhishek__anand__ Жыл бұрын
Great Video
@AyushiPanday12
@AyushiPanday12 Жыл бұрын
Understood bhaiya!!
@afredhussain7042
@afredhussain7042 6 ай бұрын
understood 💯
@Anurag_Badwahe
@Anurag_Badwahe 10 күн бұрын
I am thinking if i apply djikstra for all edges then the time complexity will be O(V.ElogV) . I am thinking it is better than O(V^³). Please correct me
@p38_amankuldeep75
@p38_amankuldeep75 Жыл бұрын
understood🔥🔥
@pavankaranam7469
@pavankaranam7469 Жыл бұрын
best.PERIOD.
@DreamLife-05
@DreamLife-05 Жыл бұрын
Understood 🙌💯
@ankitmeena5637
@ankitmeena5637 Жыл бұрын
hats off to you bhaiya
@siddharthkardam321
@siddharthkardam321 Жыл бұрын
striver my data is lost on your website after my mac has updated. I have made important notes on your website. What should I do now?
@its_my_type
@its_my_type 2 ай бұрын
Understood ❤❤
@mdshohanurrahman4998
@mdshohanurrahman4998 Жыл бұрын
understood💚
@banothutharun2743
@banothutharun2743 3 күн бұрын
understood brother
@user-lw9dj8we7k
@user-lw9dj8we7k 8 ай бұрын
Understood Sir
@shreyashachoudhary480
@shreyashachoudhary480 Жыл бұрын
Amazing!
@psionl0
@psionl0 11 күн бұрын
I know it is an old debate whether modifying the input counts as extra space but if you are instructed to do the solution IN-PLACE then I would argue that it shouldn't count as extra space. For example, Sorting is almost always done in-place and you never see the space complexity analysis include the size of the list that is being sorted.
@aditya_raj7827
@aditya_raj7827 4 ай бұрын
understood sir ❤❤
@anmolkaushik4576
@anmolkaushik4576 Жыл бұрын
Understood!
@garimakumari4346
@garimakumari4346 Жыл бұрын
thanks man
@user-tk2vg5jt3l
@user-tk2vg5jt3l 2 ай бұрын
Thank you bhaiya
@sauravfarkade7032
@sauravfarkade7032 Ай бұрын
Understood!!
@harshitsoni9103
@harshitsoni9103 Жыл бұрын
24:24 :that evil smile @takeUforward😁😂
@destructaphoenix6679
@destructaphoenix6679 Жыл бұрын
My favorite algo
@-VLaharika
@-VLaharika Жыл бұрын
Understood 👍
@vardhamanbhandari5644
@vardhamanbhandari5644 Жыл бұрын
understood!
@harshdasila6680
@harshdasila6680 Жыл бұрын
understooodddd
@alokprasad6260
@alokprasad6260 Жыл бұрын
Understood, any problem using this algorithm?
@VishaliniT-f3g
@VishaliniT-f3g 3 күн бұрын
Understood 👍☺️
@lonewolf-_-8634
@lonewolf-_-8634 Жыл бұрын
UNDERSTOOD
@user-fs6qz7be2z
@user-fs6qz7be2z Жыл бұрын
Another point: Even if all the weights are positive, why Dijkstra might not be preferred over Floyd Warshall is because for Dijkstra, time complexity is V * E * log(V) For dense graphs, E ~ V^2 since E = V (V-1)/2 for a fully connected graph Making the time complexity V^3 * log(V) Tell me if I interpreted that correctly.
@vibhorgupta641
@vibhorgupta641 Жыл бұрын
yes
@pulkitgupta669
@pulkitgupta669 11 ай бұрын
Bro Djikstra time Complexity is O(V + E)Log V and if there is a dense graph then E = V^2 that makes it O(V^2 LogV) which is better than Floyd Warshall
@CEBMANURBHAVARYA
@CEBMANURBHAVARYA 11 ай бұрын
@@pulkitgupta669 bro, we are doing dijkstra for all the nodes so, time complexity will be V * whatever u calculated, which is at worst V^3 log(V), whereas floyd warshall has V^3
@pulkitgupta669
@pulkitgupta669 11 ай бұрын
@@CEBMANURBHAVARYA Yeah sorry I miscalculated. Thank you for helping.
@ACUCSPRADEEPB-up9ne
@ACUCSPRADEEPB-up9ne Жыл бұрын
Understood✌️
@ROKADEADITYA
@ROKADEADITYA Жыл бұрын
Understanding
@Ayeshasingh720
@Ayeshasingh720 11 күн бұрын
understood😊😊
@gautamsaxena4647
@gautamsaxena4647 2 ай бұрын
understood bhaiya
@manasranjanmahapatra3729
@manasranjanmahapatra3729 Жыл бұрын
Understood.
@scaffgaming
@scaffgaming Ай бұрын
Why the code not giving integer overflow in the min function whenever adding with 1e9 plus something
@heatedpants8437
@heatedpants8437 25 күн бұрын
When two numbers that are close to INT_MAX are added, the result can exceed the maximum value that can be stored in an int, causing overflow. Overflow wraps around to negative values, which would then be incorrectly considered as a shorter path. Using 1e9 even if you add two paths with this maximum value, you get 2e9, which is still within the range of a 32-bit integer.
@priyanshvatsal9791
@priyanshvatsal9791 Жыл бұрын
Understood 😇
@advaithprasad2535
@advaithprasad2535 2 ай бұрын
Hi, can you please explain with an example why the "via" or the "k" loop must be outside, and won't work if it is inside? because i tried this code ''' for i in range(n): for j in range(n): for k in range(n): d[i][j] = min(d[i][j], d[i][k]+d[k][j]) ''' and it gives wrong answer but ''' for k in range(n): for i in range(n): for j in range(n): d[i][j] = min(d[i][j], d[i][k]+d[k][j]) ''' gives correct answer I am very confused. Please help! Thanks in advance :)
@harshith4549
@harshith4549 Ай бұрын
you didn't watched the lecture right!! you would have understood it right away if yes. So hear me out, the d[i][j] = min(d[i][j], d[i][k]+d[k][j]) formula you are calculating is done for the node i to node j via k. In the the first algo you are changing the k(the node you are passing through to reach j) which will use shorter distances that are not calculated yet. If you do a dry run on second algo, you will understand that d[i][k] and d[k][j] values are constant for that iteration of k which is not the case in the first algo. Not understanding!! Just take a pen and paper and do a dry run.
@harshitjaiswal9439
@harshitjaiswal9439 4 күн бұрын
understood.
@sanamdeepsingh7914
@sanamdeepsingh7914 Жыл бұрын
Understood
@vanshikakumari5744
@vanshikakumari5744 Ай бұрын
understood
@AmanGupta-ib5ss
@AmanGupta-ib5ss Жыл бұрын
understood :)
@vishalbairagi7238
@vishalbairagi7238 24 күн бұрын
my code is not working when i use INT_MAX when node is unreachable, but same code is working when i use 1e9 in gfg.
@ajaybabupatel1665
@ajaybabupatel1665 Жыл бұрын
understood ++, Addicted bruhh with graph playlist, Thanks a lot @raj
@KunalSingh-kn2ij
@KunalSingh-kn2ij Жыл бұрын
why the code is giving wrong output if we are using INT_MAX instead of 1e9?
@shivasai7707
@shivasai7707 Жыл бұрын
Out of bounds
@googlepay4295
@googlepay4295 Жыл бұрын
even if you add +1 to int max it goes out of bounds and produces error . hence always use 1e9 or 1e8 for graph algos
@kashviagrawal4553
@kashviagrawal4553 3 ай бұрын
understood😃😃
@pratyushpandey6139
@pratyushpandey6139 Жыл бұрын
why if == -1 ? condition not get it
@rohitkumar-dc3xb
@rohitkumar-dc3xb Ай бұрын
One thing i don't get it u say even if we use same matrix still we are using O(n*n) time complexity, but in array problems if we try to find the answer using same array without using any extra space we say TC is O(1). Isn't it contracdicting ? Btw nice explaination.....
@gouthamikarshikalam4037
@gouthamikarshikalam4037 Жыл бұрын
why am i getting wrong output if iam using INT_MAX instead of 1e9??
@takeUforward
@takeUforward Жыл бұрын
Because INT_MAX. + a valuue > INT_MAX and cannot be stored in int
@namamiNarayana
@namamiNarayana Жыл бұрын
Understood Sir! :) Thank you! _/\_ ^^
@manishpandey2725
@manishpandey2725 Жыл бұрын
understod
@dishant930
@dishant930 Жыл бұрын
Love your content striver just want to tell you forgot to write the if statement -: if(matrix[i][k] != 1e9 && matrix[k][j]!=1e9) before matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k][j]); If we dono't add this condition then if fail for below test case For Input: 3 0 -1 -1 -1 0 -2 -1 -1 0 Your Output: 0 -1 999999998 999999998 0 -2 -1 -1 0 Expected Output: 0 -1 -1 -1 0 -2 -1 -1 0
@gaganagarwal7218
@gaganagarwal7218 Жыл бұрын
void shortest_distance(vector&mat){ int n = mat.size(); for(int via=0; via
@KratosProton
@KratosProton Жыл бұрын
great
@aniruddhapaul6728
@aniruddhapaul6728 8 ай бұрын
where is the mathematical proof, that the approach gives the correct ans?
How to start a Career in Data Science - [Hindi] - Quick Support
9:17
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 36 МЛН
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 19 МЛН
Задержи дыхание дольше всех!
00:42
Аришнев
Рет қаралды 3,7 МЛН
Fast and Furious: New Zealand 🚗
00:29
How Ridiculous
Рет қаралды 45 МЛН
Day 120 Of Doing Codeforces Everyday Until I Reach Expert
1:11:36
Algo Wizard
Рет қаралды 2,6 М.
G-41. Bellman Ford Algorithm
27:43
take U forward
Рет қаралды 190 М.
Why The Olympics Banned Nike
9:57
Athletic Interest
Рет қаралды 276 М.
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
Why "pop-up" restaurants are everywhere now
6:05
Vox
Рет қаралды 237 М.
Indonesia’s $33B Capital Relocation Plan Is Imploding | WSJ Breaking Ground
7:11
The Wall Street Journal
Рет қаралды 623 М.
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 36 МЛН