CSE Winter 2015 Quiz 2 Solutions

Similar documents
Randomized Algorithms: Selection

Test 1 Review Questions with Solutions

Question Points Score TOTAL 50

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

Chapter 5. Decrease-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

U.C. Berkeley CS170 : Algorithms, Fall 2013 Midterm 1 Professor: Satish Rao October 10, Midterm 1 Solutions

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 17: Divide and Conquer Design examples.

COMP Analysis of Algorithms & Data Structures

1 (15 points) LexicoSort

Sorting Pearson Education, Inc. All rights reserved.

CSE 101- Winter 18 Discussion Section Week 2

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Midterm solutions. n f 3 (n) = 3

Recursion: The Beginning

FINAL EXAM SOLUTIONS

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Lecture 22 Tuesday, April 10

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Master Theorem, Introduction to Graphs

Merge Sort. Yufei Tao. Department of Computer Science and Engineering Chinese University of Hong Kong

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 14 Component-I (A) - Personal Details

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Computer Science 431 Algorithms The College of Saint Rose Spring Topic Notes: Decrease and Conquer

Design and Analysis of Algorithms - - Assessment

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

1. (a) O(log n) algorithm for finding the logical AND of n bits with n processors

Trees. 3. (Minimally Connected) G is connected and deleting any of its edges gives rise to a disconnected graph.

EECS 2011M: Fundamentals of Data Structures

We can use a max-heap to sort data.

COL 702 : Assignment 1 solutions

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

CSE Winter 2015 Quiz 1 Solutions

CS473 - Algorithms I

Programming II (CS300)

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

SORTING AND SELECTION

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Mergesort again. 1. Split the list into two equal parts

Name: Lirong TAN 1. (15 pts) (a) Define what is a shortest s-t path in a weighted, connected graph G.

CSE 101 Homework 5. Winter 2015

Divide & Conquer Design Technique

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Greedy algorithms is another useful way for solving optimization problems.

Strongly Connected Components

Divide-and-Conquer. Dr. Yingwu Zhu

COMP 251 Winter 2017 Online quizzes with answers

CMPSCI 311: Introduction to Algorithms Practice Final Exam

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Pseudo code of algorithms are to be read by.

Framework for Design of Dynamic Programming Algorithms

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Encoding/Decoding and Lower Bound for Sorting

CSI 604 Elementary Graph Algorithms

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

CS 6463: AT Computational Geometry Spring 2006 Convex Hulls Carola Wenk

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Algorithms - Ch2 Sorting

CSC Design and Analysis of Algorithms

II (Sorting and) Order Statistics

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

CPSC W2 Midterm #2 Sample Solutions

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Selection (deterministic & randomized): finding the median in linear time

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

Trees. Arash Rafiey. 20 October, 2015

Dr. Amotz Bar-Noy s Compendium of Algorithms Problems. Problems, Hints, and Solutions

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Recursion: The Beginning

Multiple-choice (35 pt.)

CSC Design and Analysis of Algorithms. Lecture 5. Decrease and Conquer Algorithm Design Technique. Decrease-and-Conquer

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Data Structures and Algorithms Running time, Divide and Conquer January 25, 2018

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

CMSC Introduction to Algorithms Spring 2012 Lecture 16

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Divide and Conquer Algorithms

INDIAN STATISTICAL INSTITUTE

Principles of Algorithm Design

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Reading for this lecture (Goodrich and Tamassia):

5. DIVIDE AND CONQUER I

Data Structures and Algorithms CSE 465

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

g(n) time to computer answer directly from small inputs. f(n) time for dividing P and combining solution to sub problems

Reference Sheet for CO142.2 Discrete Mathematics II

CSCE 411 Design and Analysis of Algorithms

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

Transcription:

CSE 101 - Winter 2015 Quiz 2 s January 27, 2015 1. True or False: For any DAG G = (V, E) with at least one vertex v V, there must exist at least one topological ordering. (Answer: True) Fact (from class) If G is a DAG, then G has at least one source (a vertex of indegree 0) and at least one sink (a vertex of outdegree 0). Base Case: When G has one vertex, the only topological ordering is this single vertex, so the Claim is true for n = 1. Induction Hypothesis: The Claim is true for all DAGs with n k vertices. Induction Step: Let G be any DAG with k + 1 vertices. Since G is a DAG, by the Fact, there is at least one sink vertex v in G. Remove v (and any edges incident to v) from G to obtain graph G with k vertices. By the Induction Hypothesis, G has a topological ordering. We can obtain a topological ordering for G by placing vertex v at the end of G s topological ordering. Thus the Claim is true for n = k +1. 2. Which of the following are possible topological orderings of the vertices of this graph? Mark ALL answers that give valid topological orderings. You will get credit ONLY for marking all valid topological orderings (and no other orderings). (Answer: ADBFEC, ADFBEC, AFDBEC) 1

By inspection: A must be first and C must be last in any topological ordering. Either D or F can follow A in a topological ordering; both of these must be ordered before E. If the ordering starts with AD, then either B or F can be next. There are three possible topological orderings: ADBF EC, ADF BEC and AF DBEC. 3. How many distinct topological orderings does the following directed graph have? (Answer: 12) a) 0 b) 4 c) 24 d) 12 e) undefined In any topological ordering, C must precede D (because there is a directed edge from C to D in the graph). The isolated vertices A and B can occur in any positions in a topological ordering. Notice that of the 4! = 24 permutations of A, B, C, D, half have C before D, and half have D before C. So, there are 12 topological orderings of the given graph. 4. Execute DFS on the following directed graph. How many tree, forward, back and cross edges will there be? Remember to break ties correctly! (Answer: 1 tree, 0 forward, 0 back, 4 cross) 2

a) 1 tree, 1 forward, 1 back, 2 cross b) 2 tree, 1 forward, 1 back, 1 cross c) 1 tree, 0 forward, 1 back, 2 cross d) 1 tree, 0 forward, 0 back, 4 cross Ties are broken by lexicographic order. Thus, (pre, post) of A = (1,4), of B = (2,3), of C = (5,6), of D = (7,8). The directed edge (A, B) is a tree edge. Edges (C, A), (C, B), (D, B) and (D, C) are all cross edges. 5. Consider the following pseudo-code: function func(n): if (n > 1): else: endif return func(n/2) + func(n/2) return 1 Which of the following is the correct recurrence relation for the running time T (n) of the function func(n)? (Answer: T (n) = 2T (n/2) + 1) a) T (n) = 2T (n/2) + n b) T (n) = 3T (n/2) + 1 c) T (n) = 3T (n/2) + n d) T (n) = 2T (n/2) + 1 It can be observed that in each recursive step, two subproblems are generated, each half the size of the original problem. The single addition to compose the subsolutions takes unit time. 3

6. The runtime of a divide-and-conquer algorithm is described by the following recurrence: T (n) = 3T (n/2) + O(1). Assume that n is a sufficiently large integer >> (i.e., much greater than) 3 5. How many subproblems will we have at the 5 th level of recursion if the top level is considered to be the 0 th level? (Answer: 243) a) 15 b) 27 c) 81 d) 243 From the recurrence relation, we note that the branching factor a = 3. At level 0, there is one problem. At each subsequent level, the number of subproblems is multiplied by a. Therefore, at the 5 th level of recursion, the total number of subproblems is given by a 5 = 3 5 = 243. 7. The runtime of a divide-and-conquer algorithm is described by the following recurrence: T (n) = 4T (n/2)+n. What is the total contribution to the runtime of the + n term over all subproblems at the third level of the recursion, if the top level is considered to be the 0 th level? Assume that n is a sufficiently large integer such that more than 4 levels of recursion will occur, and hence non-zero work will be done at the 3 rd level. (Answer: 8n) a) n b) 2n c) 8n d) 16n e) 32n The +n term indicates that the amount of work charged to a given subproblem is 1 times the subproblem s size. The total amount of work done at a particular level is given by the number of subproblems at the level, times the problem size of each subproblem at the level. 4

From the recurrence, at the third level of recursion there will be 4 3 = 64 subproblems of size n/2 3 = n/8 each. Hence, the total amount of work done at the third level of recursion is 64 n/8 = 8n. 8. True or False: You are given an array A of size n, whose elements are in sorted order (ascending or descending), and a number k. Every algorithm that takes A and k as inputs and returns true if k is an element in A, and false otherwise, requires Ω(n log n) time in the worst case. (Answer: False) Binary search allows us to search in O(log n) time in the worst case. 9. Consider an algorithm whose running time T (n) is given by the following recurrence: T (n) = 27T (n/3) + O(n d ). What is the largest possible positive integer value of d such that the time complexity of the algorithm is O(n 3 )? (Answer: 2) a) 1 b) 2 c) 3 d) 4 In the given recurrence, a = 27 and b = 3. By the Master Theorem, we will have a running time of O(n 3 ) until a = b d. This happens when d = 3, and the running time with d = 3 is O(n 3 log n). So the largest integer value of d that we seek is 2. 10. A divide-and-conquer algorithm for finding the largest and smallest elements in a given array works by dividing the array into two halves, recursively finding the min and max element in each of the two smaller arrays, and then comparing the two max elements and the two min elements to obtain the overall max and min elements. What is the correct recurrence relation for the running time T (n) of this algorithm? (Answer: T (n) = 2T (n/2) + 2) a) T (n) = 2T (n/2) + 2 b) T (n) = T (3n/4) + T (n/4) + 2 c) T (n) = T (2n/3) + T (n/3) + 2 5

d) None of the above At the recursive step, two subproblems are generated, each with half the problem size. We use exactly two comparisons to compose the subsolutions i.e., one comparison of the two max elements and one comparison of the two min elements. 6