CMPS 102 Solutions to Homework 1

Similar documents
Section IV.1: Recursive Algorithms and Recursion Trees

Analysis of Binary Search algorithm and Selection Sort algorithm

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Sample Induction Proofs

Lecture Notes on Linear Search

Data Structures and Algorithms Written Examination

6.2 Permutations continued

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Near Optimal Solutions

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Data Structures Fibonacci Heaps, Amortized Analysis

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Math 55: Discrete Mathematics

Full and Complete Binary Trees

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Midterm Practice Problems

Sequential Data Structures

Data Structure [Question Bank]

Binary search algorithm

Algorithms. Margaret M. Fleck. 18 October 2010

Data Structures and Algorithms

Binary Search Trees CMPSC 122

Pseudo code Tutorial and Exercises Teacher s Version

Biostatistics 615/815

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Loop Invariants and Binary Search

SCORE SETS IN ORIENTED GRAPHS

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

6. Standard Algorithms

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Solutions to Homework 6

Generating Elementary Combinatorial Objects

Algorithm Design and Analysis Homework #1 Due: 5pm, Friday, October 4, 2013 TA === Homework submission instructions ===

CS473 - Algorithms I

Notes on Determinant

Mathematical Induction. Lecture 10-11

Computer Science 210: Data Structures. Searching

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Lemma 5.2. Let S be a set. (1) Let f and g be two permutations of S. Then the composition of f and g is a permutation of S.

Lecture 3: Finding integer solutions to systems of linear equations

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Lecture 1: Course overview, circuits, and formulas

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Symbol Tables. Introduction

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Approximation Algorithms

CS 103X: Discrete Structures Homework Assignment 3 Solutions

3. Mathematical Induction

Data Structures. Algorithm Performance and Big O Analysis

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Analysis of Algorithms I: Optimal Binary Search Trees

Linear Programming. March 14, 2014

Chapter 3. if 2 a i then location: = i. Page 40

Regular Languages and Finite Automata

HW4: Merge Sort. 1 Assignment Goal. 2 Description of the Merging Problem. Course: ENEE759K/CMSC751 Title:

Sample Questions Csci 1112 A. Bellaachia

Reading 13 : Finite State Automata and Regular Expressions

Analysis of a Search Algorithm

Ordered Lists and Binary Trees

Planar Graphs. Complement to Chapter 2, The Villas of the Bellevue

CS/COE

Fairness in Routing and Load Balancing

Now is the time. For all good men PERMUTATION GENERATION. Princeton University. Robert Sedgewick METHODS

Closest Pair Problem

GRID SEARCHING Novel way of Searching 2D Array

Lecture 16 : Relations and Functions DRAFT

Shortest Path Algorithms

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Small Maximal Independent Sets and Faster Exact Graph Coloring

Lecture 17 : Equivalence and Order Relations DRAFT

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

8 Primes and Modular Arithmetic

Connectivity and cuts

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Dynamic Programming. Lecture Overview Introduction

Dynamic Programming Problem Set Partial Solution CMPSC 465

recursion, O(n), linked lists 6/14

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Data Structures and Data Manipulation

4. How many integers between 2004 and 4002 are perfect squares?

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Lecture Notes on Binary Search Trees

Triangle deletion. Ernie Croot. February 3, 2010

GRAPH THEORY LECTURE 4: TREES

CS473 - Algorithms I

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Data Structures. Topic #12

Problem Set 7 Solutions

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Lecture 12: More on Registers, Multiplexers, Decoders, Comparators and Wot- Nots

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Opposites are all around us. If you move forward two spaces in a board game

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Transcription:

CMPS 0 Solutions to Homework Lindsay Brown, lbrown@soe.ucsc.edu September 9, 005 Problem..- p. 3 For inputs of size n insertion sort runs in 8n steps, while merge sort runs in 64n lg n steps. For which values of n does insertion sort beat merge sort? We want to find where 8n = 64n lg n, this reduces to n = 8 lg n. n 8 lg n 0 8 4 6 8 4 6 3 3 40 64 48 We can see from this table that insertion sort beats merge sort for all values of n below 3, and for values of n above 64 merge sort beats insertion sort. Evaluating the functions for n in the interval (3, 64), we find that for all values of n < 44 insertion sort beats merge sort. See figure. Problem. Induction Claim: For integers n n i(i + ) = n (n + ) Proof by induction on n, the upper limit of the sum. Base case: Let n =. i(i + ) = ( + ) =

Figure : Merge sort and insertion sort running times. and n n + = Inductive step: Suppose it is true for all k such that k n. We want to show it is true for n. Let k = n. Then using the inductive hypothesis: as desired. = n n i(i + ) = i(i + ) + n(n + ) = n n + (n + )(n ) + n(n + ) n(n + ) = n n(n + ) = n n + Problem 3..3-7 p. 37 Describe a Θ(n lg n)-time algorithm that, given a set S of n integers and another integer x, determines whether or not there exist two elements in S whose sum is exactly x. There is more than one algorithm that satisfies this problem. You should have described an algorithm and done the necessary proof for correctness, as well as proved the asymptotic running time. If your algorithm utilizes an algorithm covered in the text, you may assume it is correct and use the results of its run-time analysis.

First run merge sort on S to get the sorted array S = s, s,..., s n, where s i s i+ for all i. As proved in Cormen, this runs in Θ(n lg n)-time. SUM-X(S, x) MERGE SORT (S) i 3 j n 4 while i < j 5 do if S[i] + S[j] < x 6 then i i + 7 else if S[i] + S[j] > x 8 then j j 9 else return S[i], S[j] 0 if i = j then return nil Proof of correctness: Assume that the set S is sorted, so that s < s <... < s n. For p < q n, call (p, q) an x pair if s p + s q = x. There may be many such x pairs. Initially i = and j = n. SUM-X maintains a pair (i, j) and either increases i or decreases j until it finds an x pair, or i = j. If s i + s j > x then j is decremented, and if s i + s j < x then i is incremented. If there are no x pairs in S then SUM-X terminates without finding such a pair. Assume there is at least one x pair. Let (i, j) be the first loop at which either the left index i or the right index j agrees with some x pair. Without loss of generality suppose that j = q and i p, thus s i + s q x. This means that s i + s q = x and i = p, or s i + s q < x and i is increased until i = p. Hence, SUM-X finds an x pair. The worst-case run time of SUM-X is O(n lg n). After the set is sorted, the procedure to find the two elements that sum to x has a worst-case time of O(n). A second solution is to run merge sort to get the sorted array S. Then for each element in S compute d i = s i x and do a binary search for d i in S. In the worst-case we search for n elements using binary search. The worst-case run time analysis of binary search is O(lg n). Therefore, the algorithm is Θ(n lg n). Problem 4..- p. 38 Bubble Sort a. To prove BUBBLESORT is correct you must proved that it terminates, that the output is a permutation of the input array in sorted order, and that the loop invariants for both for loops hold. b. Loop invariant: At the start of each iteration of the for loop in lines -4, there are no elements of lesser value to the subarray to the right of A[j], that is A[j] A[k] for all k where j + k n. Initialization: When j = length[a] there are no higher indexed elements. Maintenance: If A[j ] A[j] then the elements are exchanged, otherwise the elements remain unchanged. The counter j is only decremented after the 3

comparison. Termination: The loop ends when j = i +, thus A[i] and A[i + ] are compared and exchanged if necessary. c. Loop invariant: At the start of each iteration of the for loop in lines -4 the elements in the subarray A[]...A[i ] are in sorted order. Initialization: Prior to the first iteration i =, so the subarray A[]...A[i ] is empty. Maintenance: The smallest element in the subarray A[i]...A[length[A]] is moved to A[i], and the elements in the subarray A[]...A[i ] are unchanged, therefore the loop invariant is maintained. Termination: The loop ends when i = length[a], thus A[] A[]... A[length[A]]. d. The worst case running time for bubble sort occurs if the input array is in reverse sorted order. In this case the algorithm must compare and swap n i = (n )n elements. The worst case for insertion sort is also when the input array is in reverse sorted order. Again, the algorithm must do (n )n/ comparisons and swaps. Problem 5..4 a-c p. 39 Inversions a. List the five inversions of the array (, 3, 8, 6, ). (,), (3,), (8,), (6, ), (8,6) b. What array with elements from the set {,,..., n} has the most inversions and how many inversions does it have? The reverse sorted array n, n,...,, has the most inversions. There are n inversions with the element, n inversions with the element,..., and inversion with the element n. Therefore, the number of inversions is the sum: n (n )n i = c. Claim: The number of swaps done executing insertion sort on array A is equal to the the number of inversions in A, and the number of comparisons done executing insertion sort is also equal to the number of inversions in A. Proof: Let A be the input array. A = a, a,..., a n. Suppose the first i elements are in sorted order and we are at the ith iteration of the for loop, so key = a i. Let (p, q) be an inversion pair such that a p < a q and p > q. For all inversion pairs (i, q) we can say that q < i. Suppose there are i k such elements q. If (i, q) 4

was an inversion before running insertion sort on the subarray A[],..., A[i ], then (i, q) is still an inversion of the resulting array, because the first i loops only modify the position of the first i elements. The i th loop will insert key = a i into the sorted subarray A [],..., A [i] at position k so that a... a k = key... a i, for i k i. The key will be compared to (i ) (k ) = i k elements. All elements a k,..., a i will be shifted one position. Therefore, the number of elements that change position in the array when inserting element i is (i ) k + = i k (we add because the key is also moved). We assumed the number of inversions for element i was i k and showed that the number of comparisons made and the number of elements that moved to insert element i was i k. Therefore, the running time of insertion sort is proportional to the number of inversions in A. Extra Credit..4 d p. 40 Design an algorithm such that: Input: Array A, length[a] = n Output: Number of inversions of A Worst-Case running time: Θ(n lg n) Modify the combine procedure of merge sort (line -7 p. 9). Each time we combine two arrays we add to the total inversion count the number of inversions that have been fixed when combining the two subarrays. When an element from the right array is added to A, add to the inversion count the number of elements remaining in the left array. Add this number of inversions to the total number of inversions of A. Merge sort runs in time Θ(nlogn), and we have only increased the number of operations by a constant factor. Another way to count the number of inversions of an n element array A is to build a graph G with n vertices. Number the vertices,..., n to represent the indices of A. For every inversion pair (p, q) in A, such that a p < a q and p > q add an edge from vertex p to vertex q in G. Then the total number of inversions is the number of edges. Use a recursive algorithm COUNT-EDGES to count the edges of G. The input is a graph with n vertices. Divide the n vertices into two sets (,..., n/) and (n/ +,..., n). Count the edges by recursively calling COUNT-EDGES on subproblems of size n/. Count the number of edges between the edges in the two sets and combine the sets of vertices. Add the edge count of the subarray to the total edge count of G. 5