I'm taking a cs101 course and i was asked to analize the complexity of 2 code samples in python. i was having real trouble in doing so, and especially with providing explanation. the first sample:
def f1(L):
n = len(L)
while n > 0:
n = n // 2
for i in range(n):
if i in L:
L.append(i)
return L
the second sample:
def f2(L):
n = len(L)
res = []
for i in range(500, n):
m = math.floor(math.log2(i))
for j in range(m):
k=1
while k<n:
k*=2
res.append(k)
return res
With regard to the 1st sample, in my opinion it has O(n^2) beacause the first 2 loops are like geometric series with the sum of n, and the in function in the if statement has another loop with O(n). what do you think?
And about the 2nd one, here I was thinking that the 1st for loop has O(n), the 2nd for loop has O(logn) and the while loop has O(logn) too, so in overall the O of the function is: O(n*logn^2), what is you opinion?
I have a question in regard to time complexity (big-O) in Python. I want to understand the general method I would need to implement when trying to find the big-O of a complex algorithm. I have understood the reasoning behind calculating the time complexity of simple algorithms, such as a for loop iterating over a list of n elements having a O(n), or having two nested for loops each iterating over 2 lists of n elements each having a big-O of n**2. But, for more complex algorithms that implement multiple if-elif-else statements coupled with for loops, I would want to see if there is a strategy to, simply based on the code, in an iterative fashion, to determine the big-O of my code using simple heuristics (such as, ignoring constant time complexity if statements or always squaring the n upon going over a for loop, or doing something specific when encountering an else statement).
I have created a battleship game, for which I would like to find the time complexity, using such an aforementioned strategy.
from random import randint
class Battle:
def __init__(self):
self.my_grid = [[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False],[False,False,False,False,False,False,False,False,False,False]]
def putting_ship(self,x,y):
breaker = False
while breaker == False:
r1=x
r2=y
element = self.my_grid[r1][r2]
if element == True:
continue
else:
self.my_grid[r1][r2] = True
break
def printing_grid(self):
return self.my_grid
def striking(self,r1,r2):
element = self.my_grid[r1][r2]
if element == True:
print("STRIKE!")
self.my_grid[r1][r2] = False
return True
elif element == False:
print("Miss")
return False
def game():
battle_1 = Battle()
battle_2 = Battle()
score_player1 = 0
score_player2 = 0
turns = 5
counter_ships = 2
while True:
input_x_player_1 = input("give x coordinate for the ship, player 1\n")
input_y_player_1 = input("give y coordinate for the ship, player 1\n")
battle_1.putting_ship(int(input_x_player_1),int(input_y_player_1))
input_x_player_2 = randint(0,9)
input_y_player_2 = randint(0,9)
battle_2.putting_ship(int(input_x_player_2),int(input_y_player_2))
counter_ships -= 1
if counter_ships == 0:
break
while True:
input_x_player_1 = input("give x coordinate for the ship\n")
input_y_player_1 = input("give y coordinate for the ship\n")
my_var = battle_1.striking(int(input_x_player_1),int(input_y_player_1))
if my_var == True:
score_player1 += 1
print(score_player1)
input_x_player_2 = randint(0,9)
input_y_player_2 = randint(0,9)
my_var_2 = battle_2.striking(int(input_x_player_2),int(input_y_player_2))
if my_var_2 == True:
score_player2 += 1
print(score_player2)
counter_ships -= 1
if counter_ships == 0:
break
print("the score for player 1 is",score_player1)
print("the score for player 2 is",score_player2)
print(game())
If it's just nested for loops and if/else statements, you can take the approach ibonyun has suggested - assume all if/else cases are covered and look at the deepest loops (being aware that some operations like sorting, or copying an array, might hide loops of their own.)
However, your code also has while loops. In this particular example it's not too hard to replace them with fors, but for code containing nontrivial whiles there is no general strategy that will always give you the complexity - this is a consequence of the halting problem.
For example:
def collatz(n):
n = int(abs(n))
steps = 0
while n != 1:
if n%2 == 1:
n=3*n+1
else:
n=n//2
steps += 1
print(n)
print("Finished in",steps,"steps!")
So far nobody has been able to prove that this will even finish for all n, let alone shown an upper bound to the run-time.
Side note: instead of the screen-breaking
self.my_grid = [[False,False,...False],[False,False,...,False],...,[False,False,...False]]
consider something like:
grid_size = 10
self.my_grid = [[False for i in range(grid_size)] for j in range(grid_size)]
which is easier to read and check.
Empirical:
You could do some time trials while increasing n (so maybe increasing the board size?) and plot the resulting data. You could tell by the curve/slope of the line what the time complexity is.
Theoretical:
Parse the script and keep track of the biggest O() you find for any given line or function call. Any sorting operations will give you nlogn. A for loop inside a for loop will give you n^2 (assuming their both iterating over the input data), etc. Time complexity is about the broad strokes. O(n) and O(n*3) are both linear time, and that's what really matters. I don't think you need to worry about the minutia of all your if-elif-else logic. Maybe just focus on worst case scenario?
import math
import sys
sys.setrecursionlimit(8000000)
f = {1:2,2:3,3:5}
def fib(n):
if n in f:
return f[n]
if n == 1:
return 2
if n == 2:
return 3
if n == 3:
return 5
val = fib(n-1) + fib(n-2)
if n not in f:
f[n] = val
return f[n]%1000000007
print(fib(4000))
This code fails to complete / command prompt crashes. How can I make this better?
Is there any setting that I need to enable to make this program complete?
Implementing the Fibonacci sequence directly from the mathematical definition is an exercise that illustrates problems with recursive solutions. It leads to an exponential explosion of recursive function calls that even modern computers cannot handle. The biggest problem is that for large values of n, you will calculate fib(1) an exponential number of times.
There are several solutions to this problem:
Use memoization to store values that have already been calculated. Then you look up the calculated value and return it immediately without doing any further calculations. This is a good exercise to learn how memoization works. However, it is still inefficient because you still unnecessarily execute recursive function calls.
Implement an iterative solution. I'm not going to get into the details here. I suggest you do some research to find the iterative solution that will implement fib(n) in linear time instead of exponential time.
Implement the closed formula. Mathematicians have already solved fib(n) as a closed formula. This solution will take constant time no matter how large of an n you use.
use automatic memorization of old vales so that it won't go into infinity loop.use lru_cache as a decorator on your function.
import sys
from functools import lru_cache
sys.setrecursionlimit(8000000)
f = {1:2,2:3,3:5}
#lru_cache(maxsize=None)
def fib(n):
if n in f:
return f[n]
if n == 1:
return 2
if n == 2:
return 3
if n == 3:
return 5
val = fib(n-1) + fib(n-2)
if n not in f:
f[n] = val
return f[n]%1000000007
print(fib(4000))
I think there are some process for calculating pow(a,nCr)%b?
But I want to know how I can efficiently solve this problem in programming?
I can think of a way that's only ~O(n^2*log(m)) and doesn't require the use of big integers. I have a notion of a faster (something like O(k*log(m)*log(n))) approach if you can factor m and you can factor totient(m/gcd(a^k,m)) for some k, but it gets pretty hairy.
In any case for the O(n^2*log(m)) approach, we'll make use of the fact that nCr == (n-1)C(r-1) + (n-1)Cr
Here's a computation of nCr which exploits that:
def nCr(n0,r0):
memoized = {}
def go(n,r):
if r == 0 or r == n:
return 1
if (n,r) not in memoized:
memoized[(n,r)] = go(n-1,r-1) + go(n-1,r)
return memoized[(n,r)]
return go(n0,r0)
The code for your powChooseMod function would be almost identical:
def powChooseMod(a,n0,r0,m):
memoized = {}
def go(n,r):
if r == 0 or r == n:
return a
if (n,r) not in memoized:
memoized[(n,r)] = go(n-1,r-1) * go(n-1,r) % m
return memoized[(n,r)]
return go(n0,r0)