0% found this document useful (0 votes)
17 views48 pages

DAA Module 1

VIT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views48 pages

DAA Module 1

VIT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Design Analysis and

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?

1. Using Natural language

2. Pseudo code;

3. flowchart.
To perform addition of two numbers

Natural language is general way to solve pseudo-code is Combination of Flow chart


any problem natural language and programming
language

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 an “Action Symbol,” this shape represents a process, action, or


Process symbol
function. It’s the most widely-used symbol in flowcharting.

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.

Represents the input or output of a document, specifically. Examples of and


Document symbol input are receiving a report, email, or order. Examples of an output using a
document symbol include generating a presentation, memo, or letter.

Indicates a question to be answered — usually yes/no or true/false. The


Decision symbol flowchart path may then split off into different branches depending on the
answer or consequences thereafter.
Flow chart Symbols
Flowchart Symbol Name Description

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.

• Algorithm analysis is an important part of a computational complexity theory.

Why need algorithm analysis ?


• Writing a working program is not good enough

• The program may be inefficient!

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

1. How to devise/design an algorithm

2. How to validate an algorithm

3. How to analyze an algorithm

4. How to test a program


▪ Debugging
▪ Profiling
1. How to devise an algorithm
To design new algorithm, we apply a existing design strategy.
Some of the existing strategies are
Divide and Conquer
Greedy Method
Dynamic Programming
Back Tracking
Branch and Bound etc.

2. How to validate an algorithm


To check whether the algorithm giving correct output or not for all
possible inputs.
3. How to analyze an algorithm
Based on performance measures such as space and time
i) How much time required
ii) How much space required

4. How to test a program


▪ Debugging
▪ Profiling

Debugging is a process of executing program on sample data sets, to


determine whether any fault results occur. If so correct them.

Profiling is a process of executing a correct program on sample data sets and


determine how much time/space required by program
Performance Analysis
• Performance analysis is the criteria for judging the algorithm.
• It have direct relationship to performance.
• When we solve a problem ,there may be more then one algorithm to solve a
problem, through analysis we find the run time of an algorithms and we choose the
best algorithm which takes lesser run time.
Complexity of an algorithm
Complexity (or efficiency) of an algorithm is a function that describes the efficiency of an algorithm in terms of
time and space.

There are two types of complexity:

Time complexity:
• Time complexity means the amount of time required by an algorithm to run.

• It expressed as an order of magnitude / degree

Space complexity:
• Space complexity means the amount of space required by an algorithm to run.

• It expressed as an order of magnitude.

• There are two important factors to compute the space complexity:

• constant and instant characteristic.


Example of Classical Multiplication Algorithms (981 X 1234)

Which algorithm is better?


Which algorithm is better?

The algorithms are correct, but which is the best?


• Measure the running time (number of operations needed).
• Measure the amount of memory used.
• Note that the running time of the algorithms increase as the size of the input increases.
Recursive relation
Substitution Method
Example 1
Substitution Method
Example 2
Substitution Method
Example 3
Master Method
Master Method
Examples
Comparison of time complexities
Asymptotic Notations
• Asymptotic notations are the mathematical notations used to describe the running time of an
algorithm when the input tends towards a particular value or a limiting value.

• 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 are mainly three asymptotic notations:

• Big-O notation “Ο” for worst case


• Omega notation “ Ώ” for best case
• Theta notation “ θ” for average case
Chart of time complexities for sorting algorithms
Types of Complexity Classes | P, NP, NP hard and NP complete

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

• There are following complexity classes:

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

This class contains many natural problems like:


• Calculating the greatest common divisor.
• Finding a maximum matching.
• Decision versions of linear programming.
NP Class
• The NP in NP class stands for Non-deterministic Polynomial Time.
• It is the collection of decision problems that can be solved by a non-deterministic machine in polynomial
time.
Features:
• The solutions of the NP class are hard to find since they are being solved by a non-deterministic machine
but the solutions are easy to verify.
• Problems of NP can be verified by a Turing machine in polynomial time.
Example:
• Suppose there is a company having a total of 1000 employees having unique employee IDs. Assume that there are 200
rooms available for them. A selection of 200 employees must be paired together, but the CEO of the company has the
data of some employees who can’t work in the same room due to some personal reasons.
• Boolean Satisfiability Problem (SAT).
• Hamiltonian Path Problem.
• Graph coloring.
NP-hard class
An NP-hard problem is at least as hard as the hardest problem in NP and it is the class of the problems
such that every problem in NP reduces to NP-hard.
Features:
• All NP-hard problems are not in NP.
• It takes a long time to check them. This means if a solution for an NP-hard problem is given then it
takes a long time to check whether it is right or not.
• A problem A is in NP-hard if, for every problem L in NP, there exists a polynomial-time reduction
from L to A.
Examples of problems in Np-hard are:
• Halting problem.
• Qualified Boolean formulas.
• No Hamiltonian cycle.
NP-complete class
A problem is NP-complete if it is both NP and NP-hard. NP-complete problems are the hard problems
in NP.
Features:
• NP-complete problems are special as any problem in NP class can be transformed or reduced into
NP-complete problems in polynomial time.
• If one could solve an NP-complete problem in polynomial time, then one could also solve any NP
problem in polynomial time.
Example problems include:
• Decision version of 0/1 Knapsack.
• Hamiltonian Cycle.
• Satisfiability.
• Vertex cover.
Complexity Class Characteristic feature
P Easily solvable in polynomial time.
NP Yes, answers can be checked in polynomial time.
All NP-hard problems are not in NP and it takes a long time to
NP-hard
check them.
NP-complete A problem that is NP and NP-hard is NP-complete.
NP Problem:
The NP problems set of problems whose solutions are hard to find but easy to verify and are solved by
Non-Deterministic Machine in polynomial time.

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.

Do not have to be a Decision problem. It is exclusively a Decision problem.

Example: Determine whether a graph has a


Example: Halting problem, Vertex cover Hamiltonian cycle, Determine whether a
problem, etc. Boolean formula is satisfiable or not, Circuit-
satisfiability problem, etc.
Definition of NP-Completeness
A language B is NP-complete if it satisfies two conditions
• B is in NP
• Every A in NP is polynomial time reducible to B.
If a language satisfies the second property, but not necessarily the first one, the
language B is known as NP-Hard. Informally, a search problem B is NP-Hard if there
exists some NP-Complete problem A that Turing reduces to B.
The problem in NP-Hard cannot be solved in polynomial time, until P = NP. If a
problem is proved to be NPC, there is no need to waste time on trying to find an
efficient algorithm for it. Instead, we can focus on design approximation algorithm.

You might also like