210 likes | 495 Views
Sets. Lecture-17 Kiran Ijaz. A Bit-Vector Implementation of Sets. A Bit-Vector (Boolean Array) can be used if all the sets in the domain are subsets of a small “universal set”, whose elements are the integers 1,….N for some fixed N .
E N D
Sets Lecture-17 Kiran Ijaz
A Bit-Vector Implementation of Sets • A Bit-Vector (Boolean Array) can be used if all the sets in the domain are subsets of a small “universal set”, whose elements are the integers 1,….N for some fixed N. • A Set is represented by a bit-vector in which the ith bit is true if i is an element of the set.
A Bit-Vector Implementation of Sets • MEMBER, INSERT and DELETE operations can be performed in constant time by directly addressing the appropriate bit. • UNION, INTERSECTION and DIFFERENCE can be performed in time proportional to the size of the universal set.
A Bit-Vector Implementation of Sets • Bit-Vector implementation can be used when the universal set is a finite set other than a set of consecutive integers. • A mapping would be required to translate the set members to the integers 1,….N
Linked-List Implementation of Sets • The Items of the linked-list are the members of the set. • Linked-list uses space proportional to the size of the set represented, not the universal set. • Linked-List can represent the sets where the universal set is infinite.
Intersection in Unsorted List • An element is in the intersection of lists L1 and L2 if and only if it is on both lists. • In unsorted lists, we must match each element of L1 with each element on L2. • The process will take O(n2) steps on lists of length n.
Intersection in Sorted List To Match an element e on one list L1 with the elements of another list L2, look down L2 until; • Either find e, that is the match has been found. • Or, find an element greater than e, which indicates the match does not exist.
Intersection in Sorted List • If d is the element on L1 that immediately precedes e. • And the first element found on L2 is f, such that d <= f, then to search L2 for an occurrence of e we can begin with f. • Thus, we can find matches for all the elements of L1, if they exist, by scanning L1 and L2 once.
Assign in Sorted List • A = B • Copy all elements in B to A. • Cannot be implemented by pointing the header cell of A to the header cell of B. • Subsequent, changes in B will result in unexpected changes in A.
Union in Sorted List • C = AB • Attach all the elements from either A or B list to the C list, in their proper, sorted order. • Compare the elements of A with B. • If the elements are equal add once to C. • If the elements are unequal, add the elements from the smaller element’s list until a larger element is found. • If one list exhausts, append the elements of the other list as it is.
Difference in Sorted List • C = A – B • Do not add an element to the C list when equal elements are found. • Add the current A list element to the C list when it is smaller than the current B list element; since the former cannot be found on the B list. • If B exhausts then append all the remaining elements of A.
Other Operations • MIN: Return the first element on the list. • FIND: Search through the list and return when the target element is found. • DELETE: Same as FIND but dispose of the target element. • INSERTION: Find out the position of the element to be inserted in order, and then change the pointers appropriately.
Dictionaries • Collection of pairs. • (key, element) • Pairs have different keys. • Operations. • get(theKey) • put(theKey, theElement) • remove(theKey)
Application • Collection of student records in this class. • (key, element) = (student name, linear list of assignment and exam scores) • All keys are distinct. • Get the element whose key is John Adams. • Update the element whose key is Diana Ross. • put() implemented as update when there is already a pair with the given key. • remove() followed by put().
Dictionary With Duplicates • Keys are not required to be distinct. • Word dictionary. • Pairs are of the form (word, meaning). • May have two or more entries for the same word. • (bolt, a threaded pin) • (bolt, a crash of thunder) • (bolt, to shoot forth suddenly) • (bolt, a gulp) • (bolt, a standard roll of cloth) • etc.
Represent As A Linear List • L = (e0, e1, e2, e3, …, en-1) • Each ei is a pair (key, element). • 5-pair dictionary D = (a, b, c, d, e). • a = (aKey, aElement), b = (bKey, bElement), etc. • Array or linked representation.
a b c d e Array Representation • get(theKey) • O(size) time • put(theKey, theElement) • O(size) time to verify duplicate, O(1) to add at right end. • remove(theKey) • O(size) time.
A B C D E Sorted Array • elements are in ascending order of key. • get(theKey) • O(log size) time • put(theKey, theElement) • O(log size) time to verify duplicate, O(size) to add. • remove(theKey) • O(size) time.
firstNode null a b c d e Unsorted Chain • get(theKey) • O(size) time • put(theKey, theElement) • O(size) time to verify duplicate, O(1) to add at left end. • remove(theKey) • O(size) time.
firstNode null A B C D E Sorted Chain • Elements are in ascending order of Key. • get(theKey) • O(size) time • put(theKey, theElement) • O(size) time to verify duplicate, O(1) to put at proper place.
firstNode null A B C D E Sorted Chain • Elements are in ascending order of Key. • remove(theKey) • O(size) time.