DAA Module 1
DAA Module 1
Algorithm
Dr. Preetam Suman
Module 1
• Algorithm
• Design
• Analysis
• Program
Why DAA is important
• One of the most important course for Placement and Competitive exams.
• Industries using Analysis of algorithms for reliable products.
• e.g. Google (Searching algorithm), Google Map (Shortest path
algorithm), YouTube (searching algorithms), Facebook, etc.
What is Algorithm
• Algorithm is finite set of steps to solve the problem.
• Instructions should be finite and time taken to execute the instruction
should also finite. (Infinite loop is not an algorithm)
• well-defined computational procedure that takes some value, or set of
values, as input and produces some value, or set of values, as output.
• A sequence of computational steps that transform the input into the output.
• Any special method of solving a certain kind of problem (e.g. Dictionary)
• Instruction should be non-ambiguous
Design of algorithm?
2. Pseudo code;
3. flowchart.
To perform addition of two numbers
Step1: read the first number (assume a) //this algo perform addition of two
Step2: read the second number (assume integers
b)
• //input two integers a and b
Step3: add 2 numbers(a and b)and store
• // output of two integers
the result in a variable c
Step4: display the result • C=a+b
• Write c
Flow chart Symbols
Flowchart Symbol Name Description
Also known as the “Terminator Symbol,” this symbol represents the start
Start/End symbol points, end points, and potential outcomes of a path. Often contains “Start” or
“End” within the shape.
Usually used within more complex charts, this symbol connects separate
Connector symbol
elements across one page.
Off-Page Frequently used within complex charts, this symbol connects separate
Connector/Link elements across multiple pages with the page number usually placed on or
symbol within the shape for easy reference.
Also referred to as the “Data Symbol,” this shape represents data that is
Input/Output available for input or output as well as representing resources used or
symbol generated. While the paper tape symbol also represents input/output, it is
outdated and no longer in common use for flowchart diagramming.
Placed along with context, this symbol adds needed explanation or comments
Comment/Note
within the specified range. It may be connected by a dashed line to the
symbol
relevant section of the flowchart as well.
Flow chart Symbols
Flow Arrow
Stored Data
Predefined process
Software development model
ALGORITHMICS
It is the science that lets designers study and evaluate the effect of
algorithms based on various factors so that the best algorithm is selected to
meet a particular task in given circumstances.
It is also the science that tells how to design a new algorithm for a particular
job.
11
Analysis of an Algorithm
• Analysis is the process of comparing two algorithm with respect to time and space.
• If the program is run on a large data set, then the running time becomes an issue
There two types of analysis:
1. Priori
Priori Analysis is the theoretical estimation of resources required.
2. Posterior • Algorithm
Posterior Analysis done after implement the algorithm on a target • Read A
machine. • Read B
• Sum A, B
• Time Complexity • Print the Result
• Posterior analysis will provide a fixed and relevant time to
execute the algorithm
• For ex it may take .5 sec • Code
• It will vary for different processor Main()
• It is dependent on hardware {
int a, b, sum;
Scanf (a);
• Priory analysis can be done before execution Scanf(b)
• If each instruction will execute once then it will take 4 sec. Sum=a+b;
• It will give approximate value Printf(sum);
• It is independent to any hardware }
• It can be calculated in best case and worst case
Types of Algorithms
• Probabilistic algorithm
In this algorithm, chosen values are used in such a way that the probability of chosen
each value is known and controlled. E.G. Randomize quick sort
• Heuristic algorithm
This type of algorithm is based largely on optimism and often with minimal
theoretical support. Here error can not be controlled but may be estimated how large it
is. Knapsak Problem, Traveling salesman
• Approximate algorithm
In this algorithm, answer is obtained that is as précised as required in decimal
notation. In other words it specifies the error we are willing to accept. For example, 8
queen problem.
What do we analyze?
• Correctness
• Does the input/output relation match algorithm requirement?
• Amount of work done
• Basic operations to do task finite amount of time
• Amount of space used
• Memory used
• Finiteness.[algo should end in finite amount of steps]
• Definiteness.[each instruction should be clear]
• Input.[valid input clearly specified ]
• Output.[single/multiple valid output]
• Effectiveness.[ steps are sufficiently simple and basic]
Areas for the study of algorithms:
Time complexity:
• Time complexity means the amount of time required by an algorithm to run.
Space complexity:
• Space complexity means the amount of space required by an algorithm to run.
• For example: In bubble sort, when the input array is already sorted, the time taken by the algorithm
is linear i.e. the best case.
• But, when the input array is in reverse condition, the algorithm takes the maximum time to sort the
elements i.e. the worst case.
• When the input array is neither sorted nor in reverse order, then it takes average time. These
durations are denoted using asymptotic notations.
• There exist some problems whose solutions are not yet found, the problems are divided into classes known
as Complexity Classes.
• In complexity theory, a Complexity Class is a set of problems with related complexity. These classes help
scientists to groups problems based on how much time and space they require to solve problems and verify
the solutions.
• P Class
• NP Class
• NP hard
• NP complete
P Class
The P in the P class stands for Polynomial Time. It is the collection of decision problems (problems with a
“yes” or “no” answer) that can be solved by a deterministic machine in polynomial time.
Features:
• The solution to P problems is easy to find.
• P is often a class of computational problems that are solvable and tractable.
• Tractable means that the problems can be solved in theory as well as in practice. But the problems that
can be solved in theory but not in practice are known as intractable.
NP-Hard Problem:
A Problem X is NP-Hard if there is an NP-Complete problem Y, such that Y is reducible to X in
polynomial time. NP-Hard problems are as hard as NP-Complete problems. NP-Hard Problem need not be
in NP class.
NP-Complete Problem:
A problem X is NP-Complete if there is an NP problem Y, such that Y is reducible to X in polynomial
time. NP-Complete problems are as hard as NP problems. A problem is NP-Complete if it is a part of both
NP and NP-Hard Problem. A non-deterministic Turing machine can solve NP-Complete problem in
polynomial time.
Difference between NP-Hard and NP-Complete:
NP-hard NP-Complete
NP-Hard problems(say X) can be solved if
NP-Complete problems can be solved by a
and only if there is a NP-Complete
non-deterministic Algorithm/Turing Machine
problem(say Y) that can be reducible into X
in polynomial time.
in polynomial time.
To solve this problem, it do not have to be in To solve this problem, it must be both NP and
NP . NP-hard problems.