Week 5: Quicksort, Lower bound, Greedy, Schemes and Mind Maps of Data Structures and Algorithms

Best case: each partition is a bipartition !!! Saving as many KC as possible every level ... The recursion tree is as short as possible ... โ€ข Recurrence ...

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 02/28/2023

anuradha
anuradha ๐Ÿ‡บ๐Ÿ‡ธ

4.6

(9)

240 documents

1 / 21

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Week 5: Quicksort, Lower bound, Greedy
Agenda:
โ€ขQuicksort: Average case
โ€ขLower bound for sorting
โ€ขGreedy method
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Week 5: Quicksort, Lower bound, Greedy and more Schemes and Mind Maps Data Structures and Algorithms in PDF only on Docsity!

Week 5: Quicksort, Lower bound, Greedy

Agenda:

โ€ข Quicksort: Average case

โ€ข Lower bound for sorting

โ€ข Greedy method

Week 5: Quicksort

Recall Quicksort:

  • The ideas:
    • Pick one key
    • Compare to others: partition into โ€˜smallerโ€™ and โ€˜greaterโ€™ sublists
    • Recursively sort two sublists
  • Pseudocode:

procedure Quicksort(A, p, r)

if p < r then q โ† Partition(A, p, r) Quicksort(A, p, q โˆ’ 1) Quicksort(A, q + 1, r)

procedure Partition(A, p, r) ** A[r] is the key picked to do the partition

x โ† A[r] i โ† p โˆ’ 1 for j โ† p to r โˆ’ 1 do if A[j] โ‰ค x then i โ† i + 1 exchange A[i] โ†” A[j] exchange A[i + 1] โ†” A[r] return i + 1

Week 5: Quicksort

Quicksort correctness:

  • It follows from the correctness of Partition.
  • Partition correctness:
    • Loop invariant: At the start of for loop:
  1. A[p..i] โ‰ค A[r] โ€” A[s] โ‰ค A[r], p โ‰ค s โ‰ค i
  2. A[(i + 1)..(j โˆ’ 1)] > A[r]
  3. x = A[r]
    • Proof of LI: (pages 147 โ€“ 148)
      1. Initialization
      2. Maintenance
      3. Termination
    • LI correctness implies Partition correctness
  • Why we study QuickSort and its analysis:
    • very efficient, in use
    • divide-and-conquer, randomization
    • huge literature
    • a model for analysis of algorithms

Week 5: Quicksort analysis

Quicksort recursion tree:

  • Observations:
    • (Again) key comparison is the dominant operation
    • Counting KC โ€” only need to know (at each call) the rank of the split key
  • An example: 06

04 09

02 05 07 12

01 03 08 11 13

10 14

15

  • More observations:
    • In the resulting recursion tree, at each node (all keys in left subtree) โ‰ค (key in this node) < (all keys in right subtree)
    • 1-1 correspondence: quicksort recursion tree โ†โ†’ binary search tree

Week 5: Quicksort analysis

Quicksort BC running time:

  • Notice that when both subarrays are non-empty, we will be saving 1 KC ...
  • Best case: each partition is a bipartition !!! Saving as many KC as possible every level ... The recursion tree is as short as possible ...
  • Recurrence:

T (n) = 2 ร— T (

n โˆ’ 1 2

) + (n โˆ’ 1),

  • Solving the recurrence โ€” apply Master Theorem? not exactly T (n) โˆˆ ฮ˜(n log n)
  • Question:
    • What is the best case array? for n = 7?
  • Conclusion:
    • In order to save time, A[n] better BI-partitions the array ... โ€” usually it might not bipartition ... we will push it by a technique called randomization (future lectures)

Week 5: Quicksort

Quicksort BC running time (contโ€™d):

  • In the recursion tree, what is the number of KC at each level? Answer: - n โˆ’ 1 at the top level - at most 2 nodes at the 2nd level, at least (n 1 โˆ’ 1) + (n โˆ’ 1 โˆ’ n 1 โˆ’ 1) = n โˆ’ 3 KC - at most 4 nodes at the 3rd level, at least (n 1 โˆ’ 3) + (n โˆ’ 1 โˆ’ n 1 โˆ’ 3) = n โˆ’ 7 KC -... - at kth level, at most 2kโˆ’^1 nodes, at least n โˆ’ 2 k^ + 1 KC
  • How many levels are there? Answer: - At least lg n levels โ€” binary tree
  • So, at least we need

โˆ‘lg nโˆ’ 1

i=1 (n^ โˆ’^2

i (^) + 1) KC, and

lg โˆ‘ nโˆ’ 1

i=

(n โˆ’ 2 i^ + 1) = (n + 1)(lg n โˆ’ 1) โˆ’ (n โˆ’ 2) โˆˆ ฮ˜(n log n)

  • Try n = 2k^ โˆ’ 1 to get the closed form for the following recur- rence

T (n) =

0 , if n = 1 (n โˆ’ 1) + T (bnโˆ’ 2 1 c) + T (dnโˆ’ 2 1 e), if n โ‰ฅ 2

Week 5: Quicksort

Solving T (n):

T (n) = (^) n^1 (T (0) + T (n โˆ’ 1))

  • (^1) n (T (1) + T (n โˆ’ 2))

+...

  • (^1) n (T (n โˆ’ 2) + T (1))

  • (^1) n (T (n โˆ’ 1) + T (0))

  • (n โˆ’ 1)

= (^) n^2

โˆ‘nโˆ’ 1

i=0 T^ (i) + (n^ โˆ’^ 1)

  • Therefore,
    • n ร— T (n) = 2

โˆ‘nโˆ’ 1

i=0 T^ (i) +^ n(n^ โˆ’^ 1)

  • (n โˆ’ 1) ร— T (n โˆ’ 1) = 2

โˆ‘nโˆ’ 2

i=0 T^ (i) + (n^ โˆ’^ 1)(n^ โˆ’^ 2)

  • Subtract the two terms:

n ร— T (n) โˆ’ (n โˆ’ 1) ร— T (n โˆ’ 1) = 2T (n โˆ’ 1) + 2(n โˆ’ 1)

Rearrange it:

nT (n) = (n + 1)T (n โˆ’ 1) + 2(n โˆ’ 1)

Week 5: Quicksort

Solving T (n) (contโ€™d):

  • Or we can say: T (n) n+1 =^

T (nโˆ’1) n +^

2(nโˆ’1) n(n+1)

= T^ (n nโˆ’ 1)+ (^) n+1^2 โˆ’ 2(^1 n โˆ’ (^) n+1^1 )

= T^ (n nโˆ’ 1)+ (^) n+1^4 โˆ’ (^2) n

which gives you (iterated substitution)

T (n) n + 1

โˆ‘^ n

i=

i + 1

n + 1

Recall that

โˆ‘n

i=

1 i =^ Hn^ = ln^ n^ +^ ฮณ^ โ€” the Harmonic number where ฮณ โ‰ˆ 0. 577 ยท ยท ยท

  • So, from

T (n) n + 1

โˆ‘^ n

i=

i + 1

n + 1

we have T (n) = 2(n + 1)Hn+1 โˆ’ (4n + 2)

โ‰ˆ 2(n + 1)(ln(n + 1) + ฮณ) โˆ’ (4n + 2)

โˆˆ ฮ˜(n log n)

  • Conclusion: Quicksort AC running time in ฮ˜(n log n).

Week 5: Lower Bounds for Comparison-Based Sorting

Two useful trees in algorithm analysis:

  • Recursion tree
    • node โ†โ†’ recursive call
    • describes algorithm execution for one particular input by showing all calls made
    • one algorithm execution โ†โ†’ all nodes (a tree)
    • useful in analysis: sum the numbers of operations over all nodes

Week 5: Lower Bounds for Comparison-Based Sorting

Recursion tree example:

  • Mergesort pseudocode

Merge(A; lo, mid, hi) **pre-condition: lo โ‰ค mid โ‰ค hi **pre-condition: A[lo, mid] and A[mid + 1, hi] sorted **post-condition: A[lo, hi] sorted

MergeSort(A; lo, hi)

if lo < hi then mid โ† b(lo + hi)/ 2 c MergeSort(A; lo, mid) MergeSort(A; mid + 1, hi) Merge(A; lo, mid, hi)

[1]

[2] [3]

[4]

[5] [6]

  • โ€ข For different input instance, the number of operations at each node could be different.

Week 5: Lower Bounds for Comparison-Based Sorting

Selectionsort decision tree:

  • Assume input keys in array A[1..3] = {a, b, c}
  • Tree node: if A[k] > A[j] โ€” 2-way key comparison
  • Node label A[j]

SelectionSort(A; n)

if n โ‰ฅ 1 then for j โ† n downto 2 do psn โ† j for k โ† j โˆ’ 1 downto 1 do if A[k] > A[psn] then psn โ† k exchange A[j] โ†” A[psn] return A[2] > A[3]?

No. Yes.

abc bac bca acb cab cba

  • In every case โ€” whatever input instance is, 3 KC !!!

Week 5: Lower Bounds for Comparison-Based Sorting

Sorting lower bound:

  • Comparison-based sort: keys can be (2-way) compared only!
  • This lower bound argument considers only the comparison- based sorting algorithms. For example, - Insertionsort, Mergesort, Heapsort, Quicksort,
  • Binary tree facts:
    • Suppose there are t leaves and k levels. Then,
    • t โ‰ค 2 kโˆ’^1
    • So, lg t โ‰ค (k โˆ’ 1)
    • Equivalently, k โ‰ฅ 1 + lg t โ€” binary tree with t leaves has at least (1 + lg t) levels
  • Comparison-based sorting algorithm facts:
    • Look at its Decision Tree. We have,
    • Itโ€™s a binary tree.
    • It should contain every possible permutation of the posi- tions { 1 , 2 ,... , n}.
    • So, it contains at least n! leaves ...
    • Equivalently, it has at least 1 + lg(n!) levels.
    • A longest root-to-leaf path of length at least lg(n!).
    • The worst case number of KC is at least lg(n!).
    • lg(n!) โˆˆ ฮ˜(n log n)
  • Therefore, Mergesort and Heapsort are asymptotically optimal (comparison-based) sorting algorithms.

Week 5: Greedy method

Example 1: Fractional Knapsack

  • Suppose we have a set S of n items, each with a profit/value bi and weight wi.
  • We also have a knapsack of capacity W ,
  • Assume that each item can be picked at any fraction, that is we can pick 0 โ‰ค xi โ‰ค wi amount of item i.
  • Our goal is to fill the knapsack (without exceeding its capac- ity) with a combination of the items with maximum profit.
  • Formally, find 0 โ‰ค xi โ‰ค wi for 1 โ‰ค i โ‰ค n such that

โˆ‘n

i=1 xi^ โ‰ค^ W and

โˆ‘n

i=

xi wi ร—^ bi^ is maximized.

  • Greedy idea: start picking the items with more โ€œvalueโ€:

value โ‰ก

bi wi So let vi = (^) wbii. The algorithm will be as follows:

Week 5: Greedy method

  • The pseudocode is:

Procedure Frac-Knapsack (S, W )

for i โ† 1 to n do xi โ† 0 vi โ† (^) wbii CurrentW โ† 0 While CurrentW < W do let ai be the next item in S with highest value xi โ† min{wi, W โˆ’ CurrentW } add xi amount of i to knapsack CurrentW โ† CurrentW + xi

  • How to find next highest value in each step?
  • One way is to sort S at the begining based on viโ€™s in non- increasing order.
  • Another way is to keep a PQ (max-heap) based on values.
  • Since we check at most n items, the total time is O(n log n).

Correctness of the algorithm:

  • We prove, for all i โ‰ฅ 0, that if we have picked x 1 ,... , xi from items 1,... , i in the first i iterations (respectively), then this partial solution can be extended to an optimal solution.
  • In other words, there is some optimal solution call it OPT which has xj amount from item j for 1 โ‰ค j โ‰ค i.