Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide and Conquer Strategy
Divide-and-Conquer Matrix multiplication and Strassen’s algorithm Median Problem –In general finding the kth largest element of an unsorted list of numbers.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CSE 421 Algorithms Richard Anderson Lecture 11 Recurrences.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
10/13/20151 CS 3343: Analysis of Algorithms Lecture 9: Review for midterm 1 Analysis of quick sort.
Project 2 due … Project 2 due … Project 2 Project 2.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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],
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
COMP 171 Data Structures and Algorithms Tutorial 3 Merge Sort & Quick Sort.
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Order Statistics(Selection Problem)
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Divide-and-Conquer. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
Divide and Conquer Strategy
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Divide and Conquer (Part II) Multiplication of two numbers Let U = (u 2n-1 u 2n-2 … u 1 u 0 ) 2 and V = (v 2n-1 v 2n-2 …v 1 v 0 ) 2, and our goal is to.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 7: Sorting and Directed Graphs.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
1 Overview Divide and Conquer Merge Sort Quick Sort.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Analysis of Algorithms CS 477/677
Quick Sort Divide: Partition the array into two sub-arrays
Algorithm Design & Analysis
CSC 413/513: Intro to Algorithms
CO 303 Algorithm Analysis And Design Quicksort
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Richard Anderson Lecture 12 Recurrences and Divide and Conquer
Algorithms: Design and Analysis
Divide & Conquer Algorithms
CS200: Algorithm Analysis
Quicksort Quick sort Correctness of partition - loop invariant
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương

Lecture 2: Divide and Conquer algorithms 0. Introduction 1. Mergesort 2. Quicksort 3. *Strassen’s algorithm for matrix multiplication

0. Introduction Divide: Divide the problem to subproblems. Conquer: Solve recursively subproblems. Combine: Use results of subproblems and combine them to obtain result of initial problem. Determine Threshold: for which problem, the algorithm return directly result without dividing to smaller problems.

1. Merge sort algorithm Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted answer. MERGE-SORT(A, p, r) if p < r then q← ⌊(p+r)/2⌋ MERGE-SORT(A, p, q) MERGE-SORT(A, q + 1, r) MERGE(A, p, q, r)

Example

Analyzing Merge sort algorithms Divide: D(n) = Θ(1). Conquer: solve two subproblems, each of size n/2, which contributes 2T (n/2) to the running time. Combine: the MERGE procedure on an n-element subarray takes time Θ(n), so C(n) = Θ(n). T(n) = θ(1) if n = 1 2 T(n/2) + θ(n) if n >1 And then T(n) = O (n lg n)

2. Quick Sort algorithms Quick Sort is a sorting algorithm whose worst-case running time is Θ(n2). In spite of this slow worst-case running time, Quick Sort is often the best practical choice for sorting because it is remarkably efficient on the average: its expected running time is Θ(n lg n), and the constant factors hidden in the Θ(n lg n) notation are quite small. It also has the advantage of sorting in place, and it works well even in virtual memory environments. Randomized Quick Sort has an expected running time of 0(n lg n): really efficient.

Idea of Quick Sort Algorithm Divide: Partition (rearrange) the array A[p.. r] into two (possibly empty) subarrays A[p.. q - 1] and A[q + 1..r] such that each element of A[p.. q - 1] is less than or equal to A[q], which is smaller than each element of A[q + 1.. r]. Compute the index q as part of this partitioning procedure. Conquer: Sort the two subarrays A[p.. q -1] and A[q +1.. r] by recursive calls to quicksort. Combine: Since the subarrays are sorted in place, no work is needed to combine them: the entire array A[p.. r] is now sorted.

Quick Sort QUICKSORT(A, p, r) 1 if p < r 2 then q ← PARTITION(A, p, r) 3 QUICKSORT(A, p, q - 1) 4 QUICKSORT(A, q + 1, r)

Partition algorithm PARTITION(A, p, r) 1 x←A[r] 2 i←p-1 3 For j←p to r-1 4 do if A[j] ≤ x 5 then i ← i + 1 6 exchange A[i] ↔ A[j] 7 exchange A[i + 1] ↔ A[r] 8 return i+1 The running time of PARTITION on the subarray A[p.. r] is Θ(n), where n = r - p + 1

Analyzing Quicksort: worst-case partitioning T(n) = T(n-1) + T(0) + Θ(n) = T(n-1) + Θ(n) = Θ(n^2) Question: In each case we have the worst-case ?

Best-case partitioning T(n) ≤ 2T(n/2) + Θ(n) = Θ(n lg n)

Balanced partitioning T(n) ≤ T(9n/10) + T(n/10) + cn

3. Strassen’s algorithm for matrix multiplication Presents Strassen's remarkable recursive algorithm for multiplying n × n matrices, which runs in Θ(n^lg 7) = O(n2.81) time. For sufficiently large values of n, therefore, it outperforms the naive Θ(n3) matrix-multiplication algorithm MATRIX-MULTIPLY from

Idea of Strassen’s algorithm We wish to compute the product C = AB, where each of A, B, and C are n × n matrices. Assuming that n is an exact power of 2, we divide each of A, B, and C into four n/2 × n/2 matrices, rewriting the equation C = AB as follows r = ae + bg s = af + bh t = ce + dg u = cf + dh

Analyzing of Strassen’s algorithm Naïve Algorithm T(n) = 8 T(n/2) + Θ(n^2) Strassen’s algorithm: T(n) = 8 T(n/2) + Θ(n^2) = Θ(n^lg 7) = O(n ^2,81)

Home Works Implement on C (or C++, or Pascal, or Java) the algorithms of sorting: Insertion Sort, Heap Sort, Merge Sort and Quick Sort. Run your program on inputs of arrays of 8 elements, of 16 elements, of 12 elements. Implement the Strassen’s algorithm, and run your program on: an input of two matrix of size 4 an input of two matrix of size 8