Docsity

Divide and Conquer Algorithms: Merge Sort, Counting Inversions, and Binary Search, Study notes of Computer Science

Partial preview of the text

Download Divide and Conquer Algorithms: Merge Sort, Counting Inversions, and Binary Search and more Study notes Computer Science in PDF only on Docsity!

9/19/ Sofya Raskhodnikova

Algorithm Design and Analysis

LECTURE 11

Divide and Conquer

  • Merge Sort
  • Counting Inversions
  • Binary Search
  • Exponentiation Solving Recurrences
  • Recursion Tree Method

9/19/

Divide and Conquer

  • Break up problem into several parts.
  • Solve each part recursively.
  • Combine solutions to sub-problems into overall solution.
  • Most common usage.
  • Break up problem of size n into two equal parts of size n/2.
  • Solve two parts recursively.
  • Combine two solutions into overall solution in linear time.
  • Consequence.
  • Brute force: Θ(n 2 ).
  • Divide-and-conquer: Θ (n log n). Divide et impera. Veni, vidi, vici.
  • Julius Caesar

9/19/

Mergesort

  • Divide array into two halves.
  • Recursively sort each half.
  • Merge two halves to make sorted whole. merge sort divide A L G O R I T H M S A L G O R I T H M S A G L O R H I M S T A G H I L M O R S T Jon von Neumann (1945) O(n) 2T(n/2) O(1)

9/19/

Merging

  • Combine two pre-sorted lists into a sorted whole.
  • How to merge efficiently?
    • Linear number of comparisons.
    • Use temporary array.
  • Challenge for the bored: in-place merge [Kronrud, 1969] using only a constant amount of extra storage A G L O R H I M S T A G H I

9/19/

Recursion Tree Method

  • Technique for guessing solutions to recurrences
    • Write out tree of recursive calls
    • Each node gets assigned the work done during that call to the procedure (dividing and combining)
    • Total work is sum of work at all nodes
  • After guessing the answer, can prove by induction that it works.

9/19/

Recursion Tree for Mergesort

Solve T ( n ) = 2 T ( n /2) + cn , where c > 0 is constant. T ( n) T ( n /4) T ( n /4) T ( n /4) T ( n /4) T ( n /2) T ( n /2) Τ(1) T(n / 2 k ) h = lg n #leaves = n

9/19/

Recursion Tree for Mergesort

Solve T ( n ) = 2 T ( n /2) + cn , where c > 0 is constant. T ( n /4) T ( n /4) T ( n /4) T ( n /4) Τ(1) T(n / 2 k ) cn cn /2 cn / h = lg n #leaves = n

9/19/

Recursion Tree for Mergesort

Solve T ( n ) = 2 T ( n /2) + cn , where c > 0 is constant. Τ(1) T(n / 2 k ) cn cn /2 cn / cn /4 cn /4 cn /4 cn / h = lg n #leaves = n

9/19/

  • 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 , …, a n .
    • Songs i and j inverted if i < j, but a i > a j .
  • Brute force: check all Θ(n 2 ) pairs i and j.

Counting Inversions

You Me 1 3 4 2 5 1 2 3 4 5 A B C D E Songs Inversions 3-2, 4-

9/19/

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).

9/19/

Counting Inversions: Algorithm

  • Divide-and-conquer
    • Divide : separate list into two pieces. 1 5 4 8 10 2 6 9 12 11 3 7 1 5 4 8 10 2 6 9 12 11 3 7 Divide: Θ(1).

9/19/

Counting Inversions: Algorithm

  • Divide-and-conquer
    • Divide: separate list into two pieces.
    • Conquer : recursively count inversions in each half. 1 5 4 8 10 2 6 9 12 11 3 7 1 5 4 8 10 2 6 9 12 11 3 7 5 blue-blue inversions 8 green-green inversions Divide: Θ(1). Conquer: 2T(n / 2) 5-4, 5-2, 4-2, 8-2, 10-2 6-3, 9-3, 9-7, 12-3, 12-7, 12-11, 11-3, 11-

9/19/ 2 11 16 17 23 25 6 3 2 2 0 0

Counting Inversions: Combine

Combine: count blue-green inversions

  • Assume each half is sorted.
  • Count inversions where a i and a j are in different halves.
  • Merge two sorted halves into sorted whole. to maintain sorted invariant 13 blue-green inversions: 6 + 3 + 2 + 2 + 0 + 0 Count:^ Θ(n) Merge: Θ(n) 3 7 10 14 18 19 2 3 7 10 11 14 16 17 18 19 23 25 T ( n ) = 2 T ( n /2) + Θ ( n ). Solution: T ( n ) = Θ ( n log n ).

9/19/

Implementation

  • Pre-condition. [Merge-and-Count] A and B are sorted.
  • Post-condition. [Sort-and-Count] L is sorted. Sort-and-Count(L) { if list L has one element return 0 and the list L Divide the list into two halves A and B (rA, A) ← Sort-and-Count(A) (rB, B) ← Sort-and-Count(B) (rB, L) ← Merge-and-Count(A, B) return r = rA + rB + r and the sorted list L }