CS 360 Exam 1 Fall 2014 Name. 1. Answer the following questions about each code fragment below. [8 points]

Similar documents
1. Answer the following questions about each code fragment below. [8 points]

CS 360 Exam 1 Fall 2014 Solution. 1. Answer the following questions about each code fragment below. [8 points]

1. Answer the following questions about each code fragment below. [8 points]

We can use a max-heap to sort data.

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

CS 303 Design and Analysis of Algorithms

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CSC 273 Data Structures

4.4 Algorithm Design Technique: Randomization

Chapter 2: Complexity Analysis

CS 171: Introduction to Computer Science II. Quicksort

Another Sorting Algorithm

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

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

Pseudo code of algorithms are to be read by.

CS:3330 (22c:31) Algorithms

Design and Analysis of Algorithms - - Assessment

Data Structures and Algorithms

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

CSC Design and Analysis of Algorithms

Faster Sorting Methods

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Lecture 9: Sorting Algorithms

Final Exam in Algorithms and Data Structures 1 (1DL210)

CS 171: Introduction to Computer Science II. Quicksort

4. Sorting and Order-Statistics

Final Exam in Algorithms and Data Structures 1 (1DL210)

Introduction to Algorithms March 12, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 1

Algorithms and Data Structures

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

EECS 2011M: Fundamentals of Data Structures

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

Big-O-ology. Jim Royer January 16, 2019 CIS 675. CIS 675 Big-O-ology 1/ 19

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

O(n): printing a list of n items to the screen, looking at each item once.

CSE 3101: Introduction to the Design and Analysis of Algorithms. Office hours: Wed 4-6 pm (CSEB 3043), or by appointment.

Divide and Conquer. Algorithm D-and-C(n: input size)

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

ECE250: Algorithms and Data Structures Midterm Review

Ceng 111 Fall 2015 Week 12b

Divide and Conquer Algorithms: Advanced Sorting

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2017

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

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

DATA STRUCTURES AND ALGORITHMS

CS302 Topic: Algorithm Analysis #2. Thursday, Sept. 21, 2006

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Data Structures and Algorithms CSE 465

Sorting Algorithms. + Analysis of the Sorting Algorithms

CS 411 Analysis of Algorithms, Fall 2012 Midterm Exam Solutions. The Midterm Exam was given in class on Wednesday, October 17, 2012.

DATA STRUCTURES AND ALGORITHMS

1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

CS61BL. Lecture 5: Graphs Sorting

Divide and Conquer 4-0

Programming in Haskell Aug-Nov 2015

Question 7.11 Show how heapsort processes the input:

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

CS S-02 Algorithm Analysis 1

Introduction to Computers and Programming. Today

CSE 247 Data Structures and Algorithms Fall Exam I

Design and Analysis of Algorithms

CSE Winter 2015 Quiz 1 Solutions

Sorting is ordering a list of objects. Here are some sorting algorithms

The divide and conquer strategy has three basic parts. For a given problem of size n,

Classic Data Structures Introduction UNIT I

Data Structures Question Bank Multiple Choice

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

CS61B Lectures # Purposes of Sorting. Some Definitions. Classifications. Sorting supports searching Binary search standard example

CS141: Intermediate Data Structures and Algorithms Analysis of Algorithms

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

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

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Parallel Sorting Algorithms

CS211 Spring 2005 Prelim 2 April 19, Instructions

Data Structures and Algorithms. Part 2

Remember to also pactice: Homework, quizzes, class examples, slides, reading materials.

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

Cpt S 122 Data Structures. Sorting

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Quiz 1 Practice Problems

Practice Problems for the Final

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Lecture 5: Sorting Part A

Examination Questions Midterm 2

CISC 320 Midterm Exam

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Introduction to Data Structure

Transcription:

CS 360 Exam 1 Fall 2014 Name 1. Answer the following questions about each code fragment below. [8 points] for (v=1; v<=n; v++) for (w=1; w<=v; w++) for (x=v; x<=n; x++) for (y=w; y<=x; y++) z++; for (r=1; r<=n; r++) for (s=n+1; s<=2*n; s++) for (t=r; t<=s; t++) u++; If n=3, how many total times is z incremented? If n=2, how many total times is z incremented? If n=2, how many total times is u incremented? If n=3, how many total times is u incremented? class A { int F( ) { return 3; } int G( ) { return 3 + F( ); } int H( ) { return 6 + G( ); } } class B extends A { int H( ) { return 7 + G( ); } } class C extends B { int G( ) { return 5 + F( ); } } class D extends C { int F( ) { return 6; } } class E extends D { int H( ) { return 9 + G( ); } } A b, c, d, e; b = new B( ); b.h( ); c = new C( ); c.h( ); d = new D( ); d.h( ); e = new E( ); e.h( ); This code is object-oriented. Therefore it uses both inheritance and late method binding (also known as dynamic method binding or dynamic dispatch). What value is returned from the call b.h( )? What value is returned from the call c.h( )? What value is returned from the call d.h( )? What value is returned from the call e.h( )?

2. Evaluate each expression and write the answer in simplest form. [12 points] 100 mod 13 = 13 mod 100 = 7777 9 mod 25 = 3 lg 256 = lg 80 + lg 48 lg 30 = 100 mod 25 = 200 11 k=0 = k=100 k = k=0 2kk = 4 kk log3 243 = log10 0.001 = log16 2 = 3. Questions about running times. [4 points] First suppose we run a program, and we find that T(2) = 0.01 seconds and T(8) = 40.96 seconds. a. If T(n) = c k n, determine the value of constant k. b. If T(n) = c n k, determine the value of constant k. Next we run a different program using an input of size 100, and we find that T(100) = 5 seconds. c. If T(n) = c n 3 for some constant c, then determine T(200). d. If T(n) = c n for some constant c, determine the value of n0 such that T(n0) = 15 seconds.

4. Suppose f(n) and g(n) are related by at least one of the relations O, o, θ, Ω, ω. Match each condition below to its equivalent relation. [13 points] A. f(n) is O(g(n)) B. f(n) is o(g(n)) C. f(n) is θ(g(n)) D. f(n) is Ω(g(n)) E. f(n) is ω(g(n)) F. Contradictory or impossible situation f(n) is θ(g(n)) or f(n) is not Ω(g(n)). f(n) is not o(g(n)) and f(n) is not ω(g(n)). f(n) is Ο(g(n)) and f(n) is ω(g(n)). f(n) is Ο(g(n)) and f(n) is Ω(g(n)). f(n) is Ω(g(n)) and f(n) is o(g(n)). f(n) is θ(g(n)) or f(n) is o(g(n)). f(n) is θ(g(n)) or f(n) is ω(g(n)). f(n) is not θ(g(n)) and f(n) is not o(g(n)). f(n) is not θ(g(n)) and f(n) is not ω(g(n)). f(n) is not Ο(g(n)) and f(n) is not Ω(g(n)). f(n) is not O(g(n)). f(n) is not Ω(g(n)). f(n) is θ(g(n)) or f(n) is not Ο(g(n)).

5. State the running time of each code fragment as a simplest θ function of n. [9 points] for (k=1; k<=n*n*n; k++) for (k=1; k*k*k <=n; k++) for (k=2; k<=n; k*=k) for (k=1; k<=n; k+=k) for (j=1; j<=n; j++) if (j>100 && j<n 100) for (k=1; k<=n; k++) print (j, k); for (j=1; j<=n; j++) if (j<100 j>n 100) for (k=1; k<=n; k++) print (j, k); m=1; for (j=1; j<=n; j++) m*=m; for (k=1; k<=m; k++) m=2; for (j=1; j<=n; j++) m*=m; for (k=1; k<=m; k++) x=0; y=1; for (z=1; z<=n; x+=6, y+=x, z+=y) print (z);

6. Questions about divide-and-conquer recurrences. [9 points] Solve each recurrence, and write the solution as a simplest θ function of n: a. T(n) = 625 T(n/5) + n 4. b. T(n) = 1000 T(n/10) + n 4. c. T(n) = 32 T(n/2) + n 4. Toom-k is a divide-and-conquer algorithm for polynomial multiplication that works as follows: Split each n-term polynomial into k pieces that each have n/k terms. Perform 2k 1 recursive multiplications, along with a constant number of additions and/or subtractions on polynomials that each have n/k terms. d. When k=3, we have Toom-3 or Toom-Cook. Write a recurrence for T(n), and also solve for T(n). e. Next consider Toom-k with arbitrary k. Write a recurrence for T(n), and also solve for T(n). f. Show that for each ε>0 there exist values of k such that Toom-k has running time o(n 1+ε ). Also determine such values of k expressed in terms of ε.

7. Use the array [9, 5, 8, 4, 7, 3, 6, 2] as input for each sorting algorithm below. [6 points] Which choice shows the array contents after two passes of insertion sort? Which choice shows the array contents after four passes of insertion sort? Which choice shows the array contents after two passes of selection sort? Which choice shows the array contents after four passes of selection sort? A. [5, 8, 9, 4, 7, 3, 6, 2] B. [4, 3, 5, 2, 6, 7, 8, 9] C. [4, 5, 3, 6, 2, 7, 8, 9] D. [2, 3, 8, 4, 7, 5, 6, 9] E. [2, 3, 4, 5, 7, 8, 6, 9] F. [4, 5, 8, 9, 7, 3, 6, 2] G. [5, 4, 7, 3, 6, 2, 8, 9] H. [4, 5, 7, 8, 9, 3, 6, 2] I. [9, 5, 8, 4, 2, 3, 6, 7] J. [2, 3, 4, 8, 7, 5, 6, 9] K. None of the above Which choice shows the array contents after two passes of bubble sort? Which choice shows the array contents after four passes of bubble sort? 8. Use the input array [237, 564, 267, 534, 567, 234, 537, 264]. [4 points] Which choice shows the array after the first iteration of an (ascending-order) radix sort using radix r=10? Which choice shows the array after the second iteration of an (ascending-order) radix sort using radix r=10? Which choice shows the array after the first iteration of a descending-order radix sort using radix r=10? Which choice shows the array after the second iteration of a descending-order radix sort using radix r=10? A. [264, 537, 234, 567, 534, 267, 564, 237] B. [237, 267, 567, 537, 564, 534, 234, 264] C. [564, 534, 234, 264, 237, 267, 567, 537] D. [267, 567, 564, 264, 237, 537, 534, 234] E. [534, 234, 237, 537, 564, 264, 267, 567] F. [237, 267, 234, 264, 564, 534, 567, 537] G. [237, 234, 534, 537, 267, 264, 564, 567] H. [564, 534, 567, 537, 237, 267, 234, 264] I. [564, 567, 267, 264, 534, 537, 237, 234] J. [234, 237, 264, 267, 534, 537, 564, 567] K. [567, 564, 537, 534, 267, 264, 237, 234] L. None of the above

9. Use the array [7, 5, 8, 4, 1, 6, 2, 0, 3] as input for each sorting algorithm below. [7 points] Which choice shows the two subarray parameters sent to the two recursive calls of merge sort? Which choice shows the two subarrays returned from the two recursive calls of merge sort? For the following, choose the pivot using median-of- 3 with the low, middle, and high indexes. What is the pivot value? Which choice shows the two subarray parameters sent to the two recursive calls of (not-in-place) quick sort? A. [0, 1, 2, 3] and [4, 5, 6, 7, 8] B. [0, 1, 2, 3] and [5, 6, 7, 8] C. [1, 2, 0] and [7, 5, 8, 4, 6] D. [4, 5, 7, 8] and [0, 1, 2, 3, 6] E. [7, 5, 8, 4, 1] and [6, 2, 0, 3] F. [3, 0, 2, 1] and [4, 6, 8, 5, 7] G. [1, 4, 5, 7, 8] and [0, 2, 3, 6] H. [0, 1, 2, 3, 4] and [5, 6, 7, 8] I. [7, 5, 8, 4] and [1, 6, 2, 0, 3] J. [0, 1, 2] and [4, 5, 6, 7, 8] K. None of the above Which choice shows the two subarrays returned from the two recursive calls of (not-in-place) quick sort? Which choice shows the two subarray parameters sent to the two recursive calls of in-place quick sort? Which choice shows the two subarrays returned from the two recursive calls of in-place quick sort? 10. Questions about sorting algorithms. [4 points] Which algorithm has θ(n lg n) worst-case running time and is also stable? Which algorithm has θ(n lg n) worst-case running time and is also in-place? Which two algorithms are simultaneously both stable and in-place? A. Heap sort B. Counting sort C. Bin sort D. Insertion sort E. Selection sort F. Bubble sort G. Merge sort H. Quick sort I. Radix sort J. None of the above

11. Use the input array [5, 7, 3, 6, 8, 4, 2]. [4 points] Which choice shows the result after inserting each given array element (in the given order) into an array-based minordered binary heap? Which choice shows the result after inserting each given array element (in the given order) into an array-based maxordered binary heap? Which choice shows the result after applying Build-Min- Heap to the given input array? Which choice shows the result after applying Build-Max- Heap to the given input array? A. [2, 4, 8, 6, 3, 7, 5] B. [2, 3, 4, 5, 6, 7, 8] C. [8, 7, 6, 5, 4, 3, 2] D. [2, 6, 3, 7, 8, 4, 5] E. [2, 6, 3, 7, 8, 5, 4] F. [8, 7, 4, 6, 5, 3, 2] G. [8, 7, 4, 5, 6, 3, 2] H. [2, 3, 6, 4, 5, 7, 8] I. [2, 3, 6, 5, 4, 7, 8] J. [8, 4, 7, 3, 2, 6, 5] K. [8, 4, 7, 3, 2, 5, 6] L. None of the above

12. Suppose you are given a sorted list of n elements followed by f(n) additional randomly ordered elements. Make no assumptions about the type or range of the elements. [10 points] a. Explain how to sort the entire list in O(n) time when f(n) is O(1). Justify your answer. b. Explain how to sort the entire list in O(n) time when f(n) is O( n). Justify your answer. c. How large can f(n) be for the entire list still to be sortable in O(n) time? Justify your answer.

13. Divide-and-conquer: [15 points] a. First write the in-place partition function that is used by the in-place quick sort algorithm. You may then call this function as a helper function in parts (b) and (c) below. b. Write a recursive divide-and-conquer algorithm for the selection problem that runs in O(n) average-case time and that uses O(lg n) average-case extra space beyond the input array. c. Write an algorithm for the selection problem that runs in O(n) average-case time and that uses O(1) extra space beyond the input array. Hint: each function call uses some space.