Tải bản đầy đủ (.pdf) (6 trang)

INTRODUCTION TO COMPUTER SCIENCE - PART 2 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (132.94 KB, 6 trang )

INTRODUCTION TO COMPUTER SCIENCE
HANDOUT #2. SET THEORY
K5 & K6, Computer Science Department, Vaên Lang University
Second semester Feb, 2002
Instructor: Traàn Ñöùc Quang
Major themes:
1. The Set Data Model
2. Set Algebra
3. Implementation of Sets
Reading: Sections 7.2, 7.3, 7.4, 7.7, and 7.10.
2.1 THE SET DATA MODEL
The set is the most fundamental concept in mathematics, and this is true in computer
science. The term "set" is not defined explicitly; at a basic level, we can think of a set
as a collection of objects.
Basic notation
• x ∈ S "element x is a member of set S"
• S = {x
1
, x
2
, . . . , x
n
} "elements x
1
, x
2
, . . . , x
n
are members of set S"
• each x
i


must be distinct
• sets contain unique objects in any order
• ∅ "the empty set" — the set with no members
Defining Sets
• S = {1, 4, 5, {3, 4}, 9} definition by enumeration
• T = { x | x ∈ S, x is odd } definition by abstraction.
The latter means the set of elements x such that x is an odd element of S.
"set former" notation (general form): We write
{ x | x ∈ X and P(x) }
and read "the set of elements x in X such that x has property P."
12 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #2. SET THEORY
Equality of Sets
Two sets are equal if they have exactly the same members. This doesn’t necessarily
mean their representations must be identical. For example, the set {1, 2} is equal to
the set {2, 1} because they both have exactly the elements 1 and 2.
2.2 SET ALGEBRA
We introduce the three basic operations on sets.
• Union S ∪ T the set containing the elements that are in S or T (or both)
• Intersection S ∩ T the set containing the elements that are in both S and T
• Difference S − T the set containing the elements that are in S but not in T
A Venn Diagram illustrating these relationships:
See algebraic laws for those operations in the text (page 343).
Subsets
1. S ⊆ T means:
• S is a subset of T
• T is a superset of S
• S is contained in T
• T contains S
2. S ⊂ T means:
• S is a proper subset of T

• T is a proper superset of S
Region 1
Region 2 Region 3 Region 4
S T
2.3 IMPLEMENTATION OF SETS 13
• S is properly contained in T
• T properly contains S
and is true if S ⊆ T and there is at least one element of T that is not also a mem-
ber of S.
Power Sets
Let S be any set. The power set of S is the set of all subsets of S. We denote the power
set of S by P(S) or 2
S
.
Example: S = {a, b, c, d}
P(S) = {∅, {a}, {b}, {c}, {d}, {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}, {a, b, c},
{a, b, d}, {a, c, d}, {b, c, d}, {a, b, c, d}}
Theorem: If a set S has n members, then P(S) has 2
n
members.
2.3 IMPLEMENTATION OF SETS
I will briefly present list implementation of sets. You could see some other implemen-
tations in Section 7.5.
Note that the set is a general concept, thus we only show the basic set operations.
When the set is a specific model, for example, dictionary or list, we can efficiently
implement those operations.
For simplicity, sets are implemented by a sorted linked list; each element is a cell.
4
6



31
9
42
L
M
14 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #2. SET THEORY
The listing below is a code for the function setUnion(L,M). It makes use of an aux-
iliary function assemble(x,L,M) that produces a list whose header is x and whose
tail is the union of lists L and M. I will explain it in class.
typedef struct CELL *LIST;
struct CELL {
int element;
LIST next;
};
/* assemble produces a list whose head element is x and
whose tail is the union of lists L and M */
LIST assemble(int x, LIST L, LIST M)
{
LIST first;
first = (LIST) malloc(sizeof(struct CELL));
first->element = x;
first->next = setUnion(L, M);
return first;
}
/* setUnion returns a list that is the union of L and M */
LIST setUnion(LIST L, LIST M)
{
if (L == NULL && M == NULL)
return NULL;

else if (L == NULL) /* M cannot be NULL here */
return assemble(M->element, NULL, M->next);
else if (M == NULL) /* L cannot be NULL here */
return assemble(L->element, L->next, NULL);
/* if we reach here, neither L nor M can be NULL */
else if (L->element == M->element)
return assemble(L->element, L->next, M->next);
else if (L->element < M->element)
return assemble(L->element, L->next, M);
else /* here, M->element < L->element */
return assemble(M->element, L, M->next);
}
2.4 GLOSSARY 15
2.4 GLOSSARY
Concept: Khái niệm, ý niệm. An original idea.
Conceptual design: Thiết kế mức khái niệm.
Set: Tập hợp (tập). See the definition in text.
Collection: commonly used (informally) as a synonym of set.
Set former: Lập tử tập hợp. An operator to form a set.
Empty set: Tập trống, tập rỗng.
Element, member: Phần tử (của tập hợp).
Subset: Tập con.
Superset: Tập cha, tập bao hàm.
Term. Various meanings (1) Thuật ngữ, thuật từ; (2) Học kỳ, dùng chung cho cả quarter
(khoảng 10 đến 15 tuần) và semester (khoảng 20 đến 25 tuần); (3) Hạng thức.
Notation: Ký pháp. A system of signs or symbols to represent words, phrases, num-
bers, quantities, etc. as in mathematics, chemistry, musics, etc.
Equality: Tính bằng nhau; Đẳng thức.
Set Algebra: Đại số tập hợp.
Union: Phép hợp; phần hợp. See the definition in text.

Intersection: Phép giao; phần giao. See the definition in text.
Difference: Phép hiệu; phần hiệu. See the definition in text.
Algebraic law: Luật đại số. A rule to tranform an algebraic expression into another
equivalent expression.
Power set: Tập lũy thừa. See the definition in text.
Theorem: Đònh lý.
Lemma: Bổ đề.
Corollary: Hệ quả.
Proof: Chứng minh.
Function: Hàm; Chức năng. (1) a subprogram to perform some task, usually as a syn-
onym of procedure. (2) the natural, required or expected activity of a person or
thing.
16 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #2. SET THEORY
Code: Bản mã chương trình. Any written program (in any programming languages,
including machine languages).
Listing: Chương trình minh họa. A code used in programming texts for illustration.
Coding, Programming: Lập trình, viết chương trình, thảo chương.
NULL: Giá trò rỗng; con trỏ hư ảo. The value of a variable or pointer indicating that the
variable or pointer currently has no values; especially for the pointer, it indicates
that the pointer points to nowhere. In Pascal, this symbol is nil.

×