I am learning graph theory, and I found a problem Shortest Route II on CSES website. https://cses.fi/problemset/task/1672/ is the problem. I found the problem could be solved using Floyd Warshall Algorithm and the time complexity for the algorithm is O(n^3). I am confused because here the constraints are N<=500, Time limit: 1s, so when N=500 the computations should be 500^3 = 125,000,000 and keeping in mind that in 1 second, 10^8 computations are performed, therefore it should at least take 1.25 seconds causing TLE, but the solution gets accepted. I want to learn what I am doing wrong in my calculations or understanding.
Asked
Active
Viewed 190 times
0
-
Keep in mind that the big O notation is worst case. Also, any scalar values are ignored complexity-wise (I.E O(2 * n^2) == O(n^2) == O(0.1 * n^2)). I don't know if that is the solution to your problem though. – Schottky Sep 07 '21 at 20:18
-
@Schottky The algorithm uses 3 nested loops and each loop iterates from 1 to N, so it results in O(n^3) time complexity and I am pretty sure about it. – Pioneer Sep 08 '21 at 07:57
-
Your problem is that you assume `10^8 computations are performed`. Have you actually run the code and counted the computations? – Guy Coder Sep 08 '21 at 10:33
-
@GuyCoder I checked running a specific test case which on the CSES website took **0.97 seconds** (N = 500 in this test case) and on my computer, it takes **1277ms or 1.277 seconds** which is close enough to my estimated calculation. Note: I use java. – Pioneer Sep 08 '21 at 13:20
-
The comparison is done 500^3 times ignoring the fact the graph is connected or not. The logic @GuyCoder : Assuming **10^8** computations cost **1 second.** So let's say for **10^9** computations, we can rewrite as, **10 x 10^8** computations which results **10 X 1 seconds.** Similarly, **125,000,000** is equal to **1.25 X 10^8** which results in **1.25 X 1 seconds.** I found many tutorials where they just did this problem with the Floyd-Warshall algorithm and made me think why the solution is getting accepted when on my computer it takes an estimated time of 1.27 s. – Pioneer Sep 08 '21 at 14:27
-
Why do you think that the computer behind _the CSES website_ couldn't do more _computations_ per _second_ than your _computer_? – Armali Sep 13 '21 at 19:41
-
Please provide enough code so others can better understand or reproduce the problem. – Community Sep 13 '21 at 22:06