Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views11 pages

02 CS351 Algorithms Analysis

The document discusses the analysis of algorithms, focusing on time complexity and the various O-notation classifications. It outlines different complexity categories, including constant, logarithmic, linear, polynomial, exponential, and factorial complexities, providing examples for each. Additionally, it explains how to calculate time complexity for loops and nested loops, emphasizing the importance of understanding growth rates in algorithm efficiency.

Uploaded by

Tala Akour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

02 CS351 Algorithms Analysis

The document discusses the analysis of algorithms, focusing on time complexity and the various O-notation classifications. It outlines different complexity categories, including constant, logarithmic, linear, polynomial, exponential, and factorial complexities, providing examples for each. Additionally, it explains how to calculate time complexity for loops and nested loops, emphasizing the importance of understanding growth rates in algorithm efficiency.

Uploaded by

Tala Akour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 1

Analysis of Algorithms

Design and Analysis of Computer


Algorithms
O-notation - Seven Important Functions

Complexity Term
Most efficient
O(1) constant
O(log n) logarithmic
O(n) linear
O(n lg n) n log n “linear-logarithmic”
O(nb) b > 1 polynomial(n2 :square or quadratic
n3 :cubic)
Least efficient
O(bn) b > 1 exponential
(hard problems) O(n!) factorial
Complexity categories
growth rates of some common complexity functions.
Sample : O(1)
Time complexity of a function (or set of statements) is considered as
O(1) if it doesn’t contain loop, recursion and call to any other non-
constant time function.

1.define swap(a, b)
2. temp := a -------------------- 1
3. a := b -------------------- 1
4. b := temp -------------------- 1
___________________________________________
Total = f(n) = 3 ➔ O(1)
1.// c is a constant
2. for (int i = 1 ; i <= c ; i++) {
3. cout << i
4. }
___________________________________________________
Total = f(n) = c ➔ O(1)
Sample : O(n)
Time Complexity of a loop is considered as O(n) if the loop variables is
incremented / decremented by a constant amount. For example
following functions have O(n) time complexity.

N
1. for (int i = 1; i <= n; i += 1) { 1. for (int i = 1; i <= n; i += 2) { N-1
.
2. print(i) 2. print(i) .
3. } 3. } .

2
Total = f(n) = n ➔ O(n) Total = f(n) = ½ *n ➔ O(n) 1

1. for (int i = n; i > 0; i -= 1) { 1. // c is a constant


2. // some O(1) expressions 2. for (int i = n; i > 0; i -= c) { N
3. } 3. // some O(1) expressions N-1
.
4. } .
Total = f(n) = n ➔ Total = f(n) = n/c ➔ .
O(n) O(n)
2
1
Sample : O(nc)
O(nc): Time complexity of nested loops is equal to the number of
times the innermost statement is executed.
For example the following sample loops have O(n2) time complexity

Number of inner
i j loop iterations
1. for (int i = 1; i <=n; i += 1){ ------- n
1 1,2,..., n N
2. for (int j = 1; j <=n; j += 1) ------- n2
3. print (j) ------- n2 2 1,2,..., n N
4. Print(i)} ------------ n . . .
---------------------------------------------------------------- . . .
Total = F(n) = 2n + 2n2 ➔ O(n2) . . .
N 1,2,..., n N

𝑻𝒐𝒕𝒂𝒍 𝒐𝒇 𝒊𝒏𝒏𝒆𝒓 𝒍𝒐𝒐𝒑 = 𝑵 + 𝑵 + ⋯ . . +𝑵 = ෍ 𝑵 = 𝑵 × 𝑵 = 𝑵𝟐


𝒊=𝟏
Sample : O(nc)
O(nc): Time complexity of nested loops is equal to the number of
times the innermost statement is executed.
For example the following sample loops have O(n2) time complexity

Number of inner
i j loop iterations
1. for (int i = 1; i <= N; i += 1) ------- N
1 1,2,..., M M
2. for (int j = 1; j <= M ; j += 1) ------- N2
3. print (j) ------- N2 2 1,2,..., M M
4.
. . .
---------------------------------------------------------------- . . .
Total = F(n) = N + 2N2 ➔ O(n2) . . .
N 1,2,..., M M

𝑻𝒐𝒕𝒂𝒍 𝒐𝒇 𝒊𝒏𝒏𝒆𝒓 𝒍𝒐𝒐𝒑 = 𝑴 + 𝑴 + ⋯ . . +𝑴 = ෍ 𝑴 = 𝑵 × 𝑴 = 𝑵𝟐


𝒊=𝟏
Sample : O(nc)
O(nc): Time complexity of nested loops is equal to the number of times
the innermost statement is executed.
For example the following sample loops have O(n3) time complexity

1. for x in range(n): -------------------- n


2. print(x) -------------------- n
3. for y in range(n): -------------------- n2
4. print(x,y) -------------------- n2
5. for k in range(n): -------------------- n3
6. print(x,y,k) -------------------- n3
_________________________________________________________
Total = F(n) = 2n + 2n2 +2n3 ➔ O(n3)
Sample : O(log n)
O(Logn) Time Complexity of a loop is considered as O(Logn) if
the loop variables is divided / multiplied by a constant
amount (e.g 2).
1. for (int i = n; i > 0; i /= 2) { 1. for (int i = 1; i <=n; i *= 2) {
2. // some O(1) expressions 2. // some O(1) expressions
3. } 3. }
➔ O(log n) ➔ O(log n)

n
n
16 16

8 8

4 4

1 1
1 1
Sample : O(log n) -Logarithmic

F(n) = log2n , F(n) = log3n , F(n) = log10n ➔ O(log n)

1. x = n 1. x = 1
2. while (x > 0): 2. while (x <= n):
3. print(x) 3. print(x)
4. x = x/3 4. x = x*5

f(n) = log3n ➔ O(log n) f(n) = log5n ➔ O(log n)


n n

1 1
Sample : time complexities of consecutive loops?
When there are consecutive loops, we calculate time
complexity as sum of time complexities of individual loops.

1. for (int i = 1; i <=m; i += c) {


2. // some O(1) expressions -------------------- 1/c *M
3. }

4. for (int i = 1; i <=n; i += c) {


5. // some O(1) expressions -------------------- 1/c *N
6. }

➔ O(n)

• Time complexity of above code is O(m) + O(n) which is O(m+n)

• If m == n, the time complexity becomes O(2n) which is O(n).

You might also like