• What is an Algorithm?
• One Problem, Many Algorithms Example:
• What properties an algorithm should have ?
• Generality Finiteness Non-ambiguity Efficiency
• Important thing before analysis
• How to analyze an algorithm?
• Problem Solving Process
• Important Designing Techniques
What is an Algorithm?
• An algorithm is a set of rules for carrying out calculation either by
hand or on a machine.
• An algorithm is a finite step-by-step procedure to achieve a required
result.
• An algorithm is a sequence of computational steps that transform
the input into the output.
• An algorithm is a sequence of operations performed on data that
have to be organized in data structures.
• An algorithm is an abstraction of a program to be executed on a
physical machine (model of Computation).
One Problem, Many Algorithms
Problem: Defines the desired input/output relationship in general
terms.
Algorithm: Specifies a computational procedure to achieve this
input/output relationship.
It means a single problem can have multiple algorithmic
approaches, each with different efficiencies, complexities, and
optimizations.
Example: Sorting a sequence of numbers in non-decreasing order.
Algorithms: Various sorting algorithms can be used, such as:
Merge Sort, Quick Sort, Heap Sort
What properties an algorithm
should have ?
• Generality
• Finiteness
• Non-ambiguity
• Efficiency
Example:
Let’s consider the problem of increasingly sorting a
sequence of values.
For instance:
(2,1,4,3,5) (1,2,3,4,5)
input data result
Method: Description:
Step 1: 2 1 4 3 5 - compare the first two
elements:
Step 2: 1 2 4 3 5 if there are not in the desired
order swap them
Step 3: 1 2 4 3 5 - compare the second and the
third element and do the same
…..
Step 4: 1 2 3 4 5
- continue until the last two
elements were compared
The sequence has been ordered
Finiteness
How does this algorithm work and what does it produce?
Step1: Assign 1 to x;
Step2: Increase x by 2;
Step3: If x=10
x=1
then STOP;
x=3 x=5 x=7 x=9 x=11
else Print x; GO TO Step 2;
The algorithm generates odd numbers but it never stops
!
Step1: Assign 1 to x;
Step2: Increase x by 2;
Step3: If x>=10
then STOP;
else Print x; GO TO Step 2
Algorithm Analysis
• How to analyze an algorithm?
• Predicting the resources that the algorithm
requires.
• Machine
• Operating system
• Programming languages
• Compiler, etc.
This means that computational models and algorithm
analysis should not depend on specific hardware, software,
or programming environments. Instead, they focus on
abstract principles that remain valid across different
implementations.
Algorithm Analysis
• Important thing before analysis
– Model of the machine upon which the algorithms is executed.
– Random Access Machine (RAM) (Sequential)
• Running Time: No. of primitive operations or “steps executed”.
Problem Solving Process
• Problem
• Strategy
• Algorithm
– Input
– Output
– Steps
• Analysis
• Correctness
• Time & Space
• Optimality
• Implementation
• Verification
Algorithm Analysis
• Running Time
– We need a measure that is independent of computer,
programming language and the programmer.
– Concept of basic operation
• In sorting comparison is a basic operation.
• What are the constructs / Keywords.
• Time for each construct
• Total Time
• Total time as a function of input size
Important Designing Techniques
• Decrease-and-Conquer
– Reduces the problem size step by step.
– Example: Binary search, where the search space is halved at
each step.
• Transform-and-Conquer
– Modifies the problem to make it easier before solving.
– Example: Changing data representation or preprocessing for
optimization.
• Space and Time Tradeoffs
– Uses additional memory to speed up computations.
– Example: Caching, dynamic programming, or lookup tables.
Representation of Set
1.Descriptive Form:
•The set is described in words:
S = "Set of all prime numbers"
• This means that the set contains all numbers that are only
divisible by 1 and themselves.
2.Tabular Form (Roster Form):
S = {2, 3, 5, 7, 11, ...}
If we recognize this statement of descriptive form through any
computer tool then it doesn’t know that what is prime no.
Drawbacks of Tabular form:
1. If we have infinite no of set then we cant represent it in tabular
form
2. If we have a big set of finite numbers then its not possible to write
all its elements in tabular form.
Postage Stamp Problem
Problem Statement:
We need to prove that any postage ticket amount n≥12 cents can
be formed using only 4-cent and 5-cent stamps.
Basis:
P(12) is true, since 12=4×3;
P(13) is true, since 13=4×2+5×1;
P(14) is true, since 14=4×1+5×2;
P(15) is true, since 15=5×3