ELEC1007 HKU

Embed Size (px)

Citation preview

  • 7/29/2019 ELEC1007 HKU

    1/39

    1

    ENGG1007Foundations of Computer Science

    Hubert Chan

    Sep 17, 2012

    Introduction

  • 7/29/2019 ELEC1007 HKU

    2/39

    2

    Teaching Team

    Hubert ChanOffice: CB429

    Email: hubert at cs

    I check my email frequently!

    Teaching Assistants

    Ning Li (HW-335A, lning at cs)

    Xiaowei Wu (CB-LG101, xwwu at cs)

    Dongxiao Yu (HW-335A, dxyu at cs)Please use ENGG1007: in subject.

  • 7/29/2019 ELEC1007 HKU

    3/39

    3

    Lecture Times

    Monday 12:30-2:20pm LE8 (Class A)

    NO class on Oct 1 and Oct 29

    Oct 4(Thursday) 12:30-1:20pm LE8

    Monday 3:30-5:20pm LE8 (Class B)

    NO class on Oct 1 and Oct 29

    Oct 4(Thursday) 3:30-4:20pm LE8

  • 7/29/2019 ELEC1007 HKU

    4/39

    4

    Tutorial (Compulsory)

    Every Thursday afternoon (except Nov 1)

    Class A: 12:30-1:20pm LE8

    Class B: 3:30-4:20pm LE8

    There will be exercises during tutorials

    You need to attend at least 6 of 10 tutorials

    Only the highest6 of the 10 tutorials will be counted(score 0 will be given to those tutorials you do not attend)

  • 7/29/2019 ELEC1007 HKU

    5/39

    5

    Consultation (Optional)

    Instructor Office Hours

    One hour after each lecture, CYC 429

    TA Office Hours

    Li Ning (One hour after each tutorial) HW-335AXiaowei Wu: (One hour after each tutorial) CYC-LG101

    Please inform the instructor (TA) after each lecture (tutorial)

    if you need to use his office hour. You can also send us emails to arrange appointments.

  • 7/29/2019 ELEC1007 HKU

    6/39

    6

    Timetable

    Mon Tue Wed Thu Fri

    12:30-2:20pmLE8 (Class A)

    3:30-5:20pmLE8 (Class B)

    12:30-1:20pmLE8 (Class A)

    3:30-4:20pmLE8 (Class B)

    Outside consultation hours: by appointments

  • 7/29/2019 ELEC1007 HKU

    7/397

    Homepage and Newsgroup

    Students are suggested to visit the coursehomepage and moodle regularly

    Homepage:

    http://i.cs.hku.hk/~engg1007/

    We also use Moodle to publish announcements.

    You can use Moodle to communicate with thewhole class.

    Please send us email for a prompt response.

    http://i.cs.hku.hk/~engg1007/http://i.cs.hku.hk/~engg1007/
  • 7/29/2019 ELEC1007 HKU

    8/398

    Textbook

    Discrete Mathematics and Its

    Applications (6th Edit ion)

    Kenneth H. Rosen,McGraw-Hill International Editions.

    Exercise questions from the book could appear in Homeworks and Exam!

    http://highered.mcgraw-hill.com/sites/0072880082
  • 7/29/2019 ELEC1007 HKU

    9/399

    Assessment

    3-hour examination + continuous assessment

    Examination: 50%

    Continuous assessment: 50%

    3 homeworks: 25%

    10 tutorials/exercise: 25%

    Only the highest 6 of the 10 tutorials will be counted

  • 7/29/2019 ELEC1007 HKU

    10/3910

    Homeworks

    Assignment Box (B1 for class A, B2 for class B):CYC Building 3/F

    Late Policy:

    1 day late: 50% penalty2 days late: no marks will be given

    Assignments should be submitted before 7 pmon the due date.

    Scheduled assignments (deadlines): Oct 15,Nov 12, Dec 10

  • 7/29/2019 ELEC1007 HKU

    11/3911

    Plagiarism

    Plagiarism is the action of using or copying someone else's idea orwork and pretending that you thought of it or created it.

    First Attempt:

    Students who admit committing plagiarism for the first time shall be

    warned in writing and receive a zero mark for the componentconcerned. For those who do not confess, the case would bereferred to the Programme Director for consideration.

    Subsequent Attempt:

    If students commit plagiarism more than once during the course ofstudies, the case shall be referred to the Programme Director for

    consideration. The Programme Director will investigate the case andconsider referring it to the University Disciplinary Committee, whichmay impose any of the following penalties: a published reprimand,suspension of study for a period of time, fine, or expulsion from theUniversity.

    But discussing with classmates is encouraged!

  • 7/29/2019 ELEC1007 HKU

    12/3912

    Outcome Based Learning: Objectives

    [O1 Abstract Concepts] Understand abstract mathematicalconcepts which are fundamental to computer science,e.g., logic, sets, functions, basic probability, graphtheory.

    [O2 Proof Techniques] Be able to perform abstract thinkingand present logical argument using techniques such asmathematical induction, proof by contradiction.

    [O3 Basic Analysis Techniques] Be able to

    apply formal reasoning to analyze and enumerate thepossible outcomes of a computational problem e.g.model and compute the number of operations usingrecursion, counting and combinatorics.

  • 7/29/2019 ELEC1007 HKU

    13/3913

    Topics

    Logic

    Sets and relations

    Counting

    Probability

    Graphs

  • 7/29/2019 ELEC1007 HKU

    14/39

    14

    Logic (Chapter 1)

    Propositional Logic

    Basic Definitions Logical operators

    Predicate Logic

    Predicates Quantified expressions

    ([O1, O2]; chapters 1.1, 1.2)

  • 7/29/2019 ELEC1007 HKU

    15/39

    15

    A Puzzle In the middle of the journey to a village, you need toselect whether to go East or West at a branch. One is the path to hell and the other is to heaven, but you

    cannot tell which is which. Each villager always tells the truth or always lies and will

    only give a Yes or a No response to a question. You are only allowed to ask a villager one question to

    determine the way to heaven. What to ask?

    If you can ask two questions, the problem is trivial.

    Question 1: 4 > 5? Question 2: Is East the way to heaven?

    Yet you are allowed to ask only one question!

  • 7/29/2019 ELEC1007 HKU

    16/39

    16

    Propositions Definition

    Def: A proposition is a statement that is either true (T) orfalse (F), but not both.

    Which of the followings are propositions?

    1 + 1 = 7.

    Do you feel hungry?

    Hong Kong is the largest city of China.

    1 + 3. x + 2.

    Come here quickly!

  • 7/29/2019 ELEC1007 HKU

    17/39

    17

    Compound Proposition

    A proposition (compound proposition) can be formed bycombining several propositions using logical operators.

    Logical operators: (negation), (and), (or),(exclusive or), (implication), (biconditional)

    Example: Mr. Wong is stupid and his wife is lazy.p: Mr. Wong is stupid

    q: his wife is lazy.

    pq: where stands for connective and

    A compound proposition also has a truth value (T or F)depending on the truth values of its constitutingpropositions and the operator(s).

  • 7/29/2019 ELEC1007 HKU

    18/39

    18

    Logical operators

    And (Conjunction) - Let p and q be two propositions, (pq) is defined to be true if both p and q are true, and false

    otherwise.

    Example:

    (Today is Friday)

    (2 is a factor of 15) ---- False Or (Disjunction) - Let p and q be two propositions, (p q)

    is defined to be true if either p or q or both are true, andfalse otherwise.

    Exclusive-or - Let p and q be two propositions, then (p

    q) is defined to be true if either p or q, but not both, istrue, and false otherwise.

    Not (Negation) - Let p be a proposition, p is defined tobe true if p is false. Otherwise p is false.

  • 7/29/2019 ELEC1007 HKU

    19/39

    19

    Truth Table Truth table can be used to display the truth values of

    propositions and is useful in determining the truth valuesof complicated proposition.

    Truth Tables for (negation), (and), (or), (exclusive-or)

    p q p q p q p q

    T T T T F

    T F F T T

    F T F T T

    F F F F F

    p and q are two propositions

    Allpossible

    cases.Howmany?

    p p

    T F

    F T

  • 7/29/2019 ELEC1007 HKU

    20/39

    20

    Logical operators: (negation), (and), (or),(exclusive or), (implication), (biconditional)

    The implication pq is the proposition that is falsewhen p is true and q is false, and true otherwise.

    p q pq

    T T TT F F

    F T T

    F F T

    pq is often read as p implies qor if p, then q

    Example:Let p denote J ohn is a CS major,

    q denote Mary is J ohns girl friend.r denote pq

    Whats the truth value of r if(a) J ohn is NOT a CS major, Mary is J ohns girl friend(b) J ohn is NOT a CS major, Mary is NOT J ohns girl friend

    (c) J ohn is a CS major, Mary is NOT J ohns girl friend

    TT

    F

    Intuitively, the statement has no problem ifthe hypothesis p is not true.

  • 7/29/2019 ELEC1007 HKU

    21/39

    21

    Remarks:

    1 The mathematical concept of an implication vsdaily usage of English

    If today is Friday, then 2+3 = 5 daily usage

    Assumes a relationship between the hypothesis & conclusion

    But the implication is true according to the definition.

    2Also, the meaning of the if-then statement is different whenused in a program

    if (x > 3) then y = x;

    If the value of x is greater than 3, then execute thestatement y = x

  • 7/29/2019 ELEC1007 HKU

    22/39

    22

    The bicondition pq is the proposition that is true bothp and q have the same truth values and is falseotherwise.

    p q pq

    T T T

    T F FF T F

    F F T

    pq is often read as p if and only ifq or p is necessary and sufficientfor q

    Example:(X is an even number) (X+1 is an odd number)

    Remark:pq is true when both pq andqp are true.

  • 7/29/2019 ELEC1007 HKU

    23/39

    23

    Exercise:Let p and q be the propositions.

    p: It is below freezingq: It is snowing

    Write the following propositions using p and q and logicalconnectives (operators).

    (a) It is below freezing but not snowing. p q

    (b) It is either below freezing or it issnowing, but it is not snowing if it isbelow freezing.

    (p q) (pq)

    (c) It is either below freezing orsnowing, but not both. p q

  • 7/29/2019 ELEC1007 HKU

    24/39

    24

    Logically equivalence

    We say that the two propositions p and q are logically equivalent ifpq is always true. We denote this relationship by pq (or p q).

    That is, either both aretrue or both are false.

    Claim:(p q) (p q)

    Again, we can prove it using truth table!

  • 7/29/2019 ELEC1007 HKU

    25/39

    25

    Some important equivalences

    p T pp F p

    p T Tp F F

    p p pp p p

    (p) p

    p q q pp q q p

    CommutativeQ: Is addition commutative? (p + q = q + p ?)Q: Is subtraction commutative? (p q = q p?)

    (p q) r p (q r)(p q) r p (q r)

    Associative

    Q: Is multiplication associative?Q: Is division associative?

    Q: (p q) r p (q r) ??

    To show that it is not correct: Let p = F, q = F, r = T

    Obvious Rules

  • 7/29/2019 ELEC1007 HKU

    26/39

    26

    (p q)p q(p q)p q

    (De Morgans Law)

    p (q r) (p q) (p r)p (q r) (p q) (p r)

    (Distributive Law)

    More.

    (p q) (p q)

    (Implication Law)

    p p T p p F

    (Negation Law)

  • 7/29/2019 ELEC1007 HKU

    27/39

    27

    Question to be asked: If I were to ask you whether Eastis the way to heaven, would you answer yes?

    If the correct answer is East:

    The honest answer is Yes.

    For the dishonest villager, If he is asked whether East is the way to heaven? (Q1)

    What would be his answer?

    No

    So, if we ask him If I were to ask you whether East is theway to heaven, would you answer yes?

    Since his own answer to Q1 is No, so he lies, and wouldanswer Yes to this question.

    Similarly for the incorrect answer, both will answer No.

  • 7/29/2019 ELEC1007 HKU

    28/39

    28

    ENGG1007Foundations of Computer Science

    Predicates and Quantifiers

    ([O1, O2]; chapters 1.3, 1.4)

  • 7/29/2019 ELEC1007 HKU

    29/39

    29

    PredicatesIs x > 3 a proposition?

    No, unless the value of x is fixed.

    Let P(x) denote the statement x > 3 where P(x) is called apropositional function.

    P(x) has a truth value once the value of x is fixed.E.g. P(5) is true; P(1) is false.

    P actually refers to the property is greater than 3 and is called thepredicate. Note that x is a variable (the subject).

    A propositional function can have more than one variable (multi-value predicates).E.g. Let Q(x, y) denote x + y > 10.Then, Q(4, 5) is false; Q(5, 6) is true.

  • 7/29/2019 ELEC1007 HKU

    30/39

    30

    Universe of Discourse

    The collection of values that a variable x may take iscalled the universe of discourse or domain.

    Example:

    x is rich, x can refer to people in HK, the world,movies stars, IT people,

    For the statement x is prime, the universe ofdiscourse ofx is the set of all positive integers.

  • 7/29/2019 ELEC1007 HKU

    31/39

    31

    Quantification (Quantifiers)

    Two ways to convert a propositional function, say P(x), into aproposition. Assign value(s) to variable(s). Quantify variable(s):

    P(x) is true for all possible values of x (Universal Quantification)

    These exists at least one value of x such that P(x) is true(Existential Quantification)

    Example:Let P(x) denote the statement x has a million dollars.

    P(x) has no truth value and is not a proposition.P(J ohn) is a proposition.

    x P(x) is a proposition.for all possible values of x (universe of discourse), P(x) is true

    e.g.

    x P(x) is false where domain is people in this room.

  • 7/29/2019 ELEC1007 HKU

    32/39

    32

    Universal and Existential Quantification

    Suppose the universe of discourse (domain) of P(x) is

    {x1, x2, x3 }

    The universal quantification

    x P(x)P(x1) P(x2) P(x3)

    P(x) is true for all possible values of x

    The existential quantification

    x P(x)P(x1) P(x2) P(x3)

    P(x) is true for at least one value of x

  • 7/29/2019 ELEC1007 HKU

    33/39

    33

    Example:Let P(x) denote the statement x is married.

    P(x) has no truth value and is not a proposition.

    P(Peter) is a proposition.x P(x) is a proposition.

    there exist an x from the possible values, P(x) is true

    e.g. x P(x) is true where u.o.d. is the postgrad students in our dept.

    Examples (the universe of discourse is the set of integers):What is the truth value of the following?x (x2 = x)x (x2 = x)x [y (x+y = x-y)]x [y (x+y = x-y)]

  • 7/29/2019 ELEC1007 HKU

    34/39

    34

    To express:

    Every student in the FCS class is smart. All students in the FCS class are smart. etc.

    Lets make the universe of discourse be the set of all

    students (including those not in FCS class). Let P(x) be the statement x is smart.

    Let Q(x) be the statement x is in the FCS class.

    Can you write down the expression? x (Q(x)P(x)) which means all students are in FCS class and smart

    Answer:x (Q(x)P(x))

    Example

  • 7/29/2019 ELEC1007 HKU

    35/39

    35

    Example To express: Some students in the FCS class are smart.

    At least one student in the FCS class is smart.

    Not all students in the FCS class are stupid

    Let P(x) be the statement x is smart.

    Let Q(x) be the statement x is in the FCS class.

    Lets make the universe of discourse be the set of all students.

    Can you write down the expression?

    x (Q(x)P(x))

    which cannot guarantee at least one student in FCS class issmart (e.g. there can be no students in FCS class and there isone student, say Peter, who is not in FCS class, the statementis still true!).Answer:x (Q(x) P(x))

  • 7/29/2019 ELEC1007 HKU

    36/39

    36

    Quantifier Equivalences

    Suppose the universe of discourse is {x1, x2, x3,}.

    x P(x)P(x1) P(x2) P(x3)

    x P(x)P(x1) P(x2) P(x3)

    By the De Morgans Law,

    (p q)p q

    (p q)p q

    we can prove that

    x P(x)x P(x)

    x P(x)x P(x)

    Compared the previous example with this one

  • 7/29/2019 ELEC1007 HKU

    37/39

    37

    Compared the previous example with this one.

    Let P(x) denote x likes to driveA(x) denote x has an expensive car

    Are x (P(x) A(x)) and (x P(x)) (x A(x)) equivalent?

    No, here is the counterexample.Let the universe of discourse has only Peter and May.P(Peter) is true, but A(Peter) is false;P(May) is false, but A(May) is true;So, x (P(x) A(x)) is true but (x P(x)) (x A(x)) is false

  • 7/29/2019 ELEC1007 HKU

    38/39

    38

    Ordering of quantifiers

    Ifxy M(x, y) is true, can we say that y x M(x, y) is also true?

    Ify x M(x, y) is true, can we say that xy M(x, y) is also true?

    Hint: Let M(x, y) denote the statement x + y = 10

    Note: Transposing different kinds of quantifiersmay have different meanings

    However, two quantifiers of the same kind can betransposed without changing the meaning of theexpression.

    xy Q(x, y) yx Q(x, y)x y Q(x, y) y x Q(x, y)

  • 7/29/2019 ELEC1007 HKU

    39/39

    Some Conventions The universe of discourse may be restricted within the

    quantification.

    x > 0 P(x)

    x (x > 0P(x))[ For all x that are greater than zero, P(x) holds ]

    x > 0 P(x)

    x (x > 0 P(x))[ There is an x greater than zero such that P(x)

    holds. ] The universe of discourse is restricted from

    a set of integers to a set of positive integers

    Try some exercises on Sec. 1.3, 1.4