Unit 1: Introduction to AlgorithmsÂ
Lecture 1: Introduction to Algorithm – Definition, Characteristics, and Properties
What is an Algorithm?
Characteristics of an Algorithms
Examples
Algorithm +Data structure = Programming
- Algorithms – An algorithm is a step-by-step procedure or set of rules to be followed in calculations or problem-solving operations. Algorithms are fundamental in computer science, as they form the basis of programs and software. They can be described in various forms, such as natural language, pseudocode, flowcharts, or actual programming languages.
Key Characteristics of an Algorithm:
- Input: An algorithm has zero or more inputs.
- Output: It must produce at least one output.
- Definiteness: Each step of the algorithm must be clearly defined and unambiguous.
- Finiteness: The algorithm should terminate after a finite number of steps.
- Effectiveness: The operations in the algorithm should be basic enough to be carried out, in principle, by a human using only paper and pencil.
Example: Finding the Maximum of Two Numbers
Let’s look at an example algorithm to find the maximum of two numbers:
Problem:
Given two numbers,
a
andb
, find the maximum of the two.Algorithm (in pseudocode):
- Start
- Read values for
a
andb
- If
a > b
, then:- Print
a
is the maximum
- Print
- Else:
- Print
b
is the maximum
- Print
- Stop
Explanation:
- Input: Two numbers
a
andb
. - Output: The maximum number between the two.
- Definiteness: The steps are clearly defined, as the algorithm checks the condition
a > b
. - Finiteness: The algorithm will terminate after either printing
a
orb
. - Effectiveness: The comparison
a > b
is a basic operation that can be easily performed.
Example Execution:
Suppose the input values are
a = 5
andb = 7
.- Step 1: Start
- Step 2: Read values for
a
andb
(a = 5, b = 7) - Step 3: Check if
a > b
(5 > 7). This is false. - Step 4: Since the condition is false, go to the else part and print
b
is the maximum. - Step 5: Stop
So, the output will be
7 is the maximum
.