cse 373 master method fjnishing sorts intro to graphs
play

CSE 373: Master method, fjnishing sorts, intro to graphs Michael - PowerPoint PPT Presentation

CSE 373: Master method, fjnishing sorts, intro to graphs Michael Lee Monday, Feb 12, 2018 1 The tree method: precise analysis Problem: Need a rigorous way of getting a closed form We want to answer a few core questions: How much work does


  1. The tree method: precise analysis 1 height. , num recursive levels might be difgerent then happen at n Important: for other recursive functions, where base case doesn’t For this recursive function, num recursive levels is same as height. 1 2 1 1 2 4 4 How many levels are there, exactly? Is it log 2 ( n ) ? Let’s try an example. Suppose we have T (4) . What happens? Height is log 2 (4) = 2 . Important: total levels, counting base case, is height + 1 .

  2. The tree method: precise analysis 2 height. Important: for other recursive functions, where base case doesn’t For this recursive function, num recursive levels is same as height. 1 1 1 1 2 4 4 How many levels are there, exactly? Is it log 2 ( n ) ? Let’s try an example. Suppose we have T (4) . What happens? Height is log 2 (4) = 2 . Important: total levels, counting base case, is height + 1 . happen at n ≤ 1 , num recursive levels might be difgerent then

  3. The tree method: precise analysis recursiveWork baseCaseWork recursiveWork totalWork workPerLeafNode n numLeafNodes n baseCaseWork workPerNode n i numNodes i i numLevels(n) Our formulas: We discovered: 5 1. numNodes ( i ) = 2 i 2. workPerNode ( n , i ) = n 2 i 3. numLevels ( n ) = log 2 ( n ) 4. workPerLeafNode ( n ) = 1 = 2 numLevels ( n ) = 2 log 2 ( n ) = n 5. numLeafNodes ( n )

  4. The tree method: precise analysis We discovered: baseCaseWork recursiveWork totalWork workPerLeafNode n numLeafNodes n baseCaseWork numLevels(n) Our formulas: 5 1. numNodes ( i ) = 2 i 2. workPerNode ( n , i ) = n 2 i 3. numLevels ( n ) = log 2 ( n ) 4. workPerLeafNode ( n ) = 1 = 2 numLevels ( n ) = 2 log 2 ( n ) = n 5. numLeafNodes ( n ) � recursiveWork = numNodes ( i ) · workPerNode ( n , i ) i =0

  5. The tree method: precise analysis We discovered: baseCaseWork recursiveWork totalWork numLevels(n) Our formulas: 5 1. numNodes ( i ) = 2 i 2. workPerNode ( n , i ) = n 2 i 3. numLevels ( n ) = log 2 ( n ) 4. workPerLeafNode ( n ) = 1 = 2 numLevels ( n ) = 2 log 2 ( n ) = n 5. numLeafNodes ( n ) � recursiveWork = numNodes ( i ) · workPerNode ( n , i ) i =0 baseCaseWork = numLeafNodes ( n ) · workPerLeafNode ( n )

  6. The tree method: precise analysis We discovered: numLevels(n) Our formulas: 5 1. numNodes ( i ) = 2 i 2. workPerNode ( n , i ) = n 2 i 3. numLevels ( n ) = log 2 ( n ) 4. workPerLeafNode ( n ) = 1 = 2 numLevels ( n ) = 2 log 2 ( n ) = n 5. numLeafNodes ( n ) � recursiveWork = numNodes ( i ) · workPerNode ( n , i ) i =0 baseCaseWork = numLeafNodes ( n ) · workPerLeafNode ( n ) totalWork = recursiveWork + baseCaseWork

  7. The tree method: precise analysis n n . n So exact closed form is n log n n workDonePerLeafNode n numLeafNodes n baseCaseWork Solve for base case: n log Solve for recursive case: n i n log 6 log 2 ( n ) 2 i · n � recursiveWork = 2 i i =0

  8. The tree method: precise analysis n log n . n So exact closed form is n log n n workDonePerLeafNode n numLeafNodes n baseCaseWork Solve for base case: n n Solve for recursive case: 6 log 2 ( n ) 2 i · n � recursiveWork = 2 i i =0 log 2 ( n ) � = i =0

  9. The tree method: precise analysis Solve for recursive case: n . n So exact closed form is n log Solve for base case: n 6 log 2 ( n ) 2 i · n � recursiveWork = 2 i i =0 log 2 ( n ) � = i =0 = n log 2 ( n ) baseCaseWork = numLeafNodes ( n ) · workDonePerLeafNode ( n ) = n · 1 = n

  10. The tree method: precise analysis Solve for recursive case: Solve for base case: n 6 log 2 ( n ) 2 i · n � recursiveWork = 2 i i =0 log 2 ( n ) � = i =0 = n log 2 ( n ) baseCaseWork = numLeafNodes ( n ) · workDonePerLeafNode ( n ) = n · 1 = n So exact closed form is n log 2 ( n ) + n .

  11. The tree method: practice Practice: Let’s go back to our old recurrence... otherwise 7  2 if n ≤ 1  S ( n ) = 2 S ( n /3) + n 2 

  12. = n 2 nodes, n 4 nodes, n i nodes, i work per h nodes, n work per work per work per The tree method: practice 2 2 . . . 2 2 . 1 node, n work per . = n log n log numLevels n = 5. numLeafNodes n 4. workPerLeafNode n 1. numNodes i n = log 3. numLevels n i 2. workPerNode n i i = . 2 2 . 2 . . . 2 2 . . . 2 2 . . . . 2 . . . . . 2 2 2 . . 8 2 n 2 n 2 n 2 9 9 n 4 n 2 n 2 n 2 81 81 81 81

  13. 2 nodes, n 4 nodes, n i nodes, i work per h nodes, = n The tree method: practice n work per work per work per = ? 1 node, n 2 2 . . . 2 2 work per = ? . 4. workPerLeafNode n n log n log numLevels n = 5. numLeafNodes n = n = ? = log 3. numLevels n i 2. workPerNode n i i = 1. numNodes i = ? . . 8 2 . 2 2 . . . 2 . . . . 2 2 . . . 2 2 2 2 . . . 2 2 . . . . n 2 n 2 n 2 9 9 n 4 n 2 n 2 n 2 81 81 81 81 1. numNodes ( i ) 2. workPerNode ( n , i ) 3. numLevels ( n ) 4. workPerLeafNode ( n ) = ? 5. numLeafNodes ( n )

  14. = n The tree method: practice . = ? = ? 2 2 . . = ? 2 2 . . 2 2 . = ? 1. numNodes i . = n log n log numLevels n = 5. numLeafNodes n 4. workPerLeafNode n = n = log 3. numLevels n i 2. workPerNode n i i . . 2 . 2 . . . 2 2 . 2 . 2 2 . . . 2 8 2 . . . . . 2 . n 2 1 node, n 2 work per n 2 n 2 2 nodes, n 2 3 2 work per 9 9 n 4 n 2 n 2 n 2 4 nodes, n 2 3 4 work per 81 81 81 81 n 2 2 i nodes, 3 2 i work per 2 h nodes, 1 work per 1. numNodes ( i ) 2. workPerNode ( n , i ) 3. numLevels ( n ) 4. workPerLeafNode ( n ) = ? 5. numLeafNodes ( n )

  15. The tree method: practice 2 2 . . . 2 2 . . 2 . . . . 2 2 . . . 2 2 2 . . 2 . . . 2 2 . . . 2 . 8 . . . 2 2 n 2 1 node, n 2 work per n 2 n 2 2 nodes, n 2 3 2 work per 9 9 n 4 n 2 n 2 n 2 4 nodes, n 2 3 4 work per 81 81 81 81 n 2 2 i nodes, 3 2 i work per 2 h nodes, 1 work per 1. numNodes ( i ) = 2 i = n 2 2. workPerNode ( n , i ) 9 i 3. numLevels ( n ) = log 3 ( n ) 4. workPerLeafNode ( n ) = 2 = 2 numLevels ( n ) = 2 log 3 ( n ) = n log 3 (2) 5. numLeafNodes ( n )

  16. The tree method: practice i n log i i n log n n log i i i n log n n log n 9 i i n log totalWork Combine into a single expression representing the total runtime. 1. numNodes ( i ) = 2 i = n 2 2. workPerNode ( n , i ) 9 i 3. numLevels ( n ) = log 3 ( n ) 4. workPerLeafNode ( n ) = 2 = 2 numLevels ( n ) = 2 log 3 ( n ) = n log 3 (2) 5. numLeafNodes ( n )

  17. The tree method: practice n n log i i n log n n log i i i n log 9 Combine into a single expression representing the total runtime. 1. numNodes ( i ) = 2 i = n 2 2. workPerNode ( n , i ) 9 i 3. numLevels ( n ) = log 3 ( n ) 4. workPerLeafNode ( n ) = 2 = 2 numLevels ( n ) = 2 log 3 ( n ) = n log 3 (2) 5. numLeafNodes ( n )   log 3 ( n ) 2 i · n 2 �  + 2 n log 3 (2) totalWork =  9 i i =0

  18. The tree method: practice Combine into a single expression representing the total runtime. n log i i n log n 9 1. numNodes ( i ) = 2 i = n 2 2. workPerNode ( n , i ) 9 i 3. numLevels ( n ) = log 3 ( n ) 4. workPerLeafNode ( n ) = 2 = 2 numLevels ( n ) = 2 log 3 ( n ) = n log 3 (2) 5. numLeafNodes ( n )   log 3 ( n ) 2 i · n 2 �  + 2 n log 3 (2) totalWork =  9 i i =0 log 3 ( n ) 2 i = n 2 � 9 i + 2 n log 3 (2) i =0

  19. The tree method: practice Combine into a single expression representing the total runtime. 9 1. numNodes ( i ) = 2 i = n 2 2. workPerNode ( n , i ) 9 i 3. numLevels ( n ) = log 3 ( n ) 4. workPerLeafNode ( n ) = 2 = 2 numLevels ( n ) = 2 log 3 ( n ) = n log 3 (2) 5. numLeafNodes ( n )   log 3 ( n ) 2 i · n 2 �  + 2 n log 3 (2) totalWork =  9 i i =0 log 3 ( n ) 2 i = n 2 � 9 i + 2 n log 3 (2) i =0 log 3 ( n ) � 2 � i = n 2 � + 2 n log 3 (2) 9 i =0

  20. The fjnite geometric series log n log n log n n log i i n log n n log i i n n 10 i totalWork n The fjnite geometric series identity: r i r n r Plug and chug: log 3 ( n ) � 2 � i � We have: n 2 + 2 n log 3 (2) 9 i =0

  21. The fjnite geometric series log n log n log n n log i i n log n n log i i n n 10 totalWork The fjnite geometric series identity: Plug and chug: log 3 ( n ) � 2 � i � We have: n 2 + 2 n log 3 (2) 9 i =0 n − 1 r i = 1 − r n � 1 − r i =0

  22. The fjnite geometric series The fjnite geometric series identity: Plug and chug: 10 log 3 ( n ) � 2 � i � We have: n 2 + 2 n log 3 (2) 9 i =0 n − 1 r i = 1 − r n � 1 − r i =0 log 3 ( n ) � 2 � i totalWork = n 2 � + 2 n log 3 (2) 9 i =0 log 3 ( n )+1 − 1 � 2 � i = n 2 � + 2 n log 3 (2) 9 i =0 � 2 � log 3 ( n )+1 = n 2 1 − 9 + 2 n log 3 (2) 1 − 2 9

  23. Applying the fjnite geometric series With a bunch of efgort... 11 � 2 � log 3 ( n )+1 totalWork = n 2 1 − 9 + 2 n log 3 (2) 1 − 2 9 � � log 3 ( n ) � � 2 = 9 1 − 2 7 n 2 + 2 n log 3 (2) 9 9 � log 3 ( n ) = 9 7 n 2 − 2 � 2 7 n 2 + 2 n log 3 (2) 9 = 9 7 n 2 − 2 7 n 2 n log 3 (2/9) + 2 n log 3 (2) = 9 7 n 2 − 2 7 n 2 n log 3 (2) − 2 + 2 n log 3 (2) = 9 7 n 2 − 2 7 n log 3 (2) + 2 n log 3 (2) = 9 7 n 2 + 12 7 n log 3 (2)

  24. The master theorem Is there an easier way? If we want to fjnd an exact closed form, no. Must use either the unfolding technique or the tree technique. If we want to fjnd a big- bound, yes. 12

  25. The master theorem Is there an easier way? If we want to fjnd an exact closed form, no. Must use either the unfolding technique or the tree technique. If we want to fjnd a big- bound, yes. 12

  26. The master theorem Is there an easier way? If we want to fjnd an exact closed form, no. Must use either the unfolding technique or the tree technique. 12 If we want to fjnd a big- Θ bound, yes.

  27. If log b a n c log n If log b a n log b a If log b a The master theorem otherwise c , then T n c , then T n n c c , then T n Then... 13 The master theorem b aT d Suppose we have a recurrence of the following form:  if n = 1  T ( n ) = � n � + n c 

  28. The master theorem d Then... otherwise The master theorem aT b 13 Suppose we have a recurrence of the following form:  if n = 1  T ( n ) = � n � + n c  ◮ If log b ( a ) < c , then T ( n ) ∈ Θ ( n c ) ◮ If log b ( a ) = c , then T ( n ) ∈ Θ ( n c log ( n )) n log b ( a ) � � ◮ If log b ( a ) > c , then T ( n ) ∈ Θ

  29. log b a The master theorem n . . We know log c , therefore merge sort is n log n . Sanity check: try checking S n S n We have a , b , b , and c . We know log c , therefore S n n . , and c We have a Given: Then... d aT b Sanity check: try checking merge sort. 14 If log b ( a ) < c , then T ( n ) ∈ Θ ( n c )   If log b ( a ) = c , then T ( n ) ∈ Θ ( n c log ( n )) T ( n ) = � n � + n c n log b ( a ) � � If log b ( a ) > c , then T ( n ) ∈ Θ 

  30. log b a The master theorem n . . We know log c , therefore merge sort is n log n . Sanity check: try checking S n S n We have a , b , b , and c . We know log c , therefore S n n . , and c We have a Given: Then... d aT b Sanity check: try checking merge sort. 14 If log b ( a ) < c , then T ( n ) ∈ Θ ( n c )   If log b ( a ) = c , then T ( n ) ∈ Θ ( n c log ( n )) T ( n ) = � n � + n c n log b ( a ) � � If log b ( a ) > c , then T ( n ) ∈ Θ 

  31. The master theorem Given: . n therefore S n c , . We know log , and c , b We have a n . S n Sanity check: try checking S n Sanity check: try checking merge sort. 14 Then... b aT d If log b ( a ) < c , then T ( n ) ∈ Θ ( n c )   If log b ( a ) = c , then T ( n ) ∈ Θ ( n c log ( n )) T ( n ) = � n � + n c n log b ( a ) � � If log b ( a ) > c , then T ( n ) ∈ Θ  We have a = 2 , b = 2 , and c = 1 . We know log b ( a ) = log 2 (2) = 1 = c , therefore merge sort is Θ ( n log ( n )) .

  32. The master theorem Given: . n therefore S n c , . We know log , and c , b We have a Sanity check: try checking merge sort. 14 b Then... aT d If log b ( a ) < c , then T ( n ) ∈ Θ ( n c )   If log b ( a ) = c , then T ( n ) ∈ Θ ( n c log ( n )) T ( n ) = � n � + n c n log b ( a ) � � If log b ( a ) > c , then T ( n ) ∈ Θ  We have a = 2 , b = 2 , and c = 1 . We know log b ( a ) = log 2 (2) = 1 = c , therefore merge sort is Θ ( n log ( n )) . Sanity check: try checking S ( n ) = 2 S ( n /3) + n 2 .

  33. The master theorem b . Sanity check: try checking merge sort. Given: Then... 14 aT d If log b ( a ) < c , then T ( n ) ∈ Θ ( n c )   If log b ( a ) = c , then T ( n ) ∈ Θ ( n c log ( n )) T ( n ) = � n � + n c n log b ( a ) � � If log b ( a ) > c , then T ( n ) ∈ Θ  We have a = 2 , b = 2 , and c = 1 . We know log b ( a ) = log 2 (2) = 1 = c , therefore merge sort is Θ ( n log ( n )) . Sanity check: try checking S ( n ) = 2 S ( n /3) + n 2 . We have a = 2 , b = 3 , and c = 2 . We know log 3 (2) ≤ 1 < 2 = c , n 2 � � therefore S ( n ) ∈ Θ

  34. The master theorem: intuition 1. We do work more rapidly then we divide. 2. So, more of the work happens near the “top”, which means 15 Intuition, the log b ( a ) < c case: that the n c term dominates.

  35. The master theorem: intuition 1. We divide more rapidly then we do work. 2. So, most of the work happens near the “bottom”, which means the work done in the leaves dominates. 3. Note: Work in leaves is about 16 Intuition, the log b ( a ) > c case: d · a height = d · a log b ( n ) = d · n log b ( a ) .

  36. The master theorem: intuition 1. Work is done roughly equally throughout tree. 2. Each level does about the same amount of work, so we approximate by just multiplying work done on fjrst level by the 17 Intuition, the log b ( a ) = c case: height: n c log b ( n ) .

  37. Parting thoughts on sorting A few fjnal thoughts about sorting... 18

  38. Hybrid sorts Problem: Quick sort, in the best case, is pretty fast – often a constant factor faster then merge sort. But in the worst case, it’s ! Idea: If things start looking bad, stop using quicksort! Switch to a difgerent sorting algorithm. Hybrid sort A sorting algorithm which combines two or more other sorting algorithms. 19 � n 2 � O

  39. Hybrid sorts Problem: Quick sort, in the best case, is pretty fast – often a constant factor faster then merge sort. But in the worst case, it’s ! Idea: If things start looking bad, stop using quicksort! Switch to a difgerent sorting algorithm. Hybrid sort A sorting algorithm which combines two or more other sorting algorithms. 19 � n 2 � O

  40. Hybrid sorts Problem: Quick sort, in the best case, is pretty fast – often a constant factor faster then merge sort. But in the worst case, it’s ! Idea: If things start looking bad, stop using quicksort! Switch to a difgerent sorting algorithm. Hybrid sort A sorting algorithm which combines two or more other sorting algorithms. 19 � n 2 � O

  41. Hybrid sort example: introsort 2.1 Assume we’ve hit our worst case and switch to heapsort framework. Used by various C++ implementations, used by Microsoft’s .NET . n n log n , not Punchline: worst-case runtime is now 2.2 Else continue using quick sort ) Example: Introsort lg n 2. If we pass some threshold (usually, we’ve made 1. Run quicksort, but keep track of how many recursive calls in-place.) Core idea: Combine quick sort with heap sort 20 (Why heap sort? It’s also O ( n log ( n )) , and can be implemented

  42. Hybrid sort example: introsort 2.2 Else continue using quick sort framework. Used by various C++ implementations, used by Microsoft’s .NET . n n log n , not Punchline: worst-case runtime is now 2.1 Assume we’ve hit our worst case and switch to heapsort Example: Introsort we’ve made 1. Run quicksort, but keep track of how many recursive calls in-place.) Core idea: Combine quick sort with heap sort 20 (Why heap sort? It’s also O ( n log ( n )) , and can be implemented 2. If we pass some threshold (usually, 2 ⌊ lg ( n ) ⌋ )

  43. Hybrid sort example: introsort 2.2 Else continue using quick sort framework. Used by various C++ implementations, used by Microsoft’s .NET . n n log n , not Punchline: worst-case runtime is now 2.1 Assume we’ve hit our worst case and switch to heapsort Example: Introsort we’ve made 1. Run quicksort, but keep track of how many recursive calls in-place.) Core idea: Combine quick sort with heap sort 20 (Why heap sort? It’s also O ( n log ( n )) , and can be implemented 2. If we pass some threshold (usually, 2 ⌊ lg ( n ) ⌋ )

  44. Hybrid sort example: introsort Example: Introsort framework. Used by various C++ implementations, used by Microsoft’s .NET . 2.2 Else continue using quick sort 2.1 Assume we’ve hit our worst case and switch to heapsort we’ve made 1. Run quicksort, but keep track of how many recursive calls in-place.) Core idea: Combine quick sort with heap sort 20 (Why heap sort? It’s also O ( n log ( n )) , and can be implemented 2. If we pass some threshold (usually, 2 ⌊ lg ( n ) ⌋ ) n 2 � � Punchline: worst-case runtime is now O ( n log ( n )) , not O

  45. Hybrid sort example: introsort Example: Introsort framework. Used by various C++ implementations, used by Microsoft’s .NET . 2.2 Else continue using quick sort 2.1 Assume we’ve hit our worst case and switch to heapsort we’ve made 1. Run quicksort, but keep track of how many recursive calls in-place.) Core idea: Combine quick sort with heap sort 20 (Why heap sort? It’s also O ( n log ( n )) , and can be implemented 2. If we pass some threshold (usually, 2 ⌊ lg ( n ) ⌋ ) n 2 � � Punchline: worst-case runtime is now O ( n log ( n )) , not O

  46. Most adaptive sorts take advantage of how real-world data is Adaptive sorts Observation: Most real-world data is contains “mostly-sorted runs” – chunks of data that are already sorted. Idea: Modify our strategy based on what the input data actually looks like! Adaptive sort A sorting algorithm that adapts to patterns and pre-existing order in the input. often partially sorted. 21

  47. Most adaptive sorts take advantage of how real-world data is Adaptive sorts Observation: Most real-world data is contains “mostly-sorted runs” – chunks of data that are already sorted. Idea: Modify our strategy based on what the input data actually looks like! Adaptive sort A sorting algorithm that adapts to patterns and pre-existing order in the input. often partially sorted. 21

  48. Adaptive sorts Observation: Most real-world data is contains “mostly-sorted runs” – chunks of data that are already sorted. Idea: Modify our strategy based on what the input data actually looks like! Adaptive sort A sorting algorithm that adapts to patterns and pre-existing order in the input. often partially sorted. 21 Most adaptive sorts take advantage of how real-world data is

  49. Adaptive sorts: Timsort Example: Timsort Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer) 1. Works by looking for “sorted runs”. 2. Will use insertion sort to merge two small runs and merge sort to merge two large runs 3. Implementation is very complex – lots of nuances, lots of clever tricks (e.g. detecting runs that are in reverse sorted order, sometimes skipping elements) Punchline: worst-case runtime is still n log n , but best case runtime is n . Used by Python and Java. 22

  50. Adaptive sorts: Timsort Example: Timsort Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer) 1. Works by looking for “sorted runs”. 2. Will use insertion sort to merge two small runs and merge sort to merge two large runs 3. Implementation is very complex – lots of nuances, lots of clever tricks (e.g. detecting runs that are in reverse sorted order, sometimes skipping elements) Punchline: worst-case runtime is still n log n , but best case runtime is n . Used by Python and Java. 22

  51. Adaptive sorts: Timsort Example: Timsort Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer) 1. Works by looking for “sorted runs”. 2. Will use insertion sort to merge two small runs and merge sort to merge two large runs 3. Implementation is very complex – lots of nuances, lots of clever tricks (e.g. detecting runs that are in reverse sorted order, sometimes skipping elements) Punchline: worst-case runtime is still n log n , but best case runtime is n . Used by Python and Java. 22

  52. Adaptive sorts: Timsort Example: Timsort Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer) 1. Works by looking for “sorted runs”. 2. Will use insertion sort to merge two small runs and merge sort to merge two large runs 3. Implementation is very complex – lots of nuances, lots of clever tricks (e.g. detecting runs that are in reverse sorted order, sometimes skipping elements) Used by Python and Java. 22 Punchline: worst-case runtime is still O ( n log ( n )) , but best case runtime is O ( n ) .

  53. Adaptive sorts: Timsort Example: Timsort Core idea: Combine merge sort with insertion sort (Who’s Tim? A Python core developer) 1. Works by looking for “sorted runs”. 2. Will use insertion sort to merge two small runs and merge sort to merge two large runs 3. Implementation is very complex – lots of nuances, lots of clever tricks (e.g. detecting runs that are in reverse sorted order, sometimes skipping elements) Used by Python and Java. 22 Punchline: worst-case runtime is still O ( n log ( n )) , but best case runtime is O ( n ) .

  54. Linear sorts Linear sorts (aka ‘Niche’ sorts) things about the input list? 23 Basic idea: Can we do better then O ( n log ( n )) if we assume more

  55. 1. Create a temp array named arr of length k . 2. Loop through the input array. For each number num , run arr[num] += 1 Counting sort The algorithm: 4. Iterate through it to create the output list. number in the input list. 3. The temp array will now contain how many times we say each array of length k . Take inspiration from how hash tables work. Counting sort How would you implement this? Hint: start by creating a temp k n between 0 and k 24 ◮ Assumption: Input is a list of ints where every item is ◮ Worst-case runtime:

  56. Counting sort Counting sort 4. Iterate through it to create the output list. number in the input list. 3. The temp array will now contain how many times we say each The algorithm: array of length k . Take inspiration from how hash tables work. How would you implement this? Hint: start by creating a temp k n between 0 and k 24 ◮ Assumption: Input is a list of ints where every item is ◮ Worst-case runtime: 1. Create a temp array named arr of length k . 2. Loop through the input array. For each number num , run arr[num] += 1

  57. Counting sort Counting sort between 0 and k How would you implement this? Hint: start by creating a temp array of length k . Take inspiration from how hash tables work. The algorithm: 3. The temp array will now contain how many times we say each number in the input list. 4. Iterate through it to create the output list. 24 ◮ Assumption: Input is a list of ints where every item is ◮ Worst-case runtime: O ( n + k ) 1. Create a temp array named arr of length k . 2. Loop through the input array. For each number num , run arr[num] += 1

  58. Other linear sorts Other interesting linear sorts: as strings or ints (which is a sequence of digits) Bucket sort A generalization of counting sort Assumes items are randomly and uniformly distributed across a range of possibilities 25 ◮ Radix sort ◮ Assumes items in list are some kind of sequence-like thing such ◮ Assumes each “digit” is also sortable ◮ Sorts all the digits in the 1s place, then the 2s place...

  59. Other linear sorts Other interesting linear sorts: as strings or ints (which is a sequence of digits) range of possibilities 25 ◮ Radix sort ◮ Assumes items in list are some kind of sequence-like thing such ◮ Assumes each “digit” is also sortable ◮ Sorts all the digits in the 1s place, then the 2s place... ◮ Bucket sort ◮ A generalization of counting sort ◮ Assumes items are randomly and uniformly distributed across a

  60. Graphs Introduction to graphs 26

  61. What is a graph? What is a graph? This is a graph: A B C D E F G H This is also a graph: x y In this class, by “graph”, we mean the graph data structure . 27

  62. What is a graph? What is a graph? This is a graph: A B C D E F G H This is also a graph: x y In this class, by “graph”, we mean the graph data structure . 27

  63. What is a graph? G In this class, by “graph”, we mean the graph data structure . y x What is a graph? This is also a graph: H 27 F E D C B A This is a graph: 8 6 4 2 2 4 6 8

  64. What is a graph? G In this class, by “graph”, we mean the graph data structure . y x What is a graph? This is also a graph: H 27 F E D C B A This is a graph: 8 6 4 2 2 4 6 8

  65. Graph: formal defjnition Graph Notes: 28 A graph is a pair G = ( V , E ) , where... ◮ V is a set of vertices ◮ E is a set of edges (pairs of vertices) ◮ Vertices are the circle things, edges are the lines ◮ The words “node” and “vertex” are synonyms

  66. Graph: formal defjnition examples Examples: h g f e 29 d c b a V = { a } V = { b , c , d } V = { e , f , g , h } E = {} E = { ( b , c ) , ( c , d ) } E = { ( e , f ) , ( f , g ) , ( g , h ) , ( h , e ) , ( e , g ) , ( f , h ) }

  67. Applications of graphs In a nutshell: Graphs let us model the “relationship” between items. If that seems like a very general defjnition, that’s because graphs are a very general concept! Core insight: Graphs are an abstract concept that appear in many difgerent ways Many problems can be modeled as a graph problem Some examples... 30

  68. Applications of graphs In a nutshell: Graphs let us model the “relationship” between items. If that seems like a very general defjnition, that’s because graphs are a very general concept! Core insight: ways Some examples... 30 ◮ Graphs are an abstract concept that appear in many difgerent ◮ Many problems can be modeled as a graph problem

  69. Application: Airline fmight graph Questions: What is the cheapest/shortest/etc fmight from A to B ? Is the route the airline ofgering me actually the cheapest route? What happens if a city is snowed in – how can we reroute fmights? http://allthingsgraphed.com/public/images/airline-google-earth.png 31

  70. Application: Airline fmight graph Questions: What is the cheapest/shortest/etc fmight from A to B ? Is the route the airline ofgering me actually the cheapest route? What happens if a city is snowed in – how can we reroute fmights? http://allthingsgraphed.com/public/images/airline-google-earth.png 31

  71. Application: Social media graph Questions: Why does this graph look clustered? Why are all my friends more popular then me? Who do my friends know? If I want to hire somebody to promote my product, who do I pick? http://allthingsgraphed.com/public/images/facebook/facebook-friend-graph.png 32

  72. Application: Social media graph Questions: Why does this graph look clustered? Why are all my friends more popular then me? Who do my friends know? If I want to hire somebody to promote my product, who do I pick? http://allthingsgraphed.com/public/images/facebook/facebook-friend-graph.png 32

  73. Application: Social media polarization Questions: how to ideas fmow between bloggers? Right now? Over time? Who’s the most infmuential within a given party? In general? http://allthingsgraphed.com/public/images/political-blogs-2004/left-right.png 33

  74. Application: Social media polarization Questions: how to ideas fmow between bloggers? Right now? Over time? Who’s the most infmuential within a given party? In general? http://allthingsgraphed.com/public/images/political-blogs-2004/left-right.png 33

  75. Application: Analyzing code Questions: which fjles import which ones? Which fjles are most used and should be optimized? What if two fjles import each other? If a fjle has a security vulnerability, how might it propagate? http://allthingsgraphed.com/public/images/java-call-graph/cassandra-import-graph.png 34

  76. Application: Analyzing code Questions: which fjles import which ones? Which fjles are most used and should be optimized? What if two fjles import each other? If a fjle has a security vulnerability, how might it propagate? http://allthingsgraphed.com/public/images/java-call-graph/cassandra-import-graph.png 34

  77. Application of graphs Other interesting questions: How do I solve Sudoku effjciently? How do genes in my DNA infmuence and regulate each other? How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based on meaning? 35 ◮ How does Google work?

  78. Application of graphs Other interesting questions: How do genes in my DNA infmuence and regulate each other? How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based on meaning? 35 ◮ How does Google work? ◮ How do I solve Sudoku effjciently?

  79. Application of graphs Other interesting questions: How can I allocate registers to variables in a program? How similar/dissimilar are words? Based on spelling? Based on meaning? 35 ◮ How does Google work? ◮ How do I solve Sudoku effjciently? ◮ How do genes in my DNA infmuence and regulate each other?

  80. Application of graphs Other interesting questions: How similar/dissimilar are words? Based on spelling? Based on meaning? 35 ◮ How does Google work? ◮ How do I solve Sudoku effjciently? ◮ How do genes in my DNA infmuence and regulate each other? ◮ How can I allocate registers to variables in a program?

Recommend


More recommend