cs4102 algorithms
play

CS4102 Algorithms Fall 2018 Warm up Show that finding the minimum - PowerPoint PPT Presentation

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


  1. CS4102 Algorithms Fall 2018 Warm up Show that finding the minimum of an unordered list requires Ξ©(π‘œ) comparisons 1

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

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

  4. Today’s Keywords β€’ Sorting β€’ Linear time Sorting β€’ Counting Sort β€’ Radix Sort 4

  5. CLRS Readings β€’ Chapter 8 5

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

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

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

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

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

  11. 12 Exabytes 11

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

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

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

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

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

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

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

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

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

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

  22. 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 , 𝐢𝐹𝐸 π‘œ

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

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

  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 25 Find Largest sum ending at the cut 0

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

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

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

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

  30. 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 , 𝐢𝐹𝐸 π‘œ

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

  32. 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”

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