Complexity Analysis of Algorithms: Understanding Big-O Notation, Slides of Aeronautical Engineering

An in-depth analysis of algorithms, focusing on their complexity in terms of time and space. Topics covered include best and worst-case scenarios, storage vs. Computation time, big-o notation, and code comparisons. Students will learn how to write procedures, understand the importance of big-o notation, and compare the efficiency of different algorithms.

Typology: Slides

2011/2012

Uploaded on 07/20/2012

savitha_48
savitha_48 🇮🇳

3

(1)

109 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
“Just how good is my algorithm?”
3
Complexity Analysis
Time (seconds)
60
50
40
30
20
10
0 1 2 3 4 5
O(C)
O(n2)
“Just how good is my algorithm?”
4
In-class Exercise
and calculates the sum of
all integers 1..N
Best case vs. worst case
Storage vs. Computation time
Computing the computation time
Big-O notation
Write a procedure that reads an
integer N
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Complexity Analysis of Algorithms: Understanding Big-O Notation and more Slides Aeronautical Engineering in PDF only on Docsity!

“Just how good is my algorithm?”

3

Complexity Analysis

Time (seconds)

60 50 40 30 20 10 (^0 1 2 3 4 )

O(C) O(n 2 )

“Just how good is my algorithm?”

4

In-class Exercise

and calculates the sum of

all integers 1..N

• Best case vs. worst case

• Storage vs. Computation time

• Computing the computation time

• Big-O notation

• Write a procedure that reads an

integer N

5

Code Comparison

linear time?

with Ada.Integer_Text_Io, Ada.Text_Io; use Ada.Integer_Text_Io, Ada.Text_Io; procedure CalcSum is N : Integer; Total_Sum : Integer; begin Put_Line("Enter an Integer: "); Get(N); Total_Sum := 0; for I in 1..N loop Total_Sum := Total_Sum + I; ; Put(Total_Sum); end ;

6

Code Comparison

with Ada.Integer_Text_Io, Ada.Text_Io; use Ada.Integer_Text_Io, Ada.Text_Io; procedure Calcsum is N : Integer; Total_Sum : Integer; begin Put_Line("Enter an Integer: "); Get(N); Total_Sum := 0; Total_Sum := (N * (N + 1)) / 2; Put(Total_Sum); end ;

N*(N+1) 2

• How many have a solution that runs in

end loop

• How many have a solution that runs in

constant time?

9

O(1)

of what input we give to the algorithm

10

O(N)

elements to find that the element we

are looking for does not exist

does not exist

size N where a value does not exist

• Constant time or space, independently

• Examples:

Access element in an array

Retrieve the first element in a list

• We have to search through all existing

• Examples:

Searching for element in a list that

Searching through a Binary Tree of

11

O(log N)

search

  • into O(log N)

12

Binary Search

How many elements are examined in worst case?

10 11 14 17 21 33 55 57 62 71 87 89 91 93 95 97

1 2 3 4 5 6 7 8 9

Example, a full balanced Binary Search Tree

Can eliminate half of the BST every time the

Any algorithm that eliminates a large portion of the data set at each iteration is generalized

10 11 12 13 14 15 16

15

O(M N^ )

  • f (0) = 1
  • f (1) = 1
  • f ( n +2) = f( n ) + f( n +1) ∀ n≥ 0

2 N^ calculations

16

Big-O

1 and largest N 2 number in a list and generate a new list of 1 and N 2

• Example: Fibonacci algorithm

• O(N+M)

  • Sequential and unrelated tasks
  • Ex: to find the smallest N

all the numbers in between N

• O(N*M)

  • Nesting of tasks
  • Ex: initializing a n-by-m matrix

17

Asymptotic Analysis: Big-O

Definition : T (n) = O(f(n)) – “ T of n is in Big-Oh of f of n” c and n 0 such that: T (n) ≤ cf(n) for all n ≥ n 0

: The algorithm is in O( n^2 ) in [best, average, worst] case.

Meaning : For all data sets big enough (i.e., n > n 0 ), the algorithm always executes in less than cf ( n case. Big-O is said to describe an “upper bound

18

Big-O Examples

Finding value X in an array (average cost).

T ( n ) = cs n /2.

For all values of n > 1, c (^) s n /2 <= c (^) s n.

Therefore, by the definition, T ( n ) is in O( n ) for n 0 = 1 and c = cs.

T (n) = O(f(n)) iff T (n) ≤ cf(n) for all n ≥ n 0

• Mathematical concept that expresses

“how good” or “how bad” an algorithm is

iff there are constants

Usage

) steps in [best, average, worst]

” on the complexity.

21

Big-O Simplifications

O(N*M+N^2 ) Same as

O(N^2 logP+N) Same as

O(5*N^3 Reduces to

O(N+P+Q) Same as O(N+P+Q)

O( 5N^3 +2P+QR )

O( N^2 logP+N )

O( N*M+N^2 )

22

Faster Computer or Algorithm?

The old computer processes 10,

instructions per hour

What happens when we buy a computer

10 times faster?

2 n 13 16 -----

2 n^270223 3.

5 n log n 250 1,842 7.

20 n 500 5,000 10

10 n 1,000 10,000 10

T ( n ) n nn ’/ n

+ 7N+ 2P+Q*R)