CHAPTER 1 Introduction To ADT and Algorithm Analysis Basics
CHAPTER 1 Introduction To ADT and Algorithm Analysis Basics
Chapter-1
Introduction to data structure and algorithm
basics
• Implementation of an ADT
• Includes choosing a particular data structure
Figure 1
A wall of ADT operations isolates a data structure from the program that uses it
06/14/2024 UoG, IoT, ECE 3
The ADT List
• ADT List operations
• Create an empty list
• Determine whether a list is empty
• Determine the number of items in a list
• Add an item at a given position in the list
• Remove the item at a given position in the list
• Remove all the items from the list
• Retrieve (get) item at a given position in the list
“flow” of a program.
Display message “How
• The figure shown here is a flowchart for much do you get paid per
hour?”
END
06/14/2024 UoG, IoT, ECE 12
Terminal
Display message
“How many hours
did you work?”
Multiply Hours by
Pay Rate. Store
START result in Gross
Pay.
END Terminal
END
Display
message “How
many hours did
you work?”
Multiply Hours by
Pay Rate. Store
Display result in Gross
Pay.
message “How
Read Hours
many hours Display Gross Pay
Display
message “How
many hours did
you work?”
assignment
Read Pay Rate
Multiply Hours
by Pay Rate.
Process Store result in
Multiply Hours Gross Pay.
by Pay Rate.
Store result in Display Gross
Pay
Gross Pay.
END
Worked
decision
• Decision
Hours>=1hrs?
false
True
• represented by Diamond Display message
• A diamond indicates a decision “How much do
you get paid per
hour?”
Multiply Hours
by Pay Rate.
Store result in
Gross Pay.
Display Gross
Pay
END
NO YES
NO YES
x < y?
Process Process
A B
NO YES if (x < y)
x < y? a = x * 2;
else
Calculate a Calculate a a = x + y;
as x plus y. as x times
2.
NO YES if (x < y)
x < y? a = x * 2;
Calculate a
as x times 2.
YES
x < y? Process A
while (x < y)
YES x++;
x < y? Add 1 to x
YES
x < y? Display x Add 1 to x
YES
x < y? Display x Add 1 to x
YES
x < y? Display x Add 1 to x
YES
x < y?
C++ Code
Display x
do
{
Flowchart cout << x << endl;
Add 1 to x
x++;
} while (x < y);
YES
x < y?
CASE
years_employed
1 2 3 Other
If years_employed = If years_employed =
2, bonus is set to 200 3, bonus is set to 400
If years_employed = If years_employed is
CASE
1, bonus is set to 100 years_employed any other value,
bonus is set to 800
1 2 3 Other
YES
x < y? Display x Add 1 to x
Display “x is NO YES
outside the limits.”
x < max?
Display “x is Display “x is
outside the limits.” within limits.”
• Pretty close to English but precise enough for a computing agent to carry out.
Algorithm arrayMax(A, n)
Input array A of n integers
Output maximum element of A
currentMax A[0]
for i 1 to n 1 do
if A[i] currentMax then
currentMax A[i]
return currentMax
1. Time
• Instructions take time.
2. Space
• Data structures take space
The more sophisticated method is to identify the key operations and count
such operations performed till the program completes its execution.
A key operation in our algorithm is an operation that takes maximum time
among all possible operations in the algorithm.
The time complexity can now be expressed as function of number of key
operations performed.
2 ) To know the simplicity of the algorithm (performance analysis in terms of time and space)
Characterizes running time as a function of the input size, n.
Takes into account all possible inputs
Allows us to evaluate the speed of an algorithm independent of the hardware/software
environment
06/14/2024 UoG, IoT, ECE 51
When we analyze algorithms, we should employ mathematical techniques
that analyze algorithms independently of specific implementations,
computers, or data.
To analyze algorithms:
• First, we start to count the number of significant /basic / primitive
operations in a particular solution to assess its efficiency.
• Then, we will express the efficiency of algorithms using growth
functions.
5.Function return
4. Loop execution time is the sum, over the number of times the loop is
executed, of the body time + time for the loop check and update
operations + time for the loop setup.
• Always assume that the loop executes the maximum number of iterations possible
5. Running time of a function call is 1 for setup + the time for any parameter
calculations + the time required for the execution of the function body.
06/14/2024 UoG, IoT, ECE 55
Counting Primitive Operations
• By inspecting the pseudocode, we can determine the maximum number of
primitive operations executed by an algorithm, as a function of the input size
Time Units to Compute
-------------------------------------------------
Examples:
1 for the assignment statement: int k=0
int count()
{ 1 for the output statement.
int k=0; 1 for the input statement.
cout<< “Enter an integer”;
In the for loop:
cin>>n;
for (i=0;i<n;i++) 1 assignment, n+1 tests and n increments.
k=k+1; n loops of 2 units for an assignment and an addition.
return 0;
1 for the return statement.
}
-------------------------------------------------------------------
T (n)= 1+1+1+(1+n+1+n)+2n+1 = 4n+6 = O(n)
06/14/2024 UoG, IoT, ECE 56
Time Units to Compute
-------------------------------------------------
int total(int n)
1 for the assignment statement: int sum=0
{ In the for loop:
int sum=0; 1 assignment, n+1 tests, and n increments.
A sequence of operations:
count = count + 1; Cost: c1
sum = sum + count; Cost: c2
Total Cost = c1 + c2
O(2 )
n
06/14/2024 T(n) = (1*216) / 28 = 28 seconds = 256 seconds
UoG, IoT, ECE 65
A Comparison of Growth-Rate Functions
growth-rate function.
• If an algorithm is O(5n3), it is also O(n3).
n
n * (n 1) n 2
i 1
i 1 2 ... n
2
2
n
n * ( n 1) * ( 2 n 1) n 3
i 1
i 2
1 4 ... n 2
6
3
n 1
2 i
i 0
1 2 8 ... 2 n 1
2 n
1
Cost Times
i = 1; c1 1
sum = 0; c2 1
while (i <= n) { c3 n+1
i = i + 1; c4 n
sum = sum + i; c5 n
}
Cost Times
for (i=1; i<=n; i++) c1 n+1
n
( j 1)
for (j=1; j<=i; j++) c2 j 1
n j
k
x=x+1; c4 j 1 k 1
n n j n j
( j 1)
j 1
(k 1)
j 1 k 1
k
j 1 k 1
T(n) = c1*(n+1) + c2*( ) + c3* ( ) + c4*( )
Three types
“Big O” Notation O()
“Big Omega” Notation ()
“Big Theta” Notation ()
06/14/2024 UoG, IoT, ECE 74
Big-O notation
• For a given function g (n), we denote by O( g (n)) the set
of functions
f (n) : there exist positive constants c and n0 s.t.
O( g (n))
0 f ( n ) cg ( n ) for all n n 0
f (n) ( g (n))
if and only if
f (n) 3n 2 2n 5 (n 2 )
Because :
3n 2 2n 5 (n 2 )
3n 2 2n 5 O(n 2 )