Lecture 2: Divide and Conquer

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

Back to Sorting – More efficient sorting algorithms.
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 APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Lecture 32 CSE 331 Nov 18, HW 8 solutions Friday.
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.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
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],
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Lecture 28 CSE 331 Nov 9, Mini project report due WED.
Algorithms A well-defined computational procedure that takes some value as input and produces some value as output. (Also, a sequence of computational.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II.
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
1 Overview Divide and Conquer Merge Sort Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Lecture 2: Divide and Conquer
Sorting.
Analysis of Algorithms CS 477/677
Lecture 2 Sorting.
Unit 1. Sorting and Divide and Conquer
Introduction to the Design and Analysis of Algorithms
Lecture 4 Divide-and-Conquer
Chapter 7 Sorting Spring 14
Divide and Conquer.
Divide and Conquer – and an Example QuickSort
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Chapter 2: Getting Started
Chapter 4: Divide and Conquer
Divide and Conquer (Merge Sort)
Sorting Algorithms Ellysa N. Kosinaya.
Richard Anderson Lecture 11 Recurrences
Richard Anderson Lecture 13 Divide and Conquer
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Searching: linear & binary
Chapter 4.
Merge and Count Merge and count step.
Merge and Count Merge and count step.
Merge and Count Merge and count step.
CSE 373 Data Structures and Algorithms
Divide & Conquer Algorithms
Topic: Divide and Conquer
Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II
Merge Sort Overview.
Lecture 31 CSE 331 Nov 11, 2011.
Algorithms Sorting.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 19 Order Statistics
Richard Anderson Lecture 14 Divide and Conquer
COMPSCI 330 Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
CSCE 3110 Data Structures & Algorithm Analysis
Richard Anderson Lecture 12, Winter 2019 Recurrences
Lecture 27 CSE 331 Nov 4, 2016.
Divide-and-conquer approach
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Richard Anderson Lecture 12 Recurrences
Advanced Sorting Methods: Shellsort
Presentation transcript:

Lecture 2: Divide and Conquer

Basic Algorithm Design Techniques Divide and conquer Dynamic Programming Greedy Common Theme: To solve a large, complicated problem, break it into many smaller sub-problems.

Divide and Conquer Idea: Break the problem into several unrelated sub- problems, then combine the solutions to solve the original problem. Recipe: 1. (divide) Divide the problem into sub-problems 2. (conquer) Solve the sub-problems recursively. 3. (merge) Combine the solutions.

Example 1 Merge Sort Goal: Sort an array of numbers in ascending order. Input: Array a[] = {6, 2, 4, 1, 5, 3, 7, 8} Algorithm: MergeSort(a[]) IF Length(a) < 2 THEN RETURN a. (Base Case) Partition a[] evenly into two arrays b[], c[]. (Divide) b[] = MergeSort(b[]) c[] = MergeSort(c[]) (Conquer) RETURN Merge(b[], c[]). (Merge)

Merge Sort: Example: {6, 2, 4, 1, 5, 3, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8} {6, 2, 4, 1} {1, 2, 4, 6} {5, 3, 7, 8} {3, 5, 7, 8} {6, 2} {2, 6} {4, 1} {1, 4} {5, 3} {3, 5} {7, 8}

Key Design Step: How to Merge {1, 2, 4, 6} {3, 5, 7, 8} Idea: Smallest element must be between the first elements of the two arrays. Determine the smallest, remove it and continue. b[] = {1, 2, 4, 6} c[] = {3, 5, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8}

Merge Step: Pseudocode b[] = {1, 2, 4, 6} c[] = {3, 5, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8} Merge(b[], c[]) a[] = empty i = 1, j = 1 WHILE i <= Length(b[]) OR j <= Length(c[]) IF b[i] < c[j] THEN a.append(b[i]); i = i+1 ELSE a.append(c[j]); j = j+1 RETURN a[] Careful when you actually write a program (i,j can be out of bound)

Example 2 Counting Inversions Goal: Given array a[], count the number of pairs (i,j) such that i < j but a[i] > a[j]. Input: Array a[] = {6, 2, 4, 1, 5, 3, 7, 8} Answer = 9

Designing the algorithm Recall Recipe: Recipe: 1. (divide) Divide the problem into sub-problems 2. (conquer) Solve the sub-problems recursively. 3. (merge) Combine the solutions.

Failed Attempt: Partition a[] evenly to two arrays b[], c[]. Count Inversions recursively. Combine results a[] = {6, 2, 4, 1, 5, 3, 7, 8}, 9 inversions b[] = {6, 2, 4, 1}, 5 inversions c[] = {5, 3, 7, 8}, 1 inversion #inversion = #inversion in b + #inversion in c + #inversion between b[], c[] Can take O(n2) time!

How to count inversions more efficiently? b[] = {6, 2, 4, 1}, 5 inversions c[] = {5, 3, 7, 8}, 1 inversion For each number in b, want to know how many numbers in c are smaller. 6: 2, 2: 0, 4: 1, 1: 0, result = 2+0+1+0 = 3. Much easier if c[] is sorted! Idea: Can sort the arrays when we are counting inversions.

Sort&Count Sort&Count(a[]) IF Length(a) < 2 THEN RETURN (a[], 0). (Base Case) Partition a[] evenly into two arrays b[], c[]. (Divide) (b[], count_b) = Sort&Count(b[]) (c[], count_c) = Sort&Count(c[]) (Conquer) (a[], count_bc) = Merge&Count(b[], c[]). (Merge) RETURN a[], count_b+count_c+count_bc.

Merge&Count b[] = {1, 2, 4, 6} c[] = {3, 5, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8} When 4 goes into the array, pointer in c[] is pointing at 5 (2nd element), so 4 is larger than 1 element in c[]. When 6 goes into the array, pointer in c[] is pointing at 7 (3rd element), so 6 is larger than 2 elements in c[].

Merge&Count Merge&Count(b[], c[]) a[] = empty i = 1, j = 1 count = 0 WHILE i <= Length(b[]) OR j <= Length(c[]) IF b[i] < c[j] THEN a.append(b[i]); i = i+1 count = count + (j - 1) ELSE a.append(c[j]); j = j+1 RETURN a[], count The i-th element of a[] is smaller than c[j], but larger than c[1,2,…, j-1]