0% found this document useful (0 votes)
11 views13 pages

Chapter 1 - Introduction

Uploaded by

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

Chapter 1 - Introduction

Uploaded by

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

Analysis of Algorithms

Course No.: 0602302


Analyzing Algorithms
• Algorithms: Sequence of computational steps that takes some values
as Input and produce some values as Output
• Predict the amount of resources required:
• memory: how much space is needed?
• computational time: how fast the algorithm runs?

• FACT: running time grows with the size of the input


• Input size (number of elements in the input)
– Size of an array, polynomial degree, # of elements in a matrix, # of bits in
the binary representation of the input, vertices and edges in a graph

2
Analyzing Algorithms
Def: Running time = the number of primitive
operations (steps) executed before termination
– Arithmetic operations (+, -, *), data movement, control, decision making
(if, while), comparison

3
Algorithm Efficiency vs. Speed
E.g.: sorting n numbers Sort 106 numbers!

Friend’s computer = 109 instructions/second


Friend’s algorithm = 2n2 instructions

Your computer = 107 instructions/second


Your algorithm = 50nlgn instructions

2  106  instructions
2

Your friend = 9 2000seconds


10 instructions / second
50  106 lg106 instructions
You = 7
100seconds
10 instructions / second

20 times better!!
4
Algorithm Analysis: Example
• Alg.: MIN (a[1], …, a[n])
m ← a[1];
for i ← 2 to n
if a[i] < m
then m ← a[i];
• Running time:
– the number of primitive operations (steps) executed
before termination
F(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
• Order (rate) of growth:
– The leading term of the formula
– Expresses the asymptotic behavior of the algorithm

5
Typical Running Time Functions
• 1 (constant running time):
– Instructions are executed once or a few times

• logN (logarithmic)
– A big problem is solved by cutting the original problem in smaller
sizes, by a constant fraction at each step

• N (linear)
– A small amount of processing is done on each input element

• N logN
– A problem is solved by dividing it into smaller problems, solving
them independently and combining the solution

6
Typical Running Time Functions
• N2 (quadratic)
– Typical for algorithms that process all pairs of data items (double
nested loops)

• N3 (cubic)
– Processing of triples of data (triple nested loops)

• NK (polynomial)

• 2N (exponential)
– Few exponential algorithms are appropriate for practical use

7
Why Faster Algorithms?

8
Computing the Complexity
• Each statement cost 1 unit of time except the iterative and recursion
statements
• Iterative Statement:
– Linear loop
• Examples:

for (i=1;i<n; i++) For(i=1;i<=n;i++)


Statement st1
st2
stn
• Complexity: f(n)=O(n)

– Nested loops

– Dependent nested loop

– Logarithmic loop
9
Computing the Complexity
– Nested loops
• Ex:
For(i=1;i<n;i++)
for(j=1;j<n;j++)
statement

• Complexity f(n)=O(n2)

– Dependent nested loop

For(i=1;i<n;i++)
for(j=1;j<i;j++)
statement

• Complexity f(n)=O(n2)

10
Computing the Complexity
– Logarithmic loop
i=1;
While(i<=n)
st1
st2
i=i*2

– Complexity: F(n)=O(log n)

Iteration number I Value of I at the beging of


the iteration

1 1
2 2
3 4

N 2(I-1)
11
Asymptotic Notations - Examples
  notation
– n2/2 – n/2 = (n2)
– (6n3 + 1)lgn/(n + =
1)(n2lgn)
– n vs. n2 n ≠ (n2)
  notation • O notation
– n3 vs. n2 n3 = (n2) – 2n2 vs. n3 2n2 = O(n3)
– n vs. logn n = (logn) – n2 vs. n2 n2 = O(n2)
– n vs. n2 n  (n2) – n3 vs. nlognn3  O(nlgn)

12
Types of Analysis
• Worst case (e.g. cards reversely ordered)
– Provides an upper bound on running time
– An absolute guarantee that the algorithm would not
run longer, no matter what the inputs are
• Best case (e.g., cards already ordered)
– Input is the one for which the algorithm runs the
fastest
• Average case (general case)
– Provides a prediction about the running time
– Assumes that the input is random

13

You might also like