Presentation is loading. Please wait.

Presentation is loading. Please wait.

Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct.

Similar presentations


Presentation on theme: "Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct."— Presentation transcript:

1 Vishnu Kotrajaras, PhD.1 Data Structures

2 Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct data structure for any task.

3 Vishnu Kotrajaras, PhD.3 Example, storing 5 numbers 12354 P Linked ist 2 1 4 3 5 P Tree (Binary Search Tree)

4 Vishnu Kotrajaras, PhD.4 Choosing how to store Heap 5 4 3 2 1 If we want to always retrieve a maximum value, heap is the best for that.

5 Vishnu Kotrajaras, PhD.5 Estimating the program speed Big O if where c and N0 are constants and N>=N0 This is telling us how the program grows.

6 Vishnu Kotrajaras, PhD.6 BIG O example If T(N) = 339N and f(N) = N*N  Let us have N0 = 339 และ C = 1  Therefore 339N0 <= 1*(N0*N0)  ->There are other possible answers. If we let f(N)=340N, we will have  T(N) This also fits the definition.  Therefore T(N) <= 1*(340N) is also correct.

7 Vishnu Kotrajaras, PhD.7 BIG O example (cont.) Therefore T(N)=O(N) is also correct. Which one should we use as an answer? Normally, we choose the smallest one. Therefore O(N) is our answer. How does it connect to a program speed? Please read on.

8 Vishnu Kotrajaras, PhD.8 sigmaOfSquare(int n) // calculate { 1: int tempSum; 2: tempSum = 0; 3: for (int i=1;i<=n;i++) 4: tempSum += i*i; 5: return tempSum; } Find the speed of the following code 1 unit (declare only) 1 unit (assignment) 1 unit (return) 1 unit n+1 unit n unit Multiply, add, and assignment, each has n times. Therefore we have 3n unit. Total time is 5n+5 unit.

9 Vishnu Kotrajaras, PhD.9 But it ’ s unreasonable to use so detailed process It’s better to use an approximation time. That is Big O From the example, the time can be estimated from the loop (other running times become insignificant) The loop is performed n times.  Therefore, Big O = O(n) The detailed time is 5n+5, which matches O(n) -> (5n+5<= 6n).

10 Vishnu Kotrajaras, PhD.10 Big O is O(n 2 ). Finding BIG O from various loops For loop-> Its Big O is the number of repetition. Nested loop 1: for (i = 1; i <= n; i++) 2: for (j = 1; j <= n; j++) statements; n times

11 Vishnu Kotrajaras, PhD.11 Finding BIG O from various loops(cont.) Here is the Big O for Nested loop:  If T 1 (N)=O(f(N)) and T 2 (N)= O(g(N)), then  T 1 (N)* T 2 (N)= O(f(N)*g(N))  From last page -> f(n) = g(n) = n  Therefore they add up to O(n 2 ).

12 Vishnu Kotrajaras, PhD.12 Finding BIG O from various loops(cont2.) Consecutive Statements 1: for (i = 0; i <= n; i++) 2: statement1; 3: for (j = 0; j <= n; j++) 4: for (k = 0; k <= n; k++) 5: statement 2 ; O(n) O(n 2 ) The answer is their max. -> O(n 2 )

13 Vishnu Kotrajaras, PhD.13 Finding BIG O from various loops(cont3.) Big O definition for consecutive statements:  If T 1 (N)=O(f(N)) and T 2 (N)= O(g(N)), then  T 1 (N)+ T 2 (N)= max(O(f(N),O(g(N)))  From last page -> f(n) = O(n), g(n) = O(n 2 )  The answer is therefore O(n 2 )

14 Vishnu Kotrajaras, PhD.14 Finding BIG O from various loops(cont4.) Conditional statement 1: if (condition) 2: Statement1 3: Else 4: Statement2 O(f(n)) O(g(n)) Use the max -> max(O(f(n),O(g(n)))

15 Vishnu Kotrajaras, PhD.15 Finding BIG O from recursion 1:mymethod (int n) { 2: if (n == 1) { 3: return 1; 4: } else { 5: return 2*mymethod(n – 1) + 1; 6: } 7:} n times, big O = O(n)

16 Vishnu Kotrajaras, PhD.16 Maximum Subsequence Sum, choosing the best Big O Maximum Subsequence Sum is:  For integer A 1,A 2, …, A n  Maximum Subsequence Sum is that gives the maximum value. It is a consecutive sequence that gives the highest added value.  Example: -2, 11, -6, 16, -5, 7  The sum of 11, -6, 16 is 21. But the max sequence is 11, -6, 16, -5, 7 -> the sum is 23.  23 is the max. sub. Sum. consecutive

17 Vishnu Kotrajaras, PhD.17 Solving max sub sum: 1 st method 1: int maxSubSum01 ( int [] a) { 2: int maxSum = 0; 3: for (int i = 0; i < a.length; i++) { 4: for (int j = i; j < a.length; j++) { 5: int theSum = 0; 6: for (int k = i; k <= j; k++) { 7: theSum += a[k]; 8: } 9: if (theSum > maxSum) { 10: maxSum = theSum; 11: } 12: } 13: return maxSum; 14: } 15: } First index Last index Sum from first to last. Choose to store max value.

18 Vishnu Kotrajaras, PhD.18 This first method has big O = O(n 3 ). Not good enough. Too many redundant calculations.  If we have added elements from index 0 to 2, when we add elements from index 0 to 3, we should not start the addition from scratch. Solving max sub sum: 1st method(cont.)

19 Vishnu Kotrajaras, PhD.19 1: int maxSubSum02 (int [] a) { 2: int maxSum = 0; 3: for (int i = 0; i < a.length; i++) { 4: int theSum = 0; 5: for (int j = i; j < a.length; j++) { 6: theSum += a[j]; 7: if (theSum > maxSum) { 8: maxSum = theSum; 9: } 10: } 11: } 12: return maxSum; 13: } Solving max sub sum: 2 nd method Starting position Do the addition from the starting position and collect the result. BIG O = O(n 2 )

20 Vishnu Kotrajaras, PhD.20 -211-64 when i=0, j=0: theSum = -2 maxSum = 0 when i=0, j=1: theSum = -2 + 11 = 9 maxSum becomes 9. when i=0, j=2: theSum = 9 + (-6) = 3 maxSum is still 9. when i=0, j=3: theSum = 3 + 4 maxSum is still 9. Solving max sub sum: 2nd method(cont.)

21 Vishnu Kotrajaras, PhD.21 Use divide and conquer The result sequence maybe in  The left half or the array, or  The right half, or  Lie between the left half and the right half. (its sequence contains the last element of the left half and the first element of the right half.) Solving max sub sum: 3 rd method

22 Vishnu Kotrajaras, PhD.22 Solving max sub sum: 3rd method (cont.) 1-27-628-54 Max sub sum on the left with (-6) is 1.Max sub sum on the right with (2) is 10. Max sub sum on this side is 7. Max sub sum on this side is 10. Max sub sum that covers between the left side and the right side is therefore 1 +10 = 11 (this is the final answer).

23 Vishnu Kotrajaras, PhD.23 1:int maxSumDivideConquer (int [] array, int leftindex, int rightindex { 2: //assume that the array can be divided evenly. 3: if (leftindex == rightindex) { // Base Case 5: if (array[leftindex] > 0 ) 6: return array[leftindex]; 7: else 8: return 0; // min value of maxSubSum 9: } 10: int centerindex = (leftindex + rightindex)/2; 12: int maxsumleft = maxSumDivideConquer(array, leftindex, centerindex); 13: int maxsumright = maxSumDivideConquer ( array, centerindex + 1, right); Solving max sub sum: 3rd method (cont 2.) T(n) T(n/2)

24 Vishnu Kotrajaras, PhD.24 14: int maxlefthalfSum = 0, lefthalfSum = 0; 15: //max sum – from the last element of the left //side to the first element. 16: for (int i = center; i >= leftindex; i--) { 17: lefthalfSum = lefthalfSum + array[i]; 18: if (lefthalfSum > maxlefthalfSum) { 19: maxlefthalfSum = lefthalfSum; 20: } 21: } Solving max sub sum: 3rd method (cont 3.) O(n/2)

25 Vishnu Kotrajaras, PhD.25 22: int maxrighthalfSum = 0, righthalfSum = 0; 23: // max sum – from the first element of the right //side to the last element. 24: for (int i = centerindex + 1; i <= rightindex; i++) { 25: righthalfSum = righthalfSum + array [i]; 26: if (righthalfSum > maxrighthalfSum) { 27: maxrighthalfSum = righthalfSum; 28: } 29: } Solving max sub sum: 3rd method (cont 4.) O(n/2)

26 Vishnu Kotrajaras, PhD.26 30: //finally, find max of the three. 31: return max3 (maxsumleft, maxsumright, maxlefthalfSum + maxrighthalfSum) } Therefore the total time is T(n) = 2T(n/2) + 2O(n/2) Solving max sub sum: 3rd method (cont 5.) This part takes constant time. We can ignore.

27 Vishnu Kotrajaras, PhD.27 We find the total BIG O: T(n) = 2T(n/2) + 2O(n/2) = 2T(n/2) + O(n) = 2T(n/2) + cn Divide everything by n, we get: Solving max sub sum: 3rd method (cont 6.) O(n) <= c*n according to the definition (1)

28 Vishnu Kotrajaras, PhD.28 We can create a series of equations: Solving max sub sum: 3rd method (cont 7.) (X) (3) (2)

29 Vishnu Kotrajaras, PhD.29 Do (1) + (2) + (3) + …..+ (x), we get:  The left and right hand side cancel each other out. And c is added for log 2 n times.  Multiply both sides by n, we get:  Because T(1) is constant, we can conclude that  Big O = O(n log n) Solving max sub sum: 3rd method (cont 8.)

30 Vishnu Kotrajaras, PhD.30 We improve on the 2 nd method, with two points to note: First, the first element of any maximum subsequence sum cannot be a negative value.  For example: 3, -5, 1, 4, 7, -4 -5 cannot be the first element of our result. It can only make the total smaller. Any single positive number gives a better result anyway. Solving max sub sum: 4 th method

31 Vishnu Kotrajaras, PhD.31 Second, any subsequence that is negative cannot begin max sub sum.  Let us be in a loop execution. Let i be the index of the first element of a subsequence an j be the index of the last element of that subsequence.  Let the last element make this subsequence negative.  Let p be any index between i+1 and j. Solving max sub sum: 4th method (cont.) 341-3-915 ijp

32 Vishnu Kotrajaras, PhD.32 Solving max sub sum: 4th method (cont 2.)  The next step of this loop -> increment j by one. If a[j] is negative, we will not get a better max sub sum. Max sub sum value will not change. If a[j] is positive, a[i]+ … +a[j] will be greater than a[i]+ … +a[j-1]. However, because a[i]+ … +a[j-1] is negative, the new sum is never more than a stored max sub sum. The new sum cannot even match a[j] alone. Therefore if we have a negative subsequence, we should not move j. We should move i instead.

33 Vishnu Kotrajaras, PhD.33  Should we only increment i by one or more?  From our assumption, we know that a[j] makes a[i]+ … +a[j] negative. Therefore, incrementing i by one within the range between i and p will only make a[i]+ … + a[p] smaller. (p is any index between i and j).  If we want to get a larger max sub sum, we must start our subsequence from position j+1. Therefore i should be incremented to j+1. Solving max sub sum: 4th method (cont 3.) 341-3-915 ijp

34 Vishnu Kotrajaras, PhD.34 1: int maxsubsumOptimum (int[] array) { 2: int maxSum = 0, theSum = 0; 3: for (int j = 0; j < a.length; j++) { 4: theSum = theSum + array [j]; 5: if ( theSum > maxSum) { 6: maxSum = theSum; 7: } else if (theSum < 0) {// if a[j] makes the 8: //sequence negative, 9: theSum = 0;// start again from 10:// position j+1. 11: } 12: } 13: return maxSum; 14: } Solving max sub sum: 4th method (cont 4.)

35 Vishnu Kotrajaras, PhD.35 Logarithm in big O If we can spend a constant time (O(1)) to divide a problem into equal subproblems (3 rd method of the maximum subsequence sum problem), that problem will have big O = O(log n). Usually,we make an assumption that all data is in the system. Otherwise, reading data in will take O(n).

36 Vishnu Kotrajaras, PhD.36 Example: O(log n) finding 5 in a sorted array. If we start from the first array member, it takes O(n) to find a number. But we know that the array is sorted:  So we can look at the middle of the array, and search from there, going to either left or right depending on the value of that middle element.  And keep searching by looking at the middle element of the subarray we are looking at, and so on.  This is called -> Binary Search.

37 Vishnu Kotrajaras, PhD.37 int binarySearch (int[] a, int x) { int left = 0, right = a.length – 1; while (left <=right) { int mid = (left + right)/2; if (a[mid] < x ) { left = mid + 1; } else if (a[mid] > x) { right = mid – 1; } else { return mid; } return -1; // reaching this point means -> not found. } Big O = O(log 2 n)

38 Vishnu Kotrajaras, PhD.38 Example: O(log n) (cont.) Greatest common divisor long gcd (long m, long n) { while (n!=0) { long rem = m%n; m = n; n = rem; } return m; } The reduction of the remainder tells us the Big O. In this program, The remainder decreases without any specific pattern. How do we find big O?

39 Vishnu Kotrajaras, PhD.39 Big O of gcd We use the following definition: if M > N, M mod N < M/2  Prove:  if N <= M/2: Because the remainder from M mod N must be less than N, so it must also be less than M/2.  if N > M/2: M divided by N will = 1 + (M-N). The remainder is M-N or M – (> M/2). Therefore the remainder is less than M/2. If we look at the code for gcd:  The remainder from the xth loop will be used as m of the (x+2)th loop.  Therefore the remainder from the (x+2)th loop must be less than half the remainder from the xth loop.  Meaning -> with 2 iterations passed, the remainder must surely reduce by half or more.

40 Vishnu Kotrajaras, PhD.40 gcd (2564, 1988))

41 Vishnu Kotrajaras, PhD.41 Calculate x n by divide and conquer. long power (long x, int n) { if (n==0) return 1; if (isEven (n)) return power (x*x, n/2); else return power (x*x, n/2)*x; } Example: O(log n) (cont 2.) Big O = O (log 2 n) The original problem is divided by half in each method call.

42 Vishnu Kotrajaras, PhD.42 O(log n) definition log k n = O(n) when k is constant.  This definition tells us that a logarithmic function has a small growth rate. f(n) = log a n has its big O = O(log b n), where a and b is a positive number more than 1.  Any two logarithmic functions have the same growth rate.

43 Vishnu Kotrajaras, PhD.43 let and Any two logarithmic functions have the same growth rate: a proof

44 Vishnu Kotrajaras, PhD.44 Runtime – small(top) to large (bottom) c log n log k n n n log n n 2 n 3 2 n

45 Vishnu Kotrajaras, PhD.45 Definitions other than big O Big Omega ( ) T(N) = (g(N)) if there exist constant C and N 0 that  T(N) >= C g(N), where N>=N 0 From def. if f(N) = (N 2 ), then f(N) = (N) = (N 1/2 )  We should choose the most realistic answer.

46 Vishnu Kotrajaras, PhD.46 Big Theta ( ) T(N) = (h(N)) if T(N) = O(h(N)) and T(N) = (h(N)) There exist c1, c2, N 0 that make c1*h(N) = N 0 Definitions other than big O (CONT.)

47 Vishnu Kotrajaras, PhD.47 small O T(N) = o(p(N)) if T(N) = O(p(N)) but T(N) (p(N)) Definitions other than big O (CONT 2.)

48 Vishnu Kotrajaras, PhD.48 Notes from the definitions T(N) = O(f(N)) has the same meaning as f(N) = (T(N))  We can say f(N) is an “ upper bound ” of T(N), and T(N) is a lower bound of f(N). f(N) = N 2 and g(N) = 2N 2 have the same Big O และ Big. That is f(N) = (g(N)) f(N) = N 2 can have several Big O -> (O(N 3 ), O(N 4 )) but the best value is O(N 2 ).  We can use f(N) = (N 2 ) to tell that this value is the best big O.

49 Vishnu Kotrajaras, PhD.49 If T(N) is a Polynomial degree k, then T(N) = (N k ) From here,  if T(N) = 5N 4 + 4N 3 + N, we know that T(N) = (N 4 ) Thus, we have the latest definition:

50 Vishnu Kotrajaras, PhD.50 Best case, Worst case, Average case worst case = a maximum running time possible. best case = a minimum running time possible. average case?  For each input, see how long the program runs.  average case running time = total time from every input divided by the number of input.

51 Vishnu Kotrajaras, PhD.51 The average case definition is based on an assumption that:  Each input has equal chance of occurrence. If we do not want the assumption,  We must take a probability of each input into account. Average case = (prob. of input i * unit time when use input i ) Average case

52 Vishnu Kotrajaras, PhD.52 Example: Finding Average case Let ’ s say we want to find x in an array of size n. Best case: find x in the first array slot. Worst case: x is in the array ’ s last slot, or x is not in the array at all. Average case:  Assume each array slot has an equal chance of having x inside.  Therefore, a chance of x being in a slot is 1/n.

53 Vishnu Kotrajaras, PhD.53 Average Case running time = 1/n * (steps used when finding x in the first slot) + 1/n * (steps used when finding x in the second slot) +... + 1/n * (steps used when finding x in the last slot, or not finding x at all) = (1 + 2 + … + n) / n = (n+1)/2 = O(n) = big O of worst case Example: Finding Average case (cont.)


Download ppt "Vishnu Kotrajaras, PhD.1 Data Structures. Vishnu Kotrajaras, PhD.2 Introduction Why study data structure?  Can understand more code.  Can choose a correct."

Similar presentations


Ads by Google