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

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

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

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

Ch5. Divide-and-Conquer

Divide and Conquer Algorithms

Divide-and-Conquer Algorithms

5. DIVIDE AND CONQUER I

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

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

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

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

Divide and Conquer 1

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

Divide and Conquer 4-0

Quick Sort. CSE Data Structures May 15, 2002

Rank. Selection Problem

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

Divide-and-Conquer. Dr. Yingwu Zhu

Randomized Algorithms, Quicksort and Randomized Selection

We can use a max-heap to sort data.

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

Lecture 8: Mergesort / Quicksort Steven Skiena

CS 206 Introduction to Computer Science II

Divide and Conquer. Algorithm Fall Semester

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

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

CSC Design and Analysis of Algorithms

Quicksort (Weiss chapter 8.6)

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

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

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

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

Faster Sorting Methods

Unit-2 Divide and conquer 2016

Programming II (CS300)

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.

II (Sorting and) Order Statistics

Better sorting algorithms (Weiss chapter )

QuickSort

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

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

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

CS 171: Introduction to Computer Science II. Quicksort

Test 1 Review Questions with Solutions

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

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

Lecture 2: Divide and Conquer

L14 Quicksort and Performance Optimization

SORTING AND SELECTION

Copyright 2009, Artur Czumaj 1

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

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

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

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

COMP Parallel Computing. PRAM (2) PRAM algorithm design techniques

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

Sorting and Selection

Lecture 2: Getting Started

Back to Sorting More efficient sorting algorithms

SORTING, SETS, AND SELECTION

Chapter 7 Sorting. Terminology. Selection Sort

Algorithms + Data Structures Programs

Introduction to Algorithms

CSE 373: Data Structures and Algorithms

COT 5407: Introduction. to Algorithms. Giri NARASIMHAN. 1/29/19 CAP 5510 / CGS 5166

Randomized Algorithms: Selection

Data Structures and Algorithms Week 4

PRAM Divide and Conquer Algorithms

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

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

Data Structures and Algorithms Chapter 4

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Programming in Haskell Aug-Nov 2015

CS 171: Introduction to Computer Science II. Quicksort

Quicksort (CLRS 7) We saw the divide-and-conquer technique at work resulting in Mergesort. Mergesort summary:

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

Sorting. Divide-and-Conquer 1

Data Structures and Algorithms

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

The Substitution method

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

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

Analysis of Algorithms - Quicksort -

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

CHAPTER 11 SETS, AND SELECTION

Sorting and Searching

Sorting. Weiss chapter , 8.6

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

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

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

11/8/2016. Chapter 7 Sorting. Introduction. Introduction. Sorting Algorithms. Sorting. Sorting

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Analysis of Algorithms - Medians and order statistic -

Lecture 5: Sorting Part A

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

Lecture 19 Sorting Goodrich, Tamassia

Algorithms - Ch2 Sorting

: Parallel Algorithms Exercises, Batch 1. Exercise Day, Tuesday 18.11, 10:00. Hand-in before or at Exercise Day

Transcription:

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 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 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

Analysis, Example Recurrence relation: 2 2, 1 Guess the solution by drawing the recursion tree: Algorithm Theory, WS 2013/14 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 2013/14 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 2013/14 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 2013/14 Fabian Kuhn 12

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

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 14

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 15

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 16

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

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