1 / 21

Sets

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 .

august
Download Presentation

Sets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sets Lecture-17 Kiran Ijaz

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

  10. Union in Sorted List • C = AB • 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.

  11. 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.

  12. 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.

  13. Dictionaries • Collection of pairs. • (key, element) • Pairs have different keys. • Operations. • get(theKey) • put(theKey, theElement) • remove(theKey)

  14. 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().

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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.

  20. 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.

  21. firstNode null A B C D E Sorted Chain • Elements are in ascending order of Key. • remove(theKey) • O(size) time.

More Related