Gull
Gull
A program
A set of instruction which is written in order
to solve a problem.
4
Introduction to Data Structures
Data structures are used to model the static part of
the world. How?
1. The value held by a data structure represents some specific
characteristic of the world.
2. The characteristic being modeled restricts the possible values
held by a data structure and the operations to be performed
on the data structure
10
Data Structure
11
Data Structure
Example:
struct Student_Record
{
char name[20];
char ID_NO[10];
char Department[10];
int age;
};
Type: Set of values that can be stored + set of operations that can be performed.
Life time: The time interval during execution of a program while the variable exists. 12
Algorithm
14
Algorithm
An algorithm transforms data structures from one
state to another state.
What is the purpose of algorithms in programs?
Take values as input. Example: cin>>age;
Change the values held by data structures. Example: age=age+1;
Change the organization of the data structure:
Example:
Sort students by name
Produce outputs:
15
Algorithm
The quality of a data structure is related to its
ability to successfully model the characteristics
of the problem.
16
Algorithm
17
Properties of Algorithms
Finiteness:
19
Sequential:
20
Feasibility:
It must be possible to perform each
instruction.
Each instruction should have possibility to
be executed.
1) for(int i=0; i<0; i++){
cout<< i; // there is no possibility
} that this statement to
be executed.
2) if(5>7) {
cout<<“hello”; // not executed.
} 21
Correctness
It must compute correct answer for all
possible legal inputs.
The output should be as expected and required and
correct.
Language Independence:
It must not depend on any one programming
language.
Completeness:
It must solve the problem completely.
22
Effectiveness:
Doing the right thing. It should yield the correct
result all the time for all of the possible cases.
Efficiency:
cin>>x;
cout<<x*x;
2) int x,y;
cin>>x;
y=x*x;
cout<<y;
24
Example:
25
Input/output:
There must be a specified number of input
values, and one or more result values.
Zero or more inputs and one or more outputs.
Simplicity:
A good general rule is that each step should carry out one
logical step.
What is simple to one processor may not be simple to another.
26
Next slid
Complexity Analysis
Formal Approach to Analysis
Asymptotic Analysis
The Big-Oh Notation
Big-Omega Notation
Theta Notation
What is an algorithm Analysis?
• Memory Usage
• Communication Bandwidth
31
There are two approaches to measure the
efficiency of algorithms:
1. Emperical
based on the total running time of the
program.
Uses actual system clock time.
Example:
t1
for(int i=0; i<=10; i++)
cout<<i;
t2
Running time taken by the above algorithm
(TotalTime) = t2-t1;
32
It is difficult to determine efficiency of
algorithms using this approach,
Internal structure
35
2. Theoretical
Determining the quantity of resources
required using mathematical concept.
Time units
Operations performed, or
The amount of storage space required.
38
Two important ways to characterize the effectiveness
of an algorithm are its Space Complexity and Time
Complexity.
Time Complexity: Determine the approximate amount of
time (number of operations) required to solve a problem of
size n.
The limiting behavior of time complexity as size
increases is called the Asymptotic Time
Complexity.
Space Complexity: Determine the approximate memory
required to solve a problem of size n.
The limiting behavior of space complexity as size
increases is called the Asymptotic Space
Complexity.
39
Asymptotic Complexity of an algorithm determines the
size of problems that can be solved by the algorithm.
Factors affecting the running time of a program:
CPU type (80286, 80386, 80486, Pentium I---IV)
Memory used
Computer used
Programming Language
C (fastest), C++ (faster), Java (fast)
C is relatively faster than Java, because C is relatively nearer to Machine language, so,
Java takes relatively larger amount of time for interpreting/translation to machine code.
Algorithm used
Input size
Note: Important factors for this course are Input size and Algorithm used.
40
Complexity analysis involves two distinct phases:
• Algorithm Analysis: Analysis of the algorithm or
data structure to produce a function T(n) that
describes the algorithm in terms of the operations
performed in order to measure the complexity of
the algorithm.
Example: Suppose we have hardware capable of
executing 106 instructions per second. How long would it
take to execute an algorithm whose complexity function
is T(n)=2n2 on an input size of n=108?
Solution: T(n)= 2n2=2(108)2 = 2*1016
Running time=T(108)/106=2*1016/106=2*1010 seconds.
• Order of Magnitude Analysis: Analysis of the
function T (n) to determine the general complexity
category to which it belongs.
41
There is no generally accepted set of rules
for algorithm analysis.
However, an exact count of operations is
commonly used.
To count the number of operations we can
use the following Analysis Rule.
Analysis Rules:
1. Assume an arbitrary time unit.
2. Execution of one of the following operations
takes time 1 unit:
Assignment Operation
Example: i=0;
Single Input/Output Operation
Example: cin>>a;
cout<<“hello”; 42
Single Boolean Operations
Example: i>=10
Example: a+b;
Function Return
45
5. Function call:
• 1 for setup + the time for any parameter
calculations + the time required for the
execution of the function body.
Examples:
1)
int k=0,n;
cout<<“Enter an integer”;
cin>>n;
for(int i=0;i<n; i++)
k++;
T(n)= 3+1+n+1+n+n=3n+5
46
2)
int i=0;
while(i<n)
{
cout<<i;
i++;
}
int j=1;
while(j<=10)
{
cout<<j;
j++;
}
T(n)=1+n+1+n+n+1+11+2(10)
= 3n+34 47
3)
int k=0;
for(int i=1 ; i<=n; i++)
for( int j=1; j<=n; j++)
k++;
T(n)=1+1+(n+1)+n+n(1+(n+1)+n+n)
= 2n+3+n(3n+2)
= 2n+3+3n2+2n
= 3n2+4n+3
48
4). int sum=0;
for(i=1;i<=n;i++))
sum=sum+i;
T(n)=1+1+(n+1)+n+(1+1)n
=3+4n=O(n)
T(n)=1+1+1+1+1+n+1+2n+n+2(n-1)
= 6+4n+2n-2
=4+6n=O(n) 50
7). int sum(int n){
int s=0;
for(int i=1;i<=n;i++)
s=s+(i*i*i*i);
return s;
}
T(n)=1+(1+n+1+n+5n)+1
=7n+4=O(n)
53
The index and bounds of the summation are
the same as the index and bounds of the
for loop.
Suppose we count the number of additions
that are done. There is 1 addition per
iteration of the loop, hence n additions in
total.
N
å
for (int i = 1; i <= N; i++) {
sum = sum+i; 1 = N
}
i =1
54
Nested Loops: Formally
Nestedfor loops translate into multiple
summations, one for each For loop.
}
sum = sum+i+j; åå2 = å2M = 2MN
i=1 j =1 i=1
}
55
Consecutive Statements: Formally
Add the running times of the separate blocks
of your code.
if (test == 1) {
for (int i = 1; i <= N; i++) { æN N N ö
sum = sum+i; maxççå1, åå2÷÷ =
}} è i=1 i=1 j=1 ø
else for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) { ( 2
)
maxN, 2N = 2N 2
sum = sum+i+j;
}}
57
Recursive: Formally
-Usually difficult to analyze.
Example: Factorial
long factorial(int n){
if(n<=1)
return 1;
else
return n*factorial(n-1);
}
T(n)=1+T(n-1)=2+T(n-2)=3+T(n-3)=-------
=n-1 (counting the number of multiplication)
58
Categories of Algorithm Analysis
Algorithms may be examined under different
situations to correctly determine their efficiency
for accurate comparison.
Best Case Analysis:
Assumes the input data are arranged in the most
advantageous order for the algorithm.
Examples:
For sorting algorithm
If the list is already sorted (data are
arranged in the required order).
For searching algorithm
If the desired item is located at first accessed
position.
60
Worst Case Analysis:
Assumes the input data are arranged in the
most disadvantageous order for the algorithm.
Takes the worst possible set of inputs.
Causes execution of the largest number of
statements.
Computes the upper bound of T(n) where T(n) is
the complexity function.
Examples:
For sorting algorithms
If the list is in opposite order.
For searching algorithms
If the desired item is located at the last
position or is missing. 61
Worst Case Analysis:
Worst case analysis is the most common
analysis because:
It provides the upper bound for all input (even for
bad ones).
Average case analysis is often difficult to
determine and define.
If situations are in their best case, no need to
develop algorithms because data arrangements
are in the best situation.
Best case analysis can not be used to estimate
complexity.
We are interested in the worst case time since it
provides a bound for all input-this is called the
“Big-Oh” estimate.
62
Average Case Analysis:
Determine the average of the running time overall
permutation of input data.
Takes an average set of inputs.
It also assumes random input size.
It causes average number of executions.
Computes the optimal bound of T(n) where T(n) is the
complexity function.
Sometimes average cases are as bad as worst cases and
as good as best cases.
Examples:
For sorting algorithms
While sorting, considering any arrangement (order of input data).
64
Order of Magnitude
Refers to the rate at which the storage or time
grows as a function of problem size.
65
Asymptotic Notations
Asymptotic Analysis is concerned with how the
running time of an algorithm increases with the
size of the input in the limit, as the size of the
input increases without bound!
Asymptotic Analysis makes use of
O (Big-Oh) ,
(Big-Omega),
(Theta),
o (little-o),
(little-omega)
- notations in performance analysis and 66
f(n)=10n+5 and
g(n)=n.
Show that f(n) is O(g(n)) ?
f(n)=3n2+4n+1
Show that f(n)=O(n2) ?
Solution:
4n<=4n2 for all n>=1 and
1<=n2 for all n>=1
3n2+4n+1<=3n2+4n2+n2 for all n>=1<=8n2 for all
n>=1
So, we have shown that f(n)<=8n2 for all n>=1.
Therefore, f(n) is O(n2), (c=8, k=1), there exist two
constants that satisfy the constraints.
70
Big-O Theorems
For all the following theorems, assume that
f(n) is a function n and that k is an arbitrary
constant.
Theorem 1: k is O(1)
Theorem 2: A polynomial is O(the term
containing the highest power of n). if( f(n)
is a polynomial of degree d, the f(n) is O(nd)
Theorem 3: k*f(n) is O(f(n))
Theorem 4: Transitivity - if f(n) is O(g(n))
and g(n) is O(h(n)), then f(n) is O(h(n))
etc
71
2. Big-Omega ( )-Notation (Lower bound)
Definition: We write f(n)= (g(n)) if there are positive
constants no and c such that to the right of no the
value of f(n) always lies on or above c.g(n).
As n increases f(n) grows no slower than g(n).
Describes the best case analysis.
Used to represent the amount of time the algorithm
takes on the smallest possible set of inputs-“Best case”.
Example:
Find g(n) such that f(n) = (g(n)) for f(n)=n2
g(n) = n, c=1, k=1.
f(n)=n2= (n)
72
Big-Omega ( )-Notation (Lower bound)
73
3. Theta Notation ( -Notation) (Optimal bound)
Definition: We say f(n)= (g(n)) if there exist positive
constants no, c1 and c2 such that to the right of no, the
value of f(n) always lies between c1.g(n) and c2.g(n)
inclusive, i.e., c2.g(n)<=f(n)<=c1.g(n), for all n>=no.
As n increases f(n) grows as fast as g(n).
Describes the average case analysis.
To represent the amount of time the algorithm takes on an
average set of inputs- “Average case”.
Example: Find g(n) such that f(n) = Θ(g(n)) for
f(n)=2n2+3
n2 ≤ 2n2 ≤ 3n2 c1=1, c2=3 and no=1
f(n) = Θ(g(n)).
74
Theta Notation ( -Notation) (Optimal bound)
75
4. Little-oh (small-oh) Notation
Definition: We say f(n)=o(g(n)), if there are positive constants
no and c such that to the right of no, the value of f(n) lies
below c.g(n).
As n increases, g(n) grows strictly faster than f(n).
Describes the worst case analysis.
Denotes an upper bound that is not asymptotically tight.
Big O-Notation denotes an upper bound that may or may not be
asymptotically tight.
Example:
Find g(n) such that f(n) = o(g(n)) for f(n) = n2
76
5. Little-Omega ( ) notation
Definition: We write f(n)= (g(n)), if there are positive
constants no and c such that to the right of no, the value of
f(n) always lies above c.g(n).
As n increases f(n) grows strictly faster than g(n).
Describes the best case analysis.
Denotes a lower bound that is not asymptotically tight.
Big -Notation denotes a lower bound that may or may not
be asymptotically tight.
Example: Find g(n) such that f(n)= (g(n)) for f(n)=n2+3
78
T(n) Complexity Big-O
Category
functions F(n)
c, c is constant 1 C=O(1)
7n!+2n+n2+1 n! T(n)=O(n!)
T(n)=2*n=2n=O(n).
T(n)=1*n*n=n2 = O(n2).
82
Next Class
Chaptet Two
Simple Sorting and Searching
Algorithms
83