Docsity

Algorithmic Complexity Time Complexity Asymptotic notations Brute Force, Lecture notes of Data Structures and Algorithms

Partial preview of the text

Download Algorithmic Complexity Time Complexity Asymptotic notations Brute Force and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and

Algorithms

LECTURE 04: ALGORITHM ANALYSIS

  • Algorithmic Complexity
  • Time Complexity
  • Asymptotic notations
  • Brute Force

Contents

  • Calculate maximum steps to find the result
  • The input( n ) of the function is the main source of steps growth

Problem: Get Number of Steps

Solution: T(n) = 3(n ^ 2) + 3n + 3

long getOperationsCount(int n) { long counter = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) counter++; return counter; }

  • Some parts of the equation grow much faster than

others

  • T(n) = 3(n^2 ) + 3n + 3
  • We can ignore some part of this equation
  • Higher terms dominate lower terms – n > 2 , n^2 > n , n^3 > n^2
  • Multiplicative constants can be omitted – 12nn , 2n^2  n^2
  • The previous solution becomes ≈ n^2

Simplifying Step Count

  • Therefore, we need to measure all the possibilities:

Time Complexity

0

5

10

15

20

25

30

35

40

45

0 5 10 15 20 25 30 35 40

Time Complexity

Worst Average Best

  • From the previous chart we can deduce:
    • For smaller size of the input ( n ) we don't care much for the runtime. So we measure the time as n approaches infinity
    • If an algorithm has to scale , it should compute the result within a finite and practical time
    • We're concerned about the order of an algorithm's complexity , not the actual time in terms of milliseconds

Time Complexity

Asymptotic Functions

0

10

20

30

40

50

60

70

80

90

100

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

Big O Notation

O(1) O(log(n)) O(n) O(n*log(n)) O(n^2) O(2^n)

 Below are some examples of common algorithmic grow:

Typical Complexities

Complexity Notation Description

constant O(1) n = 1 000  1-2 operations logarithmic O(log n) n = 1 000  10 operations linear O(n) n = 1 000  1 000 operations linearithmic O(n*log n) n = 1 000  10 000 operations quadratic O(n2) n = 1 000  1 000 000 operations cubic O(n3) n = 1 000  1 000 000 000 operations exponential O(n^n) n = 10  10 000 000 000 operations

  • Trying all possible combinations
  • Picking the best solution
  • Usually slow and inefficient

Brute-Force Algorithms

Brute-Force Algorithms

Brute-Force Algorithms

Brute-Force Algorithms

10 x 10 x 10 x 10 x 10 = 100,000 combinations