Intro Algo PDF
Intro Algo PDF
• The term “Algorithm” was rst used in 1230 and then by Chaucer in 1391.
• Algorithms are essential to the way computers process the data.
• Any sequence of operations that can be simulated by the system.
• Order of computation is crucial to the functioning of the algorithm.
• Used to describe any high-level task in computer science.
fi
Algorithm vs Pseudocode
• An algorithm is a formal de nition with some speci c characteristics that describe a process,
executed by a machine to perform a speci c task.
ANALYSIS DESIGN
Algorithm Design
• Goal: To design an e cient algorithm with minimum time and space constraints.
• Some approaches may be e cient with respect to time, whereas others may be more
memory e cient.
• Both time consumption and memory usage can not be optimised simultaneously.
ffi
ffi
ffi
Characteristics of Algorithm
• Input/ Outputs: Algorithms must have explicitly designed set of inputs/ outputs.
• De niteness: Each instruction is clear & unambiguous.
• Finiteness: Algorithms must terminate in a nite number of steps. It should not run for
inde nite time.
• E ciency: Every instruction must be very basic and runs in short time with e ective results
better than human computations.
ffi
fi
fi
fi
ff
Problem: Design an algorithm to add two numbers and display the result
• Step 1: START
• Step 2: get values of a & b
• Step 3: c <— a + b
• Step 4: print c
• Step 5: STOP
It makes it easy for the analyst to analyse the algorithm ignoring all the unwanted de nitions.
fi
Classification of Algorithms
• By implementation
• Recursion: A recursive algorithm is one that invokes (makes reference to) itself repeatedly
until a certain condition is met (termination condition).
In pure logic programming languages, the control component is xed, and the algorithms
are speci ed by supplying only the logic component.
fi
ff
fi
Classification of Algorithms
• Serial: Algorithms designed for serial computers that execute one instruction at a time are
called serial algorithms.
• Distributed: These algorithms utilise several machines connected with a computer network.
Parallel/ Distributed algorithms divide the problem into several symmetrical or asymmetrical
subproblems and collect the results back together.
Classification of Algorithms
• Exact or approximate: While many algorithms reach a exact solution, approximate algorithms
seek a solution closer to the true solution. Approximation can be reached by either using a
deterministic or a random strategy. Such algorithms have practical value for many hard
problems.
Algorithm Analysis
• E ciency of an algorithm can be analysed at two di erent stages, before implementation and
after implementation.
• Algorithm analysis provide theoretical estimates for the resources (time &
memory) needed by an algorithm before actual implementation.
• Algorithm analysis helps us to predict - how feasible & e ective the algorithm
will be after its actual implementation.
ff
Algorithm Analysis
Complexity analysis of an algorithm & its need
• Algorithm & study of algo is abstracted without the use of any speci c
programming language.
fi
ANALYSIS
TIME SPACE
Time Complexity & Space Complexity
• It is one of the most simple and straightforward search algorithms. In this, you need to
traverse the entire list and keep comparing the current element with the target element. If a
match is found, you can stop the search else continue.
LINEAR SEARCH
Linear Search Algorithm
Time Complexity
• Array A[] = {3,4,0,9,8} ,Target element = 3 Here, the target is found at A[0].
• Worst-case - O(n), where n is the size of the list/array.
• The worst-case occurs when the target element is found at the end of the list or is not present in the list/
array. Since you need to traverse the entire list, the time complexity is O(n), as n comparisons are
needed.
• Space Complexity
• The space complexity of the linear search is O(1), as we don't need any auxiliary space for
the algorithm.
Asymptotic Notations
• This notation provides an upper bound on the growth rate of an algorithm’s running time or
space usage.
• It represents the worst-case scenario, i.e., the maximum amount of time or space an
algorithm may need to solve a problem.
• For example, if an algorithm’s running time is O(n), then it means that the running time of the
algorithm increases linearly with the input size n or less.
Asymptotic Notations
Omega (Ω) Notation
• Omega notation (Ω): This notation provides a lower bound on the growth rate of an
algorithm’s running time or space usage.
• It represents the best-case scenario, i.e., the minimum amount of time or space an algorithm
may need to solve a problem.
• For example, if an algorithm’s running time is Ω(n), then it means that the running time of the
algorithm increases linearly with the input size n or more.
Asymptotic Notations
Theta notation (⍬)
• This notation provides both an upper and lower bound on the growth rate of an algorithm’s
running time or space usage.
• It represents the average-case scenario, i.e., the amount of time or space an algorithm
typically needs to solve a problem.
• For example, if an algorithm’s running time is Θ(n), then it means that the running time of the
algorithm increases linearly with the input size n.