Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

Similar documents
Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

5. DIVIDE AND CONQUER I

Ch5. Divide-and-Conquer

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

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

Divide and Conquer 1

Divide and Conquer Algorithms

Divide-and-Conquer Algorithms

Divide and Conquer 4-0

Divide-and-Conquer. Combine solutions to sub-problems into overall solution. Break up problem of size n into two equal parts of size!n.

Chapter 5. Divide and Conquer. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Plan for Today. Finish recurrences. Inversion Counting. Closest Pair of Points

CSC Design and Analysis of Algorithms

SORTING AND SELECTION

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

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

Quicksort (Weiss chapter 8.6)

Lecture 2: Divide and Conquer

Divide-and-Conquer. Dr. Yingwu Zhu

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

CSE 421 Closest Pair of Points, Master Theorem, Integer Multiplication

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

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

Test 1 Review Questions with Solutions

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

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

Analysis of Algorithms - Medians and order statistic -

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.

Randomized Algorithms, Quicksort and Randomized Selection

II (Sorting and) Order Statistics

Quick Sort. CSE Data Structures May 15, 2002

Randomized Algorithms: Selection

Rank. Selection Problem

Selection Problem. Rank. Divide-And-Conquer Selection. Selection By Sorting

Back to Sorting More efficient sorting algorithms

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

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Lecture 5: Sorting Part A

We can use a max-heap to sort data.

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

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

Divide-Conquer-Glue Algorithms

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

PRAM Divide and Conquer Algorithms

Lecture 8: Mergesort / Quicksort Steven Skiena

Introduction to Algorithms

Searching, Sorting. part 1

Algorithms + Data Structures Programs

Divide and Conquer. Algorithm Fall Semester

CS 206 Introduction to Computer Science II

Analysis of Algorithms - Quicksort -

CSE 421 Algorithms: Divide and Conquer

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

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

CHAPTER 11 SETS, AND SELECTION

EECS 2011M: Fundamentals of Data Structures

Divide and Conquer. Divide and Conquer

Data Structures and Algorithms Week 4

Unit-2 Divide and conquer 2016

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

CSE 202 Divide-and-conquer algorithms. Fan Chung Graham UC San Diego

Union Find. Data Structures and Algorithms Andrei Bulatov

Faster Sorting Methods

SORTING, SETS, AND SELECTION

Data Structures and Algorithms Chapter 4

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

Programming II (CS300)

CSE Winter 2015 Quiz 2 Solutions

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

CPSC 320 Midterm 2 Thursday March 13th, 2014

Lecture 9: Sorting Algorithms

Better sorting algorithms (Weiss chapter )

QuickSort

CS583 Lecture 01. Jana Kosecka. some materials here are based on Profs. E. Demaine, D. Luebke A.Shehu, J-M. Lien and Prof. Wang s past lecture notes

The Substitution method

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Lecture 2: Getting Started

Approximation Algorithms

Sorting. Divide-and-Conquer 1

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

Design and Analysis of Algorithms 演算法設計與分析. Lecture 7 April 15, 2015 洪國寶

CS473 - Algorithms I

Chapter 3 Dynamic Programming Part 2 Algorithm Theory WS 2012/13 Fabian Kuhn

Advanced Algorithms. Problem solving Techniques. Divide and Conquer הפרד ומשול

CS 171: Introduction to Computer Science II. Quicksort

Midterm solutions. n f 3 (n) = 3

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Divide-and-Conquer CSE 680

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

4. Sorting and Order-Statistics

Introduction to Algorithms Third Edition

In what follows, we will focus on Voronoi diagrams in Euclidean space. Later, we will generalize to other distance spaces.

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Transcription:

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

Divide And Conquer Principle Important algorithm design method Examples from Informatik 2: Sorting: Mergesort, Quicksort Binary search can be considered as a divide and conquer algorithm Further examples Median Compairing orders Delaunay triangulation / Voronoi diagram Closest pairs Line intersections Polynomial multiplication / FFT... Algorithm Theory, WS 2015/16 Fabian Kuhn 2

Example 1: Quicksort l function Quick (: sequence): sequence; {returns the sorted sequence } begin if # 1 then return else { choose pivot element in ; partition into l with elements, and with elements return Quick( l ) Quick( ) end; Algorithm Theory, WS 2015/16 Fabian Kuhn 3

Example 2: Mergesort l sort recursively sort recursively l merge Algorithm Theory, WS 2015/16 Fabian Kuhn 4

Formulation of the D&C principle Divide and conquer method for solving a problem instance of size : 1. Divide : Solve the problem directly. : Divide the problem into subproblems of sizes 1,, ( 2). 2. Conquer Solve the subproblems in the same way (recursively). 3. Combine Combine the partial solutions to generate a solution for the original instance. Algorithm Theory, WS 2015/16 Fabian Kuhn 5

Analysis Recurrence relation: : max. number of steps necessary for solving an instance of size Special case:, cost for divide and combine: DC 1 2/2 DC Algorithm Theory, WS 2015/16 Fabian Kuhn 6

Analysis, Example Recurrence relation: 2 2, 1 Guess the solution by repeated substitution: Algorithm Theory, WS 2015/16 Fabian Kuhn 7

Analysis, Example Recurrence relation: Verify by induction: 2 2, 1 Algorithm Theory, WS 2015/16 Fabian Kuhn 8

Analysis, Example Recurrence relation: 2 2, 1 Guess the solution by drawing the recursion tree: Algorithm Theory, WS 2015/16 Fabian Kuhn 9

Comparing Orders Many web systems maintain user preferences / rankings on things like books, movies, restaurants, Collaborative filtering: Predict user taste by comparing rankings of different users. If the system finds users with similar tastes, it can make recommendations (e.g., Amazon) Core issue: Compare two rankings Intuitively, two rankings (of movies) are more similar, the more pairs are ordered in the same way Label the first user s movies from 1 to according to ranking Order labels according to second user s ranking How far is this from the ascending order (of the first user)? Algorithm Theory, WS 2015/16 Fabian Kuhn 10

Number of Inversions Formal problem: Given: array,,,, of distinct elements Objective: Compute number of inversions 0 Example: 4, 1, 5, 2, 7, 10, 6 Naive solution: Algorithm Theory, WS 2015/16 Fabian Kuhn 11

Divide and conquer l 1. Divide array into 2 equal parts l and 2. Recursively compute #inversions in l and 3. Combine: add #pairs l, such that l Algorithm Theory, WS 2015/16 Fabian Kuhn 12

Combine Step: Example Assume l and are sorted 3 5 8 13 14 18 24 25 30 6 7 9 19 21 23 28 32 33 Algorithm Theory, WS 2015/16 Fabian Kuhn 13

Combine Step Assume l and are sorted l Idea: Maintain pointers and to go through the sorted parts While going through the sorted parts, we merge the two parts into one sorted part (like in MergeSort) and we count the number of inversions between the parts Invariant: At each point in time, all inversions involving some element left of (in l ) or left of (in ) are counted and all others still have to be counted... Algorithm Theory, WS 2015/16 Fabian Kuhn 14

Combine Step Assume l and are sorted l Pointers and, initially pointing to first elements of l and If : is smallest among the remaining elements No inversion of and one of the remaining elements Do not change count If : is smallest among the remaining elements is smaller than all remaining elements in l Add number of remaining elements in l to count Increment point, pointing to smaller element Algorithm Theory, WS 2015/16 Fabian Kuhn 15

Combine Step Need sub sequences in sorted order Then, combine step is like merging in merge sort Idea: Solve sorting and #inversions at the same time! 1. Partition into two equal parts l and 2. Recursively compute #inversions and sort l and 3. Merge l and to sorted sequence, at the same time, compute number of inversions between elements in l and in Algorithm Theory, WS 2015/16 Fabian Kuhn 16

Analysis, Guessing Recurrence relation: Repeated substitution: 2 2, 1 Algorithm Theory, WS 2015/16 Fabian Kuhn 17

Analysis, Induction Recurrence relation: Verify by induction: 2 2, 1 Algorithm Theory, WS 2015/16 Fabian Kuhn 18

Geometric divide and conquer Closest Pair Problem: Given a set of points, find a pair of points with the smallest distance. Naive solution: Algorithm Theory, WS 2015/16 Fabian Kuhn 19

Divide and conquer solution 1. Divide: Divide into two equal sized sets l und. 2. Conquer: l mindist l mindist 3. Combine: l min l, l l, return min l,, l l l l Algorithm Theory, WS 2015/16 Fabian Kuhn 20

Divide and conquer solution 1. Divide: Divide into two equal sized sets l und. 2. Conquer: l mindist l mindist 3. Combine: l min l, l l, return min l,, l Computation of l : min l, l Algorithm Theory, WS 2015/16 Fabian Kuhn 21

Merge step 1. Consider only points within distance of the bisection line, in the order of increasing coordinates. 2. For each point consider all points with 3. There are at most 7 such points. Algorithm Theory, WS 2015/16 Fabian Kuhn 22

Combine step 4 2 3 1 l min l, Algorithm Theory, WS 2015/16 Fabian Kuhn 23

Implementation Initially sort the points in in order of increasing coordinates While computing closest pair, also sort according to coord. Partition into l and, solve and sort sub problems recursively Merge to get sorted according to coordinates Center points: points within distance min l, of center Go through center points in in order of incr. coordinates Algorithm Theory, WS 2015/16 Fabian Kuhn 24

Running Time Recurrence relation: 2 2, 1 Solution: Same as for computing number of number of inversions, merge sort (and many others ) log Algorithm Theory, WS 2015/16 Fabian Kuhn 25

Recurrence Relations: Master Theorem Recurrence relation, for Cases, log Ω,log Θ log,log Algorithm Theory, WS 2015/16 Fabian Kuhn 26