Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Note: A pair<T1, T2> is a simple struct with two data members, one of type T1 an

ID: 3846812 • Letter: N

Question

Note: A pair<T1, T2> is a simple struct with two data members, one of type T1 and one of type T2. A set<K> and a map<K, V> are organized as binary search trees; anunordered_set<K> and an unordered_map<K, V> are organized as hash tables that never allow the load factor to exceed some constant, and a loop that visits every item in a hash table of N items is O(N).

Suppose UCLA has C courses each of which has on average S students enrolled. For this problem, courses are represented by strings (e.g. "CS 32"), and students by their int UIDs. We will consider a variety of data structures, and for each determine the big-O time complexity of the appropriate way to use that data structure to determine whether a particular student s is enrolled in course c. For example, if the data structure were vector<pair<string, vector<int>>>, where each pair in the outer vector represents a course and all the students in that course, with those students being sorted in order, then if the pairs are in no particular order in the outer vector, the answer would be O(C + log S). (The reason is that we'd have to do a linear search through the outer vector to find the course, which is O(C), and then after that do a binary search of the S students in the sorted vector for that course, which is O(log S).) In these problems, we're just looking for the answer; you don't need to write the reason.

a. vector<pair<string, list<int>>>, where each pair in the outer vector represents a course and all the students in that class, with those students being sorted in order. The pairs are in no particular order in the outer vector. What is the big-O complexity to determine whether a particular student s is enrolled in course c?

b. map<string, list<int>>, where the students in each list are in no particular order. What is the big-O complexity to determine whether a particular student s is enrolled in course c?

c. map<string, set<int>>. What is the big-O complexity to determine whether a particular student s is enrolled in course c?

d. unordered_map<string, set<int>>. What is the big-O complexity to determine whether a particular student s is enrolled in course c?

Explanation / Answer

(a) The complexity is O(C+logS), this is explained in the question itself, same thing.

(b) map<string, list<int>>

The outer map is a binary search tree, so to search the course c it would take logC time and once we have the course and its list, to serach the student s it would take O(S) time becasue it is an unordered list so total time is O(logC+S)

(c) map<string, set<int>>

The outer map is a binary search tree, so to search the course c it would take logC time and once we have the course and its set , which is again a binary search tree so to serach the student s it would take O(logS) time.So total time is O(logC+logS)

(d) unordered_map<string, set<int>>

The outer unordered_map is a hash table, so to search the course c it would take constant time O(1). and once we have the course and its set , which is a binary search tree so to serach the student s it would take O(logS) time.So total time is O(logS)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote