G-42. Floyd Warshall Algorithm

  Рет қаралды 178,384

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
---------------------------------------------------------------------------------------------------------------------------

Пікірлер: 235
@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 11 ай бұрын
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?
@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*
@king-akash1479
@king-akash1479 11 ай бұрын
I really like the prompt that comes in which a message comes explaining what we should focus on. :)
@priyanshkumariitd
@priyanshkumariitd Ай бұрын
Yeah, very helpful
@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!
@darshika8808
@darshika8808 Жыл бұрын
understood! thanks Striver, the explanation is 🔥
@andresfgalvis2010
@andresfgalvis2010 Жыл бұрын
I can´t say enough, how much i love your videos
@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.😊
@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🤩
@cinime
@cinime Жыл бұрын
Understood! Such a fantastic explanation as always, thank you very much!
@mannumichel1925
@mannumichel1925 Жыл бұрын
Understood and I too understood your effort which is incredible.
@shikharsrivastava8600
@shikharsrivastava8600 8 ай бұрын
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 Ай бұрын
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 9 күн бұрын
@@user-kl3qv1sc8k in case of negative edges followed by an 1e9 edge. this will pose problem. so i think if condition should be proposed
@MusaafirSonu
@MusaafirSonu 2 ай бұрын
After watching lots of video , I can only understand this video . Thank you Striver .❤
@chidam333
@chidam333 3 ай бұрын
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 🔥
@saurabhsangam2737
@saurabhsangam2737 11 ай бұрын
5:46 The equation should be minimum of (d [I] [k] + d [k] [j] ) for calculating for path I => k => j
@CodeMode9313
@CodeMode9313 11 ай бұрын
Habibi katai bawaal macha rakhe ho ...1 number.chiz hai ye toh
@user-jc5vo9sz3l
@user-jc5vo9sz3l Жыл бұрын
A very fantastic explanation ❤
@stith_pragya
@stith_pragya 7 ай бұрын
Thank You So Much for this wonderful video..............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@sukhpreetsingh5200
@sukhpreetsingh5200 Жыл бұрын
understood just awesome explanation
@AYUSHKUMAR-xj4wc
@AYUSHKUMAR-xj4wc Жыл бұрын
Awesome Explanation❤❤❤
@user-yi4gx5zj9q
@user-yi4gx5zj9q 10 ай бұрын
Amazing explanation!
@arghyadeepchakraborty1977
@arghyadeepchakraborty1977 Жыл бұрын
Great explanation!
@herculean6748
@herculean6748 Жыл бұрын
Thank you striver!
@IK-xk7ex
@IK-xk7ex 11 ай бұрын
Awesome. thank you for explanation
@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.
@rohinianekar61
@rohinianekar61 Жыл бұрын
understood, nice video, well explained
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@Chandraprakash-kx4ic
@Chandraprakash-kx4ic Жыл бұрын
love u striver...Thanku sooo much
@user-gc2fy1jb6p
@user-gc2fy1jb6p Жыл бұрын
striver understood!!!! thankyou!!!👍
@GyaanGrave
@GyaanGrave Жыл бұрын
The voice editing is OP💥
@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.
@srinayan390
@srinayan390 Жыл бұрын
u r the true gem🤩
@princepal8523
@princepal8523 Жыл бұрын
amazing explanation *understood*
@venkateshvenky2880
@venkateshvenky2880 Жыл бұрын
Good explanation
@udaytewary3809
@udaytewary3809 Жыл бұрын
Understood bhaiya 🙏❤️
@AyushiPanday12
@AyushiPanday12 Жыл бұрын
Understood bhaiya!!
@original_gangsta_
@original_gangsta_ Жыл бұрын
Understood 🔥🔥
@TarunKumar-jg4jz
@TarunKumar-jg4jz Жыл бұрын
Is it not possible to apply "bellman ford" for all the vertices and store in 2d array for result. ??
@UECAshutoshKumar
@UECAshutoshKumar 7 ай бұрын
Thank you sir 🙏
@ShivamKendre-fc3su
@ShivamKendre-fc3su 6 ай бұрын
Thank you so much
@abhishek__anand__
@abhishek__anand__ Жыл бұрын
Great Video
@harshitsoni9103
@harshitsoni9103 Жыл бұрын
24:24 :that evil smile @takeUforward😁😂
@afredhussain7042
@afredhussain7042 5 ай бұрын
understood 💯
@p38_amankuldeep75
@p38_amankuldeep75 Жыл бұрын
understood🔥🔥
@DreamLife-05
@DreamLife-05 Жыл бұрын
Understood 🙌💯
@pavankaranam7469
@pavankaranam7469 Жыл бұрын
best.PERIOD.
@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
@its_my_type
@its_my_type Ай бұрын
Understood ❤❤
@ankitmeena5637
@ankitmeena5637 Жыл бұрын
hats off to you bhaiya
@destructaphoenix6679
@destructaphoenix6679 Жыл бұрын
My favorite algo
@user-qz7tx5ce7e
@user-qz7tx5ce7e 7 ай бұрын
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???
@mdshohanurrahman4998
@mdshohanurrahman4998 Жыл бұрын
understood💚
@user-lw9dj8we7k
@user-lw9dj8we7k 8 ай бұрын
Understood Sir
@shreyashachoudhary480
@shreyashachoudhary480 Жыл бұрын
Amazing!
@user-tk2vg5jt3l
@user-tk2vg5jt3l Ай бұрын
Thank you bhaiya
@garimakumari4346
@garimakumari4346 Жыл бұрын
thanks man
@sauravfarkade7032
@sauravfarkade7032 Ай бұрын
Understood!!
@aditya_raj7827
@aditya_raj7827 4 ай бұрын
understood sir ❤❤
@lonewolf-_-8634
@lonewolf-_-8634 Жыл бұрын
UNDERSTOOD
@anmolkaushik4576
@anmolkaushik4576 Жыл бұрын
Understood!
@harshdasila6680
@harshdasila6680 Жыл бұрын
understooodddd
@-VLaharika
@-VLaharika Жыл бұрын
Understood 👍
@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 10 ай бұрын
​@@ayushrai6699yes you're right don't worry
@gautamsaxena4647
@gautamsaxena4647 2 ай бұрын
understood bhaiya
@ankitpathak1604
@ankitpathak1604 9 ай бұрын
understood!
@ACUCSPRADEEPB-up9ne
@ACUCSPRADEEPB-up9ne Жыл бұрын
Understood✌️
@alokprasad6260
@alokprasad6260 Жыл бұрын
Understood, any problem using this algorithm?
@vaibhavagarwal1479
@vaibhavagarwal1479 6 ай бұрын
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
@namamiNarayana
@namamiNarayana Жыл бұрын
Understood Sir! :) Thank you! _/\_ ^^
@ROKADEADITYA
@ROKADEADITYA Жыл бұрын
Understanding
@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?
@manasranjanmahapatra3729
@manasranjanmahapatra3729 Жыл бұрын
Understood.
@venutalla5932
@venutalla5932 Жыл бұрын
Understood
@sumitchakrabortystudy651
@sumitchakrabortystudy651 Жыл бұрын
understood
@vaibhavverma6120
@vaibhavverma6120 4 ай бұрын
why we are using (via) loop first i.e for every i,j check for via k first ...... why cant we check for a particular i,j with all the vias first and then move to next i,j(means we take k loop as the third loop instead of first loop)?
@priyanshvatsal9791
@priyanshvatsal9791 Жыл бұрын
Understood 😇
@abdullah2509
@abdullah2509 Жыл бұрын
understood.
@AmanGupta-ib5ss
@AmanGupta-ib5ss Жыл бұрын
understood :)
@kashviagrawal4553
@kashviagrawal4553 2 ай бұрын
understood😃😃
@ajaybabupatel1665
@ajaybabupatel1665 Жыл бұрын
understood ++, Addicted bruhh with graph playlist, Thanks a lot @raj
@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.
@pratyushpandey6139
@pratyushpandey6139 Жыл бұрын
why if == -1 ? condition not get it
@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
@scaffgaming
@scaffgaming 25 күн бұрын
Why the code not giving integer overflow in the min function whenever adding with 1e9 plus something
@heatedpants8437
@heatedpants8437 13 күн бұрын
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.
@vaalarivan_p
@vaalarivan_p Жыл бұрын
1:10 - 16:00 23:00 - 24:35
@snehamaurya3659
@snehamaurya3659 5 ай бұрын
what about the conditions where infinity is added to infinity and gives negative result ?
@lakshmanchandukondreddy1720
@lakshmanchandukondreddy1720 3 ай бұрын
To avoid that we have to if condition like this if(matrix[i][k]!=1e9 and matrix[k][j]!=1e9)
@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
@durgeshjadhav01
@durgeshjadhav01 23 күн бұрын
nice
@manishpandey2725
@manishpandey2725 Жыл бұрын
understod
@KratosProton
@KratosProton Жыл бұрын
great
@bbcs6392
@bbcs6392 Жыл бұрын
Can I implement floyad warshall using adjacency list.If yes tell me how with proper explanation
@saitejadamaraju5408
@saitejadamaraju5408 Жыл бұрын
convert it into adjacency matrix
@shubhampitale1067
@shubhampitale1067 Жыл бұрын
So what I basically did was to apply dijkstra algorithm to find the shortest path for a source. We take all the vertices as source one by one and apply dijkstra algorithm for it. Here is code for your reference. int n=matrix.size(); vectoradj[n]; for(int i=0;i
@soumm0
@soumm0 Жыл бұрын
understand
@rohitkumar-dc3xb
@rohitkumar-dc3xb 26 күн бұрын
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.....
@akashsahu2571
@akashsahu2571 Жыл бұрын
yes
@manoranjankumarjha3776
@manoranjankumarjha3776 Жыл бұрын
🔥🔥
@muskangupta7522
@muskangupta7522 Жыл бұрын
Hi , what if i make my loop like this , For ( i loop ) For ( j loop ) For ( intermediate loop) ..i am getting wrong ans if i make loop like this , can you tell why intermediate loop should be on first , acc to me it can be at last also.
@piyushrajput0_
@piyushrajput0_ Жыл бұрын
I also have same doubt if you know its answer then please reply
@hemendrasrivastava4634
@hemendrasrivastava4634 Жыл бұрын
If you think about it, you are actually changing the algorithm by doing this. In the original algorithm you are updating the matrix for each intermediate vertex one by one. In your case you are going to update each node for each via at one go which is actually a different algorithm. Think of it like bellman ford, the changes take n steps to propogate to all nodes. You are updating each node at one go without the changes propogating
@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
@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 29 күн бұрын
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.
G-41. Bellman Ford Algorithm
27:43
take U forward
Рет қаралды 183 М.
Playing hide and seek with my dog 🐶
00:25
Zach King
Рет қаралды 32 МЛН
One moment can change your life ✨🔄
00:32
A4
Рет қаралды 34 МЛН
НРАВИТСЯ ЭТОТ ФОРМАТ??
00:37
МЯТНАЯ ФАНТА
Рет қаралды 2,8 МЛН
G-46. Disjoint Set | Union by Rank | Union by Size | Path Compression
42:15
Coding Interviews Be Like
5:31
Nicholas T.
Рет қаралды 6 МЛН
Google Data Center 360° Tour
8:29
Google Cloud Tech
Рет қаралды 5 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 362 М.
Warshall's Algorithm (Finding the Transitive Closure)
9:46
Neso Academy
Рет қаралды 248 М.
4.2 All Pairs Shortest Path (Floyd-Warshall) - Dynamic Programming
14:13
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 625 М.