Practice Final Solutions. 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) { 2 j = 0 3 if (n = 0) { 4 return j

Similar documents
McGill University Faculty of Science. Solutions to Practice Final Examination Math 240 Discrete Structures 1. Time: 3 hours Marked out of 60

Combinatorial Optimization

MATH 363: Discrete Mathematics

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Limitations of Algorithms

MATH 243E Test #3 Solutions

and e 2 ~ e 2 iff e 1

(1) Which of the following are propositions? If it is a proposition, determine its truth value: A propositional function, but not a proposition.

VIII. NP-completeness

Massachusetts Institute of Technology 6.042J/18.062J, Fall 02: Mathematics for Computer Science Professor Albert Meyer and Dr.

5. Partitions and Relations Ch.22 of PJE.

CHAPTER 1. Relations. 1. Relations and Their Properties. Discussion

(c) Give a proof of or a counterexample to the following statement: (3n 2)= n(3n 1) 2

Exam Practice Problems

Outline. 1 NP-Completeness Theory. 2 Limitation of Computation. 3 Examples. 4 Decision Problems. 5 Verification Algorithm

Spanning Trees in Grid Graphs

Outline Inverse of a Relation Properties of Relations. Relations. Alice E. Fischer. April, 2018

Name: Student ID: Instructions:

2 hours THE UNIVERSITY OF MANCHESTER. 6 June :45 11:45

RELATIONS AND FUNCTIONS

Problem 2: (Section 2.1 Exercise 10) (a.) How many elements are in the power set of the power set of the empty set?

Exam in Discrete Mathematics

Minimum spanning tree

Reexam in Discrete Mathematics

CSC 8301 Design & Analysis of Algorithms: Lower Bounds

Name: Block: Unit 2 Inequalities

HOMEWORK #2 - MATH 3260

Math Homework 5 Solutions

Polynomial-time reductions. We have seen several reductions:

1. (12 points) Give a table-driven parse table for the following grammar: 1) X ax 2) X P 3) P DT 4) T P 5) T ε 7) D 1

ABHELSINKI UNIVERSITY OF TECHNOLOGY

CS/COE

FINAL EXAM PRACTICE PROBLEMS CMSC 451 (Spring 2016)

Section Summary. Relations and Functions Properties of Relations. Combining Relations

More on NP and Reductions

Graph Theory and Optimization Computational Complexity (in brief)

CS/MATH 111 Winter 2013 Final Test

Mathematics for Computer Science MATH 1206C [P.T.O] Page 1 of 5

k 6, then which of the following statements can you assume to be

1. How many labeled trees are there on n vertices such that all odd numbered vertices are leaves?

Algorithm Design Strategies V

University of New Mexico Department of Computer Science. Final Examination. CS 362 Data Structures and Algorithms Spring, 2007

Limitations of Algorithm Power

Discrete Mathematics Fall 2018 Midterm Exam Prof. Callahan. Section: NetID: Multiple Choice Question (30 questions in total, 4 points each)

Bounds on the Traveling Salesman Problem

1 Chapter 1: SETS. 1.1 Describing a set

CS1800 Discrete Structures Fall 2016 Profs. Gold & Schnyder April 25, CS1800 Discrete Structures Final

Tutorial Obtain the principal disjunctive normal form and principal conjunction form of the statement

R(p, k) = the near regular complete k-partite graph of order p. Let p = sk+r, where s and r are positive integers such that 0 r < k.

MODERN APPLIED ALGEBRA

Data Structures and Algorithms (CSCI 340)

Math 564 Homework 1. Solutions.

Worksheet on Relations

Algorithms and Theory of Computation. Lecture 22: NP-Completeness (2)

Data Structures and Algorithms

University of Washington March 21, 2013 Department of Computer Science and Engineering CSEP 521, Winter Exam Solution, Monday, March 18, 2013

Sets, Functions and Relations

Greedy Algorithms My T. UF

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

Relations Graphical View

Data Structures in Java

Mathematical Thinking Exam 2 20 November 2017

STANDARDS OF LEARNING CONTENT REVIEW NOTES. ALGEBRA I Part I. 2 nd Nine Weeks,

Subspaces and Spanning Sets

Planar Graphs (1) Planar Graphs (3) Planar Graphs (2) Planar Graphs (4)

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

MGF 1106: Exam 1 Solutions

Question Paper Code :

Wed Feb The vector spaces 2, 3, n. Announcements: Warm-up Exercise:

1-1. Expressions and Formulas. Lesson 1-1. What You ll Learn. Active Vocabulary

(a) We need to prove that is reflexive, symmetric and transitive. 2b + a = 3a + 3b (2a + b) = 3a + 3b 3k = 3(a + b k)

CS 350 Algorithms and Complexity

X-MA2C01-1: Partial Worked Solutions

The number of Euler tours of random directed graphs

You may hold onto this portion of the test and work on it some more after you have completed the no calculator portion of the test.

CSI 4105 MIDTERM SOLUTION

CMPSCI 311: Introduction to Algorithms Second Midterm Exam

AC64/AT64/ AC115/AT115 DESIGN & ANALYSIS OF ALGORITHMS

USA Mathematical Talent Search Round 2 Solutions Year 27 Academic Year

Homework 1 Submission

Divide-and-conquer: Order Statistics. Curs: Fall 2017

Intro to Contemporary Math

Methods for solving recurrences

Problem 1: Suppose A, B, C and D are finite sets such that A B = C D and C = D. Prove or disprove: A = B.

Parameterized Domination in Circle Graphs

CS325: Analysis of Algorithms, Fall Final Exam

MATH 433 Applied Algebra Lecture 14: Functions. Relations.

Counting and Constructing Minimal Spanning Trees. Perrin Wright. Department of Mathematics. Florida State University. Tallahassee, FL

Selected Solutions to Even Problems, Part 3

Trees. A tree is a graph which is. (a) Connected and. (b) has no cycles (acyclic).

PRELIMINARIES FOR GENERAL TOPOLOGY. Contents

Decomposing planar cubic graphs

NP-completeness. Chapter 34. Sergey Bereg

MA554 Assessment 1 Cosets and Lagrange s theorem

MATH 215 DISCRETE MATHEMATICS INSTRUCTOR: P. WENG

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Introduction to Divide and Conquer

What you learned in Math 28. Rosa C. Orellana

Linear Algebra MATH20F Midterm 1

Transcription:

Practice Final Solutions 1. Consider the following algorithm. Assume that n 1. line code 1 alg(n) 2 j = 0 3 if (n = 0) 4 return j } 5 else 6 j = 2n+ alg(n 1) 7 return j } } Set up a recurrence relation (with initial conditions) whose solution gives L(n), the number of lines executed when alg(n) runs. (You do not need to solve this recurrence relation.) Also set one up for R(n), the return value for alg(n). Solution: Let L(n) be the number of lines executed when alg(n) runs. If n > 0, then we will execute lines 1, 2, 3, 5, and 6. Next we will call alg(n 1), which will execute L(n 1) lines. Last, we will execute line 7. This gives a total of 6+L(n 1) lines. If, on the other hand, n = 0, then we will execute only lines 1, 2, 3, and 4. Therefore, the recurrence relation with initial conditions is L(n) = 6 + L(n 1) n > 0. L(0) = 4 For the return value, if n > 0, the algorithm returns the value in j, which is 2n plus whatever alg(n 1) returns. If n = 0 then the algorithm also returns j, but this time j = 0. Therefore the recurrence relation with initial conditions is 2. Consider the set X = 1, 2}. R(n) = 2n + R(n 1) n > 0. R(0) = 0 (a) List all relations on this set. Which are reflexive? Transitive? Symmetric? Equivalence relations? Solution: A relation is a subset of the set X X. Therefore we need to know what the elements of X X look like. They are the following: 1

X X = (1, 1), (1, 2), (2, 1), (2, 2)}. Since this set has four elements, it should have 2 4 = 16 subsets. Therefore there are 16 relations on the set X. These relations are size sets 0 } 1 (1, 1)}, (1, 2)}, (2, 1)}, (2, 2)} 2 (1, 1), (1, 2)}, (1, 1), (2, 1)}, (1, 1), (2, 2)}, (1, 2), (2, 1)} (1, 2), (2, 2)}, (2, 1), (2, 2)} 3 (1, 1), (1, 2), (2, 1)}, (1, 1), (1, 2), (2, 2)}, (1, 1), (1, 2), (2, 2)}, (1, 2), (2, 1), (2, 2)} 4 (1, 1), (1, 2), (2, 1), (2, 2)} The reflexive relations are (1, 1), (2, 2)}, (1, 1), (2, 2), (1, 2)}, (1, 1), (2, 2), (2, 1)}, The transitive relations are (1, 1), (1, 2), (2, 1), (2, 2)}. }, (1, 1)}, (1, 2)}, (2, 1)}, (2, 2)} (1, 1), (2, 2)}, (1, 1), (2, 1)}, (1, 2), (2, 2)}, (2, 1), (2, 2)}, (1, 1), (1, 2), (2, 2)}, (1, 1), (2, 1), (2, 2)}, The symmetric relations are (1, 1), (1, 2), (2, 1), (2, 2)}. }, (1, 1)}, (2, 2)}, (1, 1), (2, 2)}, (1, 2), (2, 1)}, (1, 1), (1, 2), (2, 1)}, (1, 2), (2, 1), (2, 2)}, (1, 1), (1, 2), (2, 1), (2, 2)}. Therefore, the equivalence relations are (1, 1), (2, 2)}, (1, 1), (1, 2), (2, 1), (2, 2)}. (b) How many of those relations are functions? One to one functions? Onto functions? Invertible functions? Solution: A function is a relation R which has the property that for each x X there is a unique y X such that (x, y) R. Therefore, the functions are (1, 1), (2, 2)}, (1, 2), (2, 2)}, (1, 1), (2, 1)}, (1, 2), (2, 1)}. The one to one functions are 2

(1, 1), (2, 2)}, (1, 2), (2, 1)}. These are also the onto functions, and the invertible functions. (c) List all partitions of X. Solution: Recall that a partition P of X is a set of subsets of X such that i. for each S, T P such that S T, we have S T = }, and ii. the union of all elements of P is the set X. The above definition allows to include the empty set in a partition. I will let you choose whether or not you want to. Here, we will not. Therefore, the partitions of X are 1}, 2}}, 1, 2}}. 3. Out of 30 students, 10 do not have American citizenship and 20 do not have Canadian citizenship. There are 5 students with both American and Canadian citizenship. How many have neither Canadian nor American citizenship? Solution: You should draw a Venn diagram for this problem (and follow along with my explanation). Let A be the set of students who have American citizenship and let C be the set of students who have Canadian citizenship. Let S be the set of all students. We know that the number of elements of S \ A (which we denote by S \ A ) is 10 and that S \ C = 20. This means that In addition, we are told that A = 20 and C = 10. Since we get A C = 5. A C = A + C A C, A C = 20 + 10 5 = 25. This is the number of students with American or Canadian citizenship. Therefore the number of students without either citizenship is 5. 3

4. (a) Solve the following recurrence relation, writing the term L(n) as a function of n. L(n) = 3L(n 1) + 10L(n 2) L(1) = 2 L(2) = 3 Solution: FIrst we consider the characteristic equation of the recurrence: This gives roots Therefore the general solution is r 2 3r 10 = 0, or (r 5)(r + 2) = 0. r 1 = 2, r 2 = 5. L(n) = c 1 ( 2) n + c 2 (5) n. To solve for the constants, use the initial conditions. 2 = L(1) = c 1 ( 2) 1 + c 2 (5) 1 3 = L(2) = c 1 ( 2) 2 + c 2 (5) 2 2 = 2c 1 + 5c 2 3 = 4c 1 + 25c 2 Multiply the first equation by 2 to get 4 = 4c 1 + 10c 2 3 = 4c 1 + 25c 2 and add the two equations to get 7 = 35c 2, so that c 2 = 1/5. Plug this into the original first equation to get Therefore, the specific solution is 2 = 2c 1 + (1/5)(5) 2 = 2c 1 + 1 c 1 = 1/2. L(n) = ( 1/2)( 2) n + (1/5)(5) n = ( 2) n 1 + 5 n 1. 4

(b) Suppose that your answer L(n) above is the number of lines executed for an algorithm that you wrote. Suppose that I have another algorithm which does the same as your algorithm but has a run-time complexity of θ(n 4 ). Which algorithm is better? Prove your answer is correct. Solution: The algorithm with complexity θ(n 4 ) is better. The reason is that it has a lower complexity than the given algorithm, which has run-time complexity θ(5 n ). We can see this as follows: L(n) = ( 2) n 1 + 5 n 1 5 n 1 + 5 n 1 = (1/5)(5) n + (1/5)(5) n = (2/5)5 n, so that L(n) = O(5 n ). To show the other direction, notice that for n 2, so that 2 n 1 (5/2) n 1 (1/2)(5) n 1 (1/2)5 n, L(n) = ( 2) n 1 + 5 n 1 5 n 1 2 n 1 5 n 1 (1/2)5 n 1 = (1/2)5 n 1 = (1/10)5 n. This gives L(n) = Ω(5 n ). Therefore, L(n) = θ(5 n ), which is incredibly greater than θ(n 4 ). 5. (a) Does the graph below have an Euler cycle? Why or why not? Solution: Yes it does. The reason is that the degree of each vertex is even, and we know that this is equivalent to having an Euler cycle. 5

(b) Solve the traveling salesman problem for the graph below. After that, list the order in which edges are added into a minimal spanning tree using Kruskal s algorithm. Next do the same, but for Prim s algorithm, starting at the lower left vertex. Solution: To solve the traveling salesman problem, we note that there are 5 vertices, so any Hamiltonian cycle must have 5 edges. So we will list combinations of 5 edges, in order of increasing total weight, until we find a Hamiltonian cycle. We will list the edges by their weights since they are all distinct. edge combination Hamiltonian cycle? 12345 no 12346 no 12356 no 12456 yes So a solution is the Hamiltonian cycle consisting of edges labeled 1, 2, 4, 5, and 6. The total weight of this solution is 18. Note that this argument does not rule out the existence of other solutions. For Kruskal s algorithm, we add edges into a growing tree, in order of increasing weight, making sure to omit edges which form cycles in our tree. Therefore we will add edges 1, 2, 3, 4 (in order of our addition). Note we stop at 4 edges because there are 5 vertices in the graph, and consequently any spanning tree will have 4 edges. For Prim s algorithm, we invade from the start vertex at the lower left. The edges we will add (in order) are 1, 2, 3, 4. 6