Download presentation
Presentation is loading. Please wait.
Published byAileen Reynolds Modified over 9 years ago
1
Software Learning Resource Service Platform BASIC CONCEPT CHAPTER 1 BASIC CONCEPT 1
2
Software Learning Resource Service Platform How to create programs (Waterfall Model) Requirements Analysis Design: Architecture Design Detailed Design – Data Type 、 Algorithm User Interface Design (option) Coding and Refinement Verification and Testing Maintain 2
3
Software Learning Resource Service Platform Algorithm Definition An algorithm is a finite set of instructions that accomplishes a particular task. Criteria Input ≧ 0 Output ≧ 1 Definiteness: clear and unambiguous Finiteness: terminate after a finite number of steps Effectiveness: instruction is basic enough to be carried out 3
4
Software Learning Resource Service Platform Recursion Definition Definition: Functions can call themselves (direct recursion) or they may call other functions that invoke the calling function again (indirect recursion). Type Type : 1.Direct Recursion 2.Indirect Recursion 3.Tail Recursion. 1. 2. 3. 4 A( ){ … A( ); … } A( ){ … B( ); … } B( ){ … A( ); … } A( ){ … A( ) }
5
Software Learning Resource Service Platform Recursion 5 The advantages and disadvantages of recursion (compared with Iteration) (compared with Iteration): Advantages Advantages: 1.Powerful in expression. Allow us to express a very complex process in very clear term. 2.Save space. 3.The recursion is not need more local variables to support.Disadvantages: For the most part recursion is slower, and takes up more of the stack as well. EX EX: N! 、 Fibonacci Number 、 Ackerman’s function 、 Binary tree traversal 、 Quick sort 、 Tower of Hanoi 、 Permutation
6
Software Learning Resource Service Platform We can try to find out (a) terminal condition (b) recursive relationship →if (a) then (end or return result) else return (b) Recursion 6
7
Software Learning Resource Service Platform Abstract Data Type specification implementation An abstract data type (ADT) is a data type that is organized in such a way that the specification of the objects and the operations on the objects is separated from the representation of the objects and the implementation of the operations. 7
8
Software Learning Resource Service Platform Abstract Data Type 8 ex) Sack is an ADT specification of the objects: 1. a set of elements 2. top pointer 3. stack size the specification of the operations: 1. push (i, s) 2. pop (s) i
9
Software Learning Resource Service Platform Abstract Data Type 9 ex) Sack is an ADT representation of the objects : Array Link-list top: integer=0 top: pointer=nil size: integer=n no size limit
10
Software Learning Resource Service Platform Abstract Data Type 10 ex) Sack is an ADT the implementation of the operations push(i, s) push(i, s) if stack_full then error new t else t^data=i top=top+1 t^link=top s[top]=i top=t
11
Software Learning Resource Service Platform Performance Analysis or Measurement Performance Measurement (machine dependent) Performance Analysis (machine independent) space complexity space complexity: storage requirement fixed space requirements: code space, simple type variable, fixed size structure variable, constant variable space requirements: structure parameters, recursion time complexity time complexity: computing time 11
12
Software Learning Resource Service Platform Time Complexity compute the step count Introduce variable count into programs Tabular method step per execution frequency Determine the total number of steps contributed by each statement step per execution frequency add up the contribution of all statements 12
13
Software Learning Resource Service Platform float sum(float list[ ], int n) { float tempsum = 0; count++; /* for assignment */ int i; for (i = 0; i < n; i++) { count++; /*for the for-loop */ tempsum += list[i]; count++; /* for assignment */ } count++; /* last execution of for */ count++; /* for return */ return tempsum; } Iterative summing of a list of numbers 2n + 3 steps 13
14
Software Learning Resource Service Platform Tabular Method 14 Statements/eFrequencyTotal steps float sum(float list[ ], int n)000 {000 float tempsum = 0;111 int i;000 for (i=0;i<n; i++)1n+1 tempsum += list[i]1Nn return tempsum;111 }000 Total2n+3
15
Software Learning Resource Service Platform float rsum(float list[ ], int n) { count++; /*for if conditional */ if (n) { count++; /* for return and rsum invocation */ return rsum(list, n-1) + list[n-1]; } count++; return list[0]; } Recursive summing of a list of numbers 15 2n+2 steps
16
Software Learning Resource Service Platform Recursive Function to sum of a list of numbers 16 Statements/eFrequencyTotal steps float rsum(float list[], int n)000 {000 if (n)1n+1 return rsum(list, n-1)+list[n-1];1nn return list[0];111 }000 Total2n+2
17
Software Learning Resource Service Platform Tabular Method 17 for (i=1; i<n, i++) { a=(b+c)*d/e } for (i=1; i<n, i++) { T1=b+c T2=T1*d T3=T2/e } n?3n?
18
Software Learning Resource Service Platform Asymptotic Notation (O) 18 Definition f(n) = O(g(n)) iff there exist two positive constants c and n 0 such that f(n) cg(n) for all n, n n 0. Examples 2n+3= 3n 2 +2n+3= O(n) /* 2n+3 3n for n 3 */ O(n 2 ) /* 3n2+2n+3 4n2 for n 3 */ O(n 2 )? /* 2n+3 n2 for n 3 */
19
Software Learning Resource Service Platform Theorem 19
20
Software Learning Resource Service Platform Asymptotic Notation (O) (upper bound) 20 Examples –
21
Software Learning Resource Service Platform Basic Principle O(1): constant O(logn) O(n): linear O(nlogn) O(n 2 ): quadratic O(n 3 ): cubic O(2 n ): exponential O( ) O(n!) O( ) 21
22
Software Learning Resource Service Platform 22 *Figure 1.7:Function values (p.42)
23
Software Learning Resource Service Platform 23 *Figure 1.8:Plot of function values(p.43) Human Population Moore’s Law Cell Division
24
Software Learning Resource Service Platform 24 *Figure 1.9:Times on a 1 billion instruction per second computer(p.44)
25
Software Learning Resource Service Platform 25 Ordering by asymptotic growth rates in asceding order Question:
26
Software Learning Resource Service Platform 26 Ordering by asymptotic growth rates in asceding order Question:
27
Software Learning Resource Service Platform Asymptotic Notation (Ω) (Lower bound) Definition f(n) = Ω(g(n)) iff there exist two positive constants c and n 0 such that f(n) ≧ cg(n), for all n, n ≧ n 0 Examples 27
28
Software Learning Resource Service Platform Asymptotic Notation (θ) Definition f(n) = θ(g(n)) iff there exist three positive constants C 1,C 2 and n 0 such that C 1 g(n) ≦ f(n) ≦ C 2 g(n) for all n, n ≧ n 0 Examples 28 3n+2= (n) /* 3n+2 3n for n 2 */ 3n+2= O (n) /* 3n+2 4n for n 2 */
29
Software Learning Resource Service Platform 29 Question:
30
Software Learning Resource Service Platform 30 Question:
31
Software Learning Resource Service Platform 31 Which of the following statements is not correct? (a)20 is O(1) (b)n(n-1)/2 is O( ) (c)max(, ) is O( ) (d) is O( ) (e)If p(x) is any degree polynomial with a positive leading coefficient, then p(n) is O( ) Question: O(2 n )
32
Software Learning Resource Service Platform 32 List the functions below from lowest to highest order Question:
33
Software Learning Resource Service Platform 33 Question:
34
Software Learning Resource Service Platform Show that the following statements are correct (a) (b) (c) 34 Question:
35
Software Learning Resource Service Platform For i=1 to n do For k=(i+1) to n do x=x+1 以 x=x+1 之執行次數當 T(n) , T(n)? 35 Question:
36
Software Learning Resource Service Platform For i=1 to n do For j=i to n do For k=j to n do x=x+1 以 x=x+1 之執行次數當 T(n) , T(n)? 36 Question:
37
Software Learning Resource Service Platform For k=1 to n do For i=0 to k-1 do For j=0 to k-1 do x=x+1 以 x=x+1 之執行次數當 T(n) , T(n)? 37 Question:
38
Software Learning Resource Service Platform T(n)=2T(n-1)+T(1), T(1)=1, T(n)? 38 Question:
39
Software Learning Resource Service Platform T(n)=T(n/2)+T(1), T(1)=1, T(n)? 39 Question:
40
Software Learning Resource Service Platform T(n)=2T(n/2)+n, T(1)=1, T(n)? 40 Question:
41
Software Learning Resource Service Platform T(n)=T(n-1)+n, T(1)=1, T(n)? 41 Question:
42
Software Learning Resource Service Platform T(n)=T(n-1)+T(n-2), T(0)=0, T(1)=1, T(n)? 42 Question:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.