python - Leibniz determinant formula complexity -
i wrote code calculates determinant of given nxn matrix using leibniz formula determinants.
i trying figure out it's complexity in o-notation. think should like: o(n!) * o(n^2) + o(n) = o(n!*n^2)
or o((n+2)!)
reasoning: think o(n!)
complexity of permutations. , o(n)
complexity of perm_parity, , o(n^2)
multiplication of n items each iteration.
this code:
def determinant_leibnitz(self): assert self.dim()[0] == self.dim()[1] # o(1) dim = self.dim()[0] # o(1) det,mul = 0,1 # o(1) perm in permutations([num num in range(dim)]): in range(dim): mul *= self[i,perm[i]] # o(1) det += perm_parity(perm)*mul # o(n) ? mul = 1 # o(1) return det
the following functions wrote used in calculation:
perm_parity: given permutation of digits 0..n in order list, returns parity (or sign): +1 parity; -1 odd.
i think perm_parity should run @ o(n^2)
(is correct?).
def perm_parity(lst): parity = 1 lst = lst[:] in range(0,len(lst) - 1): if lst[i] != i: parity *= -1 mn = argmin(lst[i:]) + lst[i],lst[mn] = lst[mn],lst[i] return parity
argmin: returns index of minimal argument in list. think argmin should run @ o(n)
(is correct?)
def argmin(lst): return lst.index(min(lst))
and permutation: returns permutations of given list. e.g: input: [1,2,3], output [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]].
i think permutations should run @ o(n!)
(is correct?)
def permutations(lst): if len(lst) <= 1: return [lst] templst = [] in range(len(lst)): part = lst[:i] + lst[i+1:] j in permutations(part): templst.append(lst[i:i+1] + j) return templst
this old question still deserves answer.
the complexity looking o((n+2)!)
.
since o(n!)
complexity of this:
for perm in permutations([num num in range(dim)])
o(n)
complexity of theperm_parity
function.
o(n^2)
complexity of multiplying n
items in each iteration.
gives o(n!)*o(n)*o(n^2)=o(n!n^2)=o((n+2)!)
(and comment stated, in case ϴ((n+2)!)
)
Comments
Post a Comment