University of Waterloo CS240, Winter 2010 Assignment 2

Similar documents
University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm

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

Lecture: Analysis of Algorithms (CS )

Module 3: Sorting and Randomized Algorithms

Module 3: Sorting and Randomized Algorithms. Selection vs. Sorting. Crucial Subroutines. CS Data Structures and Data Management

Module 3: Sorting and Randomized Algorithms

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

Design and Analysis of Algorithms

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

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

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

4. Sorting and Order-Statistics

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

COMP Data Structures

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

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;

Algorithms Lab 3. (a) What is the minimum number of elements in the heap, as a function of h?

CSci 231 Final Review

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Lecture 5: Sorting Part A

Module 2: Priority Queues

Priority queues. Priority queues. Priority queue operations

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

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

DATA STRUCTURES AND ALGORITHMS

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

Midterm solutions. n f 3 (n) = 3

CS 241 Analysis of Algorithms

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

Trees 1: introduction to Binary Trees & Heaps. trees 1

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Properties of a heap (represented by an array A)

Computer Science Spring 2005 Final Examination, May 12, 2005

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

Sorting Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

CS61BL. Lecture 5: Graphs Sorting

COS 226 Midterm Exam, Spring 2009

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

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Data Structures and Algorithms Week 4

Multiple-choice (35 pt.)

Heaps and Priority Queues

Lecture 7. Transform-and-Conquer

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

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

CS 372: Computational Geometry Lecture 3 Line Segment Intersection

Sorting and Searching

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Data Structures and Algorithms

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.

Data Structures and Algorithms Chapter 4

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Outline. CS 561, Lecture 6. Priority Queues. Applications of Priority Queue. For NASA, space is still a high priority, Dan Quayle

Lecture 8: Mergesort / Quicksort Steven Skiena

1 (15 points) LexicoSort

Sorting and Searching

Priority Queues and Binary Heaps

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

The smallest element is the first one removed. (You could also define a largest-first-out priority queue)

Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort

Prelim 2, CS2110. SOLUTION

CS106X Programming Abstractions in C++ Dr. Cynthia Bailey Lee

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Question 7.11 Show how heapsort processes the input:

1.204 Computer Algorithms in Systems Engineering Spring 2010 Problem Set 4: Satellite data sets Due: 12 noon, Monday, March 29, 2010

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

Algorithms and Data Structures

COSC Advanced Data Structures and Algorithm Analysis Lab 3: Heapsort

Searching, Sorting. part 1

Module 2: Priority Queues

Module 2: Priority Queues

CS 3343 (Spring 2018) Assignment 4 (105 points + 15 extra) Due: March 9 before class starts

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

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

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

CS 310 Advanced Data Structures and Algorithms

Tirgul 4. Order statistics. Minimum & Maximum. Order Statistics. Heaps. minimum/maximum Selection. Overview Heapify Build-Heap

Lower Bound on Comparison-based Sorting

Data Structures and Algorithms. Roberto Sebastiani

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Introduction to Algorithms

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Sorting (I) Hwansoo Han

CS 240 Data Structures and Data Management. Module 2: Priority Queues

Lecture 13: AVL Trees and Binary Heaps

Priority Queue: Heap Structures

Sorting Algorithms. For special input, O(n) sorting is possible. Between O(n 2 ) and O(nlogn) E.g., input integer between O(n) and O(n)

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

CS 303 Design and Analysis of Algorithms

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

CS 5321: Advanced Algorithms Sorting. Acknowledgement. Ali Ebnenasir Department of Computer Science Michigan Technological University

Computer Science 302 Spring 2017 (Practice for) Final Examination, May 10, 2017

Transcription:

University of Waterloo CS240, Winter 2010 Assignment 2 Due Date: Wednesday, February 10, at 5:00pm Please read http://www.student.cs.uwaterloo.ca/~cs240/w10/guidelines.pdf for guidelines on submission. In particular, don t forget to make a cover page (https://www. student.cs.uwaterloo.ca/~isg/makecover?course=cs240) and attach it to the front of any written assignment. This assignment has 4 problems, each of which has multiple parts. For problems 1 3, you may use the result of any part to solve any of the following parts, even if you do not complete both parts. Problems 1 3 and Problem 4(e) are written problems; submit your solutions on paper to the assignment boxes in MC. Problem 4(a d) is a programming problem; submit all four indicated files electronically. There are 92 marks available, plus up to 14 bonus marks for the problems indicated. There are three elementary sorting methods which have been covered in previous courses. We give pseudocode for them here, in case you need to refer to them in this assignment. You may also refer to any of the algorithms covered in the course notes. insertion-sort(a) 1. for i 1 to n 1 do 2. j i 3. while j 1 and A[j] < A[j 1] do 4. swap(a[j], A[j 1]) 5. j j 1 selection-sort(a) 1. for i 0 to n 2 do 2. for j i + 1 to n 1 do 3. if A[j] < A[i] then 4. swap(a[i], A[j]) bubble-sort(a) 1. for i 1 to n 1 do 2. for j i to n 1 do 3. if A[j] < A[j 1] then 4. swap(a[j], A[j 1]) 1

Problem 1 Counting swaps [5+5+3+7=20 marks] The swap operation takes two elements in the same array and exchanges their contents. Most of the algorithms in modules 1 3 use only swaps to move items in an array (MergeSort being a notable exception). These algorithms fall under the comparison-swap model, very similar to the comparison model discussed in lectures, but slightly more restricted: In the comparison-swap model, data can only be accessed in two ways: comparing two elements swapping two elements Part 1(a) Consider the partition problem: Given an array A of size n and a pivot index p, define pivot = A[p] and rearrange A so that all items less than or equal to pivot come first, followed by pivot, followed by all items greater than pivot. Prove that any algorithm which solves the partition problem in the comparison-swap model must perform at least n/2 swaps in the worst case. (Hint: what is the worst-case input for this problem?) Part 1(b) Show that the partition algorithm on slide 5 of module 3 performs at most n/2 + 1 swap operations for any input of size n. Part 1(c) [3 marks] Prove that any sorting algorithm in the comparison-swap model must perform Ω(n) swaps in the worst case to sort an array of size n. Part 1(d) [7 marks] Give pseudocode for a sorting algorithm in the comparison-swap model that is asymptotically swap-optimal, meaning that it only performs O(n) swaps in the worst case. (Hint: this can be accomplished with a slight modification to one of the sorting algorithms we have seen.) Give a brief justification that your algorithm performs O(n) swaps in the worst case. 2

Problem 2 Sortedness detection [2+2+(5)+10+6=20 marks] We say an array A of size n is sorted if and only if A[i] < A[j] for all integers i, j satisfying 0 i < j < n. In this problem, we will consider algorithms to solve the sortedness problem: Given an array A of size n, determine whether A is sorted. If it is, return true ; otherwise, return false. Part 2(a) [2 marks] Give pseudocode for a simple, comparison-based algorithm called sort-detect1 that solves the sortedness problem. Show that your algorithm has Θ(n) worst-case running time and Θ(1) best-case running time. Part 2(b) [2 marks] Consider the following algorithm sort-detect2 : sort-detect2(a) 1. for i 0 to min( n/2, lg n ) 1 do 2. if A[2i] > A[2i + 1] then 3. return false 4. return sort-detect1(a) Show that sort-detect2 also solves the sortedness problem with Θ(n) worst-case and Θ(1) best-case running times. Part 2(c) Bonus [maximum 5 marks] There are n! total possible orderings, or permutations, of an input array A with length n. Since sort-detect2 is comparison-based, its running time depends only on the ordering of A. For any permutation σ of n elements, define f(σ) to be the largest value that i is ever set to for an input A with ordering σ. So for example, for any ordering σ where A[0] > A[1], f(σ) = 0. Prove that the number of permutations σ such that f(σ) k, for any integer k 0, is at most n!/2 k. 3

Part 2(d) [10 marks] Prove that the average running time of sort-detect2 is O(1). You should use your result from Part (c). It may also be helpful to use the fact that Part 2(e) [6 marks] i=1 i 2 i = 2. Briefly explain why randomizing the sort-detect2 algorithm would not give O(1) expected worst-case time. Problem 3 Pyramids [3+(4)+3+5+5+8+(5)=24 marks] A pyramid is a data structure similar to a heap that can be used to implement the priority queue ADT. As with a heap, a pyramid is defined by two properties: Structural property: A pyramid P consists for l levels, l 0. The ith level, for 0 i < l, contains at most i + 1 entries, indicated as P i,j for 0 j i. All levels but the last are completely filled, and the last level is left-justified. Ordering property: Any node P i,j has at most two children: P i+1,j and P i+1,j+1, if those nodes exist. The priority of a node is always greater than or equal to the priority of either child node. For example, the following diagram shows a pyramid with 9 nodes and 4 levels. The arrows indicate relationships. 79 68 46 67 35 30 15 34 22 4

Part 3(a) [3+(4) marks] A pyramid P with n nodes can be stored in an array A of size n, similarly to an array-based heap. So, for example, if n 1, the top of the pyramid P 0,0 will be stored in A[0]. Give formulas for the array index that the following pyramid entries will have. Assume that n, i, and j are such that all indicated pyramid nodes actually exist. You do not need to justify your answers. i. P i,j ii. Left and right children of P i,j iii. Left and right parents of P i,j iv. [maximum 2 bonus marks] (Left and right children of the node corresponding to A[i] v. [maximum 2 bonus marks] Left and right parents of the node corresponding to A[i] Part 3(b) [3 marks] Give upper and lower bounds for the number of nodes n in a pyramid P with l levels, where l 1. For example, a pyramid with 2 levels has at least 2 and at most 3 nodes. Make your bounds as tight as possible. Part 3(c) Give pseudocode for the delete-max operation that takes a pyramid stored in an array A of size n, removes the largest element from the pyramid, and returns it. Show that your algorithm has time complexity O( n). Part 3(d) Give pseudocode for the insert operation that takes a pyramid stored in an array A of size n and an element x and inserts the new element into the pyramid. Show that your algorithm has time complexity O( n). Part 3(e) [8 marks] Consider the contains problem: Given an array A of size n and an number x, determine whether x is an element of A. For example, if A is sorted, the contains problem can be solved in O(log n) time by using a binary search. 5

Give pseudocode for an algorithm to solve the contains problem when the input array A is a pyramid as described above. Show that the running time of your algorithm is Θ( n log n). (Hint: A pyramid contains some sorted lists.) Part 3(f) Bonus [maximum 5 marks] Prove that any algorithm to solve the contains problem on an ordinary heap must take Ω(n) time. Problem 4 Comparing comparisons [3+8+8+5+4=28 marks] In class, we analyzed the number of comparisons performed by the heap-sort and quick-sort1 algorithms and determined that they both perform Θ(n log n) comparisons for an input array A of size n. In this problem, you will implement these two algorithms and count the exact number of comparisons they perform on randomly-chosen inputs. Facilitating this task will be the CompareList class, most of which has been written for you. This class represents a list A containing the numbers 0 through n 1 and allowing comparisons and swaps between elements. The following methods have already been defined: CompareList(int n): Creates a new CompareList initialized to [0, 1,..., n 1]. void copy(comparelist L): Copies the list stored in L into this list. int size(): Returns the size of the list boolean lessthan(int i, int j): Returns true if and only if A[i] < A[j]. void swap(int i, int j): Swaps A[i] and A[j] void shuffle(): Randomly changes the order of the array. Part 4(a) [3 marks] We want to use the CompareList class to count the number of comparisons (that is, lessthan operations) that are performed. Complete the implementation of the getcount and resetcount methods, adding a new private field and modifying the lessthan method accordingly. File: compare_list.h or CompareList.java 6

Part 4(b) [8 marks] Complete the implementation of the HeapSort class. The only method you need to write is sort(), which sorts the CompareList object A using the HeapSort algorithm. You may write and use whatever helper methods you like. (Hint: You should not have to write a bubble-up function.) File: heap_sort.h or HeapSort.java Part 4(c) [8 marks] Complete the implementation of the QuickSort class. The only method you need to write is sort(), which sorts the CompareList object A using the quick-sort1 algorithm presented in class. Again, you may write and use whatever helper functions you like. File: quick_sort.h or QuickSort.java Part 4(d) Write an executable main method that does the following: 1. Initialize two integers n and reps from command line arguments 2. Create two CompareList objects A and B, both of size n 3. Shuffle A, copy A to B, and reset both counters 4. Call HeapSort.sort() on A and QuickSort.sort() on B 5. Print out the number of comparisons used by each algorithm. The format is the following, on a single line: the number of comparisons used by HeapSort, followed by a single tab character, followed by the number of comparisons used by QuickSort. 6. Repeat steps 3 5 reps times. File: sorting.cc or Sorting.java Part 4(e) [4 marks] Run some experiments on various sizes from n = 10 to 1000 and briefly summarize the results. Based on your observations, make some generalizations about the number of comparisons made by HeapSort versus QuickSort. This is a written part; no files to submit for this part. 7