Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
9 January 2021

Algorithm Complexity Analysis

by mervyn

Control Statements

These are statements which potentially alter the execution of instructions

Given

if(condition)
{
  //true body
}
else
{
  //false body
}

cont

others

switch(i){
  case 1: /* do stuff */ break;
  case 2: /* do other stuff */ break;
  case 3: /* do even more stuff */ break;
  case 4: /* well, do stuff */ break;
  case 5: /* tired yet? */ break;
  default: /* do default stuff */
}

Thus, a switch statement would appear to run in \(O(n)\) time where n is the number of cases, the same as nested if Statements

  if(i==1){/* do stuff */}
  else if(i==2)(/* do other stuff */)
  else if(i==3)(/* do even more stuff */)
  else if(i==4)(/* well, do stuff */)
  else if(i==5){/* tired yet? */}
  else {/* do default stuff */}

Switch statements were included in the original C language. Why would you introduce something into programming language which is redundant?

  1. First, you may recall that the cases must be actual values, either: - integers - characters

For example, you cannot have a case with a variable, e.g., - case n: /* do something */ break; //bad

  1. The compiler looks at the different cases and calculates an appropriate jump

For instance, assume:

Analysis of Statements

In this case, we do not know

Condition-controlled loops

The initialization, condition, and increment usually are single statements running in \(\Theta(1)\)

for(int i=0; i< n; i++)
{
  //... code which is $$\Theta(f(n))$$
}

Assuming there are no break or return statements in the loop, the run time is \(\Omega(n)\)

int sum =0;
for(int i=0; i<n; i++){ //n times
  for(int j=0; j<n; j++){
    sum +=1; //$$\Theta(1)$$ (constant) times n
  }
}

This code has run time: \(\Theta(n\cdot n\cdot 1)=\Theta(n^2)\)

Analysis of Repetition Statements

Suppose with each loop, we use a linear search an array of size m

for(int i=0; i<n; i++){
  //search through an array of size m
  //$$O(m)$$;
}

In the worst case, the complexity of this search would looking at all m values, but it could end early. Therefore Big O notation rather than \(\Theta\).

for(int i=0; i<n; i++){
  //code which is $\Theta(f(i, n))$
}

is \(\Theta \left ( 1+\sum_{i=0}^{n-1}1+f(i, n) \right )\)

int sum=0;
for(int i=0; i<n; i++){
  for(int j=0; j<i; j++){
    sum +=i+j;
  }
}
int sum=0;
for(int i=0; i<n; i++){
  for(int j=0; j<i; j++){
    for(int k=0; k<j; k++){
      sum +=i + j + k;//const
    }
  }
}

Serial Statements

Consider the following two problems:

Functions

A function (or subroutine) is code which has been separated out, either to:

Because a subroutine (function) can be called from anywhere, we must:

Fortunately, this is such a common task that all modern processors have instructions that perform most of these steps in one instruction.

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 6-2 알고리즘의 복잡도 분석 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: