Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Divide and Conquer Algorithms: Mergesort, Inversions, Closest Pair, and Quicksort, Thesis of Computer Science

An in-depth exploration of various divide-and-conquer algorithms, including mergesort, counting inversions, closest pair of points, and randomized quicksort. These algorithms are essential in computer science and are used to solve complex problems efficiently. Explanations, visualizations, and code snippets.

Typology: Thesis

2014/2015

Uploaded on 10/12/2015

xu_hao
xu_hao 🇺🇸

1 document

1 / 59

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture slides by Kevin Wayne
Copyright © 2005 Pearson-Addison Wesley
Copyright © 2013 Kevin Wayne
http://www.cs.princeton.edu/~wayne/kleinberg-tardos
Last updated on Oct 2, 2013 9:51 AM
5. DIVIDE AND CONQUER I
mergesort
counting inversions
closest pair of points
randomized quicksort
median and selection
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b

Partial preview of the text

Download Divide and Conquer Algorithms: Mergesort, Inversions, Closest Pair, and Quicksort and more Thesis Computer Science in PDF only on Docsity!

Lecture slides by Kevin Wayne Copyright © 2005 Pearson-Addison Wesley Copyright © 2013 Kevin Wayne h t t p : / / w w w. c s. p r i n c e t o n. e d u / ~ w a y n e / k l e i n b e r g - t a r d o s Last updated on Oct 2, 2013 9:51 AM

5. DIVIDE AND CONQUER I

‣ mergesort

‣ counting inversions

‣ closest pair of points

‣ randomized quicksort

‣ median and selection

Divide-and-conquer paradigm

Divide-and-conquer. Divide up problem into several subproblems. Solve each subproblem recursively. Combine solutions to subproblems into overall solution. Most common usage. Divide problem of size n into two subproblems of size n / 2 in linear time. Solve two subproblems recursively. Combine two solutions into overall solution in linear time. Consequence. Brute force: Θ( n^2 ). Divide-and-conquer: Θ( n log n ). attributed to Julius Caesar

Problem. Given a list of n elements from a totally-ordered universe, rearrange them in ascending order.

Sorting problem

Obvious applications. Organize an MP3 library. Display Google PageRank results. List RSS news items in reverse chronological order. Some problems become easier once elements are sorted. Identify statistical outliers. Binary search in a database. Remove duplicates in a mailing list. Non-obvious applications. Convex hull. Closest pair of points. Interval scheduling / interval partitioning. Minimum spanning trees (Kruskal's algorithm). Scheduling to minimize maximum lateness or average completion time. ...

Sorting applications

Merging

Goal. Combine two sorted lists A and B into a sorted whole C. Scan A and B from left to right. Compare ai and bj. If aibj , append ai to C (no larger than any remaining element in B ). If ai > bj , append bj to C (smaller than every remaining element in A ). sorted list A 5 2 2 3 7 10 11 merge to form sorted list C (^3 7 10) ai (^182 11) bj 17 23 sorted list B

A useful recurrence relation

Def. T ( n ) = max number of compares to mergesort a list of size ≤ n. Note. T ( n ) is monotone nondecreasing. Mergesort recurrence. Solution. T ( n ) is O ( n log 2 n ). Assorted proofs. We describe several ways to prove this recurrence. Initially we assume n is a power of 2 and replace ≤ with =. 0 if n = 1 T ( ⎡ n / 2⎤ ) + T ( ⎣ n / 2⎦ ) + n otherwise T ( n ) ≤

Proof by induction

Proposition. If T ( n ) satisfies the following recurrence, then T ( n ) = n log 2 n. Pf 2. [by induction on n ] Base case: when n = 1, T (1) = 0. Inductive hypothesis: assume T ( n ) = n log 2 n. Goal: show that T (2 n ) = 2 n log 2 (2 n ). assuming n 0 if n = 1 is a power of 2 2 T ( n / 2) + n otherwise T ( n ) = T (2 n ) = 2 T ( n ) + 2 n = 2 n log 2 n + 2 n = 2 n (log 2 ( 2n ) – 1) + 2 n = 2 n log 2 ( 2n ). ▪

Analysis of mergesort recurrence

Claim. If T ( n ) satisfies the following recurrence, then T ( n ) ≤ n ⎡log 2 n ⎤. Pf. [by strong induction on n ] Base case: n = 1. Define n 1 = ⎣ n / 2⎦ and n 2 = ⎡ n / 2⎤. Induction step: assume true for 1, 2, ... , n – 1. 0 if n = 1 T ( ⎡ n / 2⎤ ) + T ( ⎣ n / 2⎦ ) + n otherwise T ( n ) ≤ T ( n ) ≤ T ( n 1 ) + T ( n 2 ) + nn 1 ⎡log 2 n 1 ⎤ + n 2 ⎡log 2 n 2 ⎤ + nn 1 ⎡log 2 n 2 ⎤ + n 2 ⎡log 2 n 2 ⎤ + n = n ⎡log 2 n ⎤. ▪ = n ⎡log 2 n 2 ⎤ + nn (⎡log 2 n ⎤ – 1) + n n 2 = dn/ 2 e  l 2 dlog 2 ne / 2 m = 2 dlog 2 ne / 2 dlog 2 n 2 e  dlog 2 ne 1 n 2 = dn/ 2 e  l 2 dlog 2 ne / 2 m = 2 dlog 2 ne / 2 dlog 2 n 2 e  dlog 2 ne 1 n 2 = dn/ 2 e  l 2 dlog 2 ne / 2 m = 2 dlog 2 ne / 2 dlog 2 n 2 e  dlog 2 ne 1 n 2 = dn/ 2 e  l 2 dlog 2 ne / 2 m = 2 dlog 2 ne / 2 log 2 n 2  dlog 2 ne 1 dlog 2 n 2 e  dlog 2 ne 1

Music site tries to match your song preferences with others. You rank n songs. Music site consults database to find people with similar tastes. Similarity metric: number of inversions between two rankings. My rank: 1, 2, …, n. Your rank: a 1 , a 2 , …, an. Songs i and j are inverted if i < j , but ai > aj. Brute force: check all Θ( n 2 ) pairs.

Counting inversions

A B C D E me you 1 2 3 4 5 1 3 4 2 5 2 inversions: 3-2, 4-

14

Counting inversions: applications

Voting theory. Collaborative filtering. Measuring the "sortedness" of an array. Sensitivity analysis of Google's ranking function. Rank aggregation for meta-searching on the Web. Nonparametric statistics (e.g., Kendall's tau distance). Rank Aggregation Methods for the Web Cynthia Dwork Ravi Kumar Moni Naor D. Sivakumar ABSTRACT

1. INTRODUCTION 1.1 Motivation Rank Aggregation Methods for the Web Cynthia Dwork Ravi Kumar Moni Naor D. Sivakumar **ABSTRACT

  1. INTRODUCTION 1.1 Motivation**

Q. How to count inversions ( a , b ) with aA and bB? A. Easy if A and B are sorted! Warmup algorithm. Sort A and B. For each element bB , binary search in A to find how elements in A are greater than b.

Counting inversions: how to combine two subproblems?

2 11 16 17 23 sort A 3 7 10 14 18 sort B binary search to count inversions (a, b) with aA and bB 5 2 1 1 0 3 7 10 14 18 2 11 16 17 23 17 23 2 11 16 list A 7 10 18 3 14 list B

Count inversions ( a , b ) with aA and bB , assuming A and B are sorted. Scan A and B from left to right. Compare ai and bj. If ai < bj , then ai is not inverted with any element left in B. If ai > bj , then bj is inverted with every element left in A. Append smaller element to sorted list C.

Counting inversions: how to combine two subproblems?

count inversions (a, b) with aA and bB 5 2 2 3 7 10 11 merge to form sorted list C (^3 7 10) ai (^182 11) bj 17 23

Counting inversions: divide-and-conquer algorithm analysis

Proposition. The sort-and-count algorithm counts the number of inversions in a permutation of size n in O ( n log n ) time. Pf. The worst-case running time T ( n ) satisfies the recurrence: Θ(1) if n = 1 T ( ⎡ n / 2⎤ ) + T ( ⎣ n / 2⎦ ) + Θ( n ) otherwise T ( n ) =

SECTION 5.

5. DIVIDE AND CONQUER

‣ mergesort

‣ counting inversions

‣ closest pair of points

‣ randomized quicksort

‣ median and selection