0% found this document useful (0 votes)
39 views

Intro Algo PDF

This document discusses algorithms and their analysis. It defines an algorithm as a sequence of operations to solve a problem. Pseudocode is described as an informal way to describe algorithms without implementation details. Algorithm analysis involves determining time and space complexity based on input size. Common complexities like O(n) linear time are explained. The linear search algorithm is used as an example, with O(n) time for average and worst cases, and O(1) space complexity. Asymptotic notations like Big-O, Omega, and Theta are introduced to describe algorithm growth rates.

Uploaded by

preetarora2k18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Intro Algo PDF

This document discusses algorithms and their analysis. It defines an algorithm as a sequence of operations to solve a problem. Pseudocode is described as an informal way to describe algorithms without implementation details. Algorithm analysis involves determining time and space complexity based on input size. Common complexities like O(n) linear time are explained. The linear search algorithm is used as an example, with O(n) time for average and worst cases, and O(1) space complexity. Asymptotic notations like Big-O, Omega, and Theta are introduced to describe algorithm growth rates.

Uploaded by

preetarora2k18
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

ENCS 101: FCP

Introduction to Algorithms, Design & its


Analysis

Dr. Megha Gaur


What is an Algorithm?

• 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.

• A pseudocode is an informal and human readable description of an algorithm leaving many


granular details of it.Writing a pseudocode has no restriction of styles and its only objective is
to describe the high level steps of an algorithm in a realistic manner.
fi
fi
fi
ALGORITHM

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.

Writing step numbers is optional.

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).

• Logical: A logical algorithm is made up of logic and control component


Algorithm = Logic + Control where "Logic" represents a logic program and "Control"
represents di erent theorem-proving strategies.

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.

• Parallel: These algorithms take advantage of computer architectures where several


processors can work on a problem at the same time.

• 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

• Deterministic or non-deterministic: Deterministic algorithms solve the problems with exact


decision at every step of the algorithm whereas non-deterministic algorithms solve problems
via guessing although typical guesses are made more accurate through the use of heuristics.

• 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.

• A priori analysis: This is a theoretical analysis of an algorithm.


• A Posterior analysis: This is an empirical analysis of an algorithm. The selected algorithm is
implemented using a programming language.

• Debugging : Process of executing programs on sample data sets to determine whether


the faulty results occur if so to correct them.

• Pro ling or performance measurement is the process of executing a correct program on


datasets and measure time and space it takes to compute the results.
ffi
fi
ff
Algorithm Analysis
Complexity analysis of an algorithm & its need

• Algorithm analysis provide theoretical estimates for the resources (time &
memory) needed by an algorithm before actual implementation.

• Understanding the behaviour and performance of the algorithm in terms of


Time (Time complexity) and Space (Space complexity) requirements w.r.t
growth in input size of the data.

• Algorithm analysis helps us to predict - how feasible & e ective the algorithm
will be after its actual implementation.

• We only analyse correct algorithms.

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.

• It focusses on the underlying properties of the algorithm.


• Usually pseudocode is used for analysis as it is the simplest and most general
representation.

fi
ANALYSIS

TIME SPACE
Time Complexity & Space Complexity

• Time Complexity: It is a function describing the amount of time required to run


an algorithm in terms of the size of the input.

• Space Complexity: It is a function describing the amount of memory an


algorithm takes in terms of the size of input to the algorithm.
Linear Search Algorithm

• 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

• Best case - O(1)


• The best occurs when the target element is found at the beginning of the list/array. Since only one
comparison is made, the time complexity is O(1).

• 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.

• Average case - O(n)


• The average case complexity of the linear search is also O(n).
Linear Search Algorithm
Space Complexity

• 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

• Asymptotic notation is a way to describe the running time or space complexity of an


algorithm based on the input size. It is commonly used in complexity analysis to describe
how an algorithm performs as the size of the input grows.

• There are mainly three types of notations,


• Big-O Notation, (O)
• Omega Notation, (Ω)
• Theta Notation, (⍬)
Asymptotic Notations
Big ‘O’ Notation

• 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.

You might also like