1 1431227-3 File Organization and Processing Week 13 Divide and Conquer.

Slides:



Advertisements
Similar presentations
Divide-and-Conquer 7 2  9 4   2   4   7
Advertisements

AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
A simple example finding the maximum of a set S of n numbers.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
DIVIDE AND CONQUER. 2 Algorithmic Paradigms Greedy. Build up a solution incrementally, myopically optimizing some local criterion. Divide-and-conquer.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 11.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
1 Divide-and-Conquer CSC401 – Analysis of Algorithms Lecture Notes 11 Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm Review the.
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Divide and Conquer. Divide and Conquer is a technique for designing the algorithms that consists of decomposing the instance to be solved into a number.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Advanced Topics in Algorithms and Data Structures Page 1 Parallel merging through partitioning The partitioning strategy consists of: Breaking up the given.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
CS421 - Course Information Website Syllabus Schedule The Book:
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Parallel Merging Advanced Algorithms & Data Structures Lecture Theme 15 Prof. Dr. Th. Ottmann Summer Semester 2006.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
ADA: 3. Insertion Sort1 Objective o asymptotic analysis of insertion sort Algorithm Design and Analysis (ADA) , Semester
Divide-and-Conquer 7 2  9 4   2   4   7
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 – 2. Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Project 2 due … Project 2 due … Project 2 Project 2.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 5 Divide and Conquer Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Getting Started Introduction to Algorithms Jeff Chastine.
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 2: Getting Started.
Introduction to Complexity Analysis. Computer Science, Silpakorn University 2 Complexity of Algorithm algorithm คือ ขั้นตอนการคำนวณ ที่ถูกนิยามไว้อย่างชัดเจนโดยจะ.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
CMPT 438 Algorithms.
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
分治法.
Divide-and-Conquer 6/30/2018 9:16 AM
Chapter 4 Divide-and-Conquer
More on Merge Sort CS 244 This presentation is not given in class
Divide-and-Conquer 7 2  9 4   2   4   7
CSE 2010: Algorithms and Data Structures
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Copyright © Aiman Hanna All rights reserved
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Divide & Conquer Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

File Organization and Processing Week 13 Divide and Conquer

2 A Technique for designing algorithm that decompose instance into smaller subinstances of the same problem –Solving the sub-instances independently –Combining the sub-solutions to obtain the solution of the original instance

3 Divide and Conquer Basic Steps: –Divide: the problem into a number of subproblems that are themselves smaller instances of the same type of problem. –Conquer: Recursively solving these subproblems. If the subproblems are small enough, solve them straightforward. –Combine: the solutions to the subproblems into the solution of original problem.

4 Most common usage Break up problem of size n into two equal parts of size n/2. Solve two parts recursively Combine two solutions into overall solution in linear time.

5 Sort Obviously application –Sort a list of names. – Organize an MP3 library. – Display Google PageRank results. – List RSS news items in reverse chronological order. Problems become easy once items are in sorted order –Find the median. –Find the closest pair. –Binary search in a database. –Identify statistical outliers. –Find duplicates in a mailing list.

6 Applications Non-obvious applications –Data compression. –Computer graphics. –Computational biology. –Supply chain management. –Book recommendations on Amazon. –Load balancing on a parallel computer. –....

7 Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps: –Divide: partition S into two sequences S 1 and S 2 of about n  2 elements each –Recur: recursively sort S 1 and S 2 –Conquer: merge S 1 and S 2 into a unique sorted sequence Algorithm mergeSort(S, C) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size() > 1 (S 1, S 2 )  partition(S, n/2) mergeSort(S 1, C) mergeSort(S 2, C) S  merge(S 1, S 2 )

8 Merge Sort John von Neumann MERGE-SORT A[1.. n] 1.If n = 1, done. 2.Recursively sort A[ 1..  n/2  ] and A[  n/2  +1.. n ]. 3.“Merge” the 2 sorted lists. Key subroutine: “Merge” Divide Conquer Combine

9 Merging two sorted arrays

10 Merging two sorted arrays

11 Merging two sorted arrays

12 3 Merging two sorted arrays

13 3 Merging two sorted arrays

Merging two sorted arrays

Merging two sorted arrays

Merging two sorted arrays

17 43 Merging two sorted arrays

18 43 Merging two sorted arrays

19 43 Merging two sorted arrays Time =  (n) to merge a total of n elements (linear time).

20 Analyzing merge sort M ERGE -S ORT A[1.. n] 1.If n = 1, done. 2.Recursively sort A[ 1..  n/2  ] and A[  n/2  +1.. n ]. 3.“Merge” the 2 sorted lists T(n)  (1) 2T(n/2)  (n)

21 Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n  2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b. Likewise, the basis case ( n < 2) will take at b most steps. Therefore, if we let T(n) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation. –That is, a solution that has T(n) only on the left-hand side.

22 Iterative Substitution In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern: Note that base, T(n)=b, case occurs when 2 i =n. That is, i = log n. So, Thus, T(n) is O(n log n).

23 The Recursion Tree Draw the recursion tree for the recurrence relation and look for a pattern: depthT’ssize 01n 12 n2n2 i2i2i n2in2i ……… time bn … Total time = bn + bn log n (last level plus all previous levels)

24 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

25 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n)T(n)

26 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. T(n/2) cn

27 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn T(n/4) cn/2

28 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) …

29 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n

30 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n cn

31 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n cn

32 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n cn …

33 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n cn #leaves = n (n)(n) …

34 Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant. cn cn/4 cn/2  (1) … h = lg n cn #leaves = n (n)(n) Total  (n lg n) …