Академический Документы
Профессиональный Документы
Культура Документы
Page:1
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Computational complexity
Page:2
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
What is the COST of an algorithm???
• Time
• Space
Page:3
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
How can you practically compare 2
algorithms???
• Run them on the same machine
• Implement them using the same language
• Only time units such as nano-seconds or micro
seconds should not be used…WHY???
• Logical units expressing the relation between
the size of file ‘n’ and the amount of time ‘t’
required to process it should be used
• Same input data should be used
Page:4
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Problems with this approach
• Effort
• One program may be better written than the
other
• Choice of tests may favor one of the
algorithms
Page:5
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
What do we need?????
We require algorithm analysis!!!
Page:6
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Calculating T(n)
• A program step is the syntactically / semantically
meaningful segments of a program
• A step DOESNOT correspond to a definite time unit
• A step count is telling us how run time for a program
changes with change in data size
• Calculate the total number of steps/executable
statements in a program
• Find the frequency of each statement and sum them
up
• Don’t count comments and declarations
Page:7
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Analysis of ‘for’ Loop
1: for (i=0;i<n;++i)
2: sum++;
3: cout << sum
The condition always executes one more time than the loop
itself
Page:8
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Nested ‘for’ loops
for (i=0;i<n;++i)
{ for (j=0;j<m;++j)
sum++
}
Page:9
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Problems with T(n)
• T(n) is difficult to calculate
• T(n) is also not very meaningful as step size is not exactly
defined
• T(n) is usually very complicated so we need an approximation
of T(n)….close to T(n).
• This measure of efficiency or approximation of T(n) is called
ASYMPTOTIC COMPLEXITY or ASYMPTOTIC
ALGORITHM ANALYSIS
When approximating T(n) we can IGNORE the 100 term for very large value of n
and say that T(n) can be approximated by 7(n) Page:11
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Example 2
• T(n) = n2 + 100n + log10n +1000
n T(n) n2 100n log10n 1000
When approximating T(n) we can IGNORE the last 3 terms and say say that T(n) can
be approximated by n2
Page:12
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Big-Oh
• Definition:
f(n) is O(g(n)) if there exist positive numbers c & N such that f(n)<=cg(n)
for all n>=N
Page:13
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
What does it mean???? An example…
Page:15
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
FACTS ABOUT O(n)
Page:16
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Properties of Big-Oh
Page:17
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Growth rates
This is O(1)
sum++;
This is O(n)
for (i=0;i<n;++i)
sum++;
This is O(n2)
for (i=0;i<n;++i)
for (j=0;j<n;++j)
sum++;
Page:18
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
Growth rates
Which growth rate is better???
• Add 2n to the graph and
see where it fits
Here we have:
•Constant
•Linear
•Logarithmic
•Quadratic
•cubic
1 0 0 1 1 2
2 1 2 4 8 4
8 3 24 64 512 256
sum = 0;
i sum
for (i=1;i<n;i=i*2) 1 1
sum = sum+1; 2 1+1=2
4 2+1=3
8 3+1=4
…
n/2 log2n
n “exit loop”…
loop will not execute for i=n sum not incremented
further
Page:21
Dr. Mehreen Saeed
The value of sum is log2n at the end of the loop CSC 201:Data Structures and Algorithms
SPRING 2012
EXAMPLE OF O(log n)…suppose you are coloring the table
take half
take half
take half
You didn’t color all cells…..everytime you discarded half the cells
Page:22
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
EXAMPLE OF O(n)…suppose you are coloring the table
take half
Page:23
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
EXAMPLE OF O(n2)…suppose you are coloring the table
take half
Page:24
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
EXAMPLE OF O(2n)…suppose you are coloring the table
Page:25
Dr. Mehreen Saeed
CSC 201:Data Structures and Algorithms
SPRING 2012
SELECTION SORT
for (i=0;i<n;++i)
{
maxIndex = FindMaxIndex(arr,i,n-1);
swap(arr[i],arr[maxIndex]);
}
//FindMaxIndex(int arr[],int startIndex,int endIndex)
//finds the maximum item in the partial array
//from start index to end index