Starting from:

$29.99

CS577 Assignment 1 - Discrete Review Solution


Answer the questions in the boxes provided on the question sheets. If you run out of room for an answer, add a page to the end of the document.
Related Readings: http://pages.cs.wisc.edu/~hasti/cs240/readings/
Name: Ihnym Xu Wisc id:
Logic
1. Using a truth table, show the equivalence of the following statements.
(a) P _ (¬P ^ Q) ⌘ P _ Q

(b) ¬P _ ¬Q ⌘ ¬(P ^ Q)
P
Q p
7Q
p nQ
Me n MQ




(c) ¬P _ P ⌘ true
pnp
pvpiiipQRPVQpvRMRPVIQMNDMpvp.IT
(d) P _ (Q ^ R) ⌘ (P _ Q) ^ (P _ R)

Sets
2. Based on the definitions of the sets A and B, calculate the following: |A|, |B|, A [ B, A B, A B, B A.
(a) A = {1,2,6,10} and B = {2,4,9,10}

(b) A = {x | x 2N} and B = {x 2N | x is even}

3. For each of the following relations, indicate if it is reflexive, antireflexive, symmetric, antisymmetric, or transitive.
(a) {(x,y) : x  y}
reflexive
antisymmetric
transitive
{(x,y) : x > y}
antreflexive
antisymmetric
transitive
(b)

(c) {(x,y) : x < y}
antreflex.ve
antisymmetric transitive
(d) {(x,y) : x = y}
reflexive
transitive symmetric
4. For each of the following functions (assume that they are all f : Z!Z), indicate if it is surjective (onto), injective (one-to-one), or bijective.
(a) f(x) = x

(b) f(x) = 2x 3

(c) f(xbijeet) =ebiqeetivx2 esurjeetiveaniderf.ru

5. Show that h(x) = g(f(x)) is a bijection if g(x) and f(x) are bijections.
⼀⽇ g A B
UbEB ⼆myone aEA it gla b snugis biting
For a EA ⺕ onlyone w ⼼
fniasbbEB.weheu.hninufibijutinsiyewc.n.atwehvevmhki
ishjenion
Induction


Graphs and Trees
7. Give the adjacency matrix, adjacency list, edge list, and incidence matrix for the following graph.


10. Show by induction that, for all trees, |E| = |V | 1.
we prove
hgidNilnion.liwe have No
iihppe.lconsider V treev lTinewnith mHivertimcesthenT M at least
2 leaves we get a new gh T by removing one of leaves v fromT _TO
me
then we have T is a connecT ted graph and doesn't win simple
cycle since T is a subgraph of treeT Thenwe be T is a treeby definition
sineT has nvuthweheilinf.fm
the fort Nfntl Eīkn M 朴州 四

Counting
11. How many 3 digit pin codes are there?
not以10 1000
12. What is the expression for the sum of the ith line (indexing starts at 1) of the following:
14 5 6 the sum fl thhinie.nttoiili.li
2 3 thhnett 1 jii nl.it州
7 8 9 10... thethhnesunofijliilii lit.Hili i.li 们打了
13. A standard deck of 52 cards has 4 suits, and each suit has card number 1 (ace) to 10, a jack, a queen, 3 13 1313and a king. A standard poker hand has 5 cards. For the following, how many ways can the describedhad be drawn from a standard deck.
5 (a) A royal flush: all 5 cards have the same suit and are 10, jack, queen, king, ace.
5
(b) A straight flush: all 5 cards have the same suit and are in sequence, but not a royal flush.
5ㄨ9245

A flush: all 5 cards have the same suit, but not a royal or straight flush.
4 孕 4x 13512t14⼈了 2 1312Mx3
5148
Only one pair:
别ㄨ4 以华 ⼝器
68640 ㄨ4
(c)
(d) ix
Proofs
14. Show that 2x is even for all x 2N. (a) By direct proof.
ie
rf
x t N E Z
then

is even

15. For all x,y 2R, show that |x + y|  |x| + |y|. (Hint: use proof by cases.)

Program Correctness (and invariants)
16. For the following algorithms, describe the loop invariant(s) and prove that they are sound and complete.
Algorithm 1: findMin

Input: a: A non-empty array of integers (indexed starting at 1) The smallest element in the array
(a) formini 1 1 to len(athen) do
a[ ]
end
end return min
end
Consider a Ia a ⼀ anl where milengthy we first stk hop invariant
trkhapflal.wehwemnimi lana.iq
forweproveeveryhgindnetion.frk Ell lengthknoall itholds
consider k whids.niminln anb.fr nwp forcknn.minin1 if tanaimnitllawehantibiemifrn.it hop
thenwe prove the partial correctness ⼝ a
sine aisnm
enpyarmyofingen.tk
hopwillterminates When i in
we have the termination of algorithm then we prove correctnessof algorithm


return a
end
consider a la aml m length
wefirstkmaistampwplwpmiiinvariant l.wehaeifim.tkentriesin a
After asubilaia.cl ordered fn largest to smallest
wewprovewhenehavebykinininductifordeionasred aaftshifterfirstfirhopst2 entries elseremains swppekNla.imI ordered for kntl.vn lean FiforinI j vl.nllcnal rightvalsbeforea.lt a win hereValaajsortedthenfiVralst7mlm加aeleyajments kmpǐ
vwjeth.havenhvsortedal.iqfirsthtn_ne itemselemreentmainthe same
thenwe have partial women ⼝ then we pore termination Gina aisnm enpyarmyofingen.tk
hopwillterminates When i in
we have the termination of algorithm ⼝ then we prove correctnessof algorithm 四
Recurrences
17. Solve the following recurrences.
(a) c0 = 1;cn = cn 1 + 4

(b) d0 = 4;dn = 3 · dn 1


(c) T(1) = 1;T(n) = 2T(n/2) + n (An upper bound is sufficient.)
in
(d) f( If you are having trouble with this one. Look for a hint to be posted on Piazzanhgnf(i)) lfm ) fn yiiifcii iif lmifn
nfninfmy.fin_n iinln in f
in


Coding Question
Most assignments will have a coding question. You can code in C, C++, C#, Java, or Python. You will submit a Makefile and a source code file.
Makefile: In the Makefile, there needs to be a build command and a run command. Below is a sample Makefile for a C++ program. You will find this Makefile in assignment details. Download the sample Makefile and edit it for your chosen programming language and code.
#Build commands to copy:
#Replace g++ -o HelloWorld HelloWord.cpp below with the appropriate command.
#Java:
# javac source_file.java
#Python:
# echo "Nothing to compile."
#C#:
# mcs -out:exec_name source_file.cs
#C:
# gcc -o exec_name source_file.c
#C++:
# g++ -o exec_name source_file.cpp build:
g++ -o HelloWorld HelloWorld.cpp
#Run commands to copy:
#Replace ./HelloWorld below with the appropriate command.
#Java:
# java source_file
#Python 3:
# python3 source_file.py
#C#:
# mono exec_name
#C/C++:
# ./exec_name run:
./HelloWorld
HelloWorld Program Details The input will start with a positive integer, giving the number of instances that follow. For each instance, there will be a string. For each string s, the program should output Hello, s! on its own line. A sample input is the following:
3
World
Marc
Owen
The output for the sample input should be the following:
Hello, World!
Hello, Marc!
Hello, Owen!

More products