0% found this document useful (0 votes)
10 views

Dis01c Sol

Uploaded by

sara172425
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Dis01c Sol

Uploaded by

sara172425
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS 70 Discrete Mathematics and Probability Theory

Summer 2023 Huang, Suzani, and Tausik DIS 1C

1 Countability: True or False


Note 11 (a) The set of all irrational numbers R\Q (i.e. real numbers that are not rational) is uncountable.

(b) The set of real solutions for the equation x + y = 1 is countable.

For any two functions f : Y → Z and g : X → Y , let their composition f ◦ g : X → Z be given by


f ◦ g = f (g(x)) for all x ∈ X. Determine if the following statements are true or false.

(c) f and g are injective (one-to-one) =⇒ f ◦ g is injective (one-to-one).


(d) f is surjective (onto) =⇒ f ◦ g is surjective (onto).

Solution:

(a) True. Proof by contradiction. Suppose the set of irrationals is countable. From Lecture note
10 we know that the set Q is countable. Since union of two countable sets is countable, this
would imply that the set R is countable. But again from Lecture note 10 we know that this is
not true. Contradiction!
(b) False. Let S ⊂ R × R denote the set of all real solutions for the given equation. For any x′ ∈ R,
the pair (x′ , y′ ) ∈ S if and only if y′ = 1−x′ . Thus S = {(x, 1−x) : x ∈ R}. Besides, the mapping
x to (x, 1 − x) is a bijection from R to S. Since R is uncountable, we have that S is uncountable
too.
(c) True. Recall that a function h : A → B is injective iff a1 ̸= a2 =⇒ h(a1 ) ̸= h(a2 ) for all a1 , a2 ∈
A. Let x1 , x2 ∈ X be arbitrary such that x1 ̸= x2 . Since g is injective, we have g(x1 ) ̸= g(x2 ).
Now, since f is injective, we have f (g(x1 )) ̸= f (g(x2 )). Hence f ◦ g is injective.
(d) False. Recall that a function h : A → B is surjective iff ∀b ∈ B, ∃a ∈ A such that h(a) = b. Let
g : {0, 1} → {0, 1} be given by g(0) = g(1) = 0. Let f : {0, 1} → {0, 1} be given by f (0) = 0
and f (1) = 1. Then f ◦ g : {0, 1} → {0, 1} is given by ( f ◦ g)(0) = ( f ◦ g)(1) = 0. Here f is
surjective but f ◦ g is not surjective.

2 Counting Cartesian Products


Note 11 For two sets A and B, define the cartesian product as A × B = {(a, b) : a ∈ A, b ∈ B}.

CS 70, Summer 2023, DIS 1C 1


(a) Given two countable sets A and B, prove that A × B is countable.
(b) Given a finite number of countable sets A1 , A2 , . . . , An , prove that

A1 × A2 × · · · × An

is countable.

Solution:

(a) As shown in lecture, N × N is countable by creating a zigzag map that enumerates through
the pairs: (0, 0), (1, 0), (0, 1), (2, 0), (1, 1), . . . . Since A and B are both countable, there exists a
bijection between each set and a subset of N. Thus we know that A × B is countable because
there is a bijection between a subset of N × N and A × B : f (i, j) = (Ai , B j ). We can enumerate
the pairs (a, b) similarly.
(b) Proceed by induction.
Base Case: n = 2. We showed in part (a) that A1 × A2 is countable since both A1 and A2 are
countable.
Induction Hypothesis: Assume that for some n ∈ N, A1 × A2 × · · · × An is countable.
Induction Step: Consider A1 ×· · ·×An ×An+1 . We know from our hypothesis that A1 ×· · ·×An
is countable, call it C = A1 × · · · × An . We proved in part (a) that since C is countable and An+1
are countable, C × An+1 is countable, which proves our claim.

3 Hello World!
Note 12 Determine the computability of the following tasks. If it’s not computable, write a reduction or
self-reference proof. If it is, write the program.

(a) You want to determine whether a program P on input x prints "Hello World!". Is there a
computer program that can perform this task? Justify your answer.
(b) You want to determine whether a program P prints "Hello World!" before running the kth line
in the program. Is there a computer program that can perform this task? Justify your answer.
(c) You want to determine whether a program P prints "Hello World!" in the first k steps of its
execution. Is there a computer program that can perform this task? Justify your answer.

Solution:

(a) Uncomputable. We will reduce TestHalt to PrintsHW(P, x).


TestHalt(P, x):
P’(x):
run P(x) while suppressing print statements

CS 70, Summer 2023, DIS 1C 2


print("Hello World!")
if PrintsHW(P’, x):
return true
else:
return false
If PrintsHW exists, TestHalt must also exist by this reduction. Since TestHalt cannot
exist, PrintsHW cannot exist.
(b) Uncomputable. Reduce PrintsHW(P, x) from part (a) to this program PrintsHWByK(P, x, k).
PrintsHW(P, x):
for i in range(len(P)):
if PrintsHWByK(P, x, i):
return true
return false

(c) Computable. You can simply run the program until k steps are executed. If P has printed
“Hello World!” by then, return true. Else, return false.
The reason that part (b) is uncomputable while part (c) is computable is that it’s not possible to
determine if we ever execute a specific line because this depends on the logic of the program,
but the number of computer instructions can be counted.

4 Code Reachability
Note 12 Consider triplets (M, x, L) where

• M is a Java program
• x is some input
• L is an integer

and the question of: if we execute M(x), do we ever hit line L?


Prove this problem is undecidable.
Solution: Suppose we had a procedure that could decide the above; call it Reachable(M, x,
L). Consider the following example of a program deciding whether P(x) halts:
def Halt(P, x):
def M(t):
run P(x) # line 1 of M
return # line 2 of M
return Reachable(M, 0, 2)

Program M reaches line 2 if and only if P(x) halted. Thus, we have implemented a solution to the
halting problem — contradiction.

CS 70, Summer 2023, DIS 1C 3

You might also like