Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song.

Similar presentations


Presentation on theme: "CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song."— Presentation transcript:

1 CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song

2 Introduction Algorithm: Basic method of solving problems. Data Structure: A way of storing data so that it can be used efficiently. Goal: Correct, Efficient, Simple Efficiency: Time V.S. Space? We care Time Basic Steps: comparison / swap Analysis: Consider the worst case Consider the average case

3 An Example the example: Sequential Search Given: array A[1…n] and an element X, try to find X in A[1…n], A[ ] is not sorted. Algorithm: sequential search Basic OP: key comparison Worst case: C W (n) = worst case key comp. So, C W (n) = n Average case: C A (n) = q*(n+1)/2 + (1-q)*n Where q = probability of element X can be found If q = 1, then C A (n) = (n+1)/2 How come?

4 Asymptotic Complexity (1) Why? To measure the running time when problem size gets large Example: T 1 (n) = 10n T 2 (n) = 2n 2 Which one increases faster? It depends on n We want: Ignore the small number of n Ignore the constant factors

5 Asymptotic Complexity (2) Definition: O() If there’re positive constant C and n 0 such that T(n) = n 0, then we say T(n) is O(f(n)) Example: T(n) = 10n 2 + 5n is O(n 2 ) Prove: We arbitrarily select n >= 10 Then T(n) =10) =10) So, here n 0 = 10, C = 10.5 So, T(n) is O(n 2 )

6 Asymptotic Complexity (3) Definition:Ω() If there’re positive constant C and n 0 such that T(n) >= C * f(n) for all n >= n 0, then we say T(n) is Ω(f(n)) Example: T(n) = 10n 2 + 5n is Ω (n 2 ) Prove: We arbitrarily select n >= 1 Then T(n) >= 10n 2 (because 5n>=0) So, here n 0 = 1, C = 10 So, T(n) is Ω(n 2 ) Definition: Θ() If T(n) is both O(f(n)) and Ω(f(n)), then we say T(n) is Θ(f(n)) So, T(n) = 10n 2 + 5n finally is Θ(n 2 )

7 Asymptotic Complexity (4) Some rules: Sum Rule: If T 1 (n) is O(f(n)) and T 2 (n) is O(g(n)), then T 1 (n) + T 2 (n) is O(f(n) + g(n)) = O(max(f(n), g(n))) Product Rule: If T 1 (n) is O(f(n)) and T 2 (n) is O(g(n)), then T 1 (n) * T 2 (n) = O(f(n) * g(n))


Download ppt "CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (1) Asymptotic Complexity 10/28/2008 Yang Song."

Similar presentations


Ads by Google