Suppose each set is represented as a linked list with elements in arbitrary order. Which of the operations among will be the slowest?
View worked solution
Answer: D
Let the two sets and be represented as linked lists of sizes and respectively, with elements in arbitrary (unsorted) order.
Let's analyze the worst-case time complexity for each operation:
-
Membership: To check if an element belongs to a set of size , we perform a linear search through the linked list.
Time Complexity: -
Cardinality: To find the number of elements in a set of size , we traverse the entire list to count the nodes.
Time Complexity: -
Intersection: To find , we must check each of the elements of list against all elements of list to find common elements.
Time Complexity: -
Union: To find without duplicates, we can copy all elements of to the result list. Then, for each of the elements of , we must search the list (or the result list) to ensure it is not a duplicate before adding it.
Time Complexity:
Comparing these, union and intersection require quadratic time (assuming ), making them significantly slower than membership and cardinality, which only require linear time .
Therefore, the slowest operations are union and intersection.
Free practice · Your answers on this page are not saved.
