Ch1. Analysis of Algorithms
Ch1. Analysis of Algorithms
CS102
Subrina Thompson
Algorithm
• An algorithm is a set of instructions to be followed to solve a problem
• There can be more than one solution (more than one algorithm) to solve a given problem
• An algorithm can be implemented using different programming languages on different
platforms
• An algorithm must be correct. It should correctly solve the problem.
• e.g. For sorting, this means even if (1) the input is already sorted, or (2) it contains repeated
elements.
• Once we have a correct algorithm for a problem, we have to determine the
efficiency of that algorithm.
Algorithmic Performance
• There are two aspects of algorithmic performance:
• Time
• Instructions take time.
• How fast does the algorithm perform?
• What affects its runtime?
• Space
• Data structures take space
• What kind of data structures can be used?
• How does choice of data structure affect the runtime?
• We will focus on time:
• How to estimate the time required for an algorithm
• How to reduce the time required
Analysis of Algorithms
• Analysis of Algorithms is the area of Computer Science that provides tools to analyze the efficiency
of different methods of solutions.
• How do we compare the time efficiency of two algorithms that solve the same problem?
• Naïve Approach: implement these algorithms in a programming language (Java), and run them to
compare their time requirements. Comparing the programs (instead of algorithms) has difficulties.
• How are the algorithms coded?
• Comparing running times means comparing the implementations.
• We should not compare implementations, because they are sensitive to programming style that may cloud the issue of which algorithm is
inherently more efficient.
• What computer should we use?
• We should compare the efficiency of the algorithms independently of a particular computer.
• What data should the program use?
• Any analysis must be independent of specific data.
Analysis of Algorithms
• 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 operations in a particular
solution to assess its efficiency.
• Then, we will express the efficiency of algorithms using growth functions.
The Execution Time of Algorithms
• Each operation in an algorithm (or a program) has a cost.
Each operation takes a certain time
count = count + 1; take a certain amount of time, but it is constant
A sequence of operations:
count = count + 1; Cost: c1
sum = sum + count; Cost: c2
Total Cost: c1 + c2
The Execution Time of Algorithms
Example: Simple If-Statement
Cost Times
if( n < 0 ) c1 1
absval = -n; c2 1
else
absval = n; c3 1
Total Cost <= c1 + max( c2, c3 )
The Execution Time of Algorithms
Example: Simple Loop
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
}
Total Cost <= c1 + c2 + ((n + 1) * c3) + (n * c4) + (n * c5)
The time required for this algorithm is proportional to n
The Execution Time of Algorithms
Example: Nested Loop
Cost Times
i = 1 c1 1
sum = 0; c2 1
while( i <= n ) { c3 n + 1
j = 1; c4 n
while( j <= n ) { c5 n * (n + 1)
sum = sum + i; c6 n * n
j = j + 1; c7 n * n
i = i + 1; c8 n
Total Cost <= c1 + c2 + ((n + 1) * c3) + (n * c4) + ((n * ( n + 1)) * c5) + (n * n) * c6 + (n * n) * c7 + ( n * c8)