CENG205-Lecture2
CENG205-Lecture2
Data structures
Lecture 2:
Recursion & Performance analysis
System Life Cycle
• 2. Analysis: The first job after defining the requirements is to analyze the
problem. There are two ways for this:
• Bottom-up
• Top-down
System Life Cycle (cont’)
• 3. Design: Data objects and the possible operations between the objects are
defined in this step. Therefore, abstract data objects and the algorithm are
defined. They are all independent of the programming language.
void printStars(int n) {
if (n == 1) {
// base case; just print one star
printf("*\n");
} else {
...
}
}
Handling more cases
void printStars(int n) {
if (n == 1) {
// base case; just print one star
printf("*\n");
} else if (n == 2) {
printf("*");
printf("*\n");
} else if (n == 3) {
printf("*");
printf("*");
printf("*\n");
} else if (n == 4) {
printf("*");
printf("*");
printf("*");
printf("*\n");
} else ...
}
Handling more cases 2
void printStars(int n) {
if (n == 1) {
// base case; just print one star
printf("*\n");
} else if (n == 2) {
printf("*");
printStars(1); // prints "*"
} else if (n == 3) {
printf("*");
printStars(2); // prints "**"
} else if (n == 4) {
printf("*");
printStars(3); // prints "***"
} else ...
}
Using recursion properly
void printStars(int n) {
if (n == 1) {
// base case; just print one star
printf("*\n");
} else {
// recursive case; print one more star
printf("*");
printStars(n - 1);
}
}
Even simpler
void printStars(int n) {
if (n == 0) {
// base case; just end the line of
output
printf(“\n”);
} else {
// recursive case; print one more star
printf("*");
printStars(n - 1);
}
}
Recursive tracing
int mystery(int n) {
if (n < 10) {
return n;
} else {
int a = n / 10;
int b = n % 10;
return mystery(a + b);
}
}
mystery(648):
▪ int a = 648 / 10; // 64
▪ int b = 648 % 10; // 8
▪ return mystery(a + b); // mystery(72)
mystery(72):
▪ int a = 72 / 10; // 7
▪ int b = 72 % 10; // 2
▪ return mystery(a + b); // mystery(9)
mystery(9):
▪ return 9;
Recursive tracing 2
mystery(348)
▪ int a = mystery(34);
• int a = mystery(3);
return (10 * 3) + 3; // 33
• int b = mystery(4);
return (10 * 4) + 4; // 44
• return (100 * 33) + 44; // 3344
▪ int b = mystery(8);
return (10 * 8) + 8; // 88
• prototype:
recursive_selection_sort(list, 0, N);
Binary Search
• Iterative Definition:
•
int fval=1;
• Base case:
0! =1
Recursive definition
n! =1 if n = 0
n! = n *(n -1)! if n > 0
int fact(int n)
{
if(n==0)
return (1);
else
return (n*fact(n-1));
}
0 1 1 2 3 5 8 …
n if n is 0 or 1 (i.e. n < 2)
tn =
tn-1 + tn-2 {otherwise
Fibonacci Sequence
• Iterative solution
int fibonacci(int n)
{
if(n==0 || n==1)
return n;
else{
prev1=0;
prev2=1;
for(j=1;j<=n;j++)
{
temp = prev1+prev2;
prev1=prev2;
prev2=temp;
}
return prev2;
}
}
Fibonacci Sequence
• Recursive solution
int recfibonacci(int n)
{
if(n<2)
return n;
else
return (recfibonacci(n-2)+recfibonacci(n-1));
}
Performance Analysis
Running Time
Observations
Example-1
Observations
Example-1: 3-SUM brute force algorithm
int count(int a[], int size)
{
int count = 0;
for (int i=0; i<size; i++)
for(int j=i+1; j<size; j++)
for(int k=j+1; k<size; k++)
if(a[i]+a[j]+a[k] ==0) Check each triple
count++; For simplicity, ignore integer overflow
return count;
}
Empirical Analysis
Data Analysis
Mathematical Models for Running Time
Cost of Basic Operations
Cost of Basic Operations
Example: 1-Sum
Example: 2-Sum
Example: 2-Sum
Simplifying the Calculations
Simplification: cost model
Asymptotic Notation: 2-SUM problem
Asymptotic Notation: 3-SUM problem
Binary search
Invariant: If key appears in array a[ ], then a[lo] ≤ key ≤ a[hi]
morphemes
Mathematical Models for Running Time
Asymptotic notation: O (Big Oh)
Question
Performance Analysis
Performance Evaluation
S(P) = c + Sp(instance)
• What is the total variable memory space of this method for an input
size of 2000?
Time Complexity
2n+2
Time Complexity
2rows*cols+2rows+1
O (Big Oh) Notation
• Tsum(n)=2n+3 → Tsum(n)=O(n)
• Trsum(n)=2n+2 → Tsum(n)=O(n)
• Tadd(rows,cols)=2rows*cols+2rows+1 → Tadd(n)=O(rows*cols)
=O(n2)
Asymptotic Notation
• Example: f(n)=3n3+6n2+7n
Complexity Name 1 2 4 8 16 32
(time)
1 constant 1 1 1 1 1 1
logn logarithmic 0 1 2 3 4 5
n linear 1 2 4 8 16 32