Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 2P03 Week 11 Reasons to study Data Structures & Algorithms Provide abstraction Handling of real-world data storage Programmer’s tools Modeling of.

Similar presentations


Presentation on theme: "COSC 2P03 Week 11 Reasons to study Data Structures & Algorithms Provide abstraction Handling of real-world data storage Programmer’s tools Modeling of."— Presentation transcript:

1 COSC 2P03 Week 11 Reasons to study Data Structures & Algorithms Provide abstraction Handling of real-world data storage Programmer’s tools Modeling of real-world objects and concepts Programs more readable, understandable, maintainable If you have a good feeling for them, you are a better programmer! –You can solve the problem faster –Your program works better, with fewer bugs –You will know when you have been given an impossible task

2 COSC 2P03 Week 12 Considerations Speed Memory usage Complexity of structure Examples of tradeoffs: Insertion speed vs. search speed vs. deletion speed Structure vs. speed Your task as a programmer: think of the tradeoffs and select the best option

3 Complexity Expresses the efficiency of an algorithm Running time expressed as function of problem size Problem size: number of inputs Some complexity classes: –Logarithmic –Linear –Quadratic –Exponential COSC 2P03 Week 13

4 “Big O” notation Function g(n) is O(f(n)) if there are positive constants c and n 0 such that g(n) ≤ c*f(n) for all n ≥ n 0 Upper bound on its rate of growth Determines algorithm’s “cost” in terms of time to run Note: other measurements exist for lower bound etc COSC 2P03 Week 14

5 5 Complexity Examples 1 and 2 for(i = 0; i < n; i++) { b[i] = a[i]+1; c[i] = a[i]+b[i]; } for(i = 0; i < n; i++) { for(j = 7; j < n; j=j+3) { b[i] = a[i]+1; } c[i] = a[i]+b[i]; }

6 COSC 2P03 Week 16 Complexity Example 3 for(i = 0; i < n; i++) { if(a[i] > 0) { for(j = 0; j < n; j++) c[i] = c[i] + b[j]; } else c[i] = b[i]; } Question: what if the outer loop were changed to: for(i = 1; i < n; i=i*2) ?

7 COSC 2P03 Week 17 int xyz(int n) { if(n <= 1) return 1; else return xyz(n/2) + n; } Question: what if the last statement were changed to return xyz(n-2) + n; ? Complexity Example 4

8 COSC 2P03 Week 18 Complexity Example 5 int bc(int n) { int c; c = 0; while(n > 0) { c = c + (n % 2); n = n / 2; } return c; }

9 COSC 2P03 Week 19 Complexity Example 6 int pc(int n) { int i, c; if(n == 1) { return 1; } c = 0; for(i = 0; i < n; i++) { c = c + pc(n-1); } return c; }

10 COSC 2P03 Week 110 Table of Computational Complexity If a step takes 0.001 ms and the problem size is N: N=10N=100N=1000 StepsTimeStepsTimeStepsTime f(N) = N100.01ms1000.1ms10001ms f(N) = N 2 1000.1ms10 4 10ms10 6 1s f(N) = N 3 10001.0ms10 6 1s10 9 16.67min f(N) = 2 N 10241.02ms1.27x10 30 4.0x10 16 yrs 1.07x10 301 3.4x10 287 yrs

11 COSC 2P03 Week 111 Comparison: O(n 2 ) vs O(n log n) Assume a machine is running at 200 000 MIPs Assume 1 instruction per iteration. Time to sort 1 billion numbers: Bubble sort O(n 2 ) (10 9 ) 2 steps / (2x10 11 steps/sec) ≈ 2 months. Quick sort O(n log n) 10 9 * log 2 (10 9 ) steps / (2x10 11 steps/sec) ≈ 10 9 * 30 steps / (2x10 11 steps/sec) ≈ 0.15 seconds.

12 COSC 2P03 Week 112 The Rules of Recursion (Weiss, section 1.3) 1.There must be base cases that can be solved without recursion 2.Recursive calls must always make progress towards a base case 3.Assume that all recursive calls work 4.Never duplicate work by solving the same instance of a problem in separate recursive calls

13 COSC 2P03 Week 113 Recursive method printOut (Weiss, section 1.3) public static void printOut(int n) { if(n >= 10) printOut(n/10); printDigit(n % 10); }

14 COSC 2P03 Week 1 14 printOut(5034) { if(5034 >= 10) // true printOut(5034 / 10); printDigit(5034 % 10); } printOut(503) { if(503 >= 10) // true printOut(503 / 10); printDigit(503 % 10); } printOut(50) { if(50 >= 10) // true printOut(50 / 10); printDigit(50 % 10); } printOut(5) { if(5 >= 10) // false printDigit(5 % 10); } Output 4 3 0 5

15 COSC 2P03 Week 115 Sequence of calls to determine F 5 fib(5) fib(4)fib(3) fib(2)fib(2) fib(3)fib(2) fib(2) fib(1) fib(2) fib(2) fib(1) fib(1) fib(0) fib(1)fib(0) fib(1) fib(0)


Download ppt "COSC 2P03 Week 11 Reasons to study Data Structures & Algorithms Provide abstraction Handling of real-world data storage Programmer’s tools Modeling of."

Similar presentations


Ads by Google