CS4102 Algorithms Fall 2018 Warm up Show that finding the minimum of an unordered list requires Ξ©(π) comparisons 1
Find Min, Lower Bound Proof Show that finding the minimum of an unordered list requires Ξ©(π) comparisons Suppose (toward contradiction) that there is an algorithm for π 2 = Ξ©(π) comparisons. Find Min that does fewer than This means there is at least one β uncompared β element We canβt know that this element wasnβt the min! 2 8 19 20 β10 100 3 9 -4 0 1 2 3 4 5 6 7 2
Homeworks β’ Regrade office hours TODAY 4-5pm β Check for new HW2 scores this afternoon β’ Hw3 Due 11pm Wednesday Oct 3 β Divide and conquer β Written (use LaTeX!) β’ Hw4 is out β Sorting β Written 3
Todayβs Keywords β’ Sorting β’ Linear time Sorting β’ Counting Sort β’ Radix Sort 4
CLRS Readings β’ Chapter 8 5
Sorting in Linear Time β’ Cannot be comparison-based β’ Need to make some sort of assumption about the contents of the list β Small number of unique values β Small range of values β Etc. 6
Counting Sort β’ Idea: Count how many things are less than each element π = 3 6 6 1 3 4 1 6 1 2 3 4 5 6 7 8 Range is [1, π] (here [1,6] ) 1. make an array π· of size π π· = 2 0 2 1 0 3 populate with counts of each value 1 2 3 4 5 6 For π in π : running sum + +C π π π· = 2 2 4 5 5 8 Take β running sum β of π· 2. to count things less than each value 1 2 3 4 5 6 To sort: last item of For π = 1 to len(π·) : value 3 goes at index 4 π· π = π· π β 1 + π·[π] 7
Counting Sort β’ Idea: Count how many things are less than each element π· = 2 2 4 5 5 8 π = 3 6 6 1 3 4 1 6 7 1 2 3 4 5 6 1 2 3 4 5 6 7 8 Last item of value 6 goes at index 8 For π = len(π) downto 1 : For each element of π (last to first): Use π· to find its proper place in πΆ πΆ π· π π = π π Decrement that position of C π· π π = π· π π β 1 6 πΆ = 1 2 3 4 5 6 7 8 8
Counting Sort β’ Idea: Count how many things are less than each element π· = 2 2 4 5 5 7 π = 3 6 6 1 3 4 1 6 1 1 2 3 4 5 6 1 2 3 4 5 6 7 8 Last item of value 1 goes at index 2 For π = len(π) downto 1 : For each element of π (last to first) : Use π· to find its proper place in πΆ πΆ π· π π = π π Decrement that position of C π· π π = π· π π β 1 Run Time: π π + π 1 6 πΆ = Memory: π π + π 1 2 3 4 5 6 7 8 9
Counting Sort β’ Why not always use counting sort? β’ For 64-bit numbers, requires an array of length 2 64 > 10 19 β 5 GHz CPU will require > 116 years to initialize the array β 18 Exabytes of data β’ Total amount of data that Google has 10
12 Exabytes 11
Radix Sort β’ Idea: Stable sort on each digit, from least significant to most significant 103 801 401 323 255 823 999 101 113 901 555 512 245 800 018 121 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Place each element into a βbucketβ according to its 1βs place 801 103 401 255 323 800 101 512 555 018 999 823 901 245 113 121 0 1 2 3 4 5 6 7 8 9 12
Radix Sort β’ Idea: Stable sort on each digit, from least significant to most significant 801 103 401 255 323 800 101 512 555 018 999 823 901 245 Place each element into 113 121 a βbucketβ according to 0 1 2 3 4 5 6 7 8 9 its 10βs place 800 801 512 121 401 255 113 323 245 999 101 555 018 823 901 103 0 1 2 3 4 5 6 7 8 9 13
Radix Sort β’ Idea: Stable sort on each digit, from least significant to most significant 800 801 512 121 401 255 113 323 245 999 101 555 018 823 Place each element into 901 103 a βbucketβ according to 0 1 2 3 4 5 6 7 8 9 its 100βs place 101 800 Run Time: π π π + π 103 245 512 901 018 323 401 801 113 255 555 999 π = digits in largest value 823 121 π = base of representation 0 1 2 3 4 5 6 7 8 9 14
Maximum Sum Continuous Subarray Problem The maximum-sum subarray of a given array of integers π΅ is the interval [π, π] such that the sum of all values in the array between π and π inclusive is maximal. Given an array of π integers (may include both positive and negative values), give a π(π log π) algorithm for finding the maximum-sum subarray. 15
Divide and Conquer 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Recursively Divide in half Solve on Right Solve on Left 16
Divide and Conquer Largest sum Largest sum that ends here + that starts here 6 8 -7 2 5 -20 1 -3 -6 -13 -12 13 -37 -42 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Recursively Divide in half Solve on Right Solve on Left 25 19 Find Largest sum that spans the cut 17
Divide and Conquer Return the Max of Left, Right, Center 6 8 -7 2 5 -20 1 -3 -6 -13 -12 13 -37 -42 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Recursively Divide in half Solve on Right Solve on Left 25 19 Find Largest sum that spans the cut 19 18
Divide and Conquer Summary Typically multiple subproblems. β’ Divide Typically all roughly the same size. β Break the list in half β’ Conquer β Find the best subarrays on the left and right β’ Combine β Find the best subarray that βspans the divideβ β I.e. the best subarray that ends at the divide concatenated with the best that starts at the divide
Types of βDivide and Conquerβ β’ Divide and Conquer β Break the problem up into several subproblems of roughly equal size, recursively solve β E.g. Karatsuba, Closest Pair of Points, Mergesortβ¦ β’ Decrease and Conquer β Break the problem into a single smaller subproblem, recursively solve β E.g. Gotham City Police, Quickselect, Binary Search
Pattern So Far β’ Typically looking to divide the problem by some fraction (Β½, ΒΌ the size) β’ Not necessarily always the best! β Sometimes, we can write faster algorithms by finding unbalanced divides.
Unbalanced Divide and Conquer β’ Divide β Make a subproblem of all but the last element β’ Conquer β Find best subarray on the left ( πΆππ(π β 1) ) β Find the best subarray ending at the divide ( πΆπΉπΈ(π β 1) ) β’ Combine β New Best Ending at the Divide: β’ πΆπΉπΈ π = max(πΆπΉπΈ π β 1 + ππ π π , 0) β New best on the left: β’ πΆππ π = max πΆππ π β 1 , πΆπΉπΈ π
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 25 Find Largest sum ending at the cut 22
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 25 Find Largest sum ending at the cut 0
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 25 Find Largest sum ending at the cut 0
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 25 Find Largest sum ending at the cut 25
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 19 Find Largest sum ending at the cut 17
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 19 Find Largest sum ending at the cut 0
5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Recursively Divide Solve on Left 13 Find Largest sum ending at the cut 12
Unbalanced Divide and Conquer β’ Divide β Make a subproblem of all but the last element β’ Conquer β Find best subarray on the left ( πΆππ(π β 1) ) β Find the best subarray ending at the divide ( πΆπΉπΈ(π β 1) ) β’ Combine β New Best Ending at the Divide: β’ πΆπΉπΈ π = max(πΆπΉπΈ π β 1 + ππ π π , 0) β New best on the left: β’ πΆππ π = max πΆππ π β 1 , πΆπΉπΈ π
Why was unbalanced better? β’ Old: β We divided in Half π π = Ξ(π log π) β We solved 2 different problems: β’ Find the best overall on BOTH the left/right β’ Find the best which end/start on BOTH the left/right respectively β Linear time combine β’ New: π π = 1π π β 1 + 1 β We divide by 1, n-1 β We solve 2 different problems: π π = Ξ(π) β’ Find the best overall on the left ONLY β’ Find the best which ends on the left ONLY β Constant time combine
Maximum Sum Continuous Subarray Problem Redux β’ Solve in π(π) by increasing the problem size by 1 each time. β’ Idea: Only include negative values if the positives on both sides of it are βworth itβ
Solution 5 8 -4 3 7 -15 2 8 -20 17 8 -50 -5 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Begin here Remember two values: Best So Far Best ending here 5 5 33
Recommend
More recommend