SET ADT
Jun Y. Ercia
The Set ADT
Learning Outcomes
At the end of this lesson, students should be able to
• discuss set processing
• define a set abstract data type
• compare set implementations
• demonstrate how a set can be used to solve problems
• write codes for various set implementations
The Set ADT
 a collection of elements.
Properties
1. Elements are drawn from a single universe
2. Each element is either a member of the set or not.
3. Elements may be a set in itself or a primitive
4. No duplicate elements
5. No particular ordering of elements
Examples
o Set of first 5 natural numbers: {1,2,3,4,5}
o {x | x is a positive integer and x < 100}
o {x | x is a driver with > 10 years of driving experience and 0 accidents in the last 3 years}
Operations on a Set
Examples
1. Sets: Articles in Yahoo Science (A), Technology (B), and Sports (C)
o Find all articles on Wright brothers.
o Find all articles dealing with sports medicine
2. Sets: Students in CS10 (A), CS20 (B), and CS40 (C)
o Find all students enrolled in these courses
o Find students registered for CS10 only
o Find students registered for both CS10 and CS20
Uses of Sets in Computing
• Many applications deal with sets
1. Compilers have symbol tables (set of keywords, classes, etc.)
2. Dictionary is a set of words.
3. Routers have sets of forwarding rules.
4. Web servers have set of clients, etc
Implementation of Set ADT
1. Bit-Vector implementation of set
▪ The set is represented by a bit-vector (i.e., an array containing 0’s and 1’s or, equivalently,
true or false) in which the ith bit (cell) is true if i is a member of the set.
▪ Suitable for applications in which the set to be represented is a subset of a small finite
universal set.
0 1 2 3 4 5 6 7 8 9 10
Code for inserting new element into a set
Code for deleting an element from a set
Code for determining whether the set is empty
Code for determining whether the element is a
member of a set
Code for making the set empty
Code for union of two sets
Code for intersection of two sets
Code for difference of two sets
Implementation of Queue ADT
▪ Linked List implementation of Set [SLL]
▪ The entire set is represented by a linked list of nodes.
▪ Suitable for applications in which the set to be
represented does not necessarily have to be a subset of
some finite universal set.
Code for inserting new element into a set
Code for deleting an element from a set
Code for determining whether the set is empty
Code for determining whether the element is a
member of a set
Code for making the set empty
Code for union of two sets
Code for intersection of two sets
Code for difference of two sets
Set Implementation Issue
• Are the elements of the set belongs to a universal set or a subset of some universal set?

Lesson 5 - Set ADT.pdf

  • 1.
  • 2.
    The Set ADT LearningOutcomes At the end of this lesson, students should be able to • discuss set processing • define a set abstract data type • compare set implementations • demonstrate how a set can be used to solve problems • write codes for various set implementations
  • 3.
    The Set ADT a collection of elements. Properties 1. Elements are drawn from a single universe 2. Each element is either a member of the set or not. 3. Elements may be a set in itself or a primitive 4. No duplicate elements 5. No particular ordering of elements Examples o Set of first 5 natural numbers: {1,2,3,4,5} o {x | x is a positive integer and x < 100} o {x | x is a driver with > 10 years of driving experience and 0 accidents in the last 3 years}
  • 4.
  • 5.
    Examples 1. Sets: Articlesin Yahoo Science (A), Technology (B), and Sports (C) o Find all articles on Wright brothers. o Find all articles dealing with sports medicine 2. Sets: Students in CS10 (A), CS20 (B), and CS40 (C) o Find all students enrolled in these courses o Find students registered for CS10 only o Find students registered for both CS10 and CS20
  • 6.
    Uses of Setsin Computing • Many applications deal with sets 1. Compilers have symbol tables (set of keywords, classes, etc.) 2. Dictionary is a set of words. 3. Routers have sets of forwarding rules. 4. Web servers have set of clients, etc
  • 7.
    Implementation of SetADT 1. Bit-Vector implementation of set ▪ The set is represented by a bit-vector (i.e., an array containing 0’s and 1’s or, equivalently, true or false) in which the ith bit (cell) is true if i is a member of the set. ▪ Suitable for applications in which the set to be represented is a subset of a small finite universal set. 0 1 2 3 4 5 6 7 8 9 10
  • 8.
    Code for insertingnew element into a set
  • 9.
    Code for deletingan element from a set
  • 10.
    Code for determiningwhether the set is empty
  • 11.
    Code for determiningwhether the element is a member of a set
  • 12.
    Code for makingthe set empty
  • 13.
    Code for unionof two sets
  • 14.
  • 15.
  • 16.
    Implementation of QueueADT ▪ Linked List implementation of Set [SLL] ▪ The entire set is represented by a linked list of nodes. ▪ Suitable for applications in which the set to be represented does not necessarily have to be a subset of some finite universal set.
  • 17.
    Code for insertingnew element into a set
  • 18.
    Code for deletingan element from a set
  • 19.
    Code for determiningwhether the set is empty
  • 20.
    Code for determiningwhether the element is a member of a set
  • 21.
    Code for makingthe set empty
  • 22.
    Code for unionof two sets
  • 23.
  • 24.
  • 25.
    Set Implementation Issue •Are the elements of the set belongs to a universal set or a subset of some universal set?