Upload
taqi-shah
View
222
Download
0
Embed Size (px)
Citation preview
8/3/2019 Lec-17 Sets
1/21
Sets
Lecture-17Kiran Ijaz
8/3/2019 Lec-17 Sets
2/21
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,.Nfor
some fixed N.
A Set is represented by a bit-vector in which the ith
bit is true ifiis an element of the set.
8/3/2019 Lec-17 Sets
3/21
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.
8/3/2019 Lec-17 Sets
4/21
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
8/3/2019 Lec-17 Sets
5/21
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 theset represented, not the universal set.
Linked-List can represent the sets where the
universal set is infinite.
8/3/2019 Lec-17 Sets
6/21
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 ofL1with each element on L2.
The process will take O(n2) steps on lists of length
n.
8/3/2019 Lec-17 Sets
7/21
Intersection in Sorted List
To Match an element e on one list L1 with the
elements of another list L2, look down L
2until;
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/3/2019 Lec-17 Sets
8/21
Intersection in Sorted List
Ifd is the element on L1 that immediately precedes e.
And the first element found on L2 is f, such that d
8/3/2019 Lec-17 Sets
9/21
Assign in Sorted List
A =B
Copy all elements in B to A.
Cannot be implemented by pointing the header cell
ofA to the header cell ofB.
Subsequent, changes in B will result in unexpected
changes in A.
8/3/2019 Lec-17 Sets
10/21
Union in Sorted List
C=A B Attach all the elements from eitherA or B list to the C list, in their
proper, sorted order.
Compare the elements ofA with B.
If the elements are equal add once to C.
If the elements are unequal, add the elements from the smallerelements list until a larger element is found.
If one list exhausts, append the elements of the other list as it is.
8/3/2019 Lec-17 Sets
11/21
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.
IfB exhausts then append all the remaining elements of
A.
8/3/2019 Lec-17 Sets
12/21
OtherOperations
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.
8/3/2019 Lec-17 Sets
13/21
Dictionaries Collection of pairs.
(key, element)
Pairs have different keys.
Operations.
get(theKey)
put(theKey, theElement)
remove(theKey)
8/3/2019 Lec-17 Sets
14/21
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 isD
ianaRoss.
put() implemented as update when there isalready a pair with the given key.
remove() followed by put().
8/3/2019 Lec-17 Sets
15/21
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.
8/3/2019 Lec-17 Sets
16/21
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.
8/3/2019 Lec-17 Sets
17/21
Array Representationa b c d e
get(theKey)
O(size) time
put(theKey, theElement)
O(size) time to verify duplicate, O(1) to add at rightend.
remove(theKey)
O(size) time.
8/3/2019 Lec-17 Sets
18/21
Sorted ArrayA B C D E
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.
8/3/2019 Lec-17 Sets
19/21
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.
a b c d e
nullfirstNode
8/3/2019 Lec-17 Sets
20/21
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 properplace.
A B C D E
nullfirstNode
8/3/2019 Lec-17 Sets
21/21
Sorted Chain
Elements are in ascending order of Key.
A B C D E
nullfirstNode
remove(theKey)
O(size) time.