Sei sulla pagina 1di 10

Lecture 14: Sets

Data Structures

Sets

Zahid Halim

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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 elements list
until a larger element is found.
If one list exhausts, append the elements of the other list as it is.

FAST, National University of Computer and Emerging Sciences, Islamabad

Lecture 14: Sets

Data Structures

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.

FAST, National University of Computer and Emerging Sciences, Islamabad

Potrebbero piacerti anche