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

Similar documents
Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 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

Divide and Conquer Algorithms

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

Ch5. Divide-and-Conquer

Divide and Conquer 4-0

CSC Design and Analysis of Algorithms

Divide-and-Conquer Algorithms

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

Divide and Conquer 1

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

Divide-and-Conquer. Dr. Yingwu Zhu

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

Test 1 Review Questions with Solutions

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

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

Randomized Algorithms: Selection

SORTING AND SELECTION

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

Lecture 2: Divide and Conquer

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

II (Sorting and) Order Statistics

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

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Analysis of Algorithms - Medians and order statistic -

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

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

Back to Sorting More efficient sorting algorithms

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.

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

PRAM Divide and Conquer Algorithms

Quicksort (Weiss chapter 8.6)

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

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

CHAPTER 11 SETS, AND SELECTION

Quick Sort. CSE Data Structures May 15, 2002

Rank. Selection Problem

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

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

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

CSE Winter 2015 Quiz 2 Solutions

Programming II (CS300)

We can use a max-heap to sort data.

Unit-2 Divide and conquer 2016

Divide and Conquer. Divide and Conquer

Randomized Algorithms, Quicksort and Randomized Selection

Union Find. Data Structures and Algorithms Andrei Bulatov

SORTING, SETS, AND SELECTION

Lecture 8: Mergesort / Quicksort Steven Skiena

Divide and Conquer. Algorithm Fall Semester

Searching, Sorting. part 1

Lecture 9: Sorting Algorithms

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

CS 206 Introduction to Computer Science II

Analysis of Algorithms - Quicksort -

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

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

L14 Quicksort and Performance Optimization

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

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

EECS 2011M: Fundamentals of Data Structures

Data Structures and Algorithms Week 4

Lecture 5: Sorting Part A

Faster Sorting Methods

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

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

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Divide-Conquer-Glue Algorithms

Data Structures and Algorithms Chapter 4

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

4. Sorting and Order-Statistics

Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

CSE 421 Algorithms: Divide and Conquer

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

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

CPSC 320 Midterm 2 Thursday March 13th, 2014

Better sorting algorithms (Weiss chapter )

QuickSort

CS483 Analysis of Algorithms Lecture 03 Divide-n-Conquer

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

Sorting. Divide-and-Conquer 1

Union Find 11/2/2009. Union Find. Union Find via Linked Lists. Union Find via Linked Lists (cntd) Weighted-Union Heuristic. Weighted-Union Heuristic

Sorting and Selection

Introduction to Algorithms

CS473 - Algorithms I

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

Algorithms + Data Structures Programs

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

CS Divide and Conquer

Lecture 19 Sorting Goodrich, Tamassia

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

Midterm solutions. n f 3 (n) = 3

Data Structures and Algorithms CSE 465

COMP Data Structures

CS 171: Introduction to Computer Science II. Quicksort

COMP Analysis of Algorithms & Data Structures

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

Transcription:

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 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 Integer factorization / FFT... Algorithm Theory, WS 2013/14 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 2013/14 Fabian Kuhn 3

Example 2: Mergesort l sort recursively sort recursively l merge l Algorithm Theory, WS 2013/14 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 2013/14 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 2013/14 Fabian Kuhn 6

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

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

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 2013/14 Fabian Kuhn 9

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 2013/14 Fabian Kuhn 10

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 2013/14 Fabian Kuhn 11

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 2013/14 Fabian Kuhn 12

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 2013/14 Fabian Kuhn 13

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 2013/14 Fabian Kuhn 14

Analysis, Guessing Recurrence relation: Repeated substitution: 2 2, 1 Algorithm Theory, WS 2013/14 Fabian Kuhn 15

Analysis, Alternative Guessing Recurrence relation: Recursion Tree: 2 2, 1 Algorithm Theory, WS 2013/14 Fabian Kuhn 16

Analysis, Induction Recurrence relation: Verify by induction: 2 2, 1 Algorithm Theory, WS 2013/14 Fabian Kuhn 17

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 2013/14 Fabian Kuhn 18

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 2013/14 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 Computation of l : min l, l Algorithm Theory, WS 2013/14 Fabian Kuhn 20

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 within distance less than 3. There are at most 7 such points. Algorithm Theory, WS 2013/14 Fabian Kuhn 21

Combine step 4 2 3 1 l min l, Algorithm Theory, WS 2013/14 Fabian Kuhn 22

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 2013/14 Fabian Kuhn 23

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 2013/14 Fabian Kuhn 24

Recurrence Relations: Master Theorem Recurrence relation, for Cases, log Ω,log Θ log,log Algorithm Theory, WS 2013/14 Fabian Kuhn 25