0 views

Uploaded by Enmusk

373

- 7648147-23-Slide-Algorithm-Efficiency
- 4 - The Master Method
- Data.pdf
- 3 Analysis.gtm
- 23slide.ppt
- Analysis
- Contention Resolution with Heterogeneous Job Sizes
- AOA Analyzing Algorithms
- Ch5-Searching-Part1-The Design and Analysis of Parallel Algori - Selim G Akl
- Cormen Algo-lec2
- Chapter Handout
- 1 Asymptotic Notation -Algorithms (series lecture)
- Lecture 03
- ADA Course Plan NAG
- 3-Analysis.GTM.ppt
- Rambidi Biomolecular Computer
- 18. Upgrading the Archos Operating System
- R09220505-DESIGNANDANALYSISOFALGORITHMS
- CIRRELT-2007-41 - BOX PACKING.pdf
- L03-BigOh.ppt

You are on page 1of 52

Analysis of algorithms

CSE373: Design and Analysis of Algorithms

Definition

In simple terms, an algorithm is a series of instructions to

solve a problem (complete a task)

Business

Get a part from Dhaka to Sylhet by morning

Allocate manpower to maximize profit

Life

I am hungry. How do I order pizza?

Explain how to tie shoelaces to a five year old child

Definition

An algorithm is a finite set of precise instructions for

performing a computation or for solving a problem.

It must produce the correct result

It must finish in some finite time

You can represent an algorithm using pseudocode, flowchart, or

even actual code

Algorithm Description

Course Objective

The theoretical study of design and analysis of computer

algorithms

resources and performance

The Problem of Sorting

Input: The sequence a1, a2, …, an of numbers.

a'2 … a'n .

Example:

Input: 8 2 4 9 3 6

Output: 2 3 4 6 8 9

Insertion Sort

for j ← 2 to n

do key ← A[ j]

i←j–1

“pseudocode” while i > 0 and A[i] > key

do A[i+1] ← A[i]

i←i–1

A[i+1] ← key

1 i j n

A:

key

sorted

Example

8 2 4 9 3 6

Example

8 2 4 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Example

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done

Analysis of Algorithms

What does it mean to analyze an algorithm?

To have an estimate about how much time an algorithm may

take to finish, or in other words, to analyze its running time

Sometimes, instead of running time, we are interested in how

much memory the algorithm may consume while it runs

It enables us to compare between two algorithms

What do we mean by running time analysis?

Also referred to as time-complexity analysis

To determine how running time increases as the size of the

problem increases.

Analysis of Algorithms

Size of the problem can be a range of things, including

size of an array

.

and so on…

How Do We Analyze Running Time?

We need to define an objective measure.

(1) Compare execution times?

Not good: times are specific to a particular computer !!

How Do We Analyze Running Time?

We need to define an objective measure.

(2) Count the number of statements executed?

Associate a "cost" with each statement.

Find the "total cost“ by finding the total number of times each statement

is executed.

Not good: number of statements vary with the programming language as

well as the style of the individual programmer.

Algorithm 1 Algorithm 2

Cost c1 c2 c3 Cost

arr[0] = 0; c1 for(i=0; i<N; i++) c1+c2(N+1)+ c3N

arr[1] = 0; c1 arr[i] = 0; c1N

arr[2] = 0; c1 -----------------------------

... c1+c2(N+1)+c3N+c1N = (c1+c2+c3)N+(c1+c2)

arr[N-1] = 0; c1

----------------------

c1+c1+...+c1 = c1N

Insertion Sort

for j ← 2 to n

do key ← A[ j]

i←j–1

while i > 0 and A[i] > key

do A[i+1] ← A[i]

i←i–1

A[i+1] ← key

Insertion Sort

Statement cost

INSERTION-SORT (A, n) ⊳ A[1 . . n]

for j ← 2 to n 𝑐1 𝑛

do key ← A[ j] 𝑐2 (𝑛 − 1)

i←j–1 𝑐3 (𝑛 − 1)

while i > 0 and A[i] > key 𝑐4 σ𝑛𝑗=2 𝑡𝑗

do A[i+1] ← A[i] 𝑐5 σ𝑛𝑗=2(𝑡𝑗 − 1)

i←i–1 𝑐6 σ𝑛𝑗=2(𝑡𝑗 − 1)

A[i+1] ← key 𝑐7 (𝑛 − 1)

𝑇 𝑛

𝑛 𝑛 𝑛

= 𝑐1 𝑛 + 𝑐2 𝑛 − 1 + 𝑐3 𝑛 − 1 + 𝑐4 𝑡𝑗 + 𝑐5 (𝑡𝑗 − 1) + 𝑐6 (𝑡𝑗 − 1) + 𝑐7 (𝑛 − 1)

𝑗=2 𝑗=2 𝑗=2

Time Complexity Analysis

Express running time as a function of the input size n (i.e., f(n)).

Compare different functions of running times in an asymptotic

manner.

Such an analysis is independent of machine type, programming style,

etc.

To compare two algorithms with running times f(n) and g(n), we need

a rough measure that characterizes how fast each function grows

(rate of growth).

Visualizing Orders of Growth

On a graph, as you go to the right, a faster growing function

eventually becomes larger.

fA(n)=30n+8

Running time→

fB(n)=n2+1

Increasing n →

Growth of Functions

Complexity Graphs

log(n)

Complexity Graphs

n log(n)

log(n)

Complexity Graphs

n10 n3

n2

n log(n)

Complexity Graphs (log scale)

3n

nn

n20

2n

n10

1.1n

Asymptotic Notations

Compare functions for large values of n in the limit, that is,

asymptotically!

Asymptotic Notations

Consider the example of buying elephants and goldfish:

Cost: cost_of_elephants + cost_of_goldfish

Cost ~ cost_of_elephants (approximation)

The low order terms, as well as constants in a function are

relatively insignificant for large n

6n + 4 ~ n

n4 + 100n2 + 10n + 50 ~ n4

the same rate of growth

Asymptotic Notations

• O-notation

with smaller or same order of

growth as g(n)

Asymptotic Notations

Example:

Show that f(x) = x2 + 2x + 1 is O(x2).

x2 + 2x + 1 x2 + 2x2 + x2

x2 + 2x + 1 4x2

Therefore, for C = 4 and k = 1:

f(x) Cx2 whenever x > k.

f(x) is O(x2).

Asymptotic Notations

We say 𝑓(𝑛) = 30000 is in the order of 1, or 𝑶(𝟏)

Growth rate of 30000 is constant, that is, it is not dependent on

problem size.

𝑓(𝑛) = 30𝑛 + 8 is in the order of 𝑛, or 𝑶(𝒏)

Growth rate of 30𝑛 + 8 is roughly proportional to the growth rate

of 𝑛.

𝑓(𝑛) = 𝑛2 + 1 is in the order of 𝑛2, or 𝑶 𝒏𝟐

Growth rate of 𝑛2 + 1 is roughly proportional to the growth rate

of 𝑛2.

In general, any 𝑂(𝑛2) function is faster- growing than any

𝑂(𝑛) function.

For large 𝑛, a 𝑂(𝑛2) algorithm runs a lot slower than a 𝑂(𝑛)

algorithm.

Asymptotic Notations

Question: If f(x) is O(x2), is it also O(x3)?

Yes. x3 grows faster than x2, so x3 grows also faster than f(x).

Therefore, we always have to find the smallest simple function g(x)

for which f(x) is O(g(x)).

Asymptotic Notations

• - notation

with larger or same order of

growth as g(n)

Asymptotic Notations

-notation

with the same order of growth

as g(n)

Some Examples

Determine the time complexity for the following algorithm.

count = 0;

for(i=0; i<10000; i++)

count++;

Some Examples

Determine the time complexity for the following algorithm.

count = 0;

for(i=0; i<10000; i++)

count++; 𝑶(𝟏)

Some Examples

Determine the time complexity for the following algorithm.

count = 0;

for(i=0; i<N; i++)

count++;

Some Examples

Determine the time complexity for the following algorithm.

count = 0;

for(i=0; i<N; i++)

count++; 𝑶(𝒏)

Some Examples

Determine the time complexity for the following algorithm.

sum = 0;

for(i=0; i<N; i++)

for(j=0; j<N; j++)

sum += arr[i][j];

Some Examples

Determine the time complexity for the following algorithm.

sum = 0;

for(i=0; i<N; i++)

𝑶(𝒏 𝟐)

for(j=0; j<N; j++)

sum += arr[i][j];

Some Examples

Determine the time complexity for the following algorithm.

count = 0;

for(i=1; i<N; i=i*2)

count++;

Some Examples

Determine the time complexity for the following algorithm.

count = 0;

for(i=1; i<N; i=i*2)

count++; 𝑶(log 𝒏)

Some Examples

Determine the time complexity for the following algorithm.

char someString[10];

gets(someString);

for(i=0; i<strlen(someString); i++)

someString[i] -= 32;

Some Examples

Determine the time complexity for the following algorithm.

char someString[10];

gets(someString);

for(i=0; i<strlen(someString); i++)

someString[i] -= 32; 𝑶(𝒏𝟐)

Types of Analysis

Is input size everything that matters?

int find_a(char *str)

{

int i;

for (i = 0; str[i]; i++)

{

if (str[i] == ’a’)

return i;

}

return -1;

}

Time complexity: 𝑂(𝑛)

Types of Analysis

So how does the running time vary with respect to various input?

Three scenarios

Best case

𝑏𝑒𝑠𝑡𝑓𝑖𝑛𝑑_𝑎 = min 𝑟𝑢𝑛𝑡𝑖𝑚𝑒𝑓𝑖𝑛𝑑_𝑎

𝑥∈𝑋𝑛

Worst case

𝑤𝑜𝑟𝑠𝑡𝑓𝑖𝑛𝑑_𝑎 = max 𝑟𝑢𝑛𝑡𝑖𝑚𝑒𝑓𝑖𝑛𝑑_𝑎

𝑥∈𝑋𝑛

Average case

1

𝑎𝑣𝑔𝑓𝑖𝑛𝑑_𝑎 = 𝑟𝑢𝑛𝑡𝑖𝑚𝑒𝑓𝑖𝑛𝑑_𝑎

𝑋𝑛

𝑥∈𝑋𝑛

Types of Analysis

Worst-case: (usually done)

upper bound on running time

maximum running time of algorithm on any input of size n

Average-case: (sometimes done)

we take all possible inputs and calculate computing time for all of the

inputs

sum all the calculated values and divide the sum by total

number of inputs

we must know (or predict) distribution of cases

Best-case: (bogus)

lower bound on running time of an algorithm

minimum running time of algorithm on any input of size n

- 7648147-23-Slide-Algorithm-EfficiencyUploaded byAl Yemani Comandante
- 4 - The Master MethodUploaded byjzegan
- Data.pdfUploaded byEmily Wright
- 3 Analysis.gtmUploaded byaslam jan
- 23slide.pptUploaded byQueenie Quach
- AnalysisUploaded bySree Ps
- Contention Resolution with Heterogeneous Job SizesUploaded byneuraltic
- AOA Analyzing AlgorithmsUploaded byRavneet Shah Singh
- Ch5-Searching-Part1-The Design and Analysis of Parallel Algori - Selim G AklUploaded byHendro Utomo
- Cormen Algo-lec2Uploaded bygeniusamit
- Chapter HandoutUploaded byVds Krishna
- 1 Asymptotic Notation -Algorithms (series lecture)Uploaded byturjo987
- Lecture 03Uploaded bysaahil480
- ADA Course Plan NAGUploaded byJerin George
- 3-Analysis.GTM.pptUploaded byaslam jan
- Rambidi Biomolecular ComputerUploaded byPrashant Kumar
- 18. Upgrading the Archos Operating SystemUploaded byAek Benabadja
- R09220505-DESIGNANDANALYSISOFALGORITHMSUploaded bynveman
- CIRRELT-2007-41 - BOX PACKING.pdfUploaded byAlexandru Dutuc
- L03-BigOh.pptUploaded byViswanathan
- Micom p127 Directional an Non-directional RelayUploaded byMario Sitorus
- Uncapacitated Multiple AllocationUploaded byLeonardo Cetre
- Test prepation for algorithm.docxUploaded byM. Yasir zia aulakh
- unit-IUploaded bymayank yadav
- ex no 4Uploaded by90gaurang
- Weekly Time Sheet_KleosUploaded byGlory
- Prime GapsUploaded byanon020202
- DocuUploaded bydawn.dev
- Mcq in DAAUploaded bykamakshi2
- Lecture 2Uploaded byryancraig4

- L13 Graph Part02Uploaded byEnmusk
- L08 DynamicProgramming_Part02Uploaded byEnmusk
- Last ClassUploaded byEnmusk
- L06 Greedy Part02Uploaded byEnmusk
- L16 Graph Part05Uploaded byEnmusk
- L05 Greedy Part01Uploaded byEnmusk
- L03 DivideConquer_Part02Uploaded byEnmusk
- L10 DynamicProgramming_Part04Uploaded byEnmusk
- L15 Graph Part04Uploaded byEnmusk
- L09 DynamicProgramming_Part03Uploaded byEnmusk
- L11 DynamicProgramming_Part05Uploaded byEnmusk
- L04 DivideConquer_Part03Uploaded byEnmusk
- L07 DynamicProgramming_Part01Uploaded byEnmusk
- L12 Graph Part01Uploaded byEnmusk
- L14 Graph Part03Uploaded byEnmusk
- Lec 4,5 Chemistry of LifeUploaded byEnmusk
- Lec 17 Circulatory SystemUploaded byEnmusk
- Lec 1,2,3 RM.pdfUploaded byEnmusk
- Lec 19 Diabetes, Lipid ProfileUploaded byEnmusk
- Lec 3 Classification of Living ThingsUploaded byEnmusk
- Lec 1,2 Intro to Biology and LifeUploaded byEnmusk
- List_of_vitamins.docUploaded byEnmusk
- Lec 18 Respiratory, Excretory SystemUploaded byEnmusk
- Lec 16 DigestionUploaded byEnmusk
- Lec 1,2,3 RMUploaded byEnmusk
- Assignment 5CapacitorUploaded byEnmusk
- Lec 15 HomeostasisUploaded byEnmusk
- L02 DivideConquer_Part01Uploaded byEnmusk
- 373 Assignment 1Uploaded byEnmusk

- literacy retreat reflectionUploaded byapi-301712485
- advance reading in managementUploaded byBary Patwal
- nvas posterUploaded byapi-310282544
- Theories and Empirical Studies of International InstitutionsUploaded byLígia Magalhães
- Six Sigma ToolsUploaded byunknown139
- WAJ3102 English Language Proficiency TOPIC 1Uploaded bymazni bin othman
- a perspectives of educatioinal technologyUploaded byapi-247091657
- New Microsoft Word Document list of pronounsUploaded byShivanand Indi
- Self Awareness UsUploaded byarbed
- CRI Marking CriteriaUploaded byMikeyy Nzuzi
- A Vocabulary of the Yoruba LanguageUploaded byعلي كرامر
- Planning and Control of Two-Link Rigid Flexible Manipulators in Dynamic Object Manipulation MissionsUploaded byInternational Journal of Robotics, Theory and Applications
- BUS 272 – CH 1 notesUploaded byNancy Mo
- Maslow’s Hierarchy of NeedsUploaded byRezza Jeffry
- Inculcation of Citizenship Values through Board GameUploaded byAnonymous CwJeBCAXp
- vol12_report2Uploaded byGoneWith TheWind
- Differentiating EdgeUploaded byNeelam Srivastava
- SCIENCE DAN TEORI (KEDOKTERAN).pptUploaded byYus Ani
- Make It Stick the Science of Successful LearningUploaded byqwertysamy
- copy of partial index of templates from they say - i say grade 12Uploaded byapi-253737203
- Behrens Linguistics 2009Uploaded bydcenterd
- MSc Assess Brief 18-3Uploaded byAbdan Lubis
- Museum Fatigue Museum StudiesUploaded byPablo Julián Moreno
- Ameriks,--Kant and the Self,A Retrospective 17.pdfUploaded byfreudlich
- DissertationUploaded byAppy Thianhlun
- Buyer BehaviorUploaded byneeeeeeeeeeeeeej
- English 024Uploaded byChieng Miew Sing
- [Whiteley]_Memletics_Accelerated_Learning_Manual_((Book4You).pdfUploaded byDerek Leckie
- Mark Garner, Claire Wagner, Barbara Kawulich Teaching Research Methods in the Social Sciences (1).pdfUploaded bymorafa01
- Building Your Leadership Team Value Systems MemeticsUploaded byMartín García