Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
23 December 2020

Algorithm Analysis

by mervyn

Motivation

The goal of algorithm analysis is to take a block of code and determine the asymptotic run time or asymptotic memory requirements based on various parameters.

Given an array of size n: - Merge sort requires \(\Theta(n lg n)\) time and \(\Theta(n)\) additional memory

Suppose that we have two algorithms A and B whose runtimes are \(f_{A}(n)=\Theta(n^2)\) and \(f_{B}(n)=\Theta(n lg n)\)

For \(n=2k\)

For \(n=10k\)

Binary search runs in \(\Theta(lg n)\) time:

Machine Instructions

Given any processor, it is capable of performing only a limited number of operations

An example (on the Coldfire): 0x06870000000F

Consider the operation a+=b;

Operators

Because each machine instruction can be executed in a fixed number of cycles, we may assume each operation requires a fixed number of cycles

Retrieving/storing variables from memory

Variable assignment =
Integer operations +, -, *, /, %, ++, –
Logical operations &&, ||, !
Bitwise operations &, |, ^, ~
Relational operations ==, !=, <, <=, =>, >
Memory allocation and deallocation new, delete

Operations

Of these, memory allocation and deallocation are the slowest by a significant factor (e.g. 100 times slower)

Blocks of Operations

Each operation runs in \(\Theta(1)\) time and therefore any fixed number of operations also run in \(\Theta(1)\) time.

Blocks in Sequence

Suppose that you have now analyzed a number of blocks of code run in Sequence

int* increase_Capacity(int *_array, int _n, int _delta){
  int *array_old=_array;
  _array=new int[_n+_delta];//$\Theta(1)$

  for(int i=0;i<_n;i++){
    _array[i]=array_old[i];//$\Theta(n)$
  }

  delete [] array_old;
  return _array;//$\Theta(1)$
}

Other examples include:

worder

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

back next

tags: Data - Structure

Comments

Post comment
Loading...

Share this: