python - Space & Time Complexity of Three-way Set Disjointness -
i'm looking feedback on space , time complexity of work.
here's problem: suppose given 3 sets stored in 3 lists of integers. 3 lists may contain different numbers of elements. determine whether intersection of 3 sets empty.
i implemented following helper function identify shortest list.
def find_shortest_list(lists): """returns 2-tuple: shortest list , list of larger lists.""" shortest_list = lists[0] longer_lists = [] in range(1, len(lists)): if len(lists[i]) < len(shortest_list): longer_lists.append(shortest_list) shortest_list = lists[i] else: longer_lists.append(lists[i]) return shortest_list, longer_lists
if let n represent number of lists, think runs in o(n) time. if understand correctly, space complexity o(1) because i'm storing references; however, not certain. are guesses correct?
i implemented following function determine whether given sets disjoint. sake of example, let's assume list_a has smallest number of elements , let's represent number a. similarly, let b , c represent number of elements in list_b , in list_c, respectively.
def are_disjoint4(list_a, list_b, list_c): """returns true if sets disjoint , false otherwise.""" shortest_list, longer_lists = find_shortest_list([list_a, list_b, list_c]) # assuming list_a shortest list, o(b) set1 = set(longer_lists[0]) # assuming list_a shortest list, o(c) set2 = set(longer_lists[1]) # assuming list_a shortest list, o(a) num in shortest_list: if num in set1 , num in set2: return false return true
given number of given lists 3 in scenario, on average, think runs in o(b) time if b > c or in o(c) time if b < c. in worst-case scenario, below lines make running time o(ab) or o(ac)?
for num in shortest_list: if num in set1 , num in set2:
regarding space, create 2 sets take o(b) , o(c) space, think space complexity o(b+c). once again, if provide feedback on guesses, helpful!
Comments
Post a Comment