Reasons to study Data Structures Algorithms Provide abstraction
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 COSC 2 P 03 Week 1 1
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 COSC 2 P 03 Week 1 2
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 2 P 03 Week 1 3
“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 2 P 03 Week 1 4
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]; } COSC 2 P 03 Week 1 5
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)? COSC 2 P 03 Week 1 6
Complexity Example 4 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; ? COSC 2 P 03 Week 1 7
Complexity Example 5 int bc(int n) { int c; c = 0; while(n > 0) { c = c + (n % 2); n = n / 2; } return c; } COSC 2 P 03 Week 1 8
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; } COSC 2 P 03 Week 1 9
Table of Computational Complexity If a step takes 0. 001 ms and the problem size is N: N=10 Steps Time f(N) = N 10 0. 01 ms f(N) = N 2 100 0. 1 ms N=100 Steps Time 100 0. 1 ms 104 10 ms N=1000 Steps Time 1000 1 ms 106 1 s f(N) = N 3 1000 1. 0 ms 106 1 s 109 16. 67 min f(N) = 2 N 1024 1. 02 ms 1. 27 x 1030 4. 0 x 1016 1. 07 x 10301 3. 4 x 10287 yrs COSC 2 P 03 Week 1 10
Comparison: 2 O(n ) vs O(n log n) Assume a machine is running at 300 000 MIPs Assume 1 instruction per iteration. Time to sort 1 billion numbers: Bubble sort O(n 2) (109)2 steps / (3 x 1011 steps/sec) ≈ 39 days. Quick sort O(n log n) 109 * log 2(109) steps / (3 x 1011 steps/sec) ≈ 109 * 30 steps / (3 x 1011 steps/sec) ≈ 0. 11 seconds. COSC 2 P 03 Week 1 11
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 COSC 2 P 03 Week 1 12
Recursive method print. Out (Weiss, section 1. 3) public static void print. Out(int n) { if(n >= 10) print. Out(n/10); print. Digit(n % 10); } COSC 2 P 03 Week 1 13
print. Out(5034) { if(5034 >= 10) // true print. Out(5034 / 10); print. Digit(5034 % 10); } Output 4 print. Out(503) { if(503 >= 10) // true print. Out(503 / 10); print. Digit(503 % 10); } print. Out(50) { if(50 >= 10) // true print. Out(50 / 10); print. Digit(50 % 10); } print. Out(5) { if(5 >= 10) // false print. Digit(5 % 10); } COSC 2 P 03 Week 1 3 0 5 14
Sequence of calls to determine F 5 fib(5) fib(4) fib(3) fib(2) fib(1) fib(0) fib(1) fib(0) COSC 2 P 03 Week 1 15
- Slides: 15