



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 59
This page cannot be seen from the preview
Don't miss anything!
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
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.
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. ...
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 ai ≤ bj , 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
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 ) ≤
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 ). ▪
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 ) + n ≤ n 1 ⎡log 2 n 1 ⎤ + n 2 ⎡log 2 n 2 ⎤ + n ≤ n 1 ⎡log 2 n 2 ⎤ + n 2 ⎡log 2 n 2 ⎤ + n = n ⎡log 2 n ⎤. ▪ = n ⎡log 2 n 2 ⎤ + n ≤ n (⎡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.
A B C D E me you 1 2 3 4 5 1 3 4 2 5 2 inversions: 3-2, 4-
14
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
Q. How to count inversions ( a , b ) with a ∈ A and b ∈ B? A. Easy if A and B are sorted! Warmup algorithm. Sort A and B. For each element b ∈ B , binary search in A to find how elements in A are greater than b.
2 11 16 17 23 sort A 3 7 10 14 18 sort B binary search to count inversions (a, b) with a ∈ A and b ∈ B 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 a ∈ A and b ∈ B , 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.
count inversions (a, b) with a ∈ A and b ∈ B 5 2 2 3 7 10 11 merge to form sorted list C (^3 7 10) ai (^182 11) bj 17 23
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.