1 DAA - Handout-01-Introduction
1 DAA - Handout-01-Introduction
Handout-01
Introduction
Introduction
Input output
Algorithm
• It must be correct.
• It must be composed of a series of
concrete steps.
– The execution sequence of instructions
should not be ambiguous.
• It must have finite number of instructions
and steps.
– It must terminate.
4
Syntax & Semantics
An algo. is “correct” if its: WARNINGS:
– Semantics are correct
1. An algo. can be
– Syntax is correct syntactically correct,
yet semantically
Semantics: incorrect – very
The concept embedded in dangerous situation!
an algorithm (the soul!)
2. Syntactic
correctness is easier
Syntax: to check as
The actual representation compared with
of an algorithm (the body!) semantic
Solving Problems (1)
When faced with a problem:
1. We first clearly define the problem
12
• Algorithm
• It is a method followed to solve a problem.
• A mapping of input to output. As mentioned earlier
on slide no. 4.
• A problem can have many algorithms
• Computer program
• It is a concrete representation of an algorithm in
some programming language
• A set of instructions which the computer will follow t
o solve a problem
13
Why Algorithms are Useful?
• Once we find an algorithm for solving a
problem, we do not need to re-discover it the
next time we are faced with that problem
16
Popular Algorithms, Factors of Dependence
• Most basic and popular algorithms are
– Sorting algorithms
– Searching algorithms
Which algorithm is best?
• Mainly, it depends upon various factors, for
example in case of sorting
– The number of items to be sorted
– The extent to which the items are already sorted
– The kind of storage device to be used etc.
One Problem, Many Algorithms
Problem
• The statement of the problem specifies, in general
terms, the desired input/output relationship.
Algorithm
• The algorithm describes a specific computational
procedure for achieving input/output relationship.
Example
• One might need to sort a sequence of numbers
into non-decreasing order.
Algorithms
• Various algorithms e.g. merge sort, quick sort,
heap sorts etc.
Important Designing Techniques
• Brute Force
– Straightforward, naive approach
– Mostly expensive
• Divide-and-Conquer
– Divide into smaller sub-problems
• Example: Merge sort, Quicksort etc
• Decrease-and-Conquer
– Decrease instance size
• Binary search, Interpolation search etc
• Transform-and-Conquer
– Modify problem first and then solve it
• Heap sort
Important Designing Techniques
• Greedy Approach
– Locally optimal decisions, can not change once made.
– Efficient
– Easy to implement
– The solution is expected to be optimal
– Every problem may not have greedy solution
• Dynamic programming
– Decompose into sub-problems like divide and conquer
– Sub-problems are dependant
– Record results of smaller sub-problems
– Re-use it for further occurrence
– Mostly reduces complexity exponential to polynomial
Problem Solving Process
• Problem
• Strategy
• Algorithm
– Input
– Output
– Steps
• Analysis
– Correctness
– Time & Space
– Optimality
• Implementation
• Verification
Questions