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

Similar documents
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.

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

CS Data Structures and Algorithm Analysis

Lecture 7. Transform-and-Conquer

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

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

CPS 616 TRANSFORM-AND-CONQUER 7-1

2. Instance simplification Solve a problem s instance by transforming it into another simpler/easier instance of the same problem

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

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

CSC Design and Analysis of Algorithms

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

COMP 3403 Algorithm Analysis Part 2 Chapters 4 5. Jim Diamond CAR 409 Jodrey School of Computer Science Acadia University

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

CSC Design and Analysis of Algorithms

Module 2: Classical Algorithm Design Techniques

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

4. Apply insertion sort to sort the list E, X, A, M, P, L, E in alphabetical order.

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

MA/CSSE 473 Day 12. Questions? Insertion sort analysis Depth first Search Breadth first Search. (Introduce permutation and subset generation)

Divide-and-Conquer. Dr. Yingwu Zhu

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

CSCE 411 Design and Analysis of Algorithms

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

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

We can use a max-heap to sort data.

AC64/AT64 DESIGN & ANALYSIS OF ALGORITHMS DEC 2014

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

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

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

Divide and Conquer 4-0

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.

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

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

Introduction to Computers and Programming. Today

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Anany Levitin 3RD EDITION. Arup Kumar Bhattacharjee. mmmmm Analysis of Algorithms. Soumen Mukherjee. Introduction to TllG DCSISFI &

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

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

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

Visualizing Data Structures. Dan Petrisko

CSE Winter 2015 Quiz 2 Solutions

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

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

Searching, Sorting. part 1

Lecture 1. Introduction

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

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

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

University of the Western Cape Department of Computer Science

1 Introduction 2. 2 A Simple Algorithm 2. 3 A Fast Algorithm 2

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

CS Elementary Graph Algorithms & Transform-and-Conquer

CS 171: Introduction to Computer Science II. Quicksort

CS61BL. Lecture 5: Graphs Sorting

COMP 251 Winter 2017 Online quizzes with answers

CS Divide and Conquer

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Multiple-choice (35 pt.)

Divide and Conquer. Algorithm Fall Semester

Practice Problems for the Final

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

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

COMP Data Structures

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Sorting and Selection

CS 350 Algorithms and Complexity

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

Randomized Algorithms, Quicksort and Randomized Selection

CSC Design and Analysis of Algorithms. Lecture 4 Brute Force, Exhaustive Search, Graph Traversal Algorithms. Brute-Force Approach

Overview of Sorting Algorithms

Lecture 2: Getting Started

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Introduction and Overview

COMP Analysis of Algorithms & Data Structures

CSE 373 Final Exam 3/14/06 Sample Solution

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

Sorting. Data structures and Algorithms

Design and Analysis of Algorithms - - Assessment

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Lecture 19 Sorting Goodrich, Tamassia

CSCE 411 Design and Analysis of Algorithms

Design and Analysis of Algorithms - Chapter 6 6

Quick Sort. CSE Data Structures May 15, 2002

CS 310 Advanced Data Structures and Algorithms

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

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

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

EECS 2011M: Fundamentals of Data Structures

CSC 505, Spring 2005 Week 6 Lectures page 1 of 9

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

Unit-2 Divide and conquer 2016

Data Structures and Algorithms Chapter 4

Types of recursion. Structural vs. general recursion. Pure structural recursion. Readings: none. In this module: learn to use accumulative recursion

Lecture 2: Divide&Conquer Paradigm, Merge sort and Quicksort

Pseudo code of algorithms are to be read by.

Lecture 10: Recursion vs Iteration

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

Quicksort (Weiss chapter 8.6)

Transcription:

CSC 8301- Design and Analysis of Algorithms Lecture 5 Decrease and Conquer Algorithm Design Technique Decrease-and-Conquer This algorithm design technique is based on exploiting a relationship between a solution to a given instance of the problem in question and its smaller instance. Once such a relationship is found, it can be exploited either top down (usually but not necessarily recursively) or bottom up. It s probably the first alternative to brute force one should try in facing an unfamiliar problem. 1

3 Types of Decrease and Conquer Decrease by a constant (usually by 1): insertion sort topological sorting algorithms for generating permutations, subsets Decrease by a constant factor (usually by half) binary search and bisection method exponentiation by squaring Russian peasant multiplication Variable-size decrease Euclid s algorithm selection by partition Nim-like games What s the difference? Consider the problem of exponentiation: Compute a n Decrease by one: Decrease by half: 2

Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among the sorted A[0..n-2] Usually implemented bottom up (nonrecursively) Example: Sort 6, 4, 1, 8, 5 6 4 1 8 5 4 6 1 8 5 1 4 6 8 5 1 4 6 8 5 1 4 5 6 8 Pseudocode of Insertion Sort 3

Analysis of Insertion Sort Time efficiency C worst (n) = n(n-1)/2 Θ(n 2 ) C avg (n) n 2 /4 Θ(n 2 ) C best (n) = n - 1 Θ(n) (also fast on almost sorted arrays) Space efficiency: in-place Stability: yes Best elementary sorting algorithm overall Dags and Topological Sorting A dag: a directed acyclic graph, i.e. a directed graph with no (directed) cycles a b a b a dag not a dag c d c d Arise in modeling many problems that involve prerequisite constraints (construction projects, document version control) Vertices of a dag can be linearly ordered so that for every edge its starting vertex is listed before its ending vertex (topological sorting). Being a dag is also a necessary condition for topological sorting to be possible. 4

Topological Sorting Example C1 and C2 have no prerequisites C3 requires C1 and C2 C4 C4 requires C3 C5 requires C3 and C4 C3 C5 C1 C2 Students can take only one course per term. In what order should the students take the courses? Topological Sorting: DFS-based Algorithm DFS-based algorithm for topological sorting Perform DFS traversal, noting the order vertices are popped off the traversal stack Reverse order solves topological sorting problem Back edges encountered? NOT a dag! Example: a b c d e f g h Efficiency: 5

Topological Sorting: Source Removal Algorithm Source removal algorithm Repeatedly identify and remove a source (a vertex with no incoming edges) and all the edges incident to it until either no vertex is left (problem is solved) or there is no source among remaining vertices (not a dag) Example: a b c d e f g h Efficiency: same as efficiency of the DFS-based algorithm Generating Permutations Minimal-change decrease-by-one algorithm If n = 1 return 1; otherwise, generate recursively the list of all permutations of 12 n-1 and then insert n into each of those permutations by starting with inserting n into 12...n-1 by moving right to left and then switching direction for each new permutation Example: n=3 start 1 insert 2 into 1 right to left 12 21 insert 3 into 12 right to left 123 132 312 insert 3 into 21 left to right 321 231 213 6

Other permutation generating algorithms Johnson-Trotter (p. 145 in the 3 rd ed. of the textbook) Lexicographic-order algorithm (p. 146 in the 3 rd ed. ) Heap s algorithm (Problem 4 in Exercises 4.3 in the 3 rd ed. ) Generating Subsets Binary reflected Gray code: minimal-change algorithm for generating 2 n bit strings corresponding to all the subsets of an n-element set where n > 0 If n=1 make list L of two bit strings 0 and 1 else generate recursively list L1 of bit strings of length n-1 copy list L1 in reverse order to get list L2 add 0 in front of each bit string in list L1 add 1 in front of each bit string in list L2 append L2 to L1 to get L return L 7

Decrease-by-Constant-Factor Algorithms In this variation of decrease-and-conquer, instance size is reduced by the same factor (typically, 2) Examples: Exponentiation by squaring Binary search and the method of bisection (pp. 460 463) Multiplication à la russe (Russian peasant method) Fake-coin puzzle Binary Search Very efficient algorithm for searching in sorted array: K vs A[0]... A[m]... A[n-1] If K = A[m], stop (successful search); otherwise, continue searching by the same method in A[0..m-1] if K < A[m] and in A[m+1..n-1] if K > A[m] l 0; r n-1 while l r do m (l+r)/2 if K = A[m] return m else if K < A[m] r m-1 else l m+1 return -1 8

Notes on Binary Search Time efficiency worst-case recurrence: C w (n) = 1 + C w ( n/2 ), C w (1) = 1 solution: C w (n) = log 2 (n+1) This is VERY fast: e.g., C w (10 6 ) = 20 Optimal for searching a sorted array Limitations: must be a sorted array (not linked list) Has a continuous counterpart called bisection method for solving equations in one unknown f(x) = 0 (see Sec. 12.4) Russian Peasant Multiplication The problem: Compute the product of two positive integers Can be solved by a decrease-by-half algorithm based on the following formulas. For even values of n: n n * m = 2 * 2m For odd values of n: n * m = n 1 * 2m + m if n > 1 and m if n = 1 2 9

Example of Russian Peasant Multiplication Compute 20 * 26 n m 20 26 10 52 5 104 104 2 208 + 1 416 416 520 Note: Method reduces to adding m s values corresponding to odd n s. Fake-Coin Puzzle (simpler version) There are n identically looking coins one of which is fake. There is a balance scale but there are no weights; the scale can tell whether two sets of coins weigh the same and, if not, which of the two sets is heavier (but not by how much). Design an efficient algorithm for detecting the fake coin. Assume that the fake coin is known to be lighter than the genuine ones. Decrease by factor 2 algorithm Faster algorithm? 10

Variable-Size-Decrease Algorithms In the variable-size-decrease variation of decrease-and-conquer, instance size reduction varies from one iteration to another Examples: Euclid s algorithm for greatest common divisor Partition-based algorithm for selection problem Some algorithms on binary search trees Nim and Nim-like games Euclid s Algorithm Euclid s algorithm is based on repeated application of equality gcd(m, n) = gcd(n, m mod n) Ex.: gcd(80,44) = gcd(44,36) = gcd(36, 12) = gcd(12,0) = 12 One can prove that the size, measured by the second number, decreases at least by half after two consecutive iterations. Hence, T(n) O(log n) 11

Selection Problem Find the k-th smallest element in a list of n numbers k = 1 or k = n median: k = n/2 Example: 4, 1, 10, 9, 7, 12, 8, 2, 15 median =? The median is used in statistics as a measure of an average value of a sample. In fact, it is a better (more robust) indicator than the mean, which is used for the same purpose. Algorithms for the Selection Problem The sorting-based algorithm: Sort and return the k-th element Efficiency (if sorted by mergesort): Θ(nlog n) A faster algorithm is based on the array partitioning: all are A[s] s all are A[s] Assuming that the array is indexed from 0 to n-1 and s is a split position obtained by the array partitioning: If s = k-1, the problem is solved; if s > k-1, look for the k-th smallest element in the left part; if s < k-1, look for the (k-s)-th smallest element in the right part. Note: The algorithm can simply continue until s = k-1 12

Two Partitioning Algorithms There are two principal ways to partition an array: One-directional scan (Lomuto s partitioning algorithm) Two-directional scan (Hoare s partitioning, next lecture) Both algorithms require (n-1) key comparisons Efficiency of Quickselect Average case (average split in the middle): C(n) = C(n/2)+(n-1) C(n) Θ(n) Worst case (degenerate split): C(n) Θ(n 2 ) A more sophisticated choice of the pivot leads to a complicated algorithm with Θ(n) worst-case efficiency. 13

Binary Search Tree Algorithms Several algorithms on BST requires recursive processing of just one of its subtrees, e.g., Searching Insertion of a new key Finding the smallest (or the largest) key k <k >k Searching in Binary Search Tree Algorithm BTS(x, v) //Searches for node with key equal to v in BST rooted at node x if x = NIL return -1 else if v = K(x) return x else if v < K(x) return BTS(left(x), v) else return BTS(right(x), v) Efficiency worst case: C(n) = n average case: C(n) 2ln n 1.4log 2 n 14

Homework Read: Ch. 4 and pp. 485-487 of Appendix B Exercises: 4.1: 1, 4, 7, 9, 10 4.2: 1, 9 4.3: 2a, 9a 4.4: 2, 4, 10 4.5: 2, 7, 13 Next: Divide-and-Conquer (Ch. 5) 15