Daa - Unit 1 Notes
Daa - Unit 1 Notes
Contents 52 Hrs
Introduction: What is an Algorithm? Fundamentals of Algorithmic problem solving, 10
Fundamentals of the Analysis of Algorithm Efficiency, Analysis Framework, Measuring the
input size, Units for measuring Running time, Orders of Growth, Worst-case, Best- case
and Average-case efficiencies
1
Design and Analysis of Algorithm V TH SEM
UNIT – 1
Introduction: What is an Algorithm? Fundamentals of Algorithmic problem solving,
Fundamentals of the Analysis of Algorithm Efficiency, Analysis Framework, Measuring the
input size, Units for measuring Running time, Orders of Growth, Worst-case, Best case and
Average-case efficiencies.
Introduction
What is an algorithm? Explain with a simple example (5)
Algorithm
An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of time.
OR
An algorithm is a set of guidelines or rules used to solve a problem or carry out a task.
This definition can be illustrated by a simple diagram.
Characteristics of an Algorithm
The following are the characteristics of an algorithm:
Input: An algorithm can have zero or more inputs.
Output: An algorithm should produce at least one or more outputs.
2
Design and Analysis of Algorithm V TH SEM
Definiteness: By definiteness, it is meant that the instructions should be clear and unambiguous without any
confusion. For example, division by zero is not a well-defined instruction.
Uniqueness: The order of the instructions of an algorithm should be well defined as a change in the order of
execution leads to wrong result or uncertainty.
Correctness: The algorithm should be correct and yield expected results.
Effectiveness: The algorithm should be traceable.
Finiteness: An algorithm should have finite number of steps and should terminate.
Euclid’s algorithm is based on applying repeatedly the equality gcd(m, n) = gcd(n, m mod n),where m mod n is
the remainder of the division of m by n, until m mod n is equal to 0. Since gcd(m,0) = m, the last value of m is
also the greatest common divisor of the initial m and n.gcd(60, 24) can be computed as follows:gcd(60, 24) =
gcd(24, 12) = gcd(12, 0) = 12.
Decision making
The Decision making is done on the following:
4
Design and Analysis of Algorithm V TH SEM
(a) Ascertaining the Capabilities of the Computational Device
In random-access machine (RAM), instructions are executed one after another (The central assumption is
that one operation at a time). Accordingly, algorithms designed to be executed on such machines are called
sequential algorithms.
In some newer computers, operations are executed concurrently, i.e., in parallel. Algorithms that take
advantage of this capability are called parallel algorithms.
Choice of computational devices like Processor and memory is mainly based on space and time efficiency.
Design an Algorithm:
Develop the step-by-step procedure to solve the problem based on the chosen design technique. Clearly
define the algorithm's input, output, and the sequence of operations to achieve the desired result
Analysing an Algorithm
The most important thing about an algorithm is its efficiency.
There are two types of efficiency:
1. Time Efficiency – How fast the algorithm runs.
5
Design and Analysis of Algorithm V TH SEM
2. Space Efficiency – How much extra memory (RAM) the algorithm uses.
The efficiency of an algorithm is determined by measuring both time efficiency and space efficiency.
So factors to analyze an algorithm are:
Time efficiency of an algorithm(fast)
Space efficiency of an algorithm(less memory)
Simplicity of an algorithm(easy to write and understand)
Generality of an algorithm(Useful for many situations)
Analysis Framework
Efficiency of an algorithm can be in terms of time or space. Thus, checking whether the algorithm is
efficient or not means analyzing the algorithm.
There is a systematic approach that has to be applied for analyzing any given algorithm. This
systematic approach is modelled by a framework called as Analysis Framework.
The efficiency of an algorithm can be decided by measuring the performance of an algorithm.
There are two kinds of efficiencies to analyze the efficiency of any algorithm. They are:
1. Time efficiency, indicating how fast the algorithm runs, and
2. Space efficiency, indicating how much extra memory it uses.
1. Time efficiency
The time efficiency of an algorithm is measured purely on how fast a given algorithm is executed. Since the
efficiency of an algorithm is measured using time, the word time complexity is often associated with an
algorithm.
The time efficiency of the algorithm depends on various factors that are shown below:
Components that affect time efficiency
Speed of the computer
Choice of the programming language
Compiler used
Choice of the algorithm
Number (Size) of inputs/Outputs
Since we do not have any control over speed of the computer, programming language and compiler, let
us concentrate only on next two factors such as
6
Design and Analysis of Algorithm V TH SEM
o Choice of an algorithm
o Number (size) of inputs
2. Space efficiency
The space efficiency of an algorithm is the amount of memory required to run the program completely and
efficiently.
If the efficiency is measured with respect to the space (memory required), the word space complexity is
often used. The space complexity of an algorithm depends on following factors:
Components that affect space
1. Program space
2. Data space
3. Stack space
Program space: The space required for storing the machine program generated by the compiler or
assembler is called program space.
Data space: The space required to store the constants, variables etc., is called data
Stack space: The space required to store the return address along with parameters that are passed to the
function, local variables etc., is called stack space.
Note: The new technological innovations have improved the computer’s speed and memory size by many
orders of magnitude. Now a days, space requirement for an algorithm is not a concern and hence, we are not
concentrating on space efficiency. Let us concentrate only on time efficiency.
7
Design and Analysis of Algorithm V TH SEM
Seconds (s):
The most straightforward unit, representing the actual time in seconds that an algorithm takes to run on a
specific machine.
Suitable for measuring real-world performance but can vary due to external factors like the Processor speed,
Background processes, System load, Hardware differences.
Milliseconds (ms):
A smaller unit than seconds, measuring time in thousandths of a second.
Useful for more precise measurement when algorithms have relatively fast execution times.
Microseconds (μs):
An even smaller unit than milliseconds, measuring time in millionths of a second.
Appropriate for very fast algorithms or when extremely precise timing is required.
Nanoseconds (ns):
A unit smaller than microseconds, measuring time in billionths of a second.
Common in the context of measuring operations at the hardware level or very low-level algorithmic
optimizations.
Instruction Count:
It count the number of machine instructions executed by an algorithm.
This is helpful for low-level analysis and making the program faster on a specific computer.
Comparisons or Swaps:
8
Design and Analysis of Algorithm V TH SEM
In sorting algorithms, counting how many times elements are compared or swapped helps us understand
how efficient the algorithm is.
This is important for algorithms that mainly work by comparing and rearranging data.
Basic operation
The operation that contributes most towards the running time of the algorithm is called basic operation.
A statement that executes maximum number of times in a function is also called basic operation. The number
of times basic operation is executed depends on size of the input.
The basic operation is the most time consuming operation in the algorithm.
For example:
o a statement present in the innermost loop in the algorithm
o addition operation while adding two matrices, since it is present in innermost loop
o multiplication operation in matrix multiplication since it is present in innermost loop
Now, let us see "How to compute the running time of an algorithm using basic operation or how time
efficiency is analyzed?" The time efficiency is analyzed by determining the number of times the basic
operation is executed. The running time T(n) is given by:
T(n) = b*C(n)
9
Design and Analysis of Algorithm V TH SEM
Order of growth describes how the running time or space used by an algorithm increases as the input size n
increases. This is usually expressed using Big-O notation.
10
Design and Analysis of Algorithm V TH SEM
Time grows with the cube of the input size.
If the input becomes 2 times bigger, the time needed becomes 8 times more (because 2 × 2 × 2 = 8).
This usually happens when there are three nested loops in the algorithm.
Very inefficient for large data.
Example: matrix multiplication, algorithm to solve simultaneous equations using gauss-elimination
method will have this running time
[
11
Design and Analysis of Algorithm V TH SEM
What are the Best case, Average case and Worst case efficiencies? Explain. (5)
Worst-case, Best-case and average case efficiencies
For some of the problems, the time complexity will not depend on the number of inputs alone. For example,
while searching for a specific item in an array of n elements using linear search, we have following three
situations:
An item we are searching for may be present in the very first location itself. In this case only one item is
compared and this is the best case.
The item may be present somewhere in the middle which definitely takes some time. Running time is more
when compared to the previous case for the same value of n. Since we do not know where the item is we
have to consider the average number of cases and hence this situation is an average case.
The item we are searching for may not be present in the array requiring n number of comparisons and
running time is more than the previous two cases. This may be considered as the worst case.
12
Design and Analysis of Algorithm V TH SEM
Worst case efficiency
The efficiency of an algorithm for the input of size n for which the algorithm takes longest time to execute
among all possible inputs is called worst case efficiency
For example, if we use linear search and the item to be searched is no present in the array, then it is an
example of worst case efficiency.
In the worst case, the algorithm runs for the longest duration among all possible inputs for a given size.
Here, maximum number of steps is executed for that input.
Worst-case efficiency:
• This is the scenario where the algorithm takes the maximum amount of time to complete.
• In our example, the worst-case scenario occurs when the target element is at the end of the array or is not
present in the array.
• The worst-case time complexity is O(n), where n is the number of elements in the # Worst-case scenario
arr = [1, 2, 3, 4, 5]
target = 5
13
Design and Analysis of Algorithm V TH SEM
result = search_element(arr, target)
# In this case, the algorithm has to go through the entire array to find the target element.
Best-case efficiency:
• This is the scenario where the algorithm takes the minimum amount of time to complete.
• In our example, the best-case scenario occurs when the target element is at the beginning of the array.
• The best-case time complexity is O(1), as the algorithm may find the target element in the first iteration.
# Best-case scenario
arr = [1, 2, 3, 4, 5]
target = 1
result = search_element(arr, target)
# In this case, the algorithm finds the target element in the first iteration.
Average-case efficiency:
• This is the scenario where the algorithm takes an average amount of time to complete, considering all
possible inputs.
• The average-case time complexity is often more challenging to analyze and may involve statistical analysis.
• In our example, if the target element is equally likely to be anywhere in the array, the average-case time
complexity is O(n/2), which simplifies to O(n).
# Average-case
scenarioarr = [1, 2, 3, 4, 5]
target = 3
result = search_element(arr, target)
# On average, the algorithm might need to check half of the array element
2 Selection Construct
14
Design and Analysis of Algorithm V TH SEM
The selection construct allows the algorithm to make a decision and choose between two or more paths
based on a condition.
The flow depends on the condition. This is done using if, if-else, or switch-case.
Example:
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
15