Practical Session 10 - Huffman code, Sort properties, QuickSort algorithm, Selection

Similar documents
Practical Session #11 - Sort properties, QuickSort algorithm, Selection

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

Midterm solutions. n f 3 (n) = 3

EECS 2011M: Fundamentals of Data Structures

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

4. Sorting and Order-Statistics

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

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

SORTING AND SELECTION

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

We can use a max-heap to sort data.

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

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

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

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.

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Sorting and Selection

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

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

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

Premaster Course Algorithms 1 Chapter 2: Heapsort and Quicksort

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

Data Structures and Algorithms Week 4

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

Module 2: Classical Algorithm Design Techniques

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

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

CS61BL. Lecture 5: Graphs Sorting

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

CPE702 Algorithm Analysis and Design Week 7 Algorithm Design Patterns

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

Data Structures and Algorithms Chapter 4

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

Huffman Coding. Version of October 13, Version of October 13, 2014 Huffman Coding 1 / 27

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

Lecture 19 Sorting Goodrich, Tamassia

Sorting Algorithms. + Analysis of the Sorting Algorithms

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

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

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

Data Structures and Algorithms

: Fundamental Data Structures and Algorithms. June 06, 2011 SOLUTIONS

Final Examination. Algorithms & Data Structures II ( )

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Searching, Sorting. part 1

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

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

CS 310 Advanced Data Structures and Algorithms

Lecture Notes for Advanced Algorithms

Multiple-choice (35 pt.)

Cpt S 122 Data Structures. Sorting

SORTING. Comparison of Quadratic Sorts

Solutions to relevant spring 2000 exam problems

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

Introduction. e.g., the item could be an entire block of information about a student, while the search key might be only the student's name

CSE 5095 Topics in Big Data Analytics Spring 2014; Homework 1 Solutions

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

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

SORTING, SETS, AND SELECTION

Overview of Sorting Algorithms

COMP 250 Fall Homework #4

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

CSC Design and Analysis of Algorithms

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Lecture 9: Sorting Algorithms

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

Examination Questions Midterm 2

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

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

QuickSort and Others. Data Structures and Algorithms Andrei Bulatov

Sorting. Task Description. Selection Sort. Should we worry about speed?

Question 7.11 Show how heapsort processes the input:

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Lecture 5: Sorting Part A

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

Algorithms Interview Questions

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

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

Module 3: Sorting and Randomized Algorithms

Lecture: Analysis of Algorithms (CS )

Lecturers: Sanjam Garg and Prasad Raghavendra March 20, Midterm 2 Solutions

Efficient Sequential Algorithms, Comp309. Motivation. Longest Common Subsequence. Part 3. String Algorithms

4.4 Algorithm Design Technique: Randomization

UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

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

COT 5407: Introduction. to Algorithms. Giri NARASIMHAN. 1/29/19 CAP 5510 / CGS 5166

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

Lecture 4 CS781 February 3, 2011

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

Sorting Goodrich, Tamassia Sorting 1

CS221: Algorithms and Data Structures. Sorting Takes Priority. Steve Wolfman (minor tweaks by Alan Hu)

Quicksort (Weiss chapter 8.6)

Comparison Sorts. Chapter 9.4, 12.1, 12.2

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

Transcription:

Practical Session 0 - Huffman code, Sort properties, QuickSort algorithm, Selection Huffman Code Algorithm Description Example Huffman coding is an encoding algorithm used for lossless data compression, using a priority queue. Given data comprised of symbols from the set C (C can be the English alphabet, for example), Huffman code uses a priority queue (Minimum Heap based on symbol frequency) to assign encodings to the different symbols in the. The algorithm builds a binary tree (the Huffman tree) whose leafs are the elements of C. Every symbol in C is associated with a leaf in the Huffman tree. The binary encoding of a symbol is as long as the depth of the leaf associated with it, and contains a 0 bit for every left move and a bit for every right move on the path from the root to that leaf. Example Huffman tree with 4 symbols (C={e,s,x,y}) Numbers signify symbol frequency. Encoding: e: 0 s: 0 x:0 y: 0 e 0 9 0 s 7 9 0 x y Huffman (C) n C Q { new priority queue for the letters in C } for i to n- z allocate new node x Extract_Min(Q) y Extract_Min(Q) z.left x z.right y frequency (z) frequency (x) + frequency (y) Insert(Q, z)

Question A. What is the optimal Huffman code for the following set of frequencies, based on the first 8 Fibonacci numbers? a: b: c: d:3 e:5 f:8 g:3 h: B. Generalize your answer to find the optimal code when the frequencies are the first n Fibonacci numbers, for a general n. A. Since there are 8 letters in the alphabet, the initial queue size is n = 8, and 7 merge steps are required to build the tree. The final tree represents the optimal prefix code. The codeword for a letter is the sequence of the edge labels on the path from the root to the letter. Thus, the optimal Huffman code is as follows: h : 0 g : 0 f : 0 e : 0 d : 0 c : 0 b : 0 a : 54 33 0 7 4 h g 3 f 8 e 5 d 3 c b a B. As we can see the tree is one long limb with leaves n=hanging off. This is true for Fibonacci weights in general, because the Fibonacci the recurrence is implies that F "#$ = We can prove this by induction. The numbers,,,3 provide a sufficient base. We assume the equality holds for all Fibonacci numbers smaller than F n+. Step: We prove correctness for F n+ : " F "#$ = F "#, + F " = "-, '() " '() F ' F ' + + + F " = F ' + Therefore F "#$ < '() F ' + and clearly F "#$ < F "#, so F "#$ is chosen after all smaller Fibonacci numbers have been merged into a single tree. " '()

Question Given the frequency series for a Huffman code as follows: f i ì4 = í î i i = i Î {.. n} Draw the structure of the Huffman Tree that describes this series. Explanation on each level of the tree, be written as: f f j can j = f + f +... + f j - Therefore, on each level we will choose the node with the root of the subtree of f - - - fi- created before, and we will get the tree in the diagram tree diagram 3

Quicksort quicksort( A, low, high ) if( high > low ) pivot partition( A, low, high ) // quicksort( A, low, pivot- ) quicksort( A, pivot+, high ) int partition( A, low, high ) pivot_value A[low] left low pivot left right high while ( left < right ) // Move left while item < pivot while( left < high && A[left] pivot_value) left++ // Move right while item > pivot while( A[right] > pivot_value) right-- if( left < right ) Make sure right has not passed left SWAP(A,left,right) // right is final position for the pivot A[low] A[right] A[right] pivot_item return right quicksort(a,0,length(a)-) v stable sorting algorithms: maintain the relative order of records with equal keys v in place algorithms: need only O(log N) extra memory beyond the items being sorted and they don't need to create auxiliary locations for data to be temporarily stored v QuickSort version above is not stable. 4

Question 3 Given a multi-set S of n integer elements and an index k ( k n), we define the k-smallest element to be the k-th element when the elements are sorted from the smallest to the largest. Suggest an O(n) on average time algorithm for finding the k-smallest element. Example: For the given set of numbers: 6, 3,, 4,,,, 6 The 4-smallest element is since in the is the 4 th element in the sorted set,,,, 3, 4, 6, 6. The algorithm is based on the Quick-Sort algorithm. Quick-Sort : //Reminder quicksort(a,p, r) If (p<r) q partition(a,p,r) quicksort(a,p,q-) quicksort(a,q+,r) // Partition into two parts in Θ r p time. Select(k, S) // returns k-th element in S. pick x in S partition S into: // Slightly different variant of partition() max(l) < x, E = {x}, x < min(g) if k length(l) // Searching for item x. return Select(k, L) else if k length(l) + length(e) // Found return x else // Searching for item x. return Select(k - length(l) - length(e), G) In the worst case: the chosen pivot x is the maximal element in the current array and there is only one such element. G is empty length(e) = and length L = length S T l + O l l < k T l = k l = k The solution of the recursive equation: T n = O n $ In the average case: similar to quick-sort, half of the elements in S are good pivots, for which the size of L and G are each less than 3n/ 4. Therefore, T n T 3n/4 + O n = O n, (master theorem, case c). 5

Question 4 Given an array of n numbers, suggest an Θ n expected time algorithm to determine whether there is a number in A that appears more than n/ times. If x is a number that appears more than n/ times in A, then x is the the array A. n/ + -smallest in Frequent (A,n) x Select ( n/ +, A) // find middle element count 0 for i to n do: // count appearances of middle element if (A[i] = x) count ++ if count > n/ then return TRUE else return FALSE Time Complexity: In the mean case, Select algorithm runs in Θ(n). Computing count takes Θ(n) as well. Total run time in the mean case: Θ(n) 6

Question 5 n records are stored in an array A of size n. Suggest an algorithm to sort the records in O(n) (time) and no additional space in each of the following cases: I. All the keys are 0 or II. All the keys are in the range [..k], k is constant I. Use Quicksort's partition method as we did in question 4 with pivot 0. After the completion of the partition function, the array is sorted (L={}, E will have all elements with key 0, G will have all elements with key ). Time complexity is Θ(n) the cost of one partition. II. First, partition method on A[..n] with pivot, this way all the records with key will be on the first x indices of the array. Second, partition method on A[x +,..,n] with pivot... After k- steps A is sorted Time complexity is O(kn)=O(n) the cost of k partitions. Question 6 Given the following algorithm to sort an array A of size n:. Sort recursively the first /3 of A (A[..n/3]). Sort recursively the last /3 of A (A[n/3+..n]) 3. Sort recursively the first /3 of A (A[..n/3]) * If (/3*n) is not a natural number, round it up. Prove the above algorithm sorts A and find a recurrence T(n), expressing it's running time. The basic assumption is that after the first steps, the n/3 largest number are in their places, sorted in the last third of A. In the last stage the algorithm sorts the left thirds of A. T n = 3T n 3 = 3 3T 4n 9 = 3 3 3T 8n 7 = = 3 ' T 3 ' n after i=logr n steps... S T n = 3 TUV R/SW T = 3 (TUV R ")/ TUV R Y/$ = 3 TUV R ",/(TUV R(Y/$)) = n,/(tuv R(Y/$)) = n (TUV R Y)/(TUV R (Y/$)) = n TUV R/S Y T(n) = O(n log 3/ 3 ), (also according to the Master-Theorem) 7

Question 7 Given an array A of M+N elements, where the first N elements in A are sorted and the last M elements in A are unsorted.. Evaluate the run-time complexity in terms of M and N in the worst case, of fully sorting the array using insertion sort on A?. For each of the following cases, which sort method (or methods combination) would you use and what would be the run-time complexity in the worst case? a) M = O() b) M = O(logN) c) M = O(N). O(M(M+N)) The last M elements will be inserted to their right place and that requires N, N+, N+,...,N+M shifts ( in the worst case ), or O(M + N) if we apply insertion sort to the last M elements and then merge.. a. Insertion-Sort in O(N) b. Use any comparison based sort algorithm that has a runtime of O(MlogM) (Such as merge sort) on the M unsorted elements, and then merge the two sorted parts of the array in O(M + N). Total runtime: O(MlogM + N) = O(N) c. Use any efficient comparison based sort algorithm for a runtime of O((M+N)log(M+N))=O(NlogN). Quick-Sort is bad for this case, as its worst case analysis is O(n ). 8

Question 8 How can we use an unstable sorting (comparisons based) algorithm U (for example, quicksort or heap-sort) to build a new stable sorting algorithm S with the same time complexity as the algorithm U? Solution : U is a comparisons based sorting algorithm, thus it's runtime T U (n) = Ω n log n.. Add a new field, index, to each element. This new field holds the original index of that element in the unsorted array.. Change the comparison operator so that: [key, index ] < [key, index ] Û key < key or ( key = key and index < index ) [key, index ] > [key, index ] Û key > key or (key = key and index > index ) 3. Execute the sorting algorithm U with the new comparison operation. Time complexity: adding an index field is O(n), the sorting time is the same as of the unstable algorithm, T` n, total is T(n) = T` n (as T` n = Ω n log n ). Solution :. Add a new field, index, to each element in the input array A O(n). This new field holds the original index of that element in the input.. Execute U on A to sort the elements by their key T` n 3. Execute U on each set of equal-key elements to sort them by the index field T` n Time complexity of phase 3: assume we have m different keys in the input array( m n), m n i is the number of elements with key k i, where i m and i( n i = n. That is, the time complexity of phase 3 is: T n = m i( T U n i m = Ω n i log n i i( In the worst case all keys in the array are equal (i.e., m=) and the phase 3 is in fact sorting of the array by index values: T(n) = T U (n) = Ω n log n. Time complexity (for entire algorithm): T(n) = T U n + O(n) = O(T U n ). 9