ΤΜΗΜΑ ΗΛΕΚΤΡΟΛΟΓΩΝ ΜΗΧΑΝΙΚΩΝ ΚΑΙ ΜΗΧΑΝΙΚΩΝ ΥΠΟΛΟΓΙΣΤΩΝ ΗΜΥ 311: Διακριτή Ανάλυση και Δομές Χειμερινό Εξάμηνο 016 Σειρά Ασκήσεων : Συναρτήσεις, Σχέσεις, Σειρές και Αθροίσματα, Αλγόριθμοι και Πολυπλοκότητα Exercise 1 Problem.3.1 Why is f ot a fuctio from R to R if a) f (x) = 1/x The expressio 1/x is meaigless for x = 0, which is oe of the elemets i the domai; thus the "rule" is o rule at all. I other words, f (0) is ot defied. b) f (x) = x Thigs lie 3 udefied (or, at best, are complex umbers). c) f (x) = ± (x + 1 The "rule" for f is ambiguous. We must have f (x) defied uiquely, but here there are two values associated with every x, the positive square root ad the egative square root of x + 1. Problem.3.5 (optioal) I each case we wat to fid the domai (the set o which the fuctio operates, which is implicitly stated i the problem) ad the rage (the set of possible output values). a) Clearly the domai is the set of all bit strigs. The rage is Z; the fuctio evaluated at a strig with l's ad o 0's is, ad the fuctio evaluated at a strig with 0's ad o l's is -. b) Agai the domai is clearly the set of all bit strigs. Sice there ca be ay atural umber of 0's i a bit strig, the value of the fuctio ca be 0,, 4,... Therefore the rage is the set of eve atural umbers. c) Agai the domai is the set of all bit strigs. Sice the umber of leftover bits ca be ay whole umber from 0 to 7 (if it were more, the we could form aother byte), the rage is {0, 1,, 3, 4, 5, 6, 7}. 1
d) As the problem states, the domai is the set of positive itegers. Oly perfect squares ca be fuctio values, ad clearly every positive perfect square is possible. Therefore, the rage is {l, 4, 9, 16,... }. Problem.3.3 a) Oe way to determie whether a fuctio is a bijectio is to try to costruct its iverse. This fuctio is a bijectio, sice its iverse (obtaied by solvig y = x + 1 for x) is the fuctio g(y) = (y - 1) /.Alteratively, we ca argue directly. To show that the fuctio is oe-to-oe, ote that if x + 1 = x' + 1, the x = x'. To show that the fuctio is oto, ote that ( (y - 1) /) + 1 = y, so every umber is i the rage. b) This fuctio is ot a bijectio, sice its rage is the set of real umbers greater tha or equal to 1 (which is sometimes writte [1, )), ot all of R. (It is ot ijective either.) c) This fuctio is a bijectio, sice it has a iverse fuctio, amely the fuctio f(y) = y 1/3 (obtaied by solvig y = x 3 for x). d) This fuctio is ot a bijectio. It is easy to see that it is ot ijective, sice x ad -x have the same image, for all real umbers x. A little wor shows that the rage is oly { y 0.5 y < 1} = [0.5, 1). Problem.4.7 Oe patter is that each term is twice the precedig term. A formula for this would be that the th term is -l. Aother patter is that we obtai the ext term by addig icreasig values to the previous term. Thus to move from the first term to the secod we add 1; to move from the secod to the third we add ; the add 3, ad so o. So the sequece would start out 1,, 4, 7, 11, 16,,... We could also have trivial aswers such as the rule that the first three terms are 1,, 4 ad all the rest are 17 (so the sequece is 1,, 4, 17, 17, 17,... ), or that the terms simply repeat 1,, 4, 1,, 4, 1,, 4,... Here is aother patter: Tae poits o the uit circle, ad coect each of them to all the others by lie segmets. The iside of the circle will be divided ito a umber of regios. What is the largest this umber ca be? Call that value a. If there is oe poit, the there are o lies ad therefore just the oe origial regio iside the circle; thus a1 = 1. If =, the the oe chord divides the iterior ito two parts, so a =. Three poits give us a triagle, ad that maes four regios (the iside of the triagle ad the three pieces outside the triagle), so a3 = 4. Careful drawig shows that the sequece starts out 1,, 4, 8, 16, 31. That's right: 31, ot 3.
Problem.4.9 (optioal) We eed to compute the terms of the sequece oe at a time, sice each term is depedet upo oe or more of the previous terms. a) We are give ao =. The, by the recurrece relatio a = 6a-1, we see (by lettig = 1) that a1 = 6ao = 6 = 1. Similarly a = 6a1 = 6 1 = 7, the a3 = 6a = 6 7 = 43, ad a4 = 6a3 = 6 43 = 59. b) a1 = (give), a = a1 = = 4, a3 =a = 4 = 16, a4 =a3 = 16 = 56, a5 =a4 = 56 = 65536. c) This time each term depeds o the two previous terms. We are give a0 = 1 ad a1 =. To compute a we let = i the recurrece relatio, obtaiig a = a1 + 3a0 = + 3 1 = 5. The we have a3 = a + 3a1 = 5 + 3 = 11 ad a4 = a3 + 3a = 11 + 3 5 = 6. d) ao = 1 (give), a1 = 1 (give), a = a1+ ao = 1+4 1 = 6, a3 = 3a + 3 a1 =3 6+9 1 = 7, a4 = 4a3 + 4 a = 4 7 + 16 6 = 04. e) We are give ao = 1, a1 =, ad a = 0. The a3 = a + a0 = 0 + 1 = 1 ad a4 = a3 + a1 = 1 + = 3. Problem 3.1.13 a) Note that = 8 ad x = 9. Iitially, i is set equal to 1. The while loop is executed as log as i 8 ad the i th elemet of the list is ot equal to 9. Thus, o the first pass we chec that 1 8 ad that 9 1 (sice a1 = 1), ad therefore perform the statemet i := i + 1. At this poit i =. We chec that 8 ad the i th elemet of the list is ot equal to 9. Thus o the first pass we chec that 1 8 ad 9 3, ad therefore agai icremet i, this time to 3. This process cotiues util i = 7. At that poit the coditio "i 8 ad 9 a," is false, sice a1 = 9. Therefore the body of the loop is ot executed (so i is still equal to 7), ad cotrol passes beyod the loop. The ext statemet is the if... the statemet. The coditio is satisfied, sice 7 8, so the statemet locatio := i is executed, ad locatio receives the value 7. The else clause is ot executed. This completes the procedure, so locatio has the correct value, amely 7, which idicates the locatio of the elemet x (amely 9) i the list: 9 is the seveth elemet. b) Iitially, i is set equal to 1 ad j is set equal to 8. Sice i < j at this poit, the steps of the while loop are executed. First m is set equal to [(l + 8)/] = 4. The sice x (which equals 9) is greater tha a4 (which equals 5), the statemet i := m + 1 is executed, so i ow has the value 5. At this poit the first iteratio through the loop is fiished, ad the search has bee arrowed to the sequece a5,..., a8. I the ext pass through the loop (there is aother pass sice i < j is still true), m becomes [(5 + 8)/] = 6. Sice agai x > am, we reset i to be m + 1, which is 7. The loop is ow repeated with i = 7 ad j = 8. This time m becomes 7, so the test x > am (i.e., 9 > 9) fails; thus j := m is executed, so ow j = 7. At this poit i j, so there are o more iteratios of the loop. Istead cotrol passes to the statemet beyod the loop. Sice the coditio x = a, is true, locatio is set to 7, as it should be, ad the algorithm is fiished. 3
Problem 3.1.35 There are four passes through the list. O the first pass, the 3 ad the 1 are iterchaged first, the the ext two comparisos produce o iterchages, ad fially the last compariso results i the iterchage of the 7 ad the 4. Thus after oe pass the list reads 1, 3, 5, 4, 7. Durig the ext pass, the 5 ad the 4 are iterchaged, yieldig 1, 3, 4, 5, 7. There are two more passes, but o further iterchages are made, sice the list is ow i order. Problem 3..31 If f(x) is Θ (g(x)), the f(x) C g(x) ad g(x) (x)i for all x >. Thus f(x) is O(g(x)) ad g(x) is O(f(x)). Coversely, suppose that f(x) is O(g(x)) ad g(x) is O(f(x)). The (with appropriate choice of variable ames) we may assume that f(x) Cg(x) ad g(x) Cf(x) for all x >. (The here will be the larger of the two 's ivolved i the hypotheses.) If C > 0 the we ca tae C1 = C -1 to obtai the desired iequalities i "f(x) is Θ (g(x))." If C 0, the g(x) = 0 for all x >, ad hece by the first iequality f(x) = 0 for all x > ; thus we have f(x) = g(x) for all x >, ad we ca tae C1=C=1. Problem 3.3.11 (optioal) a) We ca express the suggested algorithm i pseudocode as follows. Notice that the Boolea variable disjoit is set to true at the begiig of the compariso of sets S, ad S1, ad becomes false if ad whe we fid a elemet commo to those two sets. If disjoit is ever set to false, the we have foud a disjoit pair, ad aswer is set to true. This process is repeated for each pair of sets (cotrolled by the outer two loops). procedure disjoitpair(s1, S,..., S : subsets of {1,,..., }) aswer:= false for i := 1 to for j := i + 1 to disjoit := true for := 1 to if ϵ Si ad ϵ Sj the disjoit:= false if disjoit the aswer := true retur aswer b) The three ested loops imply that the elemethood test eeds to be applied O( 3 ) times. 4
Problem 9.1.5 Recall the defiitios: R is reflexive if (a,a)ϵr for all a; R is symmetric if (a,b)ϵr always implies (b, a)ϵr; R is atisymmetric if (a, b)ϵr ad (b, a)ϵr always implies a = b; ad R is trasitive if (a, b)ϵr ad (b, c)ϵr always implies (a, c)ϵr. a) It is tautological that everyoe who has visited Web page a has also visited Web page a, so R is reflexive. It is ot symmetric, because there surely are Web pages a ad b such that the set of people who visited a is a proper subset of the set of people who visited b (for example, the oly li to page a may be o page b). Whether R is atisymmetric i truth is hard to say, but it is certaily coceivable that there are two differet Web pages a ad b that have had exactly the same set of visitors. I this case, (a, b)ϵr ad (b, a)ϵr, so R is ot atisymmetric. Fially, R is trasitive: if everyoe who has visited a has also visited b, ad everyoe who has visited b has also visited c, the clearly everyoe who has visited a has also visited c. b) This relatio is ot reflexive, because for ay page a that has lis o it, (a, a) R. The defiitio of R is symmetric i its very statemet, so R is clearly symmetric. Also R is certaily ot atisymmetric, because there surely are two differet Web pages a ad b out there that have o commo lis foud o them. Fially, R is ot trasitive, because the two Web pages just metioed, assumig they have lis at all, give a example of the failure of the defiitio: (a, b)ϵr ad (b, a)ϵr, but (a, a) R. c) This relatio is ot reflexive, because for ay page a that has o lis o it, (a, a) R. The defiitio of R is symmetric i its very statemet, so R is clearly symmetric. Also R is certaily ot atisymmetric, because there surely are two differet Web pages a ad b out there that have a commo li foud o them. Fially, R is surely ot trasitive. Page a might have oly oe li (say to this textboo), page c might have oly oe li differet from this (say to the Erdos Number Project), ad page b may have oly the two lis metioed i this setece. The (a, b)ϵr ad (b, c)ϵr, but (a, c) R. d) This relatio is probably ot reflexive, because there probably exist Web pages out there with o lis at all to them (for example, whe they are i the process of beig writte ad tested); for ay such page a we have (a, a) R. The defiitio of R is symmetric i its very statemet, so R is clearly symmetric. Also R is certaily ot atisymmetric, because there surely are two differet Web pages a ad b out there that are refereced by some third page. Fially, R is surely ot trasitive. Page a might have oly oe page that lis. 5
Problem 9.1.51 We eed to show two thigs. First, we eed to show that if a relatio R is symmetric, the R=R -1, which meas we must show that R R -1 ad R -1 R. To do this, let (a, b) R. Sice R is symmetric, this implies that (b,a) R. But sice R -1 cosists of all pairs (a,b) such that (b,a) R, this meas that (a, b) R -1. Thus we have show that R R -1. Next let (a, b) R -1. By defiitio this meas that (b,a) R. Sice R is symmetric, this implies that (a,b) R as well. Thus we have show that R -1 R. Secod we eed to show that R = R -1 implies that R is symmetric. To this ed we let (a, b) R ad try to show that (b, a) is also ecessarily a elemet of R. Sice (a, b) R, the defiitio tells us that (b, a) R -1. But sice we are uder the hypothesis that R = R -1, this tells us that (b, a) R, exactly as desired. Problem 9..1 We simply eed to fid solutios of the iequality, which we ca do by commo sese. The set is { (1,, 3),(1,,4),(1,3,4), (,3,4)}. Exercise..: Όλα τα ζεύγη θετικών ακέραιων αριθμών a) ( (x,y) όπου x,yz με x>0 και y>0)..: Το σύνολο των θετικών ακέραιων αριθμών b) c) d)..: Z..: Z= 0,1,,...9..: Όλα τα πιθανά bit strigs..: Z= 0,1,,.....: Όλα τα πιθανά bit strigs..: Z= 0,1,,... όπου ο αριθμός του ψηφίου του strig 6
Exercise 3 a) 1:1 και επί b) Όχι 1:1, όχι επί c) 1:1 και επί Exercise 4 a) ΝΑΙ b) ΟΧΙ c) ΝΑΙ d) ΟΧΙ Exercise 5 a), ακέραιο υπάρχει άθροισμα ακεραίων που να δίνει τον ακέραιο για Z, f (,0) b) OXI π.χ. δεν μπορούν να εκφραστούν οι αρνητικοί αριθμοί c) OXI π.χ. δεν μπορούν να εκφραστούν οι αρνητικοί αριθμοί d), f (,0) ίδια αιτιολογία με διαφορά ακεραίου 7
Exercise 6 a) Cotrapositive Proof Αν η g δεν είναι 1:1, τότε: x, x (.. της g), ώστε x x και g( x ) g( x ) και 1 1 1 f g( x ) f ( g( x )) f ( g( x )) 1 1 οπότε θα είχαμε f g όχι 1:1 αρα αναγκαστικά η g πρέπει να είναι 1:1 b) g όχι επί f επί a a a a b b b b c c c f g =>f g a a a a a b b b b b c c c ΔΕΝ ΙΣΧΥΕΙ 8
Exercise 7 Η f είναι αντιστρέψιμη είναι 1:1 1 για α=0 έχουμε f(x)=0 x R ot 1:1 για α 0 έχουμε f(x )-f(x )=α( x x ) f(x ) f(x ) για x x f είναι 1:1 για α 0 y y ax x y 1 με f ( ) 1 1 1 1 y Exercise 8 a) f ( x) f ( x) f ( x) A B A B 1 Περίπτωση: x A xb x A B f ( x) f ( x) f ( x) 0 00 TRUE A B A B Περίπτωση: x A xb x A B f ( x) f ( x) f ( x) 0 01TRUE A B A B 3 Περίπτωση: x A xb x A B f ( x) f ( x) f ( x) 0 10 TRUE A B A B 4 Περίπτωση: x A xb x A B f ( x) f ( x) f ( x) 111 TRUE A B A B Ισχύει για όλες τις περιπτώσεις και άρα ΙΣΧΥΕΙ b) f ( x) f ( x) f ( x) f ( x) f ( x) A B A B A B 1 Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 0 0 0 00 TRUE A B A B A B 9
Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 1 0 1 01 TRUE A B A B A B 3 Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 11 0 10 TRUE A B A B A B 4 Περίπτωση: x A xb x A B f ( x) f ( x) f ( x) f ( x) f ( x) 11111TRUE A B A B A B Ισχύει για όλες τις περιπτώσεις και άρα ΙΣΧΥΕΙ c) f ( x) 1 f ( x) A A 1 Περίπτωση: x A x A f ( x) 1 f ( x) 0 11 TRUE A A Περίπτωση: x A x A f ( x) 1 f ( x) 11 0 TRUE A A Ισχύει για όλες τις περιπτώσεις και άρα ΙΣΧΥΕΙ d) f ( x) f ( x) f ( x) f ( x) f ( x) A B A B A B 1 Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 0 0 0 00 TRUE AB A B A B Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 1 0 1 01 TRUE AB A B A B 3 Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 1 1 0 10 TRUE AB A B A B 10
4 Περίπτωση: x A x B x A B f ( x) f ( x) f ( x) f ( x) f ( x) 1 11 11 TRUE AB A B A B Ισχύει για όλες τις περιπτώσεις και άρα ΙΣΧΥΕΙ Exercise 9 a) x x Θέτουμε m x τότε m x m 1 m x ( m 1) m x m 1 m x και m x x x b) Με τον ίδιο τρόπο λύνεται και το δεύτερο ερώτημα. Exercise 10 Αυτοπαθής Συμμετρική Αντισυμμετρική Μεταβατική a) b) c) d) e) f) 11
Exercise 11 Αυτοπαθής Συμμετρική Αντισυμμετρική Μεταβατική a) b) c) d) S R =>R S 1 1 1 1 1 1 3 3 3 3 3 3 4 4 4 4 4 4 R S = {(1,),(1,3),(1,4),(,),(,3),(,4),(3,),(3,3),(3,4)} R S S R (,) (1,1) (,1) (,3) (1,) (,) (,4) (,1) (,3) (3,) (,) (,4) (3,3) (3,3) (3,1) (3,4) (4,4) (3,) (3,3) (3,4) 1
R R R R (,) (,) (,) (,3) (,3) (,3) (,4) (,4) (,4) (3,) (3,) (3,) (3,3) (3,3) (3,3) (3,4) (3,4) (3,4) S T T S (1,1) (,4) (1,4) (1,) (4,) (,4) (,1) (4,) (,) (3,3) (4,4) T S S T (,4) (1,1) (,4) (4,) (1,) (4,1) (,1) (4,) (,) (3,3) (4,4) 13
S T R R (S T) (,4) (,) (4,) (4,1) (,3) (4,3) (4,) (,4) (4,4) (3,) (3,3) (3,4) T R S (R S) T) (,4) (1,) (4,) (4,) (1,3) (4,3) (1,4) (4,4) (,) (,3) (,4) (3,) (3,3) (3,4) R ( S T) ( R S) T Exercise 1 1 1 R R R R R Έστω R: αυτοπαθής R Rή, ό a,( a, a) R ( a, a) R R Έστω R : αυτοπαθής a a a R R a a R R 1 1,(, ) και (, ) (, ), άρα αυτοπαθής Οπότε με επαγωγή R αυτοπαθής 14
Exercise 13 R R R R R 1 1 R : ή, R R ή ό : R : ή ( a, b) R ( b, a) R ( b a) R : ή ( c, d) R ( d, c) R ( c d) 1 1 Θέλω νδο για τυχαίο ( x, y) R ( y, x) R ( x y) 1 ( x, y) R c, ώστε ( x, c) R και ( c, y) με ( x, c) R ( c, x) R ( c, y) R ( y, c) R R με R R R, έχουμε ( y, c) R ( c, x) R ( y, x) R 1 1 Άρα ( x, y) R ( y, x) R 1 1 ε επαγωγή R : ή 15
Exercise 14 a) a1 a a3 a4 a5 a6 a7 a8 3 *3+5 *11+5 *7+5 *59+5 51 507 1019 b) a1 a a3 a4 a5 a6 a7 a8 1 1 1 3 3 Exercise 15 a) 5 5 5 ( 1) 1 (1 3 4 5) 51 0 1 1 1 b) c) 4 j0 or js j 0 1 3 4 ( ) ( ) ( ) ( ) ( ) ( ) 11 4 j0 41 ( ) 1 j ( ) 11 1 1 1 1 1 176 (1/ j) 1 3 5 7 105 d) 1 (1 111) 4 js Exercise 16 a) b) c) 8 j0 8 j1 81 j 1 3 3 7533 1 j 3 9840 8 8 81 j j 0 1 ( ) 1 ( ) ( ) ( ) ( ) (1 ) 17 j j 1 16
d) 8 j0 81 ( 3) 1 j ( 3) 984 31 Exercise 17 a) b) c) d) 3 3 3 3 (i 3 j) i 3 j 4 i 9 j 4(1 3) 9(1 ) 51 i1 j1 i1 j1 i1 j1 i1 j1 3 3 3 (3i j) 3i j 1 i 6 j 1(0 1 ) 6(0 1 3) 0 i0 j0 i0 j0 i0 j0 i0 j0 3 3 ( j) j 3 j 3(0 1 ) 9 i1 j0 j0 i1 j0 3 3 3 3 3 3 3 3 3 3 i j i j i j (1 3 ) (0 1 ) 180 i0 j1 i0 j1 i0 j1 Exercise 18 1 1 1 έ a a 1 1 ( 1) 1 1 1 1 ( ) ( a a 1) ( a a0) ( 1) 1 1 1 1 1 1 ( 1) 1 1 1 1 Exercise 19 Bubble sort 5 5 5 5 5 5 5 3 3 3 3 1 1 6 6 3 3 3 3 3 5 3 1 3 3 6 5 1 1 1 3 3 3 6 1 1 1 1 1 5 4 4 4 4 4 1 1 1 1 6 4 4 4 4 4 5 5 5 5 5 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6 17
Exercise 0 a) b) c) d) e) f) f x x x x x O x C ( ) 17 1117 11 8, ( ), 8 f x x x x x O x C ( ) 1000 1000 1001, ( ), 1001 f x x x x x x O x C ( ) log, ( ), 1 4 x f ( x) O( x ) l (l ) f x x x f ( x) floor( x) ceil( x) x x x( x 1) x x x x ( ) (lim lim lim O( x ) x x x x x x x x O x C, ( ), Exercise 1 a) 3 x 7 f ( x ) O ( x ) f ( x) ( x) f ( x) 3 x, ( x) b) f x x x ( ) 7 f ( x) x x 3 x O( x ) f x f ( x) x ( for x 7) ( x ) ( ) ( x ) c) 1 1 f ( x) x x 1 x 3x f ( x) x x O( x) 1 x x f ( x) x x ( x) f ( x) ( x) 18
d) f x ( ) log( x 1) f ( x) log ( x x ) log ( x ) log( x) log() 3log x, x 10 10 f x x x f x x ( ) log( ) log ( ) (log ), Με κατάλληλη αλλαγή βάσης e) f ( x)log x (log x), f ( x) log () log x 10 10 O(log x) (log x) (log x) Exercise Έστω f(x)=x, g(x)=x (f(x) O(g(x)) όμως x C x C x x, Άρα δεν ισχύει 19
Exercise 3 1 1 1 j x j 1 j 1 x j 1 1 x ( j, j 1), Therefore x j1 1 3 1 1 1 1 1 dx ( 1) x 1 1 1 dx (3 1) x 3 3... 1 1 dx (+) x 1 1 dx x j j 1 H dx 1 l( ) 1 l( ), e H O(log ) x 1 0
Exercise 4 M max{ a, a,... a } 1 f ( x) a x... a a x a x... a 1 0 1 0 a x a x... a x M ( 1) x Cx, O( x ) 1 1 0 1, m, m : logest of ai, i a a a Τότε i i, x x a 1 a0 a f ( x) ax... a0 x a... x x x f ( x) ( x ) Αποδείχτηκε Exercise 5 x for example: 0 x x 1 x x 1 0 1 ( 1), x ( x ) θέλω τετραγωνίζοντας για, πολ/σμοί. 1 Με απλό αλγόριθμο θέλω συνολικά πολ/σμούς η Πιο αποδοτική η 1 μέθοδος. 1