Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. /*
  7.  
  8. Big O Notation
  9. - Implementation independent
  10. - Platform independent
  11.  
  12. Linear Search
  13. - Best case is 1
  14. - Worst case is n
  15. - Average case is n/2
  16.  
  17. Binary Search
  18. - Case is log(base 2) of n
  19.  
  20. To claim: f(x) is O(g(x))
  21. - f(x) is time taken / work done by the algorithm or data structure, eg linked list search
  22. - g(x) is a function that puts a ceiling on the amount of work
  23. - |f(x)| <= c * g(x), where c is a constant
  24.  
  25. | /. = g(x) (always positive, cannot do negative work to sort an array)
  26. | /.
  27. | /.
  28. | /.
  29. | /.
  30. | /.
  31. | /. -> dots cannot be above the line as the line is the MAX O(g(x)) so it can not be longer than it
  32. | /.
  33. | /.
  34. | /.
  35. | /.
  36. | /.
  37. | /.
  38. |/____________________________________________
  39.  
  40.  
  41. - For example, Algorithm x's runtime is O(n)
  42. | . _/ -> this line is just the smallest function of n that puts an upper bound on x
  43. | _/
  44. | _/
  45. | . _/
  46. | _/
  47. | _/
  48. | . _/
  49. | _/
  50. | _/
  51. | . _/
  52. | _/
  53. | ._/
  54. | _/
  55. |/____________________________________________
  56.  
  57.  
  58. - For a big enough n, constants don't matter and the log functions always wins out
  59. - Easy Problems (Worst case)
  60. - O(log(base 2)n) = binary search
  61. - O(n) = linear search
  62. - O(nlogn) = general sorting problems
  63. - O(n^2) = direct convolution (when you reduce data, image processing, signals)
  64. - O(n^3) = matrix multiplication
  65. - O(2^n) = shortest route
  66. - O(n!) = permutations of a string
  67. - Constant = determining whether or not an array is sorted
  68. - Approximation = rendering images
  69.  
  70.  
  71. Exercise 1: Identify the worst case time complexity of:
  72.  
  73. void multiply_accumulate(float input[N], float output[N]){
  74. for (int i = 0; i < N; i++){
  75. output[i] = 0
  76. for(int j = 0; j < N; j++){
  77. output[i] = output[i] + input[i] * input[j];
  78. }
  79. }
  80. } -> output[N] = summation from j of input[i]output[j]
  81. -> All cases are N^2
  82.  
  83. More efficient:
  84. void multiply_accumulate(float input[N], float output[N]){
  85. float sum = 0.0;
  86. for (int j = 0; j < N; j++){
  87. sum += input[j];
  88. }
  89. for (int i = 0; i < N; i++){
  90. output[i] += input[i] * sum
  91. }
  92. }
  93. -> All cases are now 2N
  94.  
  95.  
  96. Exercise 2: Efficiency for sorting (Bubble sort):
  97. for (int i = 0; i < N; i++){
  98. for (int j = 0; j < N- 1; j++){
  99. if arr[j] > arr[j + 1]{
  100. swap(arr[j], arr[j+1]);
  101. }
  102. }
  103. }
  104.  
  105. - Binary search with the price of sortign: O(N^2) + MO(log(base 2) N) = a big number
  106. - Quick sort has O(NlogN)
  107. - Insertion sort (inserting in the correct spot off the bat) = O(N/2) on average, O(N) worst case
  108.  
  109. Trees
  110. DATA
  111. / \
  112. DATA DATA
  113. / \ \
  114. DATA DATA DATA
  115.  
  116. Exercise 3: Build a compound data type for a binary tree node that stores one float
  117.  
  118. typedef struct treeUno{
  119. float hello;
  120. }TREE
  121.  
  122. TREE newTreeNode(){
  123. TREE h;
  124. struct treeUno *rightChild;
  125. struct treeUno *leftChild;
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. */
  137.  
  138. int main(){
  139.  
  140.  
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement