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

Questions That Will Be Answered

The document discusses analyzing the efficiency of algorithms. It begins by asking questions about measuring efficiency and analyzing simple programs. It then provides examples to illustrate analyzing the time complexity of basic algorithms using Big-O notation. The key points made are: (1) efficiency is measured by counting the number of elementary operations like additions or comparisons, (2) for a simple summing algorithm, the time complexity is O(N) where N is the input size, (3) the complexity of nested loops is the product of the complexities of each level, and (4) the overall complexity is the sum of the complexities of its parts.

Uploaded by

mahmad143
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)
43 views13 pages

Questions That Will Be Answered

The document discusses analyzing the efficiency of algorithms. It begins by asking questions about measuring efficiency and analyzing simple programs. It then provides examples to illustrate analyzing the time complexity of basic algorithms using Big-O notation. The key points made are: (1) efficiency is measured by counting the number of elementary operations like additions or comparisons, (2) for a simple summing algorithm, the time complexity is O(N) where N is the input size, (3) the complexity of nested loops is the product of the complexities of each level, and (4) the overall complexity is the sum of the complexities of its parts.

Uploaded by

mahmad143
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

Questions that will be answered

What is a good or "efficient" program?


How to measure the efficiency of a program?
How to analyse a simple program?
How to compare different programs?
What is the big-O notation?
What is the impact of input on program performance?
What are the standard program analysis techniques?
Do we need fast machines or fast algorithms?

1/31

Which is better?
The running time of a program.

Program easy to understand?


Program easy to code and debug?
Program making efficient use of resources?
Program running as fast as possible?
Ways of measuring efficiency:
how long it takes
how much memory it uses

2/31

Measuring Efficiency
Want to achieve platform-independence
Use an abstract machine that uses steps of time and units
of memory, instead of seconds or bytes
- each elementary operation takes 1 step
- each elementary instance occupies 1 unit of memory

3/31

A Simple Example

// Input: int A[N], array of N integers


// Output: Sum of all numbers in array A
int Sum(int A[], int N) {
int s=0;
for (int i=0; i< N; i++)
s = s + A[i];
return s;
}

How should we analyse this?

4/31

A Simple Example
Analysis of Sum
1.) Describe the size of the input in terms of one or more
parameters:
- Input to Sum is an array of N integers, so size is N.
2.) Then, count how many steps are used for an input of that size:
- A step is an elementary operation such as
+, <, =, A[i]

5/31

A Simple Example
Analysis of Sum (2)
// Input: int A[N], array of N integers
// Output: Sum of all numbers in array A
int Sum(int A[], int N {
int s=0;
1
for (int i=0; i< N; i++)

2
5

s = s + A[i];

return s;
}

6
8

4
1,2,8: Once
3,4,5,6,7: Once per each iteration
of for loop, N iteration
Total: 5N + 3
The complexity function of the
algorithm is : f(N) = 5N +3
6/31

Analysis: A Simple Example


How 5N+3 Grows
Estimated running time for different values of N:
N = 10
N = 100
N = 1,000
N = 1,000,000

=> 53 steps
=> 503 steps
=> 5003 steps
=> 5,000,003 steps

As N grows, the number of steps grow in linear proportion to


N for this Sum function.

7/31

Analysis: A Simple Example

What Dominates?

What about the 5 in 5N+3? What about the +3?


As N gets large, the +3 becomes insignificant
5 is inaccurate, as different operations require varying
amounts of time
What is fundamental is that the time is linear in N.
Complexity: As N gets large, concentrate on the
highest order term:
Drop lower order terms such as +3
Drop the constant coefficient of the highest order term i.e. N

8/31

Analysis: A Simple Example


Complexity

The 5N+3 time bound is said to "grow asymptotically" like N


This gives us an approximation of the complexity of the
algorithm
Ignores lots of (machine dependent) details, concentrate
on the bigger picture
Examples:
5N + 3 = O(N)
37N5 + 7N2 - 2N + 1 = O(N5)

9/31

Standard Analysis Techniques

Constant time statements


Simplest case: O(1) time statements
Assignment statements of simple data types
int x = y;
Arithmetic operations:
x = 5 * y + 4 - z;
Array referencing:
A[j] = 5;
Array assignment:
j, A[j] = 5;
Most conditional tests:
if (x < 12) ...
10/31

Standard Analysis Techniques


Analyzing Loops
Any loop has two parts:
1. How many iterations are performed?
2. How many steps per iteration?
int sum = 0,j;
for (j=0; j < N; j++)
sum = sum +j;
- Loop executes N times (0..N-1)
- O(1) steps per iteration
- Total time is N * O(1) = O(N*1) = O(N)

11/31

Standard Analysis Techniques


Nested Loops
Treat just like a single loop and evaluate each level of
nesting as needed:
int j,k;
for (j=0; j<N; j++)
for (k=N; k>0; k--)
sum += k+j;
Start with outer loop:
- How many iterations? N
- How much time per iteration? Need to evaluate inner loop
Inner loop uses O(N) time
Total time is N * O(N) = O(N*N) = O(N2)

PRODUCT RULE
12/31

Standard Analysis Techniques


Sequence of Statements
For a sequence of statements, compute their complexity functions
individually and add them up
for (j=0; j < N; j++)
for (k =0; k < j; k++)
sum = sum + j*k;
for (l=0; l < N; l++)
sum = sum -l;
printf("sum is now %f", sum);

O(N2)
O(N)
O(1)

Total cost is O(N2) + O(N) +O(1) = O(N2)

SUM RULE
13/31

You might also like